======================= swap col 3 and 4 outfile = file("out.txt", "w") for line in file("in.txt"): data = line.rstrip('\n').split('\t') data[3],data[4] = data[4],data[3] outfile.write('\t'.join(data)) ======================= pydoc pydoc sys # document a built-in module pydoc copy # document a module written in Python pydoc types # document a module written in Python pydoc abs # document a built-in function pydoc repr.Repr # document a single class pydoc -k mail # keyword search like man -k pydoc -p 6789 # start a web server at http://localhost:6789/ >>> from pydoc import help >>> help("getopt.getopt") # document something you haven't imported >>> import calendar >>> help(calendar) # document a live object ======================= quick sort def qsort(L): if len(L) <= 1: return L return qsort( [ lt for lt in L[1:] if lt < L[0] ] ) + \ [ L[0] ] + qsort( [ ge for ge in L[1:] if ge >= L[0] ] ) ======================= extract email addresses from files import sys,re print '\n'.join(re.findall('([\w\.\-]+@[\w\.\-]+)',sys.stdin.read())) ======================= gnuplot with the popen function import os f=os.popen('gnuplot', 'w') print >>f, "set yrange[-300:+300]" for n in range(300): print >>f, "plot %i*cos(x)+%i*log(x+10)" % (n,150-n) f.flush() ======================= pexpect import pexpect child = pexpect.spawn ('ftp ftp.openbsd.org') child.expect ('Name .*: ') child.sendline ('anonymous') child.expect ('[pP]assword:') child.sendline ('noah@example.com') child.expect ('ftp> ') child.sendline ('cd pub') child.expect('ftp> ') child.sendline ('get ls-lR.gz') child.expect('ftp> ') child.sendline ('bye') ----------------------- child.expect('password:') child.sendline (my_secret_password) # We expect any of these three patterns... i = child.expect (['Permission denied', 'Terminal type', '[#\$] ']) if i==0: print 'Permission denied on host. Can't login' child.kill(0) elif i==2: print 'Login OK... need to send terminal type.' child.sendline('vt100') child.expect ('[#\$] ') elif i==3: print 'Login OK.' print 'Shell command prompt', child.after .... child.expect('password:', timeout=120) .... child = pexpect.spawn ('ssh user@example.com') child.expect ('assword') child.sendline ('mypassword') ======================= ipython ----------------------- profiles ipython -p ----------------------- edit edit -x ----------------------- history hist -n ----------------------- macros macro print_l 2 Macro `print_l` created. To execute, type its name (without quotes). Macro contents: for i in l: print i ----------------------- System Shell Access In the default IPython profile, the Unix shell commands cd, pwd, and ls all work like they do from a bash shell. To execute any other shell commands, prepend a ! or !! to them. Use the %sc and %sx magic words to capture the output from shell commands. Starting IPython with a -p pysh flag will cause IPython to accept and execute any commands in the user's $PATH, while at the same time allowing access to all Python modules as well as all Python keywords and built-in functions. ipython -p pysh for i in range(500): mkdir d_${i}_d will create 500 directories ======================= GNU plot import os f=os.popen('gnuplot', 'w') print >>f, "set yrange[-300:+300]" for n in range(300): print >>f, "plot %i*cos(x)+%i*log(x+10)" % (n,150-n) f.flush() ======================= SQLite http://www.sebsauvage.net/python/snyppets/index.html #!/usr/bin/python # -*- coding: iso-8859-1 -*- from sqlite3 import dbapi2 as sqlite # Create a database: con = sqlite.connect('mydatabase.db3') cur = con.cursor() # Create a table: cur.execute('create table clients (id INT PRIMARY KEY, name CHAR(60))') # Insert a single line: client = (5,"John Smith") cur.execute("insert into clients (id, name) values (?, ?)", client ) con.commit() # Insert several lines at once: clients = [ (7,"Ella Fitzgerald"), (8,"Louis Armstrong"), (9,"Miles Davis") ] cur.executemany("insert into clients (id, name) values (?, ?)", clients ) con.commit() cur.close() con.close() ----------------------- usage #!/usr/bin/python # -*- coding: iso-8859-1 -*- from sqlite3 import dbapi2 as sqlite # Connect to an existing database con = sqlite.connect('mydatabase.db3') cur = con.cursor() # Get row by row print "Row by row:" cur.execute('select id, name from clients order by name;') row = cur.fetchone() while row: print row row = cur.fetchone() # Get all rows at once: print "All rows at once:" cur.execute('select id, name from clients order by name;') print cur.fetchall() cur.close() con.close() ----------------------- hints Hint 1: If you use sqlite.connect(':memory:'), this creates an in-memory database. As there is no disk access, this is a very very fast database. (But make sure you have enough memory to handle your data.) Hint 2: To make your program compatible with Python 2.5 and Python 2.4+pySqlLite, do the following: try: from sqlite3 import dbapi2 as sqlite # For Python 2.5 except ImportError: pass if not sqlite: try: from pysqlite2 import dbapi2 as sqlite # For Python 2.4 and pySqlLite except ImportError: pass if not sqlite: # If module not imported successfully, raise an error. raise ImportError, "This module requires either: Python 2.5 or Python 2.4 with the pySqlLite module (http://initd.org/tracker/pysqlite)" # Then use it con = sqlite.connect("mydatabase.db3") ... ======================= getopt import sys import getopt if __name__ == "__main__": opts, args = None, None try: opts, args = getopt.getopt(sys.argv[1:], "hco:",["help", "capitalize","output="]) except getopt.GetoptError, e: raise 'Unknown argument "%s" in command-line.' % e.opt for option, value in opts: if option in ('-h','--help'): print 'You asked for the program help.' sys.exit(0) if option in ('-c','--capitalize'): print "You used the --capitalize option !" elif option in ('-o','--output'): print "You used the --output option with value",value # Make sure we have our mandatory argument (file) if len(args) != 1: print 'You must specify one file to process. Use -h for help.' sys.exit(1) print "The file to process is",args[0] # The rest of the code goes here... ======================= date, time import datetime,time def dateDiffInSeconds(date1, date2): timedelta = date2 - date1 return timedelta.days*24*3600 + timedelta.seconds date1 = datetime.datetime(2006,02,17,15,30,00) date2 = datetime.datetime(2006,05,18,11,01,00) print dateDiffInSeconds(date1,date2) ----------------------- time.strptime() converts the string to a struct_time tuple. time.mktime() converts this tuple into seconds (elasped since epoch, C-style). datetime.fromtimestamp() converts the seconds to a Python datetime object. ======================= Columns to rows (and vice-versa) table = [ ('Person', 'Disks', 'Books'), ('Zoe' , 12, 24 ), ('John' , 17, 5 ), ('Julien', 3, 11 ) ] print zip(*table) ======================= ======================= ======= convert a GIF to python source code import base64 print "icon='''\\\n" + base64.encodestring(open("icon.gif", "rb").read()) + "'''" ======= import datetime now = datetime.datetime(2003, 8, 4, 12, 30, 45) print now print repr(now) print type(now) print now.year, now.month, now.day print now.hour, now.minute, now.second print now.microsecond d = datetime.date(2003, 7, 29) print d print d.year, d.month, d.day print datetime.date.today() t = datetime.time(18, 54, 32) print t print t.hour, t.minute, t.second, t.microsecond now = datetime.datetime.now() d = now.date() t = now.time() print now print d, t print datetime.datetime.combine(d, t) ------- convert a string date ("2006-05-18 19:35:00") into a datetime object import datetime,time dt = datetime.datetime.fromtimestamp(time.mktime(time.strptime(stringDate,"%Y-%m-%d %H:%M:%S"))) ------- difference between two dates, in seconds def dateDiffInSeconds(date1, date2): timedelta = date2 - date1 return timedelta.days*24*3600 + timedelta.seconds date1 = datetime.datetime(2006,02,17,15,30,00) date2 = datetime.datetime(2006,05,18,11,01,00) print dateDiffInSeconds(date1,date2) ======= import import gzip file = gzip.GzipFile("samples/sample.gz") print file.read() ----------- class gzipFile(gzip.GzipFile): # adds seek/tell support to GzipFile offset = 0 def read(self, size=None): data = gzip.GzipFile.read(self, size) self.offset = self.offset + len(data) return data def seek(self, offset, whence=0): # figure out new position (we can only seek forwards) if whence == 0: position = offset elif whence == 1: position = self.offset + offset else: raise IOError, "Illegal argument" if position < self.offset: raise IOError, "Cannot seek backwards" # skip forward, in 16k blocks while position > self.offset: if not self.read(min(position - self.offset, 16384)): break def tell(self): return self.offset # # try it file = gzipFile("samples/sample.gz") file.seek(80) print file.read() ======= import curses text = [ "a very simple curses demo", "", "(press any key to exit)" ] # connect to the screen screen = curses.initscr() # setup keyboard curses.noecho() # no keyboard echo curses.cbreak() # don't wait for newline # screen size rows, columns = screen.getmaxyx() # draw a border around the screen screen.border() # display centered text y = (rows - len(text)) / 2 for line in text: screen.addstr(y, (columns-len(line))/2, line) y = y + 1 screen.getch() curses.endwin() ======= file counter class Counter: def __init__(self): self.count = 0 def inc(self, n=1): self.count += n def count_names(count, dir, flist): count.inc(len(flist)) count = Counter() os.path.walk("/", count_names, count) print count.count,"files in all" ======= Fetch, read and parse a RSS 2.0 feed in 6 lines import urllib, sys, xml.dom.minidom address = 'http://www.sebsauvage.net/rss/updates.xml' document = xml.dom.minidom.parse(urllib.urlopen(address)) for item in document.getElementsByTagName('item'): title = item.getElementsByTagName('title')[0].firstChild.data print "Title:", title.encode('latin-1','replace') ======= redirect standard error to a file import sys class myLogger: def __init__(self): pass def write(self,data): file = open("mylog.txt","a") file.write(data) file.close() sys.stderr = myLogger() # Use my class to output errors instead of the console. print 5/0 # This will trigger an exception ======= Parsing: IP ipRegExC = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ipRegEx = re.compile(ipRegExC) ======= host info import os kernel, hostname, release, version, hardware = os.uname() import socket address = socket.gethostbyname(hostname) hostname = socket.gethostbyaddr(address) hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex(hostname) ======= socket import socket import os, os.path if os.path.exists("/tmp/mysock"): os.remove("/tmp/mysock") server = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) server.bind("/tmp/mysock") client = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) client.connect("/tmp/mysock") ------- ipaddr, port = s.getpeername() hostname, aliaslist, ipaddrlist = socket.gethostbyaddr(ipaddr) ipaddr = socket.gethostbyname('www.python.org') # '194.109.137.226' hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex('www.python.org') # ('fang.python.org', ['www.python.org'], ['194.109.137.226']) socket.gethostbyname_ex('www.google.org') # ('www.l.google.com', ['www.google.org', 'www.google.com'], # ['64.233.161.147','64.233.161.104', '64.233.161.99']) ======= import urllib2 urllib2.urlopen('http://whatismyip.org').read() ----- import uuid uuid.getnode() 67474971543L hex(uuid.getnode()) ======= =======