Posted in Python on April 2nd, 2007 2 Comments »
if you try and use os.rmdir to remove a set of nested directories in python, you’ll receive the following error message: OSError: [Errno 66] Directory not empty: to skirt this problem, use the shutil module: import shutil shutil.rmtree(tree_name)
Posted in Python on March 16th, 2007 4 Comments »
great gauss’ ghost, i had no idea how powerful the python debugger could be. basically, invoking the debugger allows you to stop your code at any point and investigate the current state of all variables in the environment. even better, the debugger allows you to try manipulating those variables; this allows you to break out […]
Posted in Python on March 15th, 2007 No Comments »
if you’re trying to install scipy on a mac and you get the following error when building: collect2: ld returned 1 exit status error: Command “/usr/local/bin/gfortran -L/sw/lib build/temp.macosx-10.3-fat-2.5/build/src.macosx-10.3-fat-2.5/Lib/fftpack/_fftpackmodule.o build/temp.macosx-10.3-fat-2.5/Lib/fftpack/src/zfft.o build/temp.macosx-10.3-fat-2.5/Lib/fftpack/src/drfft.o build/temp.macosx-10.3-fat-2.5/Lib/fftpack/src/zrfft.o build/temp.macosx-10.3-fat-2.5/Lib/fftpack/src/zfftnd.o build/temp.macosx-10.3-fat-2.5/build/src.macosx-10.3-fat-2.5/fortranobject.o -L/usr/local/lib -L/usr/local/lib/gcc/powerpc-apple-darwin8.8.0/4.3.0 -Lbuild/temp.macosx-10.3-fat-2.5 -ldfftpack -lfftw3 -lgfortran -o build/lib.macosx-10.3-fat-2.5/scipy/fftpack/_fftpack.so” failed with exit status 1 try resolving by opening up your ~/.profile and commenting […]
Posted in Python on March 8th, 2007 11 Comments »
here’s a compact way of calculating factorials in python. in fact, since this method doesn’t rely on for or while loops and their ancillary overhead, it might be one of the faster ways of calculating a factorial. let’s say you’d like to calculate k! >>> reduce(lambda i,j : i*j, range(1,k+1))
Posted in Python on January 11th, 2007 3 Comments »
to declare several variables in one line, use multiple equals signs: var1 = var2 = var3 = var4 = 0 be careful though using this form to declare dicts; due to python’s mutability rules, multiple dictionaries instantiated this way will all point to the same set of keys and values!
Posted in Python on December 16th, 2006 2 Comments »
if you’d to insert a pause into a python script, use the time module and call the “sleep” method. it takes arguments of seconds. import time time.sleep(1) note that if what you’re really trying to do is set breakpoints and debug your script, you should be looking at the python debugger instead.
Posted in Python on October 29th, 2006 No Comments »
glob is a handy way to do a directory listing in python. for instance, to return a list of all the text files in a directory, you could command: import glob glob.glob(“/mypath/*.txt”) (glob handles *,?, and [] matching).
Posted in Python on October 11th, 2006 26 Comments »
assume you’ve got a python dictionary a and you’d like to find the key associated with the minimum (or maximum) value in the dictionary: >>> a = dict() >>> a['me'] = 5 >>> a['you'] = 5 >>> a['her'] = 6 >>> a['him'] = 245 >>> print a {‘me’: 5, ‘you’: 5, ‘him’: 245, ‘her’: 6} […]
Posted in Python on October 5th, 2006 1 Comment »
i was trying to debug some code the other day by throwing an error whenever a comparison was performed. problem was, python will compare almost anything without crashing; it’ll compare lists to strings, integers to dicts, and not complain at all. it took some googling, but i finally found something python didn’t like to compare: […]
Posted in Python on October 3rd, 2006 2 Comments »
to execute a shell command in a python script, import the os module: import os and then use the popen4 function, which returns file objects corresponding to both stdin and stdout: (stdin,stdout) = os.popen4(“ls”) print stdout.read()