First, I'd like to readily admit that I'm a Python newbie and more than welcome any corrections to the below hitches I've run into that might lead to a smoother/less discombobulated path.
Some things about Python are very different from the how other scripting languages do them. There's also a fair amount of inconsistencies in the language design that require a higher level of rote memorization. Often there is not a generalized rule or naming convention you can use to navigate your way around the modules and documentation or to intuit how methods or modules should work.
This is my blog page documenting those for easy reference or maybe to just help me memorize them.
The Many Paths to Path
There are two different path modules used with any regularity.
The os.path module is the environment path of the system. What we'd normally access via constants like ENV['PATH'] in ruby or echo $PATH in bash. The sys.path is the internal path of the python interpreter, it's what we manipulate to add or remove paths to python modules.
There's nothing wrong with this, but often vars like this are available by default so I found myself double checking a lot in the beginning.
Shelling Out and Capturing STDOUT in Python
This is usually brain dead simple. You just back tic like so
output=`echo "foo"&& ls -l; sleep 5`
This will work in perl, ruby, bash, zsh, just about anything...except python. The Python way isn't really difficult, it does require that you ask around till you find the proper method/module then read a few pages to find the example. So here it is:
from subprocess import Popen
cmd1 = ["echo", "foo"]
cmd2 = ['ls", "-l"]
cmd3 = ["sleep", "5"]
tmpout = ""
(output, error) = Popen(cmd1, stdout=subprocess.PIPE).communicate()
tmpout = output
(output,error) = Popen(cmd2, stdout=subprocess.PIPE).communicate()
tmpout += output
(output,error) = Popen(cmd3, stdout=subprocess.PIPE).communicate()
tmpout += output
Directories, directories, and more directories
Need to work with directories? Get ready to start digging. Here's some quick refs to common tasks.
- To get a list of directories you can use the listdirs method in either the dircache module or the os module, though the os module is the recommended one. dircache makes a cache of the list and does not reread unless something changes.
- To create a directory you can import the os module and use mkdir
- To move or copy a directory or file you import the shutil module and use move or copy, OR you can import the os directory and use rmdir, BUT if you want to recursively remove directories you use removedirs AND if you want to remove files, but not directories you use remove. . . or at this point just shell out and use rm since it seems an easier alternative hehe (only embark on this IF you've read the above tutorial on shelling out. :P)
Python Debugger Warts
A good debugger is essential to any sort of development environment. One glaring lapse in pdb (python debugger) is the inability to watch a variables state for changes. I've looked high and low for any way to do this in python and it doesn't seem to be possible. Anyone whose had to track a variable through several copies, reference passes, parsing, modules, and methods knows what a pain the brute force technic of littering all that with "print(foo)" can be....but suck it up and get to it, there's no other way.
In django, controllers are views and views are templates
This sinks in after a while, but if you work with other frameworks and switch back and forth you'll catch yourself looking for your views in the views.py when you should be looking in the templates.
Jesse Brown
May 20, 2009 5:41 PM
Do you really have to do all that nonsense to shell out?