Feed on
Posts
Comments

Archive for the 'Python' Category

fit matplotlib figure on page

occasionally, long figure labels will cause portions of your pylab or matplotlib figure to hang off the page.  to change the size of the figure on the page, use the subplots_adjust command: import pylab as pl pl.plot() pl.subplots_adjust(left=.17,bottom=.1,right=.7,top=.82) UPDATE: A much easier way of doing this: fig.tight_layout()

to convert a numpy array to a list, use the .tolist() method built into the array class: my_list = my_array.tolist()

when debugging or trying to understand someone else’s python code, it can be helpful to know what variables and methods are associated with an object.  to get that information, use the dir() method: (Pdb) dir(pylab.cm.RdBu) ['N', '__call__', '__doc__', '__init__', '__module__', '_gamma', '_i_bad', '_i_over', '_i_under', '_init', '_isinit', '_lut', '_rgba_bad', '_rgba_over', '_rgba_under', '_segmentdata', '_set_extremes', 'from_list', 'is_gray', 'monochrome', […]

convert FloatVector to numpy array

it’s easy to convert an rpy FloatVector object (fv_obj) to a numpy array: import np np.array(fv_obj)

if you’re getting the following errors while trying to use pylab: >>> pylab.show() /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/backends/__init__.py:41: UserWarning: Your currently selected backend, ‘agg’ does not support show(). Please select a GUI backend in your matplotlibrc file (‘/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/matplotlib/mpl-data/matplotlibrc’) or with matplotlib.use() (backend, matplotlib.matplotlib_fname())) try inserting the following lines in your code: import matplotlib matplotlib.use(‘TkAgg’) (thanks google groups!)

to strip all the non-letters from a python string named ‘my_str’, you can use the following one-liner: ”.join([c for c in my_str if c.isalpha()]) to strip all the non-alphanumeric characters from that python string, use the slightly modified: ”.join([c for c in my_str if c.isalnum()])

to do string substitutions in python, use the “re” module and the “sub” function: >>> import re … >>> test = “2545423/1-4242:0.3245″ >>> test ; re.sub(“/\d-\d*:”,”:”,test) ’2545423/1-4242:0.3245′ ’2545423:0.3245′ and, to match a group and then use it in the substitution, use parentheses and the \<number> operation: >>> new_boot_s = re.sub(“(\d+:)”,”n\\1″,boot_s)

if you call a shell process from python, your script will normally trundle along without pausing for that process to complete.  if you’d like the process to finish before your script continues on, use the communicate method: proc = sub.Popen(my_job,stdout=sub.PIPE) stdout,stderr = proc.communicate()

to get the full pathname to a script that python is executing, use the handy little one-liner: >> os.path.abspath(sys.argv[0])

give a file a unique id in python

to hash a file’s contents in python, you can use the md5 module. this comes in handy if you want to screen a directory for duplicate files, for instance. import md5 my_f = open(“my.file”) my_s = my_f.read() my_f.close() my_hash = md5.new(my_s).hexdigest()

« Prev - Next »