how to "normalize" indentation sources

Discussion in 'Python' started by AndyL, May 25, 2006.

  1. AndyL

    AndyL Guest

    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.
    AndyL, May 25, 2006
    #1
    1. Advertising

  2. AndyL

    AndyL Guest

    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.
    AndyL, May 25, 2006
    #2
    1. Advertising

  3. AndyL

    John Machin Guest

    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]
    John Machin, May 25, 2006
    #3
  4. AndyL

    John Salerno Guest

    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?
    John Salerno, May 25, 2006
    #4
  5. AndyL

    Tim Peters Guest

    [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 ;-)
    Tim Peters, May 25, 2006
    #5
  6. AndyL

    John Salerno Guest

    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?)
    John Salerno, May 25, 2006
    #6
  7. AndyL

    John Machin Guest

    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()))
    ?
    John Machin, May 25, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnold
    Replies:
    1
    Views:
    596
    arnold
    Mar 5, 2006
  2. Christos TZOTZIOY Georgiou

    unicodedata . normalize (NFD - NFC) inconsistency

    Christos TZOTZIOY Georgiou, Nov 8, 2004, in forum: Python
    Replies:
    3
    Views:
    878
    Christos TZOTZIOY Georgiou
    Nov 10, 2004
  3. =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=

    Vector, matrix, normalize, rotate. What package?

    =?iso-8859-1?B?TWF0dGlhcyBCcuRuZHN0cvZt?=, Feb 27, 2007, in forum: Python
    Replies:
    5
    Views:
    6,301
  4. Mike
    Replies:
    0
    Views:
    405
  5. Jesse B.
    Replies:
    2
    Views:
    187
    Josh Cheek
    Mar 27, 2010
Loading...

Share This Page