Code Reformat And Spaces

2008-12-22
2013-03-15
  • Kenneth Love
    Kenneth Love
    2008-12-22

    I got some code using a Google search and it uses two spaces for indention.  I use four spaces.

    PyDev reports a lot of these types of errors:
    ID:W0311  Bad indentation. Found 2 spaces, expected 4    ConfidentialReports/src    fixedlenfields.py    line 33

    The code formatter doesn't appear to fix these.

    Short of manually fixing each line, I don't know how to remedy the situation.

    Is there an option to change the spacing of the code in a source file automatically?

    If it makes a difference, I'm using Python 3, Eclipse 3.3, and PyDev 1.4.0.

    adTHANKSvance,
    Kenneth

    P.S. Is there a standard for indenting Python code?

     
    • Fabio Zadrozny
      Fabio Zadrozny
      2008-12-22

      That seems like a PyLint warning... you can disable it by passing the correct parameters to PyLint (in window > preferences > pydev > pylint) -- check the pylint manual: http://www.logilab.org/card/pylint_manual to see the command line to be passed (or maybe even enabling  2 spaces).

      Note that currently the code-formatter doesn't really change the basic structure of your code (indentation, spaces for methods, classes...) nor comments (it deals mostly with spaces for parenthesis, commas, operators and some others, as it tries to keep most closely to your current code structure... although I'm starting to think about a second option which would actually change the code more -- but that probably won't happen very soon).

      You can try to use reindent.py to see if it does what you want (usually located in your python distribution at Python\Tools\Scripts\reindent.py).

      The most standard way of indenting is with 4 spaces (which is also recommended by PEP 8: http://www.python.org/dev/peps/pep-0008/ )

      Cheers,

      Fabio

       
    • liturgist
      liturgist
      2008-12-22

      Have you tried a substitution in vi or sed to see if it will give you what you want?

      s/  /    /g

       
      • Fabio Zadrozny
        Fabio Zadrozny
        2008-12-22

        I think the same can be achieved in Eclipse by the find replace (Ctrl+F: 2 spaces for 4 spaces - > replace all -- is that it? or is there something different in the vi substitution?)

        Cheers,

        Fabio

         
        • Kenneth Love
          Kenneth Love
          2008-12-22

          That fixed it.

          Such a simple answer, it never even occurred to me.

          Maybe the curmudgeons are right.  Fancy IDEs lead to mushy brains.  :(

          Thanks Fabio and Liturgist!