473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to "normalize" indentation sources

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.
May 25 '06 #1
6 2291
On 25/05/2006 12:00 PM, AndyL wrote:
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]
May 25 '06 #2
John Machin wrote:
On 25/05/2006 12:00 PM, AndyL wrote:
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.
May 25 '06 #3
John Machin wrote:
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?
May 25 '06 #4
[John Machin, quoting reindent.py docs]
remove empty lines at the end of files. Also ensure the last line ends
with a newline.

[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 ;-)
May 25 '06 #5
Tim Peters wrote:
[John Machin, quoting reindent.py docs]
remove empty lines at the end of files. Also ensure the last line ends
with a newline.


[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?)
May 25 '06 #6
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()))
?
May 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Newgene | last post by:
Hi, group, I have python2.3 installed on win2k. I noticed that when I open a dos format text file (eol is '\r\n'), readline() always returns a line ending with '\n' only, not '\r\n'. While I read...
1
by: Jawahar Rajan | last post by:
All, I am using a few Input type of "Image" instead of a classic submit button in a form to achieve various tasks for example image1 - add user image2 - modify user image3 - delete user...
1
by: Francesc Guim Bernat | last post by:
Dear colleagues, i'm getting in troubles using one XML library with Visual Studio .NET and Xerces with Xalan. When i execute the code i get the next run time error: "Run-Time Check Failure #2...
7
by: Scott W Gifford | last post by:
Hello, I'm considering using XML to represent a stream of location information, and XPath to do queries against it. I've got most of it figured out (at least on paper), but I can't figure out...
2
by: Lateralus | last post by:
headers/vector3.h: In member function ‘Vector3 Vector3::operator*(Scalar)’: headers/vector3.h:13: error: no matching function for call to ‘Vector3::Vector3(Vector3)’...
1
by: Daniel Lim | last post by:
When using XmlSerializer, I notice that it does not normalize the single quote and double quote characters, i.e. does not change ' to ' and " to &quot. However, it does normalize other...
43
by: markryde | last post by:
Hello, I saw in some open source projects a use of "!!" in "C" code; for example: in some header file #define event_pending(v) \ (!!(v)->vcpu_info->evtchn_upcall_pending & \...
4
by: Nathan Benefield | last post by:
I currently have a spreadsheet tracking votes on legislation in a matrix type format. It is something like this Name Act1 Veto1 Act1A Jones yes No Yes Johnson Yes ...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.