Python is such a simple and get-the-job-done language that many script I wrote is very short. Here are a couple of one- and two-liners:
# oneliners.py by Hai Vu # Here are some of my one- and two-liners import os, sys # To create Pascal naming from a regular phrase print 'print in order traversal'.title().replace(' ', '') # --> PrintInOrderTraversal # Print the contents of a file: print file('oneliners.py').read() # read the contents of a file and put in a list: lines = [x[:-1] for x in file('oneliners.py')] # Poor man's calculator: parse the command line and evaluate it. # If you save this to a file call eval.py, then call it as follow: # python eval.py "28 * 1.15" # The output will be: # 28 * 1.15 = 32.2 expression = ' '.join(sys.argv[1:]) print expression, ' =', eval(expression) # Prints out the path with each component in a separate line # also work for such environment variables as INCLUDE, LIB, and CDPATH print '\n'.join(os.environ['PATH'].split(os.pathsep))
Please submit your favorite one-liners in the comments section.