how to "normalize" indentation sources

A

AndyL

Hi,

I have a lot of sources with mixed indentation typically 2 or 4 or 8
spaces. Is there any way to automatically convert them in let's say 4
spaces?


Thx, A.
 
A

AndyL

John said:
Hi,

I have a lot of sources with mixed indentation typically 2 or 4 or 8
spaces. Is there any way to automatically convert them in let's say 4
spaces?

Yup. Right under your nose:

C:\junk>\python24\tools\scripts\reindent.py --help
reindent [-d][-r][-v] [ path ... ]

-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
-v (--verbose) Verbose. Print informative msgs; else no output.
-h (--help) Help. Print this usage information and exit.

Change Python (.py) files to use 4-space indents and no hard tab
characters.
Also trim excess spaces and tabs from ends of lines, and remove empty lines
at the end of files. Also ensure the last line ends with a newline.
[snip]
thx a lot, A.
 
J

John Machin

Hi,

I have a lot of sources with mixed indentation typically 2 or 4 or 8
spaces. Is there any way to automatically convert them in let's say 4
spaces?

Yup. Right under your nose:

C:\junk>\python24\tools\scripts\reindent.py --help
reindent [-d][-r][-v] [ path ... ]

-d (--dryrun) Dry run. Analyze, but don't make any changes to, files.
-r (--recurse) Recurse. Search for all .py files in subdirectories too.
-v (--verbose) Verbose. Print informative msgs; else no output.
-h (--help) Help. Print this usage information and exit.

Change Python (.py) files to use 4-space indents and no hard tab characters.
Also trim excess spaces and tabs from ends of lines, and remove empty lines
at the end of files. Also ensure the last line ends with a newline.
[snip]
 
J

John Salerno

John said:
remove empty lines
at the end of files. Also ensure the last line ends with a newline.

don't those two things conflict with one another? or is the newline
added after empty lines are removed?
 
T

Tim Peters

[John Machin, quoting reindent.py docs]
[John Salerno]
don't those two things conflict with one another?

No. This is the repr of a file with (3) empty lines at the end:

"a file\n\n \n \t \n"

reindent.py changes that to:

"a file\n"

This is the repr of a file with no newline at the end:

"a file"

reindent.py changes that to:

"a file\n"
or is the newline added after empty lines are removed?

False dichotomy ;-)
 
J

John Salerno

Tim said:
[John Machin, quoting reindent.py docs]
[John Salerno]
don't those two things conflict with one another?

No. This is the repr of a file with (3) empty lines at the end:

"a file\n\n \n \t \n"
reindent.py changes that to:

"a file\n"

This is the repr of a file with no newline at the end:

"a file"

reindent.py changes that to:

"a file\n"
or is the newline added after empty lines are removed?

False dichotomy ;-)

So the line below the last line of the file isn't actually considered an
empty line, even though you can move the cursor to it in a text editor?

If you have a file that has one line and it ends with a newline, at
least in my text editor the cursor then moves down to the next line, but
is this just a detail of the way the editor itself works, and nothing to
do with the file? (i.e., there is really only one line in the file, not
two?)
 
J

John Machin

On 26/05/2006 2:38 AM, John Salerno wrote:
[snip]
So the line below the last line of the file isn't actually considered an
empty line, even though you can move the cursor to it in a text editor?

That line doesn't exist in a file *until* you (a) type something into
the editor and (b) save the revised contents back to disk.
If you have a file that has one line and it ends with a newline, at
least in my text editor the cursor then moves down to the next line, but
is this just a detail of the way the editor itself works, and nothing to
do with the file? (i.e., there is really only one line in the file, not
two?)

Please consider, if the answer to your question were "no", how could
anyone add lines to a file using such an editor.
Why you don't fire up your Python and try something for yourself, like:
print repr(open("my_one_line_file.txt".read()))
?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top