开发者

Utility to fix Python import style?

We have a lot of files with ugly import statements like this:

from project.foo import a, b
from project.foo import c
from project.bar import d, e, f, g

Does there exist someth开发者_如何学JAVAing that will change them all to one import per line?

from project.foo import a
from project.foo import b
from project.foo import c
from project.bar import d
from project.bar import e
from project.bar import f
from project.bar import g

Clarification: The reason for this is to maintain a consistent style, like Google's style guide for Python.


As per PEP8 style guide:

Imports

- Imports should usually be on separate lines, e.g.:

    Yes: import os
         import sys

    No:  import sys, os

  it's okay to say this though:

    from subprocess import Popen, PIPE

So, I think you should be doing what you are and should not split them. I am not aware of any utility which will do that for you.

Also, if:

project.bar contains say: d,e,f,g,x,y,z then I would say just do a import project.bar, code will be much less and easy for the eyes.


sed can do that with something like this (test first as I don't have access to my Linux box to test it at the moment):

sed -i".backup" 's/from ([^ ]+) import ([^,]+), ([^,]+)/from \1 import \2\nfrom \1 import \3/' *.py
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜