在python源文件中排序类定义的最佳方式是什么? [英] What's the best way to sort class definitions in a python source file?

查看:174
本文介绍了在python源文件中排序类定义的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有许多类定义的.py源代码,如下所示:

I have a .py source with many class definitions, like so:

class C:
    # code c

class A:
    # code a

class B:
    # code b

我想把它变成:

class A:
    # code a

class B:
    # code b

class C:
    # code c

有没有一个工具?如何使用emacs?

Is there a tool for this? What about doing it with emacs?

推荐答案

我为您编码,但只因为我有兴趣看看如何 sort-subr 的作品。我坚持宽恕原始问题的意图。 :)

I coded it for you, but only because I was interested in seeing how sort-subr works. I do not condone the intent of the original question. :)

此外,如果您提出这个答案,请同时upvote @ db48x的教学答案。

Also, if you upvote this answer, plase also upvote @db48x's didactic answer.

使用python -sort-defs的方式与使用 sort-paragraph 相同。

Use python-sort-defs the same way you would use sort-paragraph.

(defun python-sort-defs (reverse beg end)
  (interactive "P\nr")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (sort-subr reverse
                 (function
                  (lambda ()
                    (while (and (not (eobp)) (looking-at paragraph-separate))
                      (forward-line 1))))
                 'python-end-of-block-must-move))))

(defun python-end-of-block-must-move ()
  (when (eq (point) (progn
                      (python-end-of-block)
                      (point)))
    (forward-line 1)))

这篇关于在python源文件中排序类定义的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆