how to format a python source file with tools?

Diez B. Roggisch deets at nospam.web.de
Fri Nov 27 16:58:01 CET 2009


李白,字一日 schrieb:
> On Nov 27, 3:35 pm, Ben Finney <ben+pyt... at benfinney.id.au> wrote:
>> 李白,字一日 <calid... at gmail.com> writes:
>>> or is it possible for large source files?
>> Is what possible? What do you want the tool to do?
>>
>> --
>>  \           “I do not believe in forgiveness as it is preached by the |
>>   `\        church. We do not need the forgiveness of God, but of each |
>> _o__)                    other and of ourselves.” —Robert G. Ingersoll |
>> Ben Finney
> 
> sometimes i need to merge some code snippets from files into a file,
> and when time comes to do this merge, i always find it difficult to
> reformat the python code
> because after pasting and copy, the code indentation different from
> one to another.
> and it is the tedious job for us to manually move the code from one
> segment to another.
> 
> so i would like to have a tool to intelligently format the code for me
> and make the code more beautiful
> and automated.

This is not possible. Consider the following situation:


code a:


if something:
     do_something()


code b:

if anything:
     do_something_else()


No lets say you copy b after a. At which level should it be? Like this


if something:
    do_something()

if anything:
     do_something_else()


Or like this?

if something:
    do_something()

    if anything:
        do_something_else()



Both are semantically radically different, and only you know which one 
is the right one.

What my editor (emacs) allows, and other IDEs/editors as well is to mark 
blocks, and shift these wholesome left or right, to get the desired 
indention level.

The only thing that migh be automatized after a piece of code is valid 
is normalization, like de-tabifying or making everything based on 4 
space characters indention. No idea if there is something out there that 
does that.

Diez



More information about the Python-list mailing list