Python Source Code Beautifier

sjdevnull at yahoo.com sjdevnull at yahoo.com
Tue Feb 27 19:14:20 EST 2007


On Feb 27, 3:45 am, Franz Steinhaeusler <franz.steinhaeus... at gmx.at>
wrote:
> Hello, I did not find any reasonable pyhton source code beautifier
> program (preferable gui).
>
> Some would ask why? Program it immediatly good.
>
> (BTW: Would be a nice project, if I would have more spare time).
>
> Ich have some foreign source, which are programed in a way
> I don't like, so I would like to have a tool, which automatically
> processes following options:
>
> Use Spaces, size: 4

reindent.py or your editor should handle this, or PythonTidy

> convert structs like: if (a > b):   to   if a > b:

If someone put the () around it, they probably have good reason (e.g.
clarity or they anticipate it being a multiline condition in the near
future)

> fill in spaces, but not in functions between operators:
>
> a+=1 => a += 1
> p(t + 1) => p(t+1)

PythonTidy

> convert:
>
> self.scriptcount = self.scriptcount + 1 => self.scriptcount += 1

Those mean different things:

>>> a=[1]
>>> b=a
>>> a += [2]
>>> a
[1, 2]
>>> b
[1, 2]
>>> a=[1]
>>> b=a
>>> a = a + [2]
>>> a
[1, 2]
>>> b
[1]


> from "is" to "==" and "is not" to "!=" (ok a find replace could do that
> easily also), but in a program that would be more comfortable.

>>> a="hello there"
>>> b="hello there"
>>> a==b
True
>>> a is b
False


> break long lines (in a reasonable way)

Not sure how this would be done.

>
> make from:
> if len(string) > 0: => if string:
> and
> if if len(string) < 1 orr if string == ""  => if not string

Impossible in a dynamically typed system, you could hack up something
that works sometimes with type inference and/or type logging after
running the program but it'd be unreliable and hardly seems worth the
effort and danger.  Especially since it might break other stringlike
classes depending on their semantics.

> detect mixed line ending
> detect tabs mixed with space
> trim trailing whitespaces.

reindent.py handles those.

> Running Pylint or Pycheck automatically afterwards
> would be the coronation. :)

I have vim automatically call PyFlakes every time I hit enter and
highlight any errors (so stupidities like "if a=1:" are caught
immediately as I'm editing)




More information about the Python-list mailing list