As I mentioned previously I’ve been spending a good deal of time learning how to write efficient and readable Python code. Jeff Knupp’s Idiomatic Python videos are absolutely incredible and have definitely helped me become a better programmer. In his first video he turned me on to list comprehensions and how they can make code more readable. Prior to watching this video I had a tendency to use multi line iterators similar to this:
ml = 
for word in list:
After watching Jeff do some crazy awesome python sorcery on bad.py I’ve started to use list comprehensions. Here is a refactored lower_a_list() that uses list comprehensions:
return [word.lower() for word in list]
I find the second much more readable and you can get the gist of what’s going on by scanning from right to left. Huge thanks to Jeff for putting out this amazing series!
I’ve been reading a lot about writing clean, readable and performant Python code. One feature that I now adore are generators. In the words of Python guru David Beazley a generator is:
“a function that produces a sequence of results instead of a single value”
The presentation above is an incredible overview of this amazing feature and I’ve been taking advantage of it whenever I can. Here is one case where I was able to use this today:
SYSFS_TAPE_PATH = "/sys/class/scsi_tape/"
Return a list of tape drives
for drive in os.listdir(SYSFS_TAPE_PATH):
if re.match(r'^nst[0-9]+$', drive):
for drive in find_drives():
Running the script produces all of the drives on the system:
In the past I would have created a list and returned it after processing all of the drives in the scsi_tape directory. With generators that is no longer necessary and I find the code is much more readable. A huge thanks to David for the great presentation! Hopefully one day I will get to take one of his awesome Python programming classes.
I’m a long time console guy and haven’t found a graphical Python development tool that suits my needs as well as vim. It takes a couple of amazing vim plug-ins but the experience is great IMHO. I’m currently using the vim-jedi, pylint and vim-syntastic plug-ins which can be installed with yum on CentOS/Fedora machines:
$ sudo yum install pylint
$ sudo yum install vim-jedi
$ sudo yum install vim-syntastic-python
To enable syntax highlighting and auto indentation you can add the following to your $HOME/.vimrc:
filetype indent plugin on
Once these are in place your code will be highlighted with a nice set of colors, hitting return will generate the correct spacing and pylint will be run automatically to note problems with your code. If you are using other plug-ins let me know via a comment.
I’ve been trying to expand my Python knowledge and recently came across Nick Parlante’s 6-part learn Python series on Youtube. I’ve watched several of the videos, and I am impressed with Nick’s teaching ability. Here are links to the 6-part series:
Ever want to immediatly serve content from a specific directory over HTTP, but didn’t want to bother messing with httpd.conf or other webserver configiurations?
If you’ve got Python installed, this is a snap. Execute python with the SimpleHTTPServer module, using port 8080 so there isn’t a need to elevate privs to root.
$ python -m SimpleHTTPServer 8080
Serving HTTP on 0.0.0.0 port 8080 …
Sure enough, pointing a browser to the IP address :8080 of the box hits my home directory listing. Super easy, super fast, super simple!
I use this to serve content to my PS3. The PS3 doesn’t support NFS or CIFS, so to download content to the hard drive, the best method is by pulling it over HTTP with the embedded web brower. On my MacBook, I change into the directory containing whatever media I want to transfer, fire up HTTP, and suck it down to the hard drive on the PS3. Nice!