Posted in Python on October 26th, 2008 No Comments »
to custom sort a list on the fly, try using the lambda function to do a quick definition of the cmp function. for instance, to sort the list ‘ids’, using each ‘id’ value in an ancillary dictionary ‘id_dict’: ids.sort(cmp=lambda a,b:id_dict[b] – id_dict[a])
Posted in Python on April 16th, 2008 6 Comments »
let’s say you’ve got a list of the numbers 0 through 4 and you wanted all 32 possible combinations of them.  to get that list: list_of_five = range(0,5)   swap_list_list = [[]] for swap in list_of_five:    temp_lists = []    for list_stub in swap_list_list:      this_list = copy.copy(list_stub)      this_list.append(swap)    […]
Posted in Python on April 16th, 2008 4 Comments »
let’s say you’ve got a dictionary keyed by strings and whose values are numbers. Â if you’d like to sort those keys by those values, here’s a quick way to do so: sorted_keys = sorted(my_dict.keys(), key=lambda x:my_dict[x]) [thanks Paul!]
Posted in Python on April 9th, 2008 2 Comments »
when you execute a program in python, the program’s name can be found with the following command: import sys print sys.argv[0]  to get the full path to the program, use the abspath command: import os os.path.abspath(sys.argv[0]) Â
Posted in Python on April 7th, 2008 No Comments »
to make a file executable from python, use the following template: Â >> import os>> my_filename = “file.txt”>> os.chmod(my_filename,0755)
Posted in Python on April 3rd, 2008 No Comments »
to flush the print buffer in python:Â >>Â sys.stdout.flush()Â
Posted in Python on March 31st, 2008 No Comments »
real simple:Â >> abs(val)Â
Posted in Python on February 27th, 2008 No Comments »
to not have the output of subprocess.check_call print out in python, pass the output to /dev/null like so: subprocess.check_call(angst_call,stdout=open(os.devnull,”w”))
Posted in Python on December 11th, 2007 No Comments »
this is almost certainly not the best way to read the output of a python system call. but, it works.   let’s say i’d like to read the results of a call to ls. here’s how to do it: import subprocess proc = subprocess .Popen([ls],stdout=sub.PIPE) print proc.stdout.read()
Posted in Python on December 10th, 2007 No Comments »
let’s say you want to make an array of all the numbers between 5 and 7, separated by the interval 0.33 in python. one way to do that would be: min = 5.0 max = 7.0 interval = 0.33 my_list = [min + val*interval for val in range(int((max-min)/interval))]