Monday, 27 April 2015

python indents!

I love python - however one of the things that initially annoyed me (coming from a perl background) was the spacing - having had to cope with horrific programs I've inherited elsewhere I now actually think having a good layout for code and comments/tests/documentation is good.

a good tool to use as a beginner is reindent:

apt-get install python-pip (on an ubuntu based box)
.
.

stuart-ThinkPad-T420 ~ # pip install reindent
Downloading/unpacking reindent
  Downloading Reindent-0.1.1.tar.gz
  Running setup.py (path:/tmp/pip_build_root/reindent/setup.py) egg_info for package reindent
 
Installing collected packages: reindent
  Running setup.py install for reindent
    changing mode of build/scripts-2.7/reindent from 644 to 755
 
    changing mode of /usr/local/bin/reindent to 755
Successfully installed reindent
Cleaning up...
stuart-ThinkPad-T420 ~ # logout
^-^ stuart@stuart-ThinkPad-T420 ~/buy-to-let  (master)$ reindent btl.py
^-^ stuart@stuart-ThinkPad-T420 ~/buy-to-let  (master)$ ls -ltr
total 12
-rw-r--r-- 1 stuart stuart 149 Apr 27 10:57 README.md
-rw-r--r-- 1 stuart stuart 470 Apr 27 11:02 btl.py.bak
-rwxr-xr-x 1 stuart stuart 466 Apr 27 11:02 btl.py

saves a bakup - looking at diffs:
sdiff -s btl.py.bak btl.py
     ival = ival *(1 + gr)      |        ival = ival *(1 + gr)
        if ival >= startval * 1.25:      |            if ival >= startval * 1.25:
         print "year     %d      val %.2f "  % ( i, ival)     |                print "year     %d      val %.2f "  % ( i, iv
-_- stuart@stuart-ThinkPad-T420 ~/buy-to-let  (master)$ 

hard to see with my bad formatting - but essentially ensures 4 spaces as per pep8


No comments:

Post a Comment

connect ubuntu to wifi from command line

so i have a server install of a beta release of bionic beaver - ubuntu 18.04 - I want to connect this to wifi as I'm not near my cobble...