mystr = "\n" mystr = r"\n" mystr = "Jon 'Maddog' Orwant" mystr = 'Jon "Maddog" Orwant' mystr = 'Jon \'Maddog\' Orwant' mystr = "Jon \"Maddog\" Orwant" mystr = """
This is a multiline string literal
enclosed in triple double quotes.
"""
mystr = '''
And this is a multiline string literal
enclosed in triple single quotes.
'''
import struct
(lead, s1, s2), tail = struct.unpack("5s 3x 8s 8s", data[:24]), data[24:]
fivers = struct.unpack("5s" * (len(data)//5), data)
fivers = print [x[i*5:i*5+5] for i in range(len(x)/5)]
chars = list(data)
mystr = "This is what you have"
first = mystr[0] start = mystr[5:7] rest = mystr[13:] last = mystr[-1] end = mystr[-4:] piece = mystr[-8:-5] mystr = "This is what you have"
mystr = mystr[:5] + "wasn't" + mystr[7:]
mystr = "This is what you have"
mystr = mystr.replace(" is ", " wasn't ")
import array
mystr = array.array("c", "This is what you have")
mystr[5:7] = array.array("c", "wasn't")
from UserString import MutableString
mystr = MutableString("This is what you have")
mystr[-12:] = "ondrous"
if txt in mystr[-10:]:
print "'%s' found in last 10 characters"%txt
if mystr.startswith(txt):
print "%s starts with %s."%(mystr, txt)
if mystr.endswith(txt):
print "%s ends with %s."%(mystr, txt)
myvar = myvar or some_default
myvar2 = myvar or some_default
myvar |= some_default
myvar = default_value
if some_condition:
pass
myvar = somefunc()
if not myvar:
myvar = default_value
def myfunc(myvar="a"):
return myvar + "b"
print myfunc(), myfunc("c")
def myfunc(myvar=[]):
myvar.append("x")
return myvar
print myfunc(), myfunc()
def myfunc(myvar=None):
if myvar is None:
myvar = []
myvar.append("x")
return myvar
print myfunc(), myfunc()
a = b or c
if b:
a = b
else:
a = c
if not x:
x = y
try:
a = b
except NameError:
a = c
foo = bar or "DEFAULT VALUE"
import getpass
user = getpass.getuser()
import os
user = os.environ.get("USER")
if user is None:
user = os.environ.get("LOGNAME")
if not starting_point:
starting_point = "Greenwich"
if not a: a = b
if b: a = b
else:
a = c
v1, v2 = v2, v1
temp = a
a = b
b = temp
a = "alpha"
b = "omega"
a, b = b, a alpha, beta, production = "January March August".split()
alpha, beta, production = beta, production, alpha
num = ord(char)
char = chr(num)
char = "%c" % num
print "Number %d is character %c" % (num, num)
print "Number %(n)d is character %(n)c" % {"n": num}
print "Number %(num)d is character %(num)c" % locals()
ascii_character_numbers = [ord(c) for c in "sample"]
print ascii_character_numbers
word = "".join([chr(n) for n in ascii_character_numbers])
word = "".join([chr(n) for n in [115, 97, 109, 112, 108, 101]])
print word
hal = "HAL"
ibm = "".join([chr(ord(c)+1) for c in hal]) print ibm
mylist = list(mystr)
for char in mystr:
pass mystr = "an apple a day"
uniq = sorted(set(mystr))
print "unique chars are: '%s'" % "".join(uniq)
ascvals = [ord(c) for c in mystr]
print "total is %s for '%s'."%(sum(ascvals), mystr)
def checksum(myfile):
values = [ord(c) for line in myfile for c in line]
return sum(values)%(2**16) - 1
import fileinput
print checksum(fileinput.input())
print checksum(open("C:/test.txt") print checksum("sometext") import sys, select
import re
DELAY = 1
if re.match("^-\d+$",sys.argv[1]):
DELAY=-int(sys.argv[1])
del sys.argv[1]
for ln in fileinput.input():
for c in ln:
sys.stdout.write(c)
sys.stdout.flush()
select.select([],[],[], 0.005 * DELAY)
revchars = mystr[::-1] revwords = " ".join(mystr.split(" ")[::-1])
mylist = list(mystr)
mylist.reverse()
revbytes = "".join(mylist)
mylist = mystr.split()
mylist.reverse()
revwords = ' '.join(mylist)
revchars = "".join(reversed(mystr))
revwords = " ".join(reversed(mystr.split(" ")))
for char in reversed(mystr):
pass word = "reviver"
is_palindrome = (word == word[::-1])
def get_palindromes(fname):
for line in open(fname):
word = line.rstrip()
if len(word) > 5 and word == word[::-1]:
yield word
long_palindromes = list(get_palindromes("/usr/share/dict/words"))
def rev_string(mystr):
mylist = list(mystr)
mylist.reverse()
return "".join(mylist)
long_palindromes=[]
for line in open("/usr/share/dict/words"):
word = line.rstrip()
if len(word) > 5 and word == rev_string(word):
long_palindromes.append(word)
print long_palindromes
mystr.expandtabs()
mystr.expandtabs(4)
text = "I am %(rows)s high and %(cols)s long"%{"rows":24, "cols":80)
print text
rows, cols = 24, 80
text = "I am %(rows)s high and %(cols)s long"%locals()
print text
import re
print re.sub("\d+", lambda i: str(2 * int(i.group(0))), "I am 17 years old")
class SafeDict(dict):
def __getitem__(self, key):
return self.get(key, "[No Variable: %s]"%key)
hi = "Hello"
text = "%(hi)s and %(bye)s!"%SafeDict(locals())
print text
from string import Template
x = Template("$hi and $bye!")
hi = "Hello"
print x.safe_substitute(locals())
print x.substitute(locals())
mystr = "bo peep".upper() mystr = mystr.lower() mystr = mystr.capitalize() beast = "python"
caprest = beast.capitalize().swapcase() print "thIS is a loNG liNE".title()
if a.upper() == b.upper():
print "a and b are the same"
import random
def randcase_one(letter):
if random.randint(0,5): return letter.lower()
else:
return letter.upper()
def randcase(myfile):
for line in myfile:
yield "".join(randcase_one(letter) for letter in line[:-1])
for line in randcase(myfile):
print line
"I have %d guanacos." % (n + 1)
print "I have", n+1, "guanacos."
from string import Template
email_template = Template("""\
To: $address
From: Your Bank
CC: $cc_number
Date: $date
Dear $name,
Today you bounced check number $checknum to us.
Your account is now closed.
Sincerely,
the management
""")
import random
import datetime
person = {"address":"Joe@somewhere.com",
"name": "Joe",
"cc_number" : 1234567890,
"checknum" : 500+random.randint(0,99)}
print email_template.substitute(person, date=datetime.date.today())
var = """
your text
goes here
"""
import re
re_leading_blanks = re.compile("^\s+",re.MULTILINE)
var1 = re_leading_blanks.sub("",var)[:-1]
var2 = "\n".join([line.lstrip() for line in var.split("\n")[1:-1]])
poem = """
Here's your poem:
Now far ahead the Road has gone,
And I must follow, if I can,
Pursuing it with eager feet,
Until it joins some larger way
Where many paths and errand meet.
And whither then? I cannot say.
--Bilbo in /usr/src/perl/pp_ctl.c
"""
import textwrap
print textwrap.dedent(poem)[1:-1]
from textwrap import wrap
output = wrap(para,
initial_indent=leadtab
subsequent_indent=nexttab)
txt = """\
Folding and splicing is the work of an editor,
not a mere collection of silicon
and
mobile electrons!
"""
from textwrap import TextWrapper
wrapper = TextWrapper(width=20,
initial_indent=" "*4,
subsequent_indent=" "*2)
print "0123456789" * 2
print wrapper.fill(txt)
"""Expected result:
01234567890123456789
Folding and
splicing is the
work of an editor,
not a mere
collection of
silicon and mobile
electrons!
"""
from textwrap import fill
import fileinput
print fill("".join(fileinput.input()))
from termwrap import wrap
import struct, fcntl
def getheightwidth():
height, width = struct.unpack(
"hhhh", fcntl.ioctl(0, TERMIOS.TIOCGWINSZ ,"\000"*8))[0:2]
return height, width
import fileinput
import re
_, width = getheightwidth()
for para in re.split(r"\n{2,}", "".join(fileinput.input())):
print fill(para, width)
mystr = '''Mom said, "Don't do that."''' #"
re.sub("['\"]", lambda i: "\\" + i.group(0), mystr)
re.sub("[A-Z]", lambda i: "\\" + i.group(0), mystr)
re.sub("\W", lambda i: "\\" + i.group(0), "is a test!")
mystr = mystr.lstrip() mystr = mystr.rstrip() mystr = mystr.strip()
import csv
def parse_csv(line):
reader = csv.reader([line], escapechar='\\')
return reader.next()
line = '''XYZZY,"","O'Reilly, Inc","Wall, Larry","a \\"glug\\" bit,",5,"Error, Core Dumped,",''' #"
fields = parse_csv(line)
for i, field in enumerate(fields):
print "%d : %s" % (i, field)
import re
def parse_csv(text):
pattern = re.compile('''"([^"\\\]*(?:\\\.[^"\\\]*)*)",?|([^,]+),?|,''')
mylist = ["".join(elem)
for elem in re.findall(pattern, text)]
if text[-1] == ",":
mylist += ['']
return mylist
for fields in cvs.reader(lines, dialect="some"):
for num, field in enumerate(fields):
print num, ":", field
def soundex(name, len=4):
""" soundex module conforming to Knuth's algorithm
implementation 2000-12-24 by Gregory Jorgensen
public domain
"""
digits = '01230120022455012623010202'
sndx = ''
fc = ''
for c in name.upper():
if c.isalpha():
if not fc:
fc = c d = digits[ord(c)-ord('A')]
if not sndx or (d != sndx[-1]):
sndx += d
sndx = fc + sndx[1:]
sndx = sndx.replace('0','')
return (sndx + (len * '0'))[:len]
user = raw_input("Lookup user: ")
if user == "":
raise SystemExit
name_code = soundex(user)
for line in open("/etc/passwd"):
line = line.split(":")
for piece in line[4].split():
if name_code == soundex(piece):
print "%s: %s\n" % line[0], line[4])
import sys, fileinput, re
data = """\
analysed => analyzed
built-in => builtin
chastized => chastised
commandline => command-line
de-allocate => deallocate
dropin => drop-in
hardcode => hard-code
meta-data => metadata
multicharacter => multi-character
multiway => multi-way
non-empty => nonempty
non-profit => nonprofit
non-trappable => nontrappable
pre-define => predefine
preextend => pre-extend
re-compiling => recompiling
reenter => re-enter
turnkey => turn-key
"""
mydict = {}
for line in data.split("\n"):
if not line.strip():
continue
k, v = [word.strip() for word in line.split("=>")]
mydict[k] = v
pattern_text = "(" + "|".join([re.escape(word) for word in mydict.keys()]) + ")"
pattern = re.compile(pattern_text)
args = sys.argv[1:]
verbose = 0
if args and args[0] == "-v":
verbose = 1
args = args[1:]
if not args:
sys.stderr.write("%s: Reading from stdin\n" % sys.argv[0])
for line in fileinput.input(args, inplace=1, backup=".orig"):
output = ""
pos = 0
while True:
match = pattern.search(line, pos)
if not match:
output += line[pos:]
break
output += line[pos:match.start(0)] + mydict[match.group(1)]
pos = match.end(0)
sys.stdout.write(output)
import sys, os, re
class PsLineMatch:
fieldnames = ("flags","uid","pid","ppid","pri","nice","size", \
"rss","wchan","stat","tty","time","command")
numeric_fields = ("flags","uid","pid","ppid","pri","nice","size","rss")
def __init__(self):
self._fields = {}
def new_line(self, ln):
self._ln = ln.rstrip()
"""
F UID PID PPID PRI NI VSZ RSS WCHAN STAT TTY TIME COMMAND"
004 0 1 0 15 0 448 236 schedu S ? 0:07 init"
. . . . . . . . . . . . .
"""
data = self._ln.split(None,12)
for fn, elem in zip(self.fieldnames, data):
if fn in self.numeric_fields: self._fields[fn] = int(elem)
else:
self._fields[fn] = elem
def set_query(self, args):
conds=[]
m = re.compile("(\w+)([=<>]+)(.+)")
for a in args:
try:
(field,op,val) = m.match(a).groups()
except:
print "can't understand query \"%s\"" % (a)
raise SystemExit
if field in self.numeric_fields:
conds.append(a)
else:
conds.append("%s%s'%s'",(field,op,val))
self._desirable = compile("(("+")and(".join(conds)+"))", "<string>","eval")
def is_desirable(self):
return eval(self._desirable, {}, self._fields)
def __str__(self):
return self._ln
if len(sys.argv)<=1:
print """usage: %s criterion ...
Each criterion is a Perl expression involving:
%s
All criteria must be met for a line to be printed.""" \
% (sys.argv[0], " ".join(PsLineMatch().fieldnames))
raise SystemExit
psln = PsLineMatch()
psln.set_query(sys.argv[1:])
p = os.popen("ps wwaxl")
print p.readline()[:-1] for ln in p.readlines():
psln.new_line(ln)
if psln.is_desirable():
print psln
p.close()
try:
myfloat = float(mystr)
print "is a decimal number"
except TypeError:
print "is not a decimal number"
try:
myint = int(mystr)
print "is an integer"
except TypeError:
print "is not an integer"
if mystr.isdigit(): print 'is a positive integer'
else:
print 'is not'
if re.match("[+-]?\d+$", mystr): print 'is an integer'
else:
print 'is not'
if re.match("-?(?:\d+(?:\.\d*)?|\.\d+)$", mystr): print 'is a decimal number'
else:
print 'is not'
def equal(num1, num2, accuracy):
return abs(num1 - num2) < 10**(-accuracy)
from __future__ import division
wage = 536 week = 40 * wage print "One week's wage is: $%.2f" % (week/100)
rounded = round(num) a = 0.255
b = "%.2f" % a
print "Unrounded: %f\nRounded: %s" % (a, b)
print "Unrounded: %f\nRounded: %.2f" % (a, a)
from math import floor, ceil
print "number\tint\tfloor\tceil"
a = [3.3, 3.5, 3.7, -3.3]
for n in a:
print "% .1f\t% .1f\t% .1f\t% .1f" % (n, int(n), floor(n), ceil(n))
num = int('0110110', 2)
import baseconvert
def dec2bin(i):
return baseconvert.baseconvert(i, baseconvert.BASE10, baseconvert.BASE2)
binstr = dec2bin(54)
for i in range(x,y):
pass
for i in range(x, y, 7):
pass
print "Infancy is:",
for i in range(0,3):
print i,
print
print "Toddling is:",
for i in range(3,5):
print i,
print
print "Childhood is:",
i = 5
while i <= 12:
print i
i += 1
import random
rand = random.randint(x, y)
rand = random.randint(25, 76)
print rand
elt = random.choice(mylist)
import string
chars = string.letters + string.digits + "!@$%^&*"
password = "".join([random.choice(chars) for i in range(8)])
random.seed()
gen1 = random.Random(6)
gen2 = random.Random(6)
gen3 = random.Random(10)
a1, b1 = gen1.random(), gen1.random()
a2, b2 = gen2.random(), gen2.random()
a3, b3 = gen3.random(), gen3.random()
import random
mean = 25
sdev = 2
salary = random.gauss(mean, sdev)
print "You have been hired at %.2f" % salary
radians = math.radians(degrees)
degrees = math.degrees(radians)
from __future__ import division
import math
def deg2rad(degrees):
return (degrees / 180) * math.pi
def rad2deg(radians):
return (radians / math.pi) * 180
import math
def degree_sine(degrees):
radians = math.radians(degrees)
return math.sin(radians)
import math
def tan(theta):
return math.sin(theta) / math.cos(theta)
try:
y = math.tan(math.pi/2)
except ValueError:
y = None
import math
log_e = math.log(VALUE)
log_10 = math.log10(VALUE)
def log_base(base, value):
return math.log(value) / math.log(base)
answer = log_base(10, 10000)
print "log10(10,000) =", answer
import Numeric
a = Numeric.array( ((3, 2, 3),
(5, 9, 8) ), "d")
b = Numeric.array( ((4, 7),
(9, 3),
(8, 1) ), "d")
c = Numeric.matrixmultiply(a, b)
print c
print a.shape, b.shape, c.shape
a = 3+5j
b = 2-2j
c = a * b
print "c =", c
print c.real, c.imag, c.conjugate()
import cmath
print cmath.sqrt(3+4j)
number = int(hexadecimal, 16)
number = int(octal, 8)
s = hex(number)
s = oct(number)
num = raw_input("Gimme a number in decimal, octal, or hex: ").rstrip()
if num.startswith("0x"):
num = int(num[2:], 16)
elif num.startswith("0"):
num = int(num[1:], 8)
else:
num = int(num)
print "%(num)d %(num)x %(num)o\n" % { "num": num }
def commify(amount):
amount = str(amount)
firstcomma = len(amount)%3 or 3 first, rest = amount[:firstcomma], amount[firstcomma:]
segments = [first] + [rest[i:i+3] for i in range(0, len(rest), 3)]
return ",".join(segments)
print commify(12345678)
import re
def commify(amount):
amount = str(amount)
amount = amount[::-1]
amount = re.sub(r"(\d\d\d)(?=\d)(?!\d*\.)", r"\1,", amount)
return amount[::-1]
def pluralise(value, root, singular="", plural="s"):
if value == 1:
return root + singular
else:
return root + plural
print "It took", duration, pluralise(duration, 'hour')
print "%d %s %s enough." % (duration,
pluralise(duration, 'hour'),
pluralise(duration, '', 'is', 'are'))
import re
def noun_plural(word):
endings = [("ss", "sses"),
("([psc]h)", r"\1es"),
("z", "zes"),
("ff", "ffs"),
("f", "ves"),
("ey", "eys"),
("y", "ies"),
("ix", "ices"),
("([sx])", r"\1es"),
("", "s")]
for singular, plural in endings:
ret, found = re.subn("%s$"%singular, plural, word)
if found:
return ret
verb_singular = noun_plural;
import sys
def factorise(num):
factors = {}
orig = num
print num, '\t',
i, sqi = 2, 4
while sqi <= num:
while not num%i:
num /= i
factors[i] = factors.get(i, 0) + 1
sqi += 2*i + 1
i += 1
if num != 1 and num != orig:
factors[num] = factors.get(num, 0) + 1
if not factors:
print "PRIME"
for factor in sorted(factors):
if factor:
tmp = str(factor)
if factors[factor]>1: tmp += "**" + str(factors[factor])
print tmp,
print
if __name__ == '__main__':
if len(sys.argv) == 1:
print "Usage:", sys.argv[0], " number [number, ]"
else:
for strnum in sys.argv[1:]:
try:
num = int(strnum)
factorise(num)
except ValueError:
print strnum, "is not an integer"
def format_factor(base, exponent):
if exponent > 1:
return "%s**%s"%(base, exponent)
return str(base)
def factorise(num):
factors = {}
orig = num
i, sqi = 2, 4
while sqi <= num:
while not num%i:
num /= i
factors[i] = factors.get(i, 0) + 1
sqi += 2*i + 1
i += 1
if num not in (1, orig):
factors[num] = factors.get(num, 0) + 1
if not factors:
return ["PRIME"]
out = [format_factor(base, exponent)
for base, exponent in sorted(factors.items())]
return out
def print_factors(value):
try:
num = int(value)
if num != float(value):
raise ValueError
except (ValueError, TypeError):
raise ValueError("Can only factorise an integer")
factors = factorise(num)
print num, "\t", " ".join(factors)
import time
import datetime
print "Today is day", time.localtime()[7], "of the current year"
today = datetime.date.today()
print "Today is day", today.timetuple()[7], "of ", today.year
print "Today is day", today.strftime("%j"), "of the current year"
today = datetime.date.today()
print "The date is", today
print t.strftime("four-digit year: %Y, two-digit year: %y, month: %m, day: %d")
t = datetime.datetime.now()
print "Epoch Seconds:", time.mktime(t.timetuple())
t = datetime.datetime.utcnow()
print "Epoch Seconds:", time.mktime(t.timetuple())
now = datetime.datetime.fromtimestamp(EpochSeconds)
print now
print now.ctime()
oldtimetuple = time.localtime(EpochSeconds)
print oldtimetuple
now = datetime.date(2003, 8, 6)
difference1 = datetime.timedelta(days=1)
difference2 = datetime.timedelta(weeks=-2)
print "One day in the future is:", now + difference1
print "Two weeks in the past is:", now + difference2
print datetime.date(2003, 8, 6) - datetime.date(2000, 8, 6)
birthtime = datetime.datetime(1973, 01, 18, 3, 45, 50)
interval = datetime.timedelta(seconds=5, minutes=17, hours=2, days=55)
then = birthtime + interval
print "Then is", then.ctime()
print "Then is", then.strftime("%A %B %d %I:%M:%S %p %Y")
when = datetime.datetime(1973, 1, 18) + datetime.timedelta(days=55)
print "Nat was 55 days old on:", when.strftime("%m/%d/%Y").lstrip("0")
diff = date2 - date1
diff = datetime.date(year1, month1, day1) - datetime.date(year2, month2, day2)
bree = datetime.datetime(1981, 6, 16, 4, 35, 25)
nat = datetime.datetime(1973, 1, 18, 3, 45, 50)
difference = bree - nat
print "There were", difference, "minutes between Nat and Bree"
weeks, days = divmod(difference.days, 7)
minutes, seconds = divmod(difference.seconds, 60)
hours, minutes = divmod(minutes, 60)
print "%d weeks, %d days, %d:%d:%d" % (weeks, days, hours, minutes, seconds)
print "There were", difference.days, "days between Bree and Nat."
when = datetime.date(1981, 6, 16)
print "16/6/1981 was:"
print when.strftime("Day %w of the week (a %A). Day %d of the month (%B).")
print when.strftime("Day %j of the year (%Y), in week %W of the year.")
time.strptime("Tue Jun 16 20:18:03 1981")
time.strptime("16/6/1981", "%d/%m/%Y")
now = datetime.datetime(*time.strptime("16/6/1981", "%d/%m/%Y")[0:5])
print datetime.datetime.now().strftime("The date is %A (%a) %d/%m/%Y")
t1 = time.clock()
t2 = time.clock()
print t2 - t1
time.clock(); time.clock()
import timeit
code = '[x for x in range(10) if x % 2 == 0]'
eval(code)
t = timeit.Timer(code)
print "10,000 repeats of that code takes:", t.timeit(10000), "seconds"
print "1,000,000 repeats of that code takes:", t.timeit(), "seconds"
import timeit
code = 'import random; l = random.sample(xrange(10000000), 1000); l.sort()'
t = timeit.Timer(code)
print "Create a list of a thousand random numbers. Sort the list. Repeated a thousand times."
print "Average Time:", t.timeit(1000) / 1000
seconds = 3.1
time.sleep(seconds)
print "boo"
import datetime, email, email.Utils
import os, sys, time
def extract_date(hop):
date_string = hop[hop.find(';')+2:]
date_string = date_string.strip()
time_tuple = email.Utils.parsedate(date_string)
EpochSeconds = time.mktime(time_tuple)
dt = datetime.datetime.fromtimestamp(EpochSeconds)
return dt
def process(filename):
f = file(filename, 'rb')
msg = email.message_from_file(f)
hops = msg.get_all('received')
hops.reverse()
results = []
for hop in hops:
hop = hop.lower()
if hop.startswith('by'): sender = "start"
receiver = hop[3:hop.find(' ',3)]
date = extract_date(hop)
else: sender = hop[5:hop.find(' ',5)]
by = hop.find('by ')+3
receiver = hop[by:hop.find(' ', by)]
date = extract_date(hop)
results.append((sender, receiver, date))
output(results)
def output(results):
print "Sender, Recipient, Time, Delta"
print
previous_dt = delta = 0
for (sender, receiver, date) in results:
if previous_dt:
delta = date - previous_dt
print "%s, %s, %s, %s" % (sender,
receiver,
date.strftime("%Y/%d/%m %H:%M:%S"),
delta)
print
previous_dt = date
def main():
if len(sys.argv) != 2:
print "Usage: mailhop.py FILENAME"
else:
filename = sys.argv[1]
if os.path.isfile(filename):
process(filename)
else:
print filename, "doesn't seem to be a valid file."
if __name__ == '__main__':
main()
non_nested = ["this", "that", "the", "other"]
nested = ["this", "that", ["the", "other"]]
tune = ["The", "Star-Spangled", "Banner"]
a = ["quick", "brown", "fox"]
a = "Why are you teasing me?".split()
text = """
The boy stood on the burning deck,
It was as hot as glass.
"""
lines = [line.lstrip() for line in text.strip().split("\n")]
biglist = [line.rstrip() for line in open("mydatafile")]
banner = "The Mines of Moria"
banner = 'The Mines of Moria'
name = "Gandalf"
banner = "Speak, " + name + ", and enter!"
banner = "Speak, %s, and welcome!" % name
his_host = "www.python.org"
import os
host_info = os.popen("nslookup " + his_host).read()
python_info = os.popen("ps %d" % os.getpid()).read()
shell_info = os.popen("ps $$").read()
banner = ["Costs", "only", "$4.95"]
banner = "Costs only $4.95".split()
brax = """ ' " ( ) < > { } [ ] """.split() #"""
brax = list("""'"()<>{}[]""") #"""
rings = '''They're "Nenya Narya Vilya"'''.split() #'''
tags = 'LI TABLE TR TD A IMG H1 P'.split()
sample = r'The backslash (\) is often used in regular expressions.'.split()
banner = "The backslash (\\) is often used in regular expressions.".split()
ships = u"Niña Pinta Santa María".split() ships = [u"Niña", u"Pinta", u"Santa María"]
def commify_series(args):
n = len(args)
if n == 0:
return ""
elif n == 1:
return args[0]
elif n == 2:
return args[0] + " and " + args[1]
return ", ".join(args[:-1]) + ", and " + args[-1]
commify_series([])
commify_series(["red"])
commify_series(["red", "yellow"])
commify_series(["red", "yellow", "green"])
mylist = ["red", "yellow", "green"]
print "I have", mylist, "marbles."
print "I have", " ".join(mylist), "marbles."
data = (
( 'just one thing', ),
( 'Mutt Jeff'.split() ),
( 'Peter Paul Mary'.split() ),
( 'To our parents', 'Mother Theresa', 'God' ),
( 'pastrami', 'ham and cheese', 'peanut butter and jelly', 'tuna' ),
( 'recycle tired, old phrases', 'ponder big, happy thoughts' ),
( 'recycle tired, old phrases',
'ponder big, happy thoughts',
'sleep and dream peacefully' ),
)
def commify_series(terms):
for term in terms:
if "," in term:
sepchar = "; "
break
else:
sepchar = ", "
n = len(terms)
if n == 0:
return ""
elif n == 1:
return terms[0]
elif n == 2:
return " and ".join(terms)
return "%s%sand %s" % (sepchar.join(terms[:-1]), sepchar, terms[-1])
for item in data:
print "The list is: %s." % commify_series(item)
del mylist[size:] mylist += [None] * size
mylist.append(4)
mylist.insert(0, 10)
list2 = [1,2,3]
mylist.extend(list2)
mylist[1:1] = list2 mylist[2:3] = list2
del mylist[idx1:idx2] x = mylist.pop(idx)
def what_about_that_list(terms):
print "The list now has", len(terms), "elements."
print "The index of the last element is", len(terms)-1, "(or -1)."
print "Element #3 is %s." % terms[3]
people = "Crosby Stills Nash Young".split()
what_about_that_list(people)
people.pop()
what_about_that_list(people)
people += [None] * (10000 - len(people))
for item in mylist:
pass for user in bad_users:
complain(user)
import os
for (key, val) in sorted(os.environ.items()):
print "%s=%s" % (key, val)
for user in all_users:
disk_space = get_usage(user) if disk_space > MAX_QUOTA: complain(user) import os
for line in os.popen("who"):
if "dalke" in line:
print line,
print "".join([line for line in os.popen("who")
if "dalke" in line]),
for line in myfile:
for word in line.split(): print word[::-1], print
for line in myfile:
for word in line.split(): chars = list(word) chars.reverse()
print "".join(chars),
print
for item in mylist:
print "i =", item
data = [1, 2, 3]
data = [i-1 for i in data]
print data
for i, elem in enumerate(data):
data[i] = elem - 1
s = s.strip()
data = [s.strip() for s in data]
for k, v in mydict.items():
mydict[k] = v.strip()
fruits = ["Apple", "Blackberry"]
for fruit in fruits:
print fruit, "tastes good in a pie."
for i in range(len(fruits)):
print fruits[i], "tastes good in a pie."
for i, fruit in enumerate(fruits):
print "%s) %s tastes good in a pie."%(i+1, fruit)
rogue_cats = ["Morris", "Felix"]
namedict = { "felines": rogue_cats }
for cat in namedict["felines"]:
print cat, "purrs hypnotically."
print "--More--\nYou are controlled."
for i in range(len(namedict["felines"])):
print namedict["felines"][i], "purrs hypnotically."
uniq = list(set(mylist))
seen = {}
for item in mylist:
seen[item] = seen.get(item, 0) + 1
uniq = seen.keys()
seen = {}
uniq = []
for item in mylist:
count = seen.get(item, 0)
if count == 0:
uniq.append(item)
seen[item] = count + 1
import os
usernames = [line.split()[0] for line in os.popen("who")]
uniq = sorted(set(usernames))
print "users logged in:", " ".join(uniq)
import os
ucnt = {}
for line in os.popen("who"):
username = line.split()[0] ucnt[username] = ucnt.get(username, 0) + 1
users = ucnt.keys()
users.sort()
print "users logged in:", " ".join(users)
aonly = [item for item in a_list if item not in b_list]
a_set = set(a_list)
b_set = set(b_list)
aonly = list(a_set - b_set)
seen = {} aonly = []
for item in b_list:
seen[item] = 1
for item in a_list:
if not item not in seen:
aonly.append(item)
seen = {} aonly = []
[seen.update({x: 1}) for x in b_list]
aonly = [item for item in a_list if item not in seen]
aonly = list(set(a_list))
seen = {}
aonly = []
for item in a_list:
if item not in seen:
aonly.append(item)
seen[item] = 1 mydict["key1"] = 1
mydict["key2"] = 2
mydict[("key1", "key2")] = (1,2)
seen = dict.fromkeys(B.keys())
seen = {}
for term in B:
seen[term] = None
seen = {}
for k, v in B:
seen[k] = 1
a = (1, 3, 5, 6, 7, 8)
b = (2, 3, 5, 7, 9)
a_set = set(a)
b_set = set(b)
union = a_set | b_set isect = a_set & b_set diff = a_set ^ b_set
union_list = []; isect_list = []; diff = []
union_dict = {}; isect_dict = {}
count = {}
for e in a:
union_dict[e] = 1
for e in b:
if union_dict.has_key(e):
isect_dict[e] = 1
union_dict[e] = 1
union_list = union_dict.keys()
isect_list = isect_dict.keys()
for e in a + b:
if union.get(e, 0) == 0:
isect[e] = 1
union[e] = 1
union = union.keys()
isect = isect.keys()
count = {}
for e in a + b:
count[e] = count.get(e, 0) + 1
union = []; isect = []; diff = []
for e in count.keys():
union.append(e)
if count[e] == 2:
isect.append(e)
else:
diff.append(e)
isect = []; diff = []; union = []
count = {}
for e in a + b:
count[e] = count.get(e, 0) + 1
for e, num in count.items():
union.append(e)
[None, diff, isect][num].append(e)
mylist1.extend(mylist2)
mylist1 = mylist1 + mylist2
mylist1 += mylist2
members = ["Time", "Flies"]
initiates = ["An", "Arrow"]
members.extend(initiates)
members[2:] = ["Like"] + initiates
print " ".join(members)
members[:1] = ["Fruit"] members[-2:] = ["A", "Banana"]
print " ".join(members)
revlist = mylist[::-1]
revlist = list(reversed(mylist))
revlist = mylist[:] revlist.reverse()
for elem in reversed(mylist):
pass
for elem in mylist[::-1]:
pass
for i, elem in reversed(list(enumerate(mylist))):
pass
for i in range(len(mylist)-1, -1, -1):
pass
descending = sorted(users, reverse=True)
mylist[:n] = []
front, mylist[:n] = mylist[:n], []
front = mylist.pop(0)
mylist[-n:] = []
end, mylist[-n:] = mylist[-n:], []
end = mylist.pop()
def shift2(terms):
front = terms[:2]
terms[:2] = []
return front
def pop2(terms):
back = terms[-2:]
terms[-2:] = []
return back
friends = "Peter Paul Mary Jim Tim".split()
this, that = shift2(friends)
beverages = "Dew Jolt Cola Sprite Fresca".split()
pair = pop2(beverages)
for item in mylist:
if criterion:
pass break
else:
pass for idx, elem in enumerate(mylist):
if criterion:
pass break
else:
pass for employee in employees:
if employee.category == 'engineer':
highest_engineer = employee
break
print "Highest paid engineer is:", highest_engineer.name
for i, employee in enumerate(employees):
if employee.category == 'engineer':
highest_engineer = employee
break
print "Highest paid engineer is: #%s - %s" % (i, highest_engineer.name)
for i in range(len(mylist)):
if criterion:
pass break
else:
pass
matching = [term for term in mylist if test(term)]
matching = []
for term in mylist:
if test(term):
matching.append(term)
bigs = [num for num in nums if num > 1000000]
pigs = [user for (user, val) in users.items() if val > 1e7]
import os
matching = [line for line in os.popen("who")
if line.startswith("gnat ")]
engineers = [employee for employee in employees
if employee.position == "Engineer"]
secondary_assistance = [applicant for applicant in applicants
if 26000 <= applicant.income < 30000]
sorted_list = sorted(unsorted_list)
import os, signal, time
for pid in sorted(pids):
print pid
pid = raw_input("Select a process ID to kill: ")
try:
pid = int(pid)
except ValueError:
raise SystemExit("Exiting ... ")
os.kill(pid, signal.SIGTERM)
time.sleep(2)
try:
os.kill(pid, signal.SIGKILL)
except OSError, err:
if err.errno != 3: raise
descending = sorted(unsorted_list, reverse=True)
allnums = [4, 19, 8, 3]
allnums.sort(reverse=True) allnums.sort() allnums.reverse() allnums = sorted(allnums, reverse=True)
ordered = sorted(unordered, cmp=compare)
ordered = sorted(unordered, key=compute)
precomputed = [(compute(x), x) for x in unordered]
precomputed.sort(lambda a, b: cmp(a[0], b[0]))
ordered = [v for k,v in precomputed.items()]
def functional_sort(mylist, function):
mylist.sort(function)
return mylist
ordered = [v for k,v in functional_sort([(compute(x), x) for x in unordered],
lambda a, b: cmp(a[0], b[0]))]
ordered = sorted(employees, key=lambda x: x.name)
for employee in sorted(employees, key=lambda x: x.name):
print "%s earns $%s" % (employee.name, employee.salary)
sorted_employees = sorted(employees, key=lambda x: x.name):
for employee in sorted_employees:
print "%s earns $%s" % (employee.name, employee.salary)
for employee in sorted_employees:
if bonus(employee.ssn):
print employee.name, "got a bonus!"
sorted_employees = sorted(employees, key=lambda x: (x.name, x.age)):
import pwd
users = pwd.getpwall()
for user in sorted(users, key=lambda x: x[0]):
print user[0]
sorted_list = sorted(names, key=lambda x: x[:1])
sorted_list = sorted(strings, key=len)
temp = [(len(s), s) for s in strings]
temp.sort(lambda a, b: cmp(a[0], b[0]))
sorted_list = [x[1] for x in temp]
def functional_sort(mylist, function):
mylist.sort(function)
return mylist
sorted_fields = [v for k,v in functional_sort(
[(int(re.search(r"(\d+)", x).group(1)), x) for x in fields],
lambda a, b: cmp(a[0], b[0]))]
entries = [line[:-1].split() for line in open("/etc/passwd")]
for entry in sorted(entries, key=lambda x: (x[3], x[2], x[0])):
print entry
import itertools
for process in itertools.cycle([1, 2, 3, 4, 5]):
print "Handling process", process
time.sleep(1)
import time
class Circular(object):
def __init__(self, data):
assert len(data) >= 1, "Cannot use an empty list"
self.data = data
def __iter__(self):
while True:
for elem in self.data:
yield elem
circular = Circular([1, 2, 3, 4, 5])
for process in circular:
print "Handling process", process
time.sleep(1)
import time
class Circular(object):
def __init__(self, data):
assert len(data) >= 1, "Cannot use an empty list"
self.data = data
def next(self):
head = self.data.pop(0)
self.data.append(head)
return head
circular = Circular([1, 2, 3, 4, 5])
while True:
process = circular.next()
print "Handling process", process
time.sleep(1)
import random
random.shuffle(mylist)
import sys
def make_columns(mylist, screen_width=78):
if mylist:
maxlen = max([len(elem) for elem in mylist])
maxlen += 1
cols = max(1, screen_width/maxlen)
rows = 1 + len(mylist)/cols
mask = "%%-%ds " % (maxlen-1)
for n in range(rows):
row = [mask%elem
for elem in mylist[n::rows]]
yield "".join(row).rstrip()
for row in make_columns(sys.stdin.readlines(), screen_width=50):
print row
import sys
def EOL(item):
return (item+1) % cols == 0
def getwinsize():
try:
import curses
stdscr = curses.initscr()
rows, cols = stdscr.getmaxyx()
return cols
except ImportError:
pass
try:
import termios
TIOCGWINSZ = termios.TIOCGWINSZ
except ImportError:
TIOCGWINSZ = 0x40087468
import struct, fcntl
s = struct.pack("HHHH", 0, 0, 0, 0)
try:
x = fcntl.ioctl(sys.stdout.fileno(), TIOCGWINSZ, s)
except IOError:
return 80
rows, cols = struct.unpack("HHHH", x)[:2]
return cols
cols = getwinsize()
data = [s.rstrip() for s in sys.stdin.readlines()]
if not data:
maxlen = 1
else:
maxlen = max(map(len, data))
maxlen += 1
cols = (cols / maxlen) or 1
rows = (len(data)+cols) / cols
mask = "%%-%ds " % (maxlen-1)
for item in range(rows * cols):
target = (item % cols) * rows + (item/cols)
if target < len(data):
piece = mask % data[target]
else:
piece = mask % ""
if EOL(item):
piece = piece.rstrip() sys.stdout.write(piece)
if EOL(item):
sys.stdout.write("\n")
if EOL(item):
sys.stdout.write("\n")
def factorial(n):
s = 1
while n:
s *= n
n -= 1
return s
def permute(alist, blist=[]):
if not alist:
yield blist
for i, elem in enumerate(alist):
for elem in permute(alist[:i] + alist[i+1:], blist + [elem]):
yield elem
for permutation in permute(range(4)):
print permutation
import fileinput
def print_list(alist, blist=[]):
if not alist:
print ' '.join(blist)
for i in range(len(alist)):
blist.append(alist.pop(i))
print_list(alist, blist)
alist.insert(i, blist.pop())
for line in fileinput.input():
words = line.split()
print_list(words)
class FactorialMemo(list):
def __init__(self):
self.append(1)
def __call__(self, n):
try:
return self[n]
except IndexError:
ret = n * self(n-1)
self.append(ret)
return ret
factorial = FactorialMemo()
import sys
import time
sys.setrecursionlimit(10000)
start = time.time()
factorial(2000)
f1 = time.time() - start
factorial(2100) f2 = time.time() - f1 - start
print "Slow first time:", f1
print "Quicker the second time:", f2
class MemoizedPermutations(list):
def __init__(self, alist):
self.permute(alist, [])
def permute(self, alist, blist):
if not alist:
self.append(blist)
for i, elem in enumerate(alist):
self.permute(alist[:i] + alist[i+1:], blist + [elem])
def __call__(self, seq, idx):
return [seq[n] for n in self[idx]]
p5 = MemoizedPermutations(range(5))
words = "This sentence has five words".split()
print p5(words, 17)
print p5(words, 81)
age = {"Nat": 24,
"Jules": 24,
"Josh": 17}
age = {}
age["Nat"] = 24
age["Jules"] = 25
age["Josh"] = 17
food_color = {"Apple": "red",
"Banana": "yellow",
"Lemon": "yellow",
"Carrot": "orange"
}
mydict[key] = value
food_color["Raspberry"] = "pink"
print "Known foods:"
for food in food_color:
print food
if key in mydict:
pass else:
pass
for name in ("Banana", "Martini"):
if name in food_color:
print name, "is a food."
else:
print name, "is a drink."
age = {}
age["Toddler"] = 3
age["Unborn"] = 0
age["Phantasm"] = None
for thing in ("Toddler", "Unborn", "Phantasm", "Relic"):
print ("%s:"%thing),
if thing in age:
print "Exists",
if age[thing] is not None:
print "Defined",
if age[thing]:
print "True",
print
import fileinput, os
size = {}
for line in fileinput.input():
filename = line.rstrip()
if filename in size:
continue
size[filename] = os.path.getsize(filename)
del mydict[key]
def print_foods():
foods = food_color.keys()
print "Keys:", " ".join(foods)
print "Values:",
for food in foods:
color = food_color[food]
if color is not None:
print color,
else:
print "(undef)",
print
print "Initially:"
print_foods()
print "\nWith Banana set to None"
food_color["Banana"] = None
print_foods()
print "\nWith Banana deleted"
del food_color["Banana"]
print_foods()
for key in ["Banana", "Apple", "Cabbage"]:
del food_color[key]
for key, value in mydict.items():
pass
for key, value in mydict.iteritems():
pass
for key in mydict.keys():
value = mydict[key]
for food, color in food_color.items():
print "%s is %s." % (food, color)
for food in food_color:
color = food_color[food]
print "%s is %s." % (food, color)
print """%(food)s
is
%(color)s.
""" % vars()
for food, color in sorted(food_color.items()):
print "%s is %s." % (food, color)
import sys
if len(sys.argv) > 1:
infile = open(sys.argv[1])
else:
infile = sys.stdin
counts = {}
for line in infile:
if line.startswith("From: "):
name = line[6:-1]
counts[name] = counts.get(name, 0) + 1
for (name, count) in sorted(counts.items()):
print "%s: %s" % (name, count)
for key, val in mydict.items():
print key, "=>", val
print "\n".join([("%s => %s" % item) for item in mydict.items()])
print mydict
import pprint
pprint.pprint(dict)
class SequenceDict(dict):
"""
Dictionary that remembers the insertion order.
The lists returned by keys(), values() and items() are
in the insertion order.
"""
def __init__(self, *args):
self._keys={} self._ids={} self._next_id=0
def __setitem__(self, key, value):
self._keys[key]=self._next_id
self._ids[self._next_id]=key
self._next_id+=1
return dict.__setitem__(self, key, value)
def __delitem__(self, key):
id=self._keys[key]
del(self._keys[key])
del(self._ids[id])
return dict.__delitem__(self, key)
def values(self):
values=[]
ids=list(self._ids.items())
ids.sort()
for id, key in ids:
values.append(self[key])
return values
def items(self):
items=[]
ids=list(self._ids.items())
ids.sort()
for id, key in ids:
items.append((key, self[key]))
return items
def keys(self):
ids=list(self._ids.items())
ids.sort()
keys=[]
for id, key in ids:
keys.append(key)
return keys
def update(self, d):
for key, value in d.items():
self[key]=value
def clear(self):
dict.clear(self)
self._keys={}
self._ids={}
self._next_id=0
def testSequenceDict():
sd=SequenceDict()
sd[3]="first"
sd[2]="second"
sd[1]="third"
print sd.keys()
print sd.items()
print sd.values()
del(sd[1])
del(sd[2])
del(sd[3])
print sd.keys(), sd.items(), sd.values()
print sd._ids, sd._keys
print "---------------"
sd["b"]="first"
sd["a"]="second"
sd.update({"c": "third"})
print sd.keys()
print sd.items()
print sd.values()
del(sd["b"])
del(sd["a"])
del(sd["c"])
print sd.keys(), sd.items(), sd.values()
print sd._ids, sd._keys
def likePerlCookbook():
food_color=SequenceDict()
food_color["Banana"]="Yellow";
food_color["Apple"]="Green";
food_color["Lemon"]="Yellow"
print "In insertion order, the foods' color are:"
for food, color in food_color.items():
print "%s is colored %s" % (food, color)
if __name__=="__main__":
likePerlCookbook()
import os
ttys = {}
who = os.popen("who")
for line in who:
user, tty = line.split()[:2]
ttys.setdefault(user, []).append(tty)
for (user, tty_list) in sorted(ttys.items()):
print user + ": " + " ".join(tty_list)
import pwd
for (user, tty_list) in ttys.items():
print user + ":", len(tty_list), "ttys."
for tty in sorted(tty_list):
try:
uid = os.stat("/dev/" + tty).st_uid
user = pwd.getpwuid(uid)[0]
except os.error:
user = "(not available)"
print "\t%s (owned by %s)" % (tty, user)
reverse = dict([(val, key) for (key, val) in lookup_dict.items()])
surname = {"Mickey": "Mantle", "Babe": "Ruth"}
first_name = dict([(last, first) for (first, last) in surname.items()])
print first_name["Mantle"]
import sys
if not sys.argv[1:]:
raise SystemExit("usage: foodfind food_or_color")
given = sys.argv[1]
color_dict = {"Apple": "red",
"Banana": "yellow",
"Lemon": "yellow",
"Carrot": "orange",
}
food_dict = dict([(color, food) for (food, color) in color_dict.items()])
if given in color_dict:
print given, "is a food with color", color_dict[given]
elif given in food_dict:
print food_dict[given], "is a food with color", given
foods_with_color = {}
for food, color in food_color.items():
foods_with_color.setdefault(color, []).append(food)
print " ".join(foods_with_color["yellow"]), "were yellow foods."
for key, value in sorted(mydict.items()):
for food, color in sorted(food_color.items()):
print "%s is %s." % (food, color)
for item in sorted(food_color.items()):
print "%s is %s." % item
def food_cmp(x, y):
return cmp(x, y)
for food, color in sorted(food_color, cmp=food_cmp):
print "%s is %s." % (food, color)
def food_len_cmp(x, y):
return cmp(len(x), len(y))
for food in sorted(food_color, cmp=food_len_cmp):
print "%s is %s." % (food, food_color[food])
for food in sorted(food_color, key=len):
print "%s is %s." % (food, food_color[food])
merged = {}
merged.update(a_dict)
merged.update(b_dict)
merged = a_dict.copy()
merged.update(b_dict)
merged = {}
for k, v in a_dict.items():
merged[k] = v
for k, v in b_dict.items():
merged[k] = v
drink_color = {"Galliano": "yellow",
"Mai Tai": "blue"}
ingested_color = drink_color.copy()
ingested_color.update(food_color)
drink_color = {"Galliano": "yellow",
"Mai Tai": "blue"}
substance_color = {}
for k, v in food_color.items():
substance_color[k] = v
for k, v in drink_color.items():
substance_color[k] = v
substance_color = {}
for mydict in (food_color, drink_color):
for k, v in mydict:
substance_color[k] = v
substance_color = {}
for item in food_color.items() + drink_color.items():
for k, v in mydict:
substance_color[k] = v
substance_color = {}
for mydict in (food_color, drink_color):
for k, v in mydict.items():
if substance_color.has_key(k):
print "Warning:", k, "seen twice. Using the first definition."
continue
substance_color[k] = v
all_colors = new_colors.copy()
common = [k for k in dict1 if k in dict2]
this_not_that = [k for k in dict1 if k not in dict2]
citrus_color = {"Lemon": "yellow",
"Orange": "orange",
"Lime": "green"}
non_citrus = [k for k in food_color if k not in citruscolor]
name = {}
for filename in ("/etc/termcap", "/vmunix", "/bin/cat"):
try:
myfile = open(filename)
except IOError:
pass
else:
names[myfile] = filename
print "open files:", ", ".join(name.values())
for f, fname in name.items():
f.seek(0, 2) print "%s is %d bytes long." % (fname, f.tell())
count = {}
for element in mylist:
count[element] = count.get(element, 0) + 1
import fileinput
father = {'Cain': 'Adam',
'Abel': 'Adam',
'Seth': 'Adam',
'Enoch': 'Cain',
'Irad': 'Enoch',
'Mehujael': 'Irad',
'Methusael': 'Mehujael',
'Lamech': 'Methusael',
'Jabal': 'Lamech',
'Tubalcain': 'Lamech',
'Enos': 'Seth',
}
for line in fileinput.input():
person = line.rstrip()
while person: print person, person = father.get(person) print
import fileinput
children = {}
for k, v in father.items():
children.setdefault(v, []).append(k)
for line in fileinput.input():
person = line.rstrip()
kids = children.get(person, ["nobody"])
print person, "begat", ", ".join(kids)
import sys, re
pattern = re.compile(r'^\s*#\s*include\s*<([^>]+)')
includes = {}
for filename in filenames:
try:
infile = open(filename)
except IOError, err:
print>>sys.stderr, err
continue
for line in infile:
match = pattern.match(line)
if match:
includes.setdefault(match.group(1), []).append(filename)
mydict = {}
for e in reduce(lambda a,b: a + b, includes.values()):
if not includes.has_key(e):
mydict[e] = 1
include_free = mydict.keys()
include_free.sort()
import os, sys
def get_input(args):
cmd = "du " + " ".join(args)
infile = os.popen(cmd)
dirsize = {}
kids = {}
for line in infile:
size, name = line[:-1].split("\t", 1)
dirsize[name] = int(size)
parent = os.path.dirname(name)
kids.setdefault(parent, []).append(name)
kids[parent].pop()
if not kids[parent]:
del kids[parent]
return name, dirsize, kids
def getdots(root, dirsize, kids):
size = cursize = dirsize[root]
if kids.has_key(root):
for kid in kids[root]:
cursize -= dirsize[kid]
getdots(kid, dirsize, kids)
if size != cursize:
dot = root + "/."
dirsize[dot] = cursize
kids[root].append(dot)
def output(root, dirsize, kids, prefix = "", width = 0):
path = os.path.basename(root)
size = dirsize[root]
fmt = "%" + str(width) + "d %s"
line = fmt % (size, path)
print prefix + line
prefix += (" " * (width-1)) + "| " + (" " * len(path))
if kids.has_key(root):
kid_list = kids[root]
kid_list.sort(lambda x, y, dirsize=dirsize:
cmp(dirsize[x], dirsize[y]))
width = len(str(dirsize[kid_list[-1]]))
for kid in kid_list:
output(kid, dirsize, kids, prefix, width)
def main():
root, dirsize, kids = get_input(sys.argv[1:])
getdots(root, dirsize, kids)
output(root, dirsize, kids)
if __name__ == "__main__":
main()
import re re.search("sheep",meadow) if not re.search("sheep",meadow):
print "no sheep on this meadow only a fat python."
meadow = meadow.replace("old","new") re.search("ovine",meadow)
meadow = """Fine bovines demand fine toreadors.
Muskoxen are polar ovibovine species.
Grooviness went out of fashion decades ago."""
meadow = "Ovines are found typically in ovaries."
if re.search(r"\bovines\b",meadow,re.I) : print "Here be sheep!"
mystr = "good food"
re.sub("o*","e",mystr,1)
echo ababacaca | python -c "import sys,re; print re.search('(a|ba|b)+(a|ac)+',sys.stdin.read()).group()"
import re, fileinput
for ln = fileinput.input():
fnd = re.findall("(\d+)",ln)
if len(fnd) > 0:
print "Found number %s" % (fnd[0])
digits = "123456789"
nonlap = re.findall("(\d\d\d)", digits)
yeslap = ["not yet"]
print "Non-overlapping:",",".join(nonlap)
print "Overlapping :",",".join(yeslap)
mystr = "And little lambs eat ivy"
fnd = re.search("(l[^s]*s)", mystr)
print "(%s) (%s) (%s)" % (mystr[:fnd.start()], fnd.group(), mystr[fnd.end():])
import re
dst = re.sub("this","that",src)
basename = re.sub(".*/(?=[^/]+)","",progname)
def cap(mo): return mo.group().capitalize()
re.sub("(?P<n>\w+)",cap,"make all words title-cased")
manpage = "/usr/man/man3/foo.1"
catpage = re.sub("man(?=\d)","cat",manpage)
bindirs = "/usr/bin /bin /usr/local/bin".split()
libdirs = [d.replace("bin", "lib") for d in bindirs]
print " ".join(libdirs)
import re
if re.match("^[A-Za-z]+$",line):
print "pure alphabetic"
if re.match(r"^[^\W\d_]+$", line, re.LOCALE):
print "pure alphabetic"
import re
import locale
try:
locale.setlocale(locale.LC_ALL, 'fr_CA.ISO8859-1')
except:
print "couldn't set locale to French Cnadian"
raise SystemExit
DATA="""
silly
façade
coöperate
niño
Renée
Molière
hæmoglobin
naïve
tschüß
random!stuff#here
"""
for ln in DATA.split():
ln = ln.rstrip()
if re.match(r"^[^\W\d_]+$",ln,re.LOCALE):
print "%s: alphabetic" % (ln)
else:
print "%s: line noise" % (ln)
"\S+" "[A-Za-z'-]+"
"A text with some\tseparator".split()
"\b*([A-Za-z]+)\b*" "\s*([A-Za-z]+)\s*"
re.search("\Bis\B","this thistle") re.search("\Bis\B","vis-a-vis")
import socket import fileinput
import re
match = re.compile("""(?P<hostname> # capture hostname
(?: # these parens for grouping only
[\w-]+ # hostname component
\. # ant the domain dot
) + # now repeat that whole thing a bunch of times
[A-Za-z] # next must be a letter
[\w-] + # now trailing domain part
) # end of hostname capture
""",re.VERBOSE)
def repl(match_obj):
orig_hostname = match_obj.group("hostname")
try:
addr = socket.gethostbyname(orig_hostname)
except socket.gaierror:
addr = "???"
return "%s [%s]" % (orig_hostname, addr)
for ln in fileinput.input():
print match.sub(repl, ln)
re.sub("""(?x) # nicer formatting
\# # a pound sign
(\w+) # the variable name
\# # another pound sign
""",
lambda m: eval(m.group(1)), line
)
re.sub("""(?x) # nicer formatting
\# # a pound sign
(\w+) # the variable name
\# # another pound sign
""",
lambda m: eval(eval(m.group(1))), line
)
import re
pond = "one fish two fish red fish blue fish"
fishes = re.findall(r"(?i)(\w+)\s+fish\b",pond)
if len(fishes)>2:
print "The third fish is a %s one." % (fishes[2])
re.findall(r"(?i)(?:\w+\s+fish\s+){2}(\w+)\s+fish",pond)
count = 0
for match_object in re.finditer(r"PAT", mystr):
count += 1
count = len(re.findall(r"PAT",mystr))
count = len(re.findall(r"aba","abaababa"))
count = len(re.findall(r"(?=aba)","abaababa"))
"abaababa".count("aba")
pond = "one fish two fish red fish blue fish"
colors = re.findall(r"(?i)(\w+)\s+fish\b",pond) color = colors[2]
color = re.findall(r"(?i)(\w+)\s+fish\b",pond)[2]
print "The third fish in the pond is %s." % (color)
import re
pond = "one fish two fish red fish blue fish"
matches = re.findall(r"(\w+)\s+fish\b",pond)
evens = [fish for (i, fish) in enumerate(matches) if i%2]
print "Even numbered fish are %s." % (" ".join(evens))
count = 0
def four_is_sushi(match_obj):
global count
count += 1
if count==4:
return "sushi%s" % (match_obj.group(2))
return "".join(match_obj.groups())
re.sub(r"""(?x) # VERBOSE
\b # makes next \w more efficient
( \w+ ) # this is what we'll be changing
(
\s+ fish \b
)""",
four_is_sushi,
pond)
last_fish = re.findall(r"(?i).*\b(\w+)\s+fish\b",pond)
pond = "One fish two fish red fish blue fish swim here"
color = re.findall(r"(?i)\b(\w+)\s+fish\b",pond)[-1]
print "Last fish is "+color+"."
lastfish = pond.rfind("fish")
color = pond[:lastfish].split()[-1]
r"""(?x)
A # find some pattern A
(?! # mustn't be able to find
.* # something
A # and A
)
$ # through the end of string
"""
pond = "One fish two fish red fish blue fish swim here"
fnd = re.findall(r"""(?xis) # VERBOSE, CASEINSENSITIVE, DOTALL
\b ( \w+ ) \s+ fish \b
(?! .* \b fish \b )""",
pond)
if len(fnd):
print "Last fish is %s." % (fnd[0])
else:
print "Failed!"
import re
import sys
text = open(sys.argv[1]).read() text = re.sub("(?ms)<.*?>","",text) print text
import sys, re
match = re.compile(r"""(?xms) # re.VERBOSE, re.MULTILINE, and re.DOTALL
\A # start of the string
(?P<chapter> # capture in g<chapter>
Chapter # literal string
\s+ # mandatory whitespace
\d+ # decimal number
\s* # optional whitespace
: # a real colon
. * # anything not a newline till end of line
)
""")
text = open(sys.argv[1]).read() for paragraph in text.split("\n"): p = match.sub("<h1>\g<chapter></h1>",paragraph)
print p
match = re.compile(r"(?ms)^START(.*?)^END")
chunk = 0
for paragraph in open(sys.argv[1]).read().split("\n\n"):
chunk += 1
fnd = match.findall(paragraph)
if fnd:
print "chunk %d in %s has <<%s>>" % (chunk,sys.argv[1],">>,<<".join(fnd))
import sys
chunks = open(sys.argv[1]).read().split() chunks = open(sys.argv[1]).read().split("\n")
import re
pattern = r"x"
chunks = re.split(pattern, open(sys.argv[1]).read())
chunks = re.split(r"(?m)^\.(Ch|Se|Ss)$",open(sys.argv[1]).read())
print "I read %d chunks." % (len(chunks))
chunks = re.split(r"(?m)^\.(?:Ch|Se|Ss)$",open(sys.argv[1]).read())
chunks = re.split(r"(?m)^(\.(?:Ch|Se|Ss))$",open(sys.argv[1]).read())
chunks = re.findall(r"""(?xms) # multiline, dot matches lineend, allow comments
((?:^\.)? # consume the separator if present
.*?) # match everything but not greedy
(?= # end the match on this but dont consume it
(?: # dont put into group [1]
^\.(?:Ch|Se|Ss)$ # either end on one of the roff commands
|\Z # or end of text
)
)""",
open(sys.argv[1]).read())
for i, line in enumerate(myfile):
if firstlinenum <= i < lastlinenum:
dosomethingwith(line)
import re
for line in myfile:
if re.match(pat1, line):
break
dosomethingwith(line)
for line in myfile:
if re.match(pat2, line):
break
dosomethingwith(line)
def extract_range(myfile, start, finish):
for i, line in enumerate(myfile):
if start <= i < finish:
yield line
elif i == finish:
break
for line in extract_range(open("/etc/passwd"), 3, 5):
print line
def patterned_range(myfile, startpat, endpat=None):
startpat = re.compile(startpat)
if endpat is not None:
endpat = re.compile(endpat)
in_range = False
for line in myfile:
if re.match(startpat, line):
in_range = True
if in_range:
yield line
if endpat is not None and re.match(endpat, line):
break
for line in patterned_range(msg, "^From:?", "^$"):
pass
tests = (("list.?",r"^list\..$"),
("project.*",r"^project\..*$"),
("*old",r"^.*old$"),
("type*.[ch]",r"^type.*\.[ch]$"),
("*.*",r"^.*\..*$"),
("*",r"^.*$"),
)
def glob2pat(globstr):
pat = globstr.replace("\\",r"\\")
pat = pat.replace(".",r"\.").replace("?",r".").replace("*",r".*")
return "^"+pat+"$"
for globstr, patstr in tests:
g2p = glob2pat(globstr)
if g2p != patstr:
print globstr, "failed! Should be", patstr, "but was", g2p
import fileinput
import re
popstates = ["CO","ON","MI","WI","MN"]
for line in fileinput.input():
for state in popstates:
if re.search(r"\b"+state+r"\b",line):
print line
import fileinput
import re
popstates = ["CO","ON","MI","WI","MN"]
state_re = []
for state in popstates:
state_re.append(re.compile(r"\b"+state+r"\b"))
for line in fileinput.input():
for state in state_re:
if state.search(line):
print line
import fileinput
import re
popstates = ["CO","ON","MI","WI","MN"]
state_re = re.compile(r"\b(?:"+"|".join(popstates)+r")\b")
for line in fileinput.input():
if state_re.search(line):
print line
import fileinput
import re
def build_match_any(words):
return re.compile("|".join(words))
def uniq(arr):
seen = {}
for item in arr:
seen[item] = seen.get(item, 0) + 1
return seen.keys()
def build_match_all(words):
r = re.compile("|".join(words))
c = lambda line: len(uniq(r.findall(line)))>=len(words)
return c
any = build_match_any(("Tom","Nat"))
all = build_match_all(("Tom","Nat"))
for line in fileinput.input():
if any.search(line):
print "any:", line
if all(line):
print "all:", line
import re
while True:
pat = raw_input("Pattern? ")
try:
re.compile(pat)
except re.error, err:
print "INVALID PATTERN", err
continue
break
def is_valid_pattern(pat):
try:
re.compile(pat)
except re.error:
return False
return True
import sys, os.path, re
if len(sys.argv)<=1:
print "usage: %s pat [files]\n" % sys.argv[0]
sys.exit(1)
pat = sys.argv[1]
try:
pat_re = re.compile(pat)
except:
print "%s: bad pattern %s: %s" % (sys.argv[1], pat, sys.exc_info()[1])
sys.exit(1)
for filename in filter(os.path.isfile,sys.argv[2:]):
parano = 0
for para in open(filename).read().split("\n\n"):
parano += 1
if pat_re.search(para):
print filename, parano, para, "\n"
import sys
import re, string
from locale import LC_CTYPE, setlocale, getlocale
name = "andreas k\xF6nig"
locale = {"German" : "de_DE.ISO_8859-1", "English" : "en_US"}
try:
setlocale(LC_CTYPE, locale["English"])
except:
print "Invalid locale %s" % locale["English"]
sys.exit(1)
english_names = []
for n in re.findall(r"(?L)\b(\w+)\b",name):
english_names.append(n.capitalize())
try:
setlocale(LC_CTYPE, locale["German"])
except:
print "Invalid locale %s" % locale["German"]
sys.exit(1)
german_names = map(string.capitalize, re.findall(r"(?L)\b(\w+)\b",name))
print "English names: %s" % " ".join(english_names)
print "German names: %s" % " ".join(german_names)
import difflib
matchlist = ["ape", "apple", "lapel", "peach", "puppy"]
print difflib.get_close_matches("appel", matchlist)
mystr = "3,4,5,9,120"
for match in re.finditer("(\d+)", mystr):
n = match.group(0)
if n == "9":
break print "Found number", n
mystr = "The year 1752 lost 10 days on the 3rd of September"
x = re.finditer("(\d+)", mystr)
for match in x:
n = match.group(0)
print "Found number", n
tail = re.match("(\S+)", mystr[match.end():])
if tail:
print "Found %s after the last number."%tail.group(0)
txt = re.sub("<.*>", "", txt)
txt = re.sub("<.*?>", "", txt) txt = "<b><i>this</i> and <i>that</i> are important</b> Oh, <b><i>me too!</i></b>"
print re.findall("<b><i>(.*?)</i></b>", txt
print re.findall("/BEGIN((?:(?!BEGIN).)*)END/", txt)
print re.findall("<b><i>((?:(?!<b>|<i>).)*)</i></b>", txt)
print re.findall("<b><i>((?:(?!<[ib]>).)*)</i></b>", txt)
print re.findall("""
<b><i>
[^<]* # stuff not possibly bad, and not possibly the end.
(?: # at this point, we can have '<' if not part of something bad
(?! </?[ib]> ) # what we can't have
< # okay, so match the '<'
[^<]* # and continue with more safe stuff
) *
</i></b>
""", re.VERBOSE, txt)
text = """
This is a test
test of the duplicate word finder.
"""
words = text.split()
for curr, next in zip(words[:-1], words[1:]):
if curr.upper() == next.upper():
print "Duplicate word '%s' found." % curr
import re
pat = r"""
\b # start at a word boundary (begin letters)
(\S+) # find chunk of non-whitespace
\b # until another word boundary (end letters)
(
\s+ # separated by some whitespace
\1 # and that very same chunk again
\b # until another word boundary
) + # one or more sets of those
"""
for match in re.finditer(pat, text, flags=re.VERBOSE|re.IGNORECASE):
print "Duplicate word '%s' found." % match.group(1)
a = 'nobody';
b = 'bodysnatcher';
text = a+" "+b
pat = r"^(\w+)(\w+) \2(\w+)$"
for match in re.finditer(pat, text):
m1, m2, m3 = match.groups()
print m2, "overlaps in %s-%s-%s"%(m1, m2, m3)
pat = r"^(\w+?)(\w+) \2(\w+)$"
try:
while True:
factor = re.match(r"^(oo+?)\1+$", n).group(1)
n = re.sub(factor, "o", n)
print len(factor)
except AttributeError:
print len(n)
def diaphantine(n, x, y, z):
pat = r"^(o*)\1{%s}(o*)\2{%s}(o*)\3{%s}$"%(x-1, y-1, z-1)
text = "o"*n
try:
vals = [len(v) for v in re.match(pat, text).groups()]
except ValueError:
print "No solutions."
else:
print "One solution is: x=%s, y=%s, z=%s."%tuple(vals)
diaphantine(n=281, x=12, y=15, z=16)
pat = "ALPHA|BETA"
pat = "^(?=.*ALPHA)(?=.*BETA)"
pat = "ALPHA.*BETA|BETA.*ALPHA"
pat = "^(?:(?!PAT).)*$"
pat = "(?=^(?:(?!BAD).)*$)GOOD"
if not re.match(pattern, text):
something()
if re.match(pat1, text) and re.match(pat2, text):
something()
if re.match(pat1, text) or re.match(pat2, text):
something()
"""minigrep - trivial grep"""
import sys, re
pat = sys.argv[1]
for line in sys.stdin:
if re.match(pat, line):
print line[:-1]
if re.match(r"^(?=.*bell)(?=.*lab)", "labelled"):
something()
if re.search("bell", s) and re.search("lab", s):
something()
if re.match("""
^ # start of string
(?= # zero-width lookahead
.* # any amount of intervening stuff
bell # the desired bell string
) # rewind, since we were only looking
(?= # and do the same thing
.* # any amount of intervening stuff
lab # and the lab part
)
""",
murray_hill,
re.DOTALL | re.VERBOSE):
print "Looks like Bell Labs might be in Murray Hill!"
if re.match(r"(?:^.*bell.*lab)|(?:^.*lab.*bell)", "labelled"):
something()
brand = "labelled"
if re.match("""
(?: # non-capturing grouper
^ .*? # any amount of stuff at the front
bell # look for a bell
.*? # followed by any amount of anything
lab # look for a lab
) # end grouper
| # otherwise, try the other direction
(?: # non-capturing grouper
^ .*? # any amount of stuff at the front
lab # look for a lab
.*? # followed by any amount of anything
bell # followed by a bell
) # end grouper
""",
brand,
re.DOTALL | re.VERBOSE):
print "Our brand has bell and lab separate."
x = "odlaw"
if re.match("^(?:(?!waldo).)*$", x):
print "There's no waldo here!"
if re.match("""
^ # start of string
(?: # non-capturing grouper
(?! # look ahead negation
waldo # is he ahead of us now?
) # is so, the negation failed
. # any character (cuzza /s)
) * # repeat that grouping 0 or more
$ # through the end of the string
""",
x,
re.VERBOSE | re.DOTALL):
print "There's no waldo here!\n";
from email._parseaddr import AddressList
print AddressList("fred&barney@stonehenge.com").addresslist[0]
print AddressList("fred&barney@stonehenge.com (Hanna Barbara)").addresslist[0]
name, address = AddressList("Mr Fooby Blah <me@nowhere.com>").addresslist[0]
print "%s's address is '%s'"%(name, address)
def get_action(answer):
answer = answer.lower()
actions = ["send", "stop", "abort", "list", "end"]
for action in actions:
if action.startswith(answer):
return action
print "Action is %s."%get_action("L")
import re
answer = "ab"
answer = re.escape(answer.strip())
for action in ("SEND", "STOP", "ABORT", "LIST", "EDIT"):
if re.match(answer, action, flags=re.IGNORECASE):
print "Action is %s."%action.lower()
import re, sys
def handle_cmd(cmd):
cmd = re.escape(cmd.strip())
for name, action in {"edit": invoke_editor,
"send": deliver_message,
"list": lambda: system(pager, myfile),
"abort": sys.exit,
}
if re.match(cmd, name, flags=re.IGNORECASE):
action()
break
else:
print "Unknown command:", cmd
handle_cmd("ab")
import re, sys, fileinput
def urlify_string(s):
urls = r'(http|telnet|gopher|file|wais|ftp)'
ltrs = r'\w';
gunk = r'/#~:.?+=&%@!\-'
punc = r'.:?\-'
any = ltrs + gunk + punc
pat = re.compile(r"""
\b # start at word boundary
( # begin \1 {
%(urls)s : # need resource and a colon
[%(any)s] +? # followed by one or more
# of any valid character, but
# be conservative and take only
# what you need to....
) # end \1 }
(?= # look-ahead non-consumptive assertion
[%(punc)s]* # either 0 or more punctuation
[^%(any)s] # followed by a non-url char
| # or else
$ # then end of the string
)
"""%locals(), re.VERBOSE | re.IGNORECASE)
return re.sub(pat, r"<A HREF=\1>\1</A>", s)
if __name__ == "__main__":
for line in fileinput.input():
print urlify_string(line)
pat = r"^m*(d?c{0,3}|c[dm])(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$"
re.match(pat, "mcmlxcvii")
txt = "one two three four five"
word1, word2, rest = txt.split(" ", 2)
print " ".join([word2, word1, rest])
frompat = r"(\S+)(\s+)(\S+)"
topat = r"\3\2\1"
print re.sub(frompat, topat, txt)
print str.split("=")
pat = r"(\w+)\s*=\s*(.*)\s*$"
print re.match(pat, "key=val").groups()
line = "such a very very very very very very very very very very very very very long line"
if len(line) > 80:
process(line)
pat = ".{80,}"
if re.match(pat, line):
process(line)
dt = time.strptime("12/11/05 12:34:56", "%d/%m/%y %H:%M:%S")
pat = r"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)"
dt = re.match(pat, "12/11/05 12:34:56").groups()
txt = "/usr/bin/python"
print txt.replace("/usr/bin", "/usr/local/bin")
print re.sub("/usr/bin", "/usr/local/bin", txt)
import re
def unescape_hex(matchobj):
return chr(int(matchobj.groups(0)[0], 16))
txt = re.sub(r"%([0-9A-Fa-f][0-9A-Fa-f])", unescape_hex, txt)
def unescape_hex(seg):
return chr(int(seg[:2], 16)) + seg[2:]
segs = txt.split("%")
txt = segs[0] + "".join(unescape_hex(seg) for seg in segs[1:])
txt = re.sub(r"""
/\* # Match the opening delimiter
.*? # Match a minimal number of characters
\*/ # Match the closing delimiter
""", "", txt, re.VERBOSE)
txt.strip()
txt = re.sub(r"^\s+", "", txt)
txt = re.sub(r"\s+$", "", txt)
txt.replace("\\n", "\n")
txt = re.sub("\\n", "\n", txt)
txt = re.sub("^.*::", "")
import socket
socket.inet_aton(txt)
octseg =r"([01]?\d\d|2[0-4]\d|25[0-5])"
dot = r"\."
pat = "^" + octseg + dot + octseg + dot + octseg + dot + octseg + "$"
if not re.match(pat, txt, re.VERBOSE)
raise ValueError
pat = r"""^([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\.
([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])$"""
fname = os.path.basename(path)
fname = re.sub("^.*/", "", path)
import os
try:
tc = os.environ["TERMCAP"]
except KeyError:
cols = 80
else:
cols = re.match(":co#(\d+):").groups(1)
name = os.path.basename(sys.argv[0])
name = re.sub("^.*/", "", sys.argv[0])
if sys.platform != "linux":
raise SystemExit("This isn't Linux")
txt = re.sub(r"\n\s+", " ", txt)
txt = txt.replace("\n", " ")
nums = re.findall(r"\d+\.?\d*|\.\d+", txt)
capwords = [word for word in txt.split() if word.isupper()]
capwords = [word for word in re.findall(r"\b(\S+)\b", txt) if word.isupper()]
capwords = re.findall(r"(\b[^\Wa-z0-9_]+\b)", txt)
lowords = [word for word in txt.split() if word.islower()]
lowords = [word for word in re.findall(r"\b(\S+)\b", txt) if word.islower()]
lowords = re.findall(r"(\b[^\WA-Z0-9_]+\b)", txt)
icwords = [word for word in txt.split() if word.istitle()]
icwords = [word for word in re.finditer(r"\b(\S+)\b") if word.istitle()]
icwords = re.findall(r"(\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b)", txt)
links = re.findall(r"""<A[^>]+?HREF\s*=\s*["']?([^'" >]+?)[ '"]?>""", txt)
##-----------------------------
names = txt.split()
if len(names) == 3:
initial = names[1][0]
else:
initial = ""
# DON'T DO THIS.
pat = "^\S+\s+(\S)\S*\s+\S"
try:
initial = re.match(pat, txt).group(1)
except AttributeError:
initial = ""
##-----------------------------
txt = re.sub('"([^"]*)"', "``\1''", txt)
##-----------------------------
sentences = [elem[0] for elem in re.findall(r"(.*?[!?.])( |\Z)", s)]
##-----------------------------
import time
dt = time.strptime(txt, "%Y-%m-%d")
# DON'T DO THIS.
year, month, day = re.match(r"(\d{4})-(\d\d)-(\d\d)", txt).groups()
pat = r"""
^
(?:
1 \s (?: \d\d\d \s)? # 1, or 1 and area code
| # ... or ...
\(\d\d\d\) \s # area code with parens
| # ... or ...
(?: \+\d\d?\d? \s)? # optional +country code
\d\d\d ([\s\-]) # and area code
)
\d\d\d (\s|\1) # prefix (and area code separator)
\d\d\d\d # exchange
$
"""
re.match(pat, txt, re.VERBOSE)
re.match(r"\boh\s+my\s+gh?o(d(dess(es)?|s?)|odness|sh)\b", txt, re.IGNORECASE)
for line in file(fname, "Ur"): process(line)
lines = [re.sub(r"^([^\012\015]*)(\012\015?|\015\012?)", "", line)
for line in file(fname)]
for line in open("/usr/local/widgets/data"):
if blue in line:
print line[:-1]
import sys, re
pattern = re.compile(r"\d")
for line in sys.stdin:
if not pattern.search(line):
sys.stderr.write("No digit found.\n")
sys.stdout.write("Read: " + line)
sys.stdout.close()
logfile = open("/tmp/log", "w")
logfile.close()
print>>logfile, "Countdown initiated ..."
print "You have 30 seconds to reach minimum safety distance."
import sys
old_output, sys.stdout = sys.stdout, logfile
print "Countdown initiated ..."
sys.stdout = old_output
print "You have 30 seconds to reach minimum safety distance."
source = open(path)
sink = open(path, "w")
import os
source_fd = os.open(path, os.O_RDONLY)
source = os.fdopen(fd)
sink_fd = os.open(path, os.O_WRONLY)
sink = os.fdopen(sink_fd)
myfile = open(filename, "w")
fd = os.open(filename, os.O_WRONLY | os.O_CREAT)
myfile = open(filename, "r+")
fd = os.open(name, flags)
fd = os.open(name, flags, mode)
myfile = open(path)
fd = os.open(path, os.O_RDONLY)
myfile = open(path, "w")
fd = os.open(path, os.O_WRONLY|os.O_TRUNC|os.O_CREAT)
fd = os.open(path, os.O_WRONLY|os.O_TRUNC|os.O_CREAT, 0600)
fd = os.open(path, os.O_WRONLY|os.O_EXCL|os.O_CREAT)
fd = os.open(path, os.O_WRONLY|os.O_EXCL|os.O_CREAT, 0600)
myfile = open(path, "a")
fd = os.open(path, os.O_WRONLY|os.O_APPEND|os.O_CREAT)
fd = os.open(path, os.O_WRONLY|os.O_APPEND|s.O_CREAT, 0600)
fd = os.open(path, os.O_WRONLY|os.O_APPEND)
myfile = open(path, "rw")
fd = os.open(path, os.O_RDWR)
fd = os.open(path, os.O_RDWR|os.O_CREAT)
fd = os.open(path, os.O_RDWR|os.O_CREAT, 0600)
fd = os.open(path, os.O_RDWR|os.O_EXCL|os.O_CREAT)
fd = os.open(path, os.O_RDWR|os.O_EXCL|os.O_CREAT, 0600)
import os
filename = os.path.expanduser(filename)
myfile = open(filename)
try:
myfile = open(filename)
except IOError, err:
raise AssertionError("Couldn't open %s for reading : %s" %
(filename, err.strerror))
import tempfile
myfile = tempfile.TemporaryFile()
import os, tempfile
while True:
name = os.tmpnam()
try:
fd = os.open(name, os.O_RDWR|os.O_CREAT|os.O_EXCL)
break
except os.error:
pass
myfile = tempfile.TemporaryFileWrapper(os.fdopen(fd), name)
import os
while True:
tmpname = os.tmpnam()
fd = os.open(tmpnam, os.O_RDWR | os.O_CREAT | os.O_EXCL)
if fd:
tmpfile = os.fdopen(fd)
break
os.remove(tmpnam)
import tempfile
myfile = tempfile.TemporaryFile(bufsize = 0)
for i in range(10):
print>>myfile, i
myfile.seek(0)
print "Tmp file has:", myfile.read()
DATA = """\
your data goes here
"""
for line in DATA.split("\n"):
pass
for line in sys.stdin:
pass
import fileinput
for line in fileinput.input():
do something with the line
import sys
def do_with(myfile):
for line in myfile:
print line[:-1]
filenames = sys.argv[1:]
if filenames:
for filename in filenames:
try:
do_with(open(filename))
except IOError, err:
sys.stderr.write("Can't open %s: %s\n" % (filename, err.strerror))
continue
else:
do_with(sys.stdin)
import sys, glob
ARGV = sys.argv[1:] or glob.glob("*.[Cch]")
import sys
args = sys.argv[1:]
chop_first = 0
if args and args[0] == "-c":
chop_first += 1
args = args[1:]
import sys, re
digit_pattern = re.compile(r"-(\d+)$")
args = sys.argv[1:]
if args:
match = digit_pattern.match(args[0])
if match:
columns = int(match.group(1))
args = args[1:]
args = sys.argv[1:]
for i in range(len(args)):
arg = args[i]
if arg == "--" or not arg.startwith("-"):
break
if arg[1:].isdigit():
columns = int(arg[1:])
continue
import sys, getopt
try:
args, filenames = getopt.getopt(sys.argv[1:], "ainu")
except getopt.error:
raise SystemExit("usage: %s [-ainu] [filenames] ..." % sys.argv[0])
append = ignore_ints = nostdout = unbuffer = 0
for k, v in args:
if k == "-a": append += 1
elif k == "-i": ignore_ints += 1
elif k == "-n": nostdout += 1
elif k == "-u": unbuffer += 1
else:
raise AssertionError("Unexpected argument: %s" % k)
import fileinput
for line in fileinput.input():
sys.stdout.write("%s:%s:%s" %
(fileinput.filename(), fileinput.filelineno(), line))
for line in fileinput.input(): if line.find("login") != -1:
sys.stdout.write(line)
for line in fileinput.input(): sys.stdout.write(line.lower())
chunks = 0
for line in fileinput.input():
for word in line.split():
if word.startswith("#"):
continue
if word in ("__DATA__", "__END__"):
fileinput.close()
break
chunks += 1
print "Found", chunks, "chunks"
import shutil
old = open("old")
new = open("new","w")
for line in old:
new.writeline(line)
new.close()
old.close()
shutil.copyfile("old", "old.orig")
shutil.copyfile("new", "old")
for i, line in enumerate(old):
if i == 20:
print>>new, "Extra line 1\n"
print>>new, "Extra line 2\n"
print>>new, line
for i, line in enumerate(old):
if 20 <= i <= 30:
continue
print>>new, line
import fileinput, sys, time
today = time.strftime("%Y-%m-%d",time.localtime())
for line in fileinput.input(inplace=1, backup=".orig"):
sys.stdout.write(line.replace("DATE",today))
import glob, re
match = re.compile("(?<=[pP])earl")
files = fileinput.FileInput(glob.glob("*.c"), inplace=1, backup=".orig")
while True:
line = files.readline()
sys.stderr.write(line)
if not line:
break
if files.isfirstline():
sys.stdout.write("This line should appear at the top of each file\n")
sys.stdout.write(match.sub("erl",line))
myfile = open(filename, "r+")
data = myfile.read()
myfile.seek(0, 0)
myfile.write(data)
myfile.truncate(myfile.tell())
myfile.close()
myfile = open(filename, "r+")
data = [process(line) for line in myfile]
myfile.seek(0, 0)
myfile.writelines(data)
myfile.truncate(myfile.tell())
myfile.close()
import fcntl
myfile = open(somepath, 'r+')
fcntl.flock(myfile, fcntl.LOCK_EX)
myfile.close()
fcntl.LOCK_SH
fcntl.LOCK_EX
fcntl.LOCK_NB
fcntl.LOCK_UN
import warnings
try:
fcntl.flock(myfile, fcntl.LOCK_EX|fcntl.LOCK_NB)
except IOError:
warnings.warn("can't immediately write-lock the file ($!), blocking ...")
fcntl.flock(myfile, fcntl.LOCK_EX)
fcntl.flock(myfile, fcntl.LOCK_UN)
myfile = open(somepath, "r+")
fcntl.flock(myfile, fcntl.LOCK_EX)
myfile.seek(0, 0)
myfile.truncate(0)
print>>myfile, "\n" myfile.close()
myfile = open(filename, "r", buffering=0) myfile = open(filename, "r", buffering=1) myfile = open(filename, "r", buffering=100) myfile = open(filename, "r", buffering=-1)
myfile.flush()
import sys
sys.stdout.flush()
import socket
mysock = socket.socket()
mysock.connect(('www.perl.com', 80))
mysock.send("GET /index.html http/1.1\n\n")
f = mysock.makefile()
print "Doc is:"
for line in f:
print line[:-1]
import select
while True:
rlist, wlist, xlist = select.select([file1, file2, file3], [], [], 0)
for r in rlist:
pass
def subroutine(myfile):
print>>myfile, "Hello, file"
variable = myfile
subroutine(variable)
for myfile in files:
print>>myfile, stuff_to_print
import os
file = os.popen("tee file1 file2 file3 >/dev/null", "w")
print>>myfile, "whatever"
import os, sys
sys.stdout.file = os.popen("tee file1 file2 file3", "w")
print "whatever"
sys.stdout.close()
class FileDispatcher(object):
def __init__(self, *files):
self.files = files
def write(self, msg):
for f in self.files:
f.write(msg)
def close(self):
for f in self.files:
f.close()
x = open("C:/test1.txt", "w")
y = open("C:/test2.txt", "w")
z = open("C:/test3.txt", "w")
fd = FileDispatcher(x, y, z)
print>>fd, "Foo" print>>fd, "Testing"
fd.close()
import os
myfile = os.fdopen(fdnum) myfile = os.fdopen(os.dup(fdnum))
outcopy = os.fdopen(os.dup(sys.stdin.fileno()), "w")
incopy = os.fdopen(os.dup(sys.stdin.fileno()), "r")
original = open("C:/test.txt")
alias = original
alias.close()
print original.closed
import copy
original = open("C:/test.txt")
dupe = copy.copy(original)
dupe.close()
print original.closed
import sys
oldstderr = sys.stderr
oldstdout = sys.stdout
sys.stderr = open("C:/stderrfile.txt")
sys.stdout = open("C:/stdoutfile.txt")
print "Blah" sys.stdout.close()
sys.stdout = oldstdout
sys.stderr = oldstderr
import msvcrt
myfile.seek(5, 0)
msvcrt.locking(myfile.fileno(), msvcrt.LK_NBLCK, 3)
import fcntl
fcntl.lockf(myfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB, 3, 5)
for line in DATAFILE:
line = line.rstrip()
size = len(line)
print size
for line in datafile:
print length(line.rstrip()) lines = datafile.readlines()
whole_file = myfile.read()
print>>myfile, "One", "two", "three" print "Baa baa black sheep." buffer = myfile.read(4096)
rv = len(buffer)
myfile.truncate(length)
open("/tmp/%d.pid" % os.getpid(), "a").truncate(length)
pos = myfile.tell()
print "I'm", pos, "bytes from the start of DATAFILE."
logfile.seek(0, 2) datafile.seek(pos) outfile.seek(-20, 1) written = os.write(datafile.fileno(), mystr)
if written != len(mystr):
warnings.warn("only read %s bytes, not %s" % (written, len(mystr)))
pos = os.lseek(myfile.fileno(), 0, 1)
def ContReader(infile):
lines = []
for line in infile:
line = line.rstrip()
if line.endswith("\\"):
lines.append(line[:-1])
continue
lines.append(line)
yield "".join(lines)
lines = []
if lines:
yield "".join(lines)
for line in ContReader(datafile):
pass
import os
count = int(os.popen("wc -l < " + filename).read())
for count, line in enumerate(open(filename)):
pass
count += 1 myfile = open(filename)
count = 0
for line in myfile:
count += 1
myfile = open(filename)
count = 0
while True:
line = myfile.readline()
if not line:
break
count += 1
count = 0
while True:
s = myfile.read(2**16)
count += s.count("\n")
for line, count in zip(open(filename), xrange(1, sys.maxint)):
pass
import fileinput
fi = fileinput.FileInput(filename)
while fi.readline(): pass
count = fi.lineno()
def SepReader(infile, sep = "\n\n"):
text = infile.read(10000)
if not text:
return
while True:
fields = text.split(sep)
for field in fields[:-1]:
yield field
text = fields[-1]
new_text = infile.read(10000)
if not new_text:
yield text
break
text += new_text
para_count = 0
for para in SepReader(open(filename)):
para_count += 1
for line in sys.stdin:
for word in line.split():
pass pat = re.compile(r"(\w[\w'-]*)")
for line in sys.stdin:
pos = 0
while True:
match = pat.search(line, pos)
if not match:
break
pos = match.end(1)
pat = re.compile(r"(\w[\w'-]*)")
for line in sys.stdin:
scanner = pat.scanner(line)
while True:
match = scanner.search()
if not match:
break
import fileinput, re
pat = re.compile(r"(\w[\w'-]*)")
seen = {}
for line in fileinput.input():
pos = 0
while True:
match = pat.search(line, pos)
if not match:
break
pos = match.end(1)
text = match.group(1).lower()
seen[text] = seen.get(text, 0) + 1
for text, count in sorted(seen.items, key=lambda item: item[1]):
print "%5d %s" % (count, text)
import fileinput, sys
seen = {}
for line in fileinput.input():
text = line.lower()
seen[text] = seen.get(text, 0) + 1
for text, count in sorted(seen.items, key=lambda item: item[1]):
sys.stdout.write("%5d %s" % (count, text))
lines = myfile.readlines()
while lines:
line = lines.pop()
for line in reversed(myfile):
pass for i in range(len(lines)):
line = lines[-i]
for paragraph in sorted(SepReader(infile)):
pass
import time
while True:
for line in infile:
pass time.sleep(SOMETIME)
infile.seek(0, 1)
import time
naptime = 1
logfile = open("/tmp/logfile")
while True:
for line in logfile:
print line.rstrip()
time.sleep(naptime)
infile.seek(0, 1)
while True:
curpos = logfile.tell()
while True:
line = logfile.readline()
if not line:
break
curpos = logfile.tell()
sleep(naptime)
logfile.seek(curpos, 0) import os
if os.stat(LOGFILENAME).st_nlink == 0:
raise SystemExit
import random, fileinput
text = None
for line in fileinput.input():
if random.randrange(fileinput.lineno()) == 0:
text = line
import sys
adage = None
for i, rec in enumerate(SepReader(open("/usr/share/games/fortunes"), "%\n")):
if random.randrange(i+1) == 0:
adage = rec
print adage
import random
lines = data.readlines()
random.shuffle(lines)
for line in lines:
print line.rstrip()
import linecache
linecache.getline(filename, DESIRED_LINE_NUMBER)
lineno = 0
while True:
line = infile.readline()
if not line or lineno == DESIRED_LINE_NUMBER:
break
lineno += 1
lines = infile.readlines()
line = lines[DESIRED_LINE_NUMBER]
for i in range(DESIRED_LINE_NUMBER):
line = infile.readline()
if not line:
break
fields = re.split(pattern_string, text)
pat = re.compile(pattern_string)
fields = pat.split(text)
re.split(r"([+-])", "3+5-2")
[3, '+', 5, '-', 2]
fields = record.split(":")
fields = re.split(r":", record)
fields = re.split(r"\s+", record)
fields = record.split(" ")
myfile = open(filename, "r")
prev_pos = pos = 0
while True:
line = myfile.readline()
if not line:
break
prev_pos = pos
pos = myfile.tell()
myfile = open(filename, "a")
myfile.truncate(prev_pos)
open(filename, "rb")
open(filename, "wb")
gifname = "picture.gif"
gif_file = open(gifname, "rb")
while True:
buff = gif.read(8 * 2**10)
if not buff:
break
sys.stdout.write(buff)
address = recsize * recno
myfile.seek(address, 0)
buffer = myfile.read(recsize)
address = recsize * (recno-1)
import posixfile
address = recsize * recno
myfile.seek(address)
buffer = myfile.read(recsize)
myfile.seek(-recsize, posixfile.SEEK_CUR)
myfile.write(buffer)
myfile.close()
myfile.seek(addr)
s = ""
while True:
c = myfile.read(1)
if not c or c == "\0":
break
s += c
myfile.seek(addr)
offset = 0
while True:
s = myfile.read(1000)
x = s.find("\0")
if x != -1:
offset += x
break
offset += len(s)
if len(s) != 1000: break
myfile.seek(addr)
s = myfile.read(offset - 1)
myfile.read(1)
import re, sys
pat = re.compile(r"([\040-\176\s]{4,})")
for block in SepReader(sys.stdin, "\0"):
pos = 0
while True:
match = pat.search(block, pos)
if not match:
break
print match.group(1)
pos = match.end(1)
import struct
RECORDSIZE= struct.calcsize(TEMPLATE)
while True:
record = FILE.read(RECORDSIZE):
if len(record)!=RECORDSIZE:
raise "short read"
FIELDS = struct.unpack(TEMPLATE, record)
import re
pat = re.compile(r"\s*=\s*")
for line in config_file:
if "#" in line: line = line[:line.index("#")]
line = line.strip() if not line: continue
m = pat.search(line)
var = line[:m.start()]
value = line[m.end():]
User_Preferences[var] = value
import os
mode, ino, dev, nlink, uid, gid, size, \
atime, mtime, ctime = os.stat(filename)
mode &= 07777
info = os.stat(filename)
if info.st_uid == 0:
print "Superuser owns", filename
if info.st_atime > info.st_mtime:
print filename, "has been read since it was written."
import os
def is_safe(path):
info = os.stat(path)
if info.st_uid not in (0, os.getuid()):
return False
if info.st_mode & 022: if not os.path.isdir(path): return False
if not (info.st_mode & 01000):
return False
return True
def is_verysafe(path):
terms = []
while True:
path, ending = os.path.split(path)
if not ending:
break
terms.insert(0, ending)
for term in terms:
path = os.path.join(path, term)
if not is_safe(path):
return False
return True
import time
import struct
import os
class WTmpRecord:
fmt = "hI32s4s32s256siili4l20s";
_fieldnames = ["type","PID","Line","inittab","User","Hostname",
"exit_status", "session", "time", "addr" ]
def __init__(self):
self._rec_size = struct.calcsize(self.fmt)
def size(self):
return self._rec_size
def unpack(self, bin_data):
rec = struct.unpack(self.fmt, bin_data)
self._rec = []
for i in range(len(rec)):
if i in (2,3,4,5):
self._rec.append( rec[i].split("\0")[0] )
else:
self._rec.append(rec[i])
return self._rec
def fieldnames(self):
return self._fieldnames
def __getattr__(self,name):
return self._rec[self._fieldnames.index(name)]
rec = WTmpRecord()
f = open("/var/log/wtmp","rb")
f.seek(0,2)
while True:
while True:
bin = f.read(rec.size())
if len(bin) != rec.size():
break
rec.unpack(bin)
if rec.type != 0:
print " %1d %-8s %-12s %-24s %-20s %5d %08x" % \
(rec.type, rec.User, rec.Line,
time.strftime("%a %Y-%m-%d %H:%M:%S",time.localtime(rec.time)),
rec.Hostname, rec.PID, rec.addr)
time.sleep(1)
f.close()
import sys
import struct
import pwd
import time
import re
f = open("/var/log/lastlog","rb")
fmt = "L32s256s"
rec_size = struct.calcsize(fmt)
for user in sys.argv[1:]:
if re.match(r"^\d+$", user):
user_id = int(user)
else:
try:
user_id = pwd.getpwnam(user)[2]
except:
print "no such uid %s" % (user)
continue
f.seek(rec_size * user_id)
bin = f.read(rec_size)
if len(bin) == rec_size:
data = struct.unpack(fmt, bin)
if data[0]:
logged_in = "at %s" % (time.strftime("%a %H:%M:%S %Y-%m-%d",
time.localtime(data[0])))
line = " on %s" % (data[1])
host = " from %s" % (data[2])
else:
logged_in = "never logged in"
line = ""
host = ""
print "%-8s UID %5d %s%s%s" % (user, user_id, logged_in, line, host)
else:
print "Read failed."
f.close()
entry = os.stat("/usr/bin/vi")
entry = os.stat("/usr/bin")
entry = os.stat(INFILE.name)
entry = os.stat("/usr/bin/vi")
ctime = entry.st_ino
size = entry.st_size
f = open(filename)
f.seek(0, 2)
if not f.tell():
raise SystemExit("%s doesn't have text in it."%filename)
for filename in os.listdir("/usr/bin"):
print "Inside /usr/bin is something called", filename
fstat = os.stat(filename)
readtime = fstat.st_atime
writetime = fstat.st_mtime
os.utime(filename, (newreadtime, newwritetime))
readtime, writetime = os.stat(filename)[7:9]
SECONDS_PER_DAY = 60 * 60 * 24
fstat = os.stat(filename)
atime = fstat.st_atime - 7 * SECONDS_PER_DAY
mtime = fstat.st_mtime - 7 * SECONDS_PER_DAY
os.utime(filename, (atime, mtime))
mtime = os.stat(filename).st_mtime
utime(filename, (time.time(), mtime))
import sys, os
if len(sys.argv) != 2:
raise SystemExit("usage: uvi filename")
filename = argv[1]
fstat = os.stat(filename)
os.system( (os.environ.get("EDITOR") or "vi") + " " + filename)
os.utime(filename, (fstat.st_atime, fstat.st_mtime))
os.remove(filename)
err_flg = 0
for filename in filenames:
try:
os.remove(filename)
except OSError, err:
err_flg = 1
if err_flg:
raise OSError("Couldn't remove all of %s: %s" % (filenames, err))
os.remove(filename)
success = 0
for filename in filenames:
try:
os.remove(filename)
success += 1
except OSError, err:
pass
if success != len(filenames):
sys.stderr.write("could only delete %d of %d files" % \
(success, len(filenames)))
import shutil
shutil.copy(oldfile, newfile)
infile = open(oldfile)
outfile = open(newfile, "w")
blksize = 16384
while True:
buf = infile.read(blksize)
if not buf:
break
outfile.write(buf)
infile.close()
outfile.close()
os.system("cp %s %s" % (oldfile, newfile)) os.system("copy %s %s" % (oldfile, newfile)) import shutil
shutil.copy("datafile.dat", "datafile.bak")
shutil.copy("datafile.new", "datafile.dat")
os.remove("datafile.new")
import os
seen = {}
def do_my_thing(filename):
fstat = os.stat(filename)
key = (fstat.st_ino, fstat.st_dev)
if not seen.get(key):
pass
seen[key] = seen.get(key, 0 ) + 1
for filename in files:
fstat = os.stat(filename)
key = (fstat.st_ino, fstat.st_dev)
seen.setdefault(key, []).append(filename)
keys = seen.keys()
keys.sort()
for inodev in keys:
ino, dev = inodev
filenames = seen[inodev]
if len(filenames) > 1:
pass
for filename in os.listdir(dirname):
pass
for filename in os.listdir(dirname):
pass
import glob
filenames = glob.glob("*.c")
filenames = [filename for filename in os.listdir(path) if filename.endswith(".c")]
import re
allowed_name = re.compile(r"\.[ch]$", re.I).search
filenames = [f for f in os.listdir(path) if allowed_name(f)]
import re, os
allowed_name = re.compile(r"\.[ch]$", re.I).search
fnames = [os.path.join(dirname, fname)
for fname in os.listdir(dirname)
if allowed_name(fname)]
dirs = [os.path.join(path, f)
for f in os.listdir(path) if f.isdigit()]
dirs = [d for d in dirs if os.path.isdir(d)]
dirs = sorted(dirs, key=int)
import os
for root, dirs, files in os.walk(top):
pass
import os, os.path
for root, dirs, files in os.walk(top):
for name in dirs:
print os.path.join(root, name) + '/'
for name in files:
print os.path.join(root, name)
import os, os.path
numbytes = 0
for root, dirs, files in os.walk(top):
for name in files:
path = os.path.join(root, name)
numbytes += os.path.getsize(path)
print "%s contains %s bytes" % (top, numbytes)
import os, os.path
saved_size, saved_name = -1, ''
for root, dirs, files in os.walk(top):
for name in files:
path = os.path.join(root, name)
size = os.path.getsize(path)
if size > saved_size:
saved_size = size
saved_name = path
print "Biggest file %s in %s is %s bytes long" % (
saved_name, top, saved_size)
import os, os.path, time
saved_age, saved_name = None, ''
for root, dirs, files in os.walk(top):
for name in files:
path = os.path.join(root, name)
age = os.path.getmtime(path)
if saved_age is None or age > saved_age:
saved_age = age
saved_name = path
print "%s %s" % (saved_name, time.ctime(saved_age))
import sys, os, os.path
argv = sys.argv[1:] or ['.']
for top in argv:
for root, dirs, files in os.walk(top):
for name in dirs:
path = os.path.join(root, name)
print path
import shutil
shutil.rmtree(path)
import os, sys
def DeleteDir(dir):
for name in os.listdir(dir):
file = os.path.join(dir, name)
if not os.path.islink(file) and os.path.isdir(file):
DeleteDir(file)
else:
os.remove(file)
os.rmdir(dir)
import os
for fname in fnames:
newname = fname
try:
os.rename(fname, newname)
except OSError, err:
print "Couldn't rename %s to %s: %s!" % \
(fname, newfile, err)
import glob
def rename(files, transfunc)
for fname in fnames:
newname = transfunc(fname)
try:
os.rename(fname, newname)
except OSError, err:
print "Couldn't rename %s to %s: %s!" % \
(fname, newfile, err)
def transfunc(fname):
return fname[:-5]
rename(glob.glob("*.orig"), transfunc)
def transfunc(fname):
return fname.lower()
rename([f for f in glob.glob("*") if not f.startswith("Make)], transfunc)
def transfunc(fname):
return fname + ".bad"
rename(glob.glob("*.f"), transfunc)
def transfunc(fname):
answer = raw_input(fname + ": ")
if answer.upper().startswith("Y"):
return fname.replace("foo", "bar")
rename(glob.glob("*"), transfunc)
def transfunc(fname):
return ".rename(glob.glob("/tmp/*~"), transfunc)
import os
base = os.path.basename(path)
dirname = os.path.dirname(path)
dirname, filename = os.path.split(path)
base, ext = os.path.splitext(filename)
path = '/usr/lib/libc.a'
filename = os.path.basename(path)
dirname = os.path.dirname(path)
print "dir is %s, file is %s" % (dirname, filename)
path = '/usr/lib/libc.a'
dirname, filename = os.path.split(path)
name, ext = os.path.splitext(filename)
print "dir is %s, name is %s, extension is %s" % (dirname, name, ext)
import macpath
path = "Hard%20Drive:System%20Folder:README.txt"
dirname, base = macpath.split(path)
name, ext = macpath.splitext(base)
print "dir is %s, name is %s, extension is %s" % (dirname, name, ext)
def extension(path):
pos = path.find(".")
if pos == -1:
return ""
ext = path[pos+1:]
if "/" in ext:
return ""
return ext
import sys, os, os.path
pgmname = sys.argv[0]
if len(sys.argv)!=3:
print "usage: %s realdir mirrordir" % pgmname
raise SystemExit
(srcdir, dstdir) = sys.argv[1:3]
if not os.path.isdir(srcdir):
print "%s: %s is not a directory" % (pgmname,srcdir)
raise SystemExit
if not os.path.isdir(dstdir):
try:
os.mkdir(dstdir)
except OSError:
print "%s: can't make directory %s" % (pgmname,dstdir)
raise SystemExit
srcdir = os.path.abspath(srcdir)
dstdir = os.path.abspath(dstdir)
def wanted(arg, dirname, names):
for direntry in names:
relname = "%s/%s" % (dirname, direntry)
if os.path.isdir(relname):
mode = os.stat(relname).st_mode
try:
os.mkdir("%s/%s" % (dstdir,relname), mode)
except:
print "can't mkdir %s/%s" % (dstdir,relname)
raise SystemExit
else:
if relname[:2] == "./":
relname = relname[2:]
os.symlink("%s/%s" % (srcdir, relname), "%s/%s" % (dstdir,relname))
os.chdir(srcdir)
os.path.walk(".",wanted,None)
greeted = 0
def hello():
global greeted
greeted += 1
print "hi there"
hello()
import math
def hypotenuse(side1, side2):
return math.sqrt(side1**2 + side2**2)
diag = hypotenuse(3, 4) print hypotenuse(3, 4)
a = (3, 4)
print hypotenuse(*a) both = men + women
nums = [1.4, 3.5, 6.7]
def int_all(nums):
retlist = [] for n in nums:
retlist.append(int(n))
return retlist
ints = int_all(nums) nums = [1.4, 3.5, 6.7]
def trunc_em(nums):
for i,elem in enumerate(nums):
nums[i] = int(elem)
trunc_em(nums)
mylist = [3,2,1]
mylist = mylist.sort() mylist = sorted(mylist) mylist.sort()
def somefunc():
variable = something import sys
name, age = sys.args[1:] start = fetch_time()
a, b = pair
c = fetch_time()
def check_x(x):
y = "whatever"
run_check()
if condition:
print "got", x
def save_list(*args):
Global_List.extend(args)
def next_counter(counter=[0]): counter[0] += 1
return counter[0]
def make_counter():
counter = 0
while True:
counter += 1
yield counter
next_counter = make_counter().next
class Counter:
def __init__(self):
self.counter = 0
def __call__(self):
self.counter += 1
return self.counter
next_counter = Counter()
def make_counter(start=0):
counter = [start]
def next_counter():
counter[0] += 1
return counter[0]
def prev_counter():
counter[0] -= 1
return counter[0]
return next_counter, prev_counter
next_counter, prev_counter = make_counter()
class Counter:
def __init__(self, start=0):
self.value = start
def next(self):
self.value += 1
return self.value
def prev(self):
self.value -= 1
return self.value
def __int__(self):
return self.value
counter = Counter(42)
next_counter = counter.next
prev_counter = counter.prev
import sys
this_function = sys._getframe(0).f_code.co_name
i = 0 module = sys._getframe(i).f_globals["__name__"]
filename = sys._getframe(i).f_code.co_filename
line = sys._getframe(i).f_lineno
subr = sys._getframe(i).f_code.co_name
has_args = bool(sys._getframe(i+1).f_code.co_argcount)
me = whoami()
him = whowasi()
def whoami():
sys._getframe(1).f_code.co_name
def whowasi():
sys._getframe(2).f_code.co_name
list_diff(list1, list2)
def add_vecpair(x, y):
return [x1+y1 for x1, y1 in zip(x, y)]
a = [1, 2]
b = [5, 8]
print " ".join([str(n) for n in add_vecpair(a, b)])
assert isinstance(x, type([])) and isinstance(y, type([])), \
"usage: add_vecpair(list1, list2)"
import inspect,dis
def expecting():
"""Return how many values the caller is expecting"""
f = inspect.currentframe().f_back.f_back
bytecode = f.f_code.co_code
i = f.f_lasti
instruction = ord(bytecode[i+3])
if instruction == dis.opmap['UNPACK_SEQUENCE']:
howmany = ord(bytecode[i+4])
return howmany
elif instruction == dis.opmap['POP_TOP']:
return 0
return 1
def cleverfunc():
howmany = expecting()
if howmany == 0:
print "return value discarded"
if howmany == 2:
return 1,2
elif howmany == 3:
return 1,2,3
return 1
cleverfunc()
x = cleverfunc()
print x
x,y = cleverfunc()
print x,y
x,y,z = cleverfunc()
print x,y,z
thefunc(increment= "20s", start="+5m", finish="+30m")
thefunc(start= "+5m",finish="+30m")
thefunc(finish= "+30m")
thefunc(start="+5m", increment="15s")
def thefunc(increment='10s',
finish='0',
start='0'):
if increment.endswith("m"):
pass
a, _, c = func() a, ignore, c = func()
def somefunc():
mylist = []
mydict = {}
return mylist, mydict
mylist, mydict = somefunc()
def fn():
return a, b, c
h0, h1, h2 = fn()
tuple_of_dicts = fn() r0, r1, r2 = fn()
return
def empty_retval():
return None
def empty_retval():
return
def empty_retval():
pass a = yourfunc()
if a:
pass
a = sfunc()
if not a:
raise AssertionError("sfunc failed")
assert sfunc(), "sfunc failed"
def myfunc(a, b, c=4):
print a, b, c
mylist = [1,2]
mydict1 = {"b": 2, "c": 3}
mydict2 = {"b": 2}
myfunc(1,2,3)
myfunc(1,2)
myfunc(*mylist)
myfunc(5, *mylist)
myfunc(5, **mydict1)
myfunc(5, **mydict2)
myfunc(c=3, b=2, a=1)
myfunc(b=2, a=1)
myfunc(mylist, mydict1)
def mypush(mylist, *vals):
mylist.extend(vals)
mylist = []
mypush(mylist, 1, 2, 3, 4, 5)
print mylist
raise ValueError("some message") raise Exception("use me rarely") raise "don't use me" import warnings, sys
try:
func()
except:
warnings.warn("func raised an exception: " + str(sys.exc_info()[1]))
try:
func()
except:
warnings.warn("func blew up: " + str(sys.exc_info()[1]))
class MoonPhaseError(Exception):
def __init__(self, phase):
self.phase = phase
class FullMoonError(MoonPhaseError):
def __init__(self):
MoonPhaseError.__init__("full moon")
def func():
raise FullMoonError()
try:
func()
except FullMoonError:
pass
try:
func()
except MoonPhaseError, err:
if err.phase != "full moon":
raise
class Local(object):
def __init__(self, globalname, val):
self.globalname = globalname
self.globalval = globals()[globalname]
globals()[globalname] = val
def __del__(self):
globals()[self.globalname] = self.globalval
foo = 4
def blah():
print foo
def blech():
temp = Local("foo", 6)
blah()
blah()
blech()
blah()
grow = expand
grow()
one.var = two.table one.big = two.small fred = barney s = red("careful here")
print s
def red(text):
return "<FONT COLOR='red'>" + text + "</FONT>"
def color_font(color, text):
return "<FONT COLOR='%s'>%s</FONT>" % (color, text)
def red(text): return color_font("red", text)
def green(text): return color_font("green", text)
def blue(text): return color_font("blue", text)
def purple(text): return color_font("purple", text)
class ColorFont:
def __init__(self, color):
self.color = color
def __call__(self, text):
return "<FONT COLOR='%s'>%s</FONT>" % (self.color, text)
colors = "red blue green yellow orange purple violet".split(" ")
for name in colors:
globals()[name] = ColorFont(name)
colors = "red blue green yellow orange purple violet".split(" ")
for name in colors:
def temp(text, color = name):
return "<FONT COLOR='%s'>%s</FONT>" % (color, text)
globals()[name] = temp
class AnyColor:
def __getattr__(self, name):
return ColorFont(name)
colors = AnyColor()
print colors.chartreuse("stuff")
def outer(arg1):
x = arg1 + 35
def inner():
return x * 19
return x + inner()
import mailbox, sys
mbox = mailbox.PortableUnixMailbox(sys.stdin)
def extract_data(msg, idx):
subject = msg.getheader("Subject", "").strip()
if subject[:3].lower() == "re:":
subject = subject[3:].lstrip()
text = msg.fp.read()
return subject, idx, msg, text
messages = [extract_data(idx, msg) for idx, msg in enumerate(mbox)]
for subject, pos, msg, text in sorted(messages):
print "%s\n%s"%(msg, text)
def subject_date_position(elem):
return (elem[0], elem[2].getdate("Date"), elem[1])
messages.sort(key=subject_date_position)
messages = sorted(messages, key=subject_date_position)
print ref ref = 3 aref = mylist
aref = [3, 4, 5] href = {"How": "Now", "Brown": "Cow"} Nat = { "Name": "Leonhard Euler",
"Address": "1729 Ramanujan Lane\nMathworld, PI 31416",
"Birthday": 0x5bb5580
}
aref = mylist
anon_list = [1, 3, 5, 7, 9]
anon_copy = anon_list
implicit_creation = [2, 4, 6, 8, 10]
anon_list.append(11)
two = implicit_creation[0]
last_idx = len(aref) - 1
print implicit_creation[-1]
num_items = len(aref)
last_idx = aref.__len__() - 1
num_items = aref.__len__()
if not isinstance(someVar, type([])):
print "Expected a list"
print list_ref
list_ref.sort()
list_ref.append(item)
def list_ref():
return []
aref1 = list_ref()
aref2 = list_ref()
list_ref[N] pie[3:6]
pie[3:6:1]
pie[3:6] = ["blackberry", "blueberry", "pumpkin"]
for item in pie:
print item
for idx in xrange(len(pie)):
print pie[idx]
hash["KEYNAME"].append("new value")
for mystr in hash.keys():
print "%s: %s" % (mystr, hash[mystr])
hash["a key"] = [3, 4, 5]
values = hash["a key"]
hash["a key"].append(value)
residents = phone2name[number]
residents = phone2name.get(number, [])
href = hash
anon_hash = { "key1":"value1", "key2" : "value2 ..." }
anon_hash_copy = anon_hash.copy()
hash = href
value = href[key]
slice = [href[k] for k in (key1, key2, key3)]
keys = hash.keys()
import types
if type(someref) != types.DictType:
raise "Expected a dictionary, not %s" % type(someref)
if isinstance(someref,dict):
raise "Expected a dictionary, not %s" % type(someref)
for href in ( ENV, INC ):
for key in href.keys():
print "%s => %s" % (key, href[key])
values = [hash_ref[k] for k in (key1, key2, key3)]
for key in ("key1", "key2", "key3"):
hash_ref[k] += 7
cref = func
cref = lambda a, b: ...
returned = cref(arguments)
funcname = "thefunc"
locals()[funcname]();
commands = {
'happy': joy,
'sad': sullen,
'done': (lambda : sys.exit()), 'mad': angry,
}
print "How are you?",
cmd = raw_input()
if cmd in commands:
commands[cmd]()
else:
print "No such command: %s" % cmd
def counter_maker():
start = [0]
def counter_function():
start[0] += 1
return start[0]-1
return counter_function
counter = counter_maker()
for i in range(5):
print counter()
counter1 = counter_maker()
counter2 = counter_maker()
for i in range(5):
print counter1()
print counter1(), counter2()
import time
def timestamp():
start_time = time.time()
def elapsed():
return time.time() - start_time
return elapsed
early = timestamp()
time.sleep(20)
later = timestamp()
time.sleep(10)
print "It's been %d seconds since early" % early()
print "It's been %d seconds since later" % later()
x = 1
y = x
print x, id(x), y, id(y)
x += 1 print x, id(x), y, id(y)
y = 4 print x, id(x), y, id(y)
a = x = [1]
b = y = 1
c = z = "s"
print a, b, c
x += x y += y z += z print a, b, c
mylist = [1, "s", [1]]
print mylist
for elem in mylist:
elem *= 2
print mylist
mylist[0] *= 2
mylist[-1] *= 2
print mylist
import math
mylist = [(val**3 * 4/3*math.pi) for val in mylist]
c1 = mkcounter(20)
c2 = mkcounter(77)
print "next c1: %d" % c1['next']() print "next c2: %d" % c2['next']() print "next c1: %d" % c1['next']() print "last c1: %d" % c1['prev']() print "old c2: %d" % c2['reset']() def mkcounter(start):
count = [start]
def next():
count[0] += 1
return count[0]
def prev():
count[0] -= 1
return count[0]
def get():
return count[0]
def set(value):
count[0] = value
return count[0]
def bump(incr):
count[0] += incr
return count[0]
def reset():
count[0] = start
return count[0]
return {
'next': next, 'prev': prev, 'get': get, 'set': set,
'bump': bump, 'reset': reset, 'last': prev}
mref = obj.meth
mref("args", "go", "here")
record = {
"name": "Jason",
"empno": 132,
"title": "deputy peon",
"age": 23,
"salary": 37000,
"pals": ["Norbert", "Rhys", "Phineas"],
}
print "I am %s, and my pals are %s." % (record["name"],
", ".join(record["pals"]))
byname = {}
byname[record["name"]] = record
rp = byname.get("Aron")
if rp:
print "Aron is employee %d."% rp["empno"]
byname["Jason"]["pals"].append("Theodore")
print "Jason now has %d pals." % len(byname["Jason"]["pals"])
for name, record in byname.items():
print "%s is employee number %d." % (name, record["empno"])
employees = {}
employees[record["empno"]] = record;
rp = employees.get(132)
if (rp):
print "Employee number 132 is %s." % rp["name"]
byname["Jason"]["salary"] *= 1.035
peons = [r for r in employees.values() if r["title"] == "peon"]
tsevens = [r for r in employees.values() if r["age"] == 27]
print employees.values()
for rp in sorted(employees.values(), key=lambda x:x["age"]):
print "%s is age %d."%(rp["name"], rp["age"])
byage = {}
byage[record["age"]] = byage.get(record["age"], []) + [record]
for age, records in byage.items():
print records
print "Age %s:"%age,
for rp in records:
print rp["name"],
print
FieldName: Value
for record in list_of_records:
for key in sorted(record.keys()):
print "%s: %s" % (key, record[key])
print
import re
list_of_records = [{}]
while True:
line = sys.stdin.readline()
if not line:
break
line = line[:1]
if not line.strip():
list_of_records.append({})
continue
key, value = re.split(r':\s*', line, 1)
list_of_records[-1][key] = value
import pprint
mylist = [[1,2,3], [4, [5,6,7], 8,9, [0,3,5]], 7, 8]
mydict = {"abc": "def", "ghi":[1,2,3]}
pprint.pprint(mylist, width=1)
fmtdict = pprint.pformat(mydict, width=1)
print fmtdict
newlist = list(mylist) newdict = dict(mydict)
import copy
newlist = copy.copy(mylist) newdict = copy.copy(mydict)
mylist = ["1", "2", "3"]
newlist = list(mylist)
mylist[0] = "0"
print mylist, newlist
mylist = [["1", "2", "3"], 4]
newlist = list(mylist)
mylist[0][0] = "0"
print mylist, newlist
import copy
newlist = copy.deepcopy(mylist) newdict = copy.deepcopy(mydict)
import copy
mylist = [["1", "2", "3"], 4]
newlist = copy.deepcopy(mylist)
mylist[0][0] = "0"
print mylist, newlist
import pickle
class Foo(object):
def __init__(self):
self.val = 1
x = Foo()
x.val = 3
p_x = pickle.dumps(x) del x
x = pickle.loads(p_x) print x.val
import os, shelve
fname = "testfile.db"
if not os.path.exists(fname):
d = shelve.open("testfile.db")
for i in range(100000):
d[str(i)] = i
d.close()
d = shelve.open("testfile.db")
print d["100"]
print d["1212010201"]
import random
import warnings
class BTree(object):
def __init__(self):
self.value = None
def insert(self, value):
if self.value is None:
self.left = BTree()
self.right = BTree()
self.value = value
elif self.value > value:
self.left.insert(value)
elif self.value < value:
self.right.insert(value)
else:
warnings.warn("Duplicate insertion of %s."%value)
def in_order(self):
if self.value is not None:
self.left.in_order()
print self.value,
self.right.in_order()
def pre_order(self):
if self.value is not None:
print self.value,
self.left.pre_order()
self.right.pre_order()
def post_order(self):
if self.value is not None:
self.left.post_order()
self.right.post_order()
print self.value,
def search(self, value):
if self.value is not None:
if self.value == value:
return self
if value < self.value:
return self.left.search(value)
else:
return self.right.search(value)
def test():
root = BTree()
for i in range(20):
root.insert(random.randint(1, 1000))
print "Pre order: ", root.pre_order()
print "In order: ", root.in_order()
print "Post order:", root.post_order()
while True:
val = raw_input("Search? ").strip()
if not val:
break
val = int(val)
found = root.search(val)
if found:
print "Found %s at %s, %s"%(val, found, found.value)
else:
print "No %s in tree" % val
if __name__ == "__main__":
test()
name = "first"
name = "last"
import Alpha, Omega
print "Alpha is %s, Omega is %s." % (Alpha.name, Omega.name)
import sys
import Cards.Poker
__all__ = ["card_deck", "shuffle"] card_deck = []
def shuffle():
pass
__version__ = (1, 0) __all__ = ["...", "..."]
import YourModule
import YourModule as Module
from YourModule import *
from YourModule import name1, name2, xxx
from YourModule import name1 as name2
__all__ = ["F1", "F2", "List"]
__all__ = ["Op_Func", "Table"]
from YourModule import Op_Func, Table, F1
from YourModule import Functions, Table
mod = "module"
try:
__import__(mod)
except ImportError, err:
raise ImportError("couldn't load %s: %s" % (mod, err))
try:
import module
except ImportError, err:
raise ImportError("couldn't load 'module': %s" % (err, ))
try:
import module
except ImportError, err:
raise ImportError("couldn't load 'module': %s" % (err, ))
mod = "module"
try:
local_name = __import__(mod)
except ImportError, err:
raise ImportError("couldn't load %s: %s" % (mod, err))
mod = "module"
try:
globals()[mod] = __import__(mod)
except ImportError, err:
raise ImportError("couldn't load %s: %s" % (mod, err))
DBs = "Giant.Eenie Giant.Meanie Mouse.Mynie Moe".split()
for mod in DBs.split():
try:
loaded_module = __import__(mod)
except ImportError:
continue
for term in mod.split(".")[:-1]:
loaded_module = getattr(loaded_module, term)
break
else:
raise ImportError("None of %s loaded" % DBs)
import sys
if __name__ == "__main__":
if len(sys.argv) != 3 or not sys.argv[1].isdigit() \
or not sys.argv[2].isdigit():
raise SystemExit("usage: %s num1 num2" % sys.argv[0])
import Some.Module
import More.Modules
if opt_b:
import math
from os import O_EXCL, O_CREAT, O_RDWR
import os
O_EXCL = os.O_EXCL
O_CREAT = os.O_CREAT
O_RDWR = os.O_RDWR
import os
O_EXCL, O_CREAT, O_RDWR = os.O_EXCL, os.O_CREAT, os.O_RDWR
load_module('os', "O_EXCL O_CREAT O_RDWR".split())
def load_module(module_name, symbols):
module = __import__(module_name)
for symbol in symbols:
globals()[symbol] = getattr(module, symbol)
__version__ = (1, 0)
__all__ = ["flip_boundary", "flip_words"]
Separatrix = ' '
def flip_boundary(sep = None):
prev_sep = Separatrix
if sep is not None:
global Separatrix
Separatrix = sep
return prev_sep
def flip_words(line):
words = line.split(Separatrix)
words.reverse()
return Separatrix.join(words)
this_pack = __name__
that_pack = sys._getframe(1).f_globals.get("__name__", "<string>")
print "I am in package", __name__
def nreadline(count, myfile):
if count <= 0:
raise ValueError("Count must be > 0")
return [myfile.readline() for i in range(count)]
def main():
myfile = open("/etc/termcap")
a, b, c = nreadline(3, myfile)
myfile.close()
if __name__ == "__main__":
main()
import sys
def nreadline(count, handle_name):
assert count > 0, "count must be > 0"
locals = sys._getframe(1).f_locals
if not locals.has_key(handle_name):
raise AssertionError("need open filehandle")
infile = locals[handle_name]
retlist = []
for line in infile:
retlist.append(line)
count -= 1
if count == 0:
break
return retlist
def main():
FH = open("/etc/termcap")
a, b, c = nreadline(3, "FH")
if __name__ == "__main__":
main()
import time, os, sys
def _getgmtime(asctime=time.asctime, gmtime=time.gmtime,
t=time.time):
return asctime(gmtime(t()))
class Logfile:
def __init__(self, file):
self.file = file
def _logmsg(self, msg, argv0=sys.argv[0], pid=os.getpid(),
_getgmtime=_getgmtime):
now = _getgmtime()
print>>self.file, argv0, pid, now + ":", msg
def logmsg(self, msg):
self._logmsg(self.file, msg)
def __del__(self):
self._logmsg("shutdown")
self.file.close()
def __getattr__(self, attr):
return getattr(self.file, attr)
LF = Logfile(open("/tmp/mylog", "a+", 0))
logmsg = LF.logmsg
if __name__ == "__main__":
import logger
logger.init("/tmp/mylog")
try:
main()
finally:
logger.close()
for i, name in zip(xrange(sys.maxint), sys.path):\
print i, repr(name)
import sys
sys.path.insert(0, "/projects/spectre/lib")
import FindBin
sys.path.insert(0, FindBin.Bin)
import FindBin
Bin = "Name"
bin = getattr(FindBin, Bin)
sys.path.insert(0, bin + "/../lib")
def open():
pass from MyModule import open
file = open()
def even_only(n):
if n & 1: raise AssertionError("%s is not even" % (n,))
def even_only(n):
if n % 2: raise TypeError("%s is not even" % (n,))
import warnings
def even_only(n):
if n & 1: warnings.warn("%s is not even, continuing" % (n))
n += 1
warnings.filterwarnings("ignore")
val = getattr(__import__(packname), varname)
vals = getattr(__import__(packname), aryname)
getattr(__import__(packname), funcname)("args")
import math
def make_log(n):
def logn(val):
return math.log(val, n)
return logn
globaldict = globals()
for i in range(2, 1000):
globaldict["log%s"%i] = make_log(i)
for i in range(2,1000):
exec "log%s = make_log(i)"%i in globals()
print log20(400)
blue = colours.blue
someobject.blue = colours.azure
class Foo(object):
"A class demonstrating docstrings."
class Foo(object):
__doc__ = "A class demonstrating docstrings."
class Foo(object):
pass
Foo.__doc__ = "A class demonstrating docstrings."
def foo():
"A function demonstrating docstrings."
def foo():
pass
foo.__doc__ = "A function demonstrating docstrings."
import pydoc
print pydoc.getdoc(int)
pydoc.help(int)
help(int)
import sys, pydoc
def print_module_info(path, modname, desc):
if modname.split(".")[-1].startswith("test_"):
return
try:
mod = pydoc.safeimport(modname)
except pydoc.ErrorDuringImport:
return
version = getattr(mod, "__version__", "unknown")
if isinstance(version, type("")):
pass
else:
".".join(map(str, version))
synopsis, text = pydoc.splitdoc(desc)
print "%s (%s) - %s" % (modname, version, synopsis)
scanner = pydoc.ModuleScanner()
scanner.run(print_module_info)
class Encoder(object):
pass
obj = [3, 5]
print type(obj), id(obj), ob[1]
obj.Stomach = "Empty" obj.NAME = "Thag" (optional)
encoded = object.encode("data")
encoded = Data.Encoder.encode("data")
class Class(object):
def __init__(self):
pass
object = Class()
class Class(object):
def class_only_method():
pass class_only_method = staticmethod(class_only_method)
class Class(object):
def instance_only_method(self):
pass lector = Human.Cannibal()
lector.feed("Zak")
lector.move("New York")
lector = Human.Cannibal()
Human.Cannibal.feed(lector, "Zak")
Human.Cannibal.move(lector, "New York")
print>>sys.stderr, "stuff here\n"
class Class(object):
pass
import time
class Class(object):
def __init__(self):
self.start = time.time() self.age = 0
import time
class Class(object):
def __init__(self, **kwargs):
self.age = 0
self.__dict__.update(kwargs)
import time
class Class(object):
def __del__(self):
print self, "dying at", time.ctime()
self.WHATEVER = self
class MyClass(object)
def __init__(self):
self.name = "default"
self.age = 0
obj = MyClass()
obj.name = "bob"
print obj.name
obj.age += 1
class MyClass(object):
def __init__(self):
self._name = "default"
self._age = 0
def get_name(self):
return self._name
def set_name(self, name):
self._name = name.title()
name = property(get_name, set_name)
def get_age(self):
return self._age
def set_age(self, val):
if val < 0:
raise ValueError("Invalid age: %s" % val)
self._age = val
age = property(get_age, set_age)
obj = MyClass()
obj.name = "bob"
print obj.name
obj.age += 1
class MyClass(object):
def __init__(self):
self.name = "default"
def get_name(self):
return self.name
def set_name(self, name):
self.name = name.title()
obj = MyClass()
obj.set_name("bob")
print obj.get_name()
class MyClass(object):
def __init__(self):
self.age = 0
def name(self, *args):
if len(args) == 0:
return self.name
elif len(args) == 1:
self.name = args[0]
else:
raise TypeError("name only takes 0 or 1 arguments")
def age(self, *args):
prev = self.age
if args:
self.age = args[0]
return prev
obj.age(1 + obj.age())
him = Person()
him.NAME = "Sylvester"
him.AGE = 23
import re, sys
def carp(s):
sys.stderr.write("WARNING: " + s + "\n")
class Class:
no_name = []
def name(self, value = no_name):
if value is Class.no_name:
return self.NAME
value = self._enforce_name_value(value)
self.NAME = value
def _enforce_name_value(self, value):
if re.search(r"[^\s\w'-]", value):
carp("funny characters in name")
if re.search(r"\d", value):
carp("numbers in name")
if not re.search(r"\S+(\s+\S+)+", value):
carp("prefer multiword name")
if not re.search(r"\S", value):
carp("name is blank")
return value.upper() class Class:
def __setattr__(self, name, value):
if name == "name":
value = self._enforce_name_value(value) self.__dict__[name] = value
def _enforce_name_value(self, value):
if re.search(r"[^\s\w'-]", value):
carp("funny characters in name")
if re.search(r"\d", value):
carp("numbers in name")
if not re.search(r"\S+(\s+\S+)+", value):
carp("prefer multiword name")
if not re.search(r"\S", value):
carp("name is blank")
return value.upper()
class Person:
def __init__(self, name = None, age = None, peers = None):
if peers is None: peers = [] self.name = name
self.age = age
self.peers = peers
def exclaim(self):
return "Hi, I'm %s, age %d, working with %s" % \
(self.name, self.age, ", ".join(self.peers))
def happy_birthday(self):
self.age += 1
return self.age
def population():
return Person.body_count[0]
class Person(object):
body_count = [0]
def __init__(self):
self.body_count[0] += 1
def __del__(self): self.body_count[0] -= 1
import Person
people = []
for i in range(10):
people.append(Person.Person())
print "There are", Person.population(), "people alive."
him = Person()
him.gender = "male"
her = Person()
her.gender = "female"
FixedArray.max_bounds = 100 alpha = FixedArray.FixedArray()
print "Bound on alpha is", alpha.max_bounds
beta = FixedArray.FixedArray()
beta.max_bounds = 50 print "Bound on alpha is", alpha.max_bounds
class FixedArray(object):
_max_bounds = [7]
def __init__(self, bounds=None):
if bounds is not None:
self.max_bounds = bounds
def get_max_bounds(self):
return self._max_bounds[0]
def set_max_bounds(self, val):
self._max_bounds[0] = val
max_bounds = property(get_max_bounds, set_max_bounds)
class Person:
def __init__(self, name=None, age=None, peers=None):
if peers is None:
peers = []
self.name = name
self.age = age
self.peers = peers
p = Person("Jason Smythe", 13, ["Wilbur", "Ralph", "Fred"])
p = Person() p.name = "Jason Smythe" p.age = 13 p.peers.extend( ["Wilbur", "Ralph", "Fred" ] )
p.peers = ["Wilbur", "Ralph", "Fred"]
p.peers[:]= ["Wilbur", "Ralph", "Fred"]
print "At age %d, %s's first friend is %s." % \
(p.age, p.name, p.peers[0])
import sys
def carp(s):
sys.stderr.write("WARNING: " + s + "\n")
class Person:
def __init__(self, name = "", age = 0):
self.name = name
self.age = age
def __setattr__(self, name, value):
if name == "age":
if not isinstance(value, type(0)):
carp("age '%s' isn't numeric" % (value,))
if value > 150: carp("age '%s' is unreasonable" % (value,))
self.__dict__[name] = value
class Family:
def __init__(self, head = None, address = "", members = None):
if members is None: members = []
self.head = head or Person()
self.address = address
self.members = members
folks = Family()
dad = folks.head
dad.name = "John"
dad.age = 34
print "%s's age is %d" % (folks.head.name, folks.head.age)
class Card:
def __init__(self, name=None, color=None, cost=None,
type=None, release=None, text=None):
self.name = name
self.color = color
self.cost = cost
self.type = type
self.release = release
self.type = type
class Card:
_names = ("name", "color", "cost", "type", "release", "type")
def __init__(self, *args):
assert len(args) <= len(self._names)
for k, v in zip(self._names, args):
setattr(self, k, None)
class Card:
_names = ("name", "color", "cost", "type", "release", "type")
def __init__(self, **kwargs):
for k in self._names: setattr(self, k, None)
for k, v in kwargs.items(): assert k in self._names, "Unexpected kwarg: " + k
setattr(self, k, v)
class hostent:
def __init__(self, addr_list = None, length = None,
addrtype = None, aliases = None, name = None):
self.addr_list = addr_list or []
self.length = length or 0
self.addrtype = addrtype or ""
self.aliases = aliases or []
self.name = name or ""
class Class(Parent):
pass
ob1 = SomeClass()
ob2 = ob1.__class__()
ob1 = Widget()
ob2 = ob1.__class__()
import time
class Person(possible,base,classes):
def __init__(self, *args, **kwargs):
for baseclass in self.__class__.__bases__:
init = getattr(baseclass, "__init__")
if init is not None:
init(self, *args, **kwargs)
self.PARENT = parent self.START = time.time()
self.AGE = 0
methname = "flicker"
getattr(obj, methname)(10)
for m in ("start", "run", "stop"):
getattr(obj, m)()
methods = ("name", "rank", "serno")
his_info = {}
for m in methods:
his_info[m] = getattr(ob, m)()
his_info = {
'name': ob.name(),
'rank': ob.rank(),
'serno': ob.serno(),
}
fnref = ob.method
fnref(10, "fred")
obj.method(10, "fred")
if isinstance(obj_target, obj.__class__):
obj.can('method_name')(obj_target, *arguments)
isinstance(obj, mimetools.Message)
issubclass(obj.__class__, mimetools.Message)
if hasattr(obj, "method_name"): pass
his_print_method = getattr(obj, "as_string", None)
__version__ = (3, 0)
Some_Module.__version__
his_vers = obj.__module__.__version__
if Some_Module.__version__ < (3, 0):
raise ImportError("Some_Module version %s is too old, expected (3, 0)" %
(Some_Module.__version__,))
assert Some_Module.__version__ >= (3, 0), "version too old"
__VERSION__ = '1.01'
class Person:
def __init__(self, name = "", age = 0):
self.name = name
self.age = age
dude = Person()
dude.name = "Jason"
dude.age = 23
print "%s is age %d." % (dude.name, dude.age)
class Employee(Person):
pass
emp = Employee()
empl.name = "Jason"
empl.age = 23
print "%s is age %d." % (empl.name, empl.age)
class Class(BaseClass):
def method(self, *args, **kwargs):
BaseClass.method(self, *args, **kwargs)
class Class(BaseClass1, BaseClass2):
def method(self, *args, **kwargs):
BaseClass2.method(self, *args, **kwargs)
class Class(BaseClass1, BaseClass2, BaseClass3):
def method(self, *args, **kwargs):
for baseclass in self.__class__.__bases__:
f = getattr(baseclass, "method")
if f is not None:
return f(*args, **kwargs)
raise NotImplementedError("method")
self.meth()
Where.meth(self)
import time
class Class:
def __init__(self, *args):
self.START = time.time()
self.AGE = 0
self.EXTRA = args obj = Widget(haircolor = "red", freckles = 121)
class Class(Base1, Base2, Base3):
def __init__(self, *args, **kwargs):
for base in self.__class__.__bases__:
f = getattr(base, "__init__")
if f is not None:
f(self, *args, **kwargs)
class Private:
def __init__(self, names):
self.__names = names
self.__data = {}
def __getattr__(self, name):
if name in self.__names:
return self.__data[name]
raise AttributeError(name)
def __setattr__(self, name, value):
if name.startswith("_Private"):
self.__dict__[name] = value
return
if name in self.__names:
self.__data[name] = value
return
raise TypeError("cannot set the attribute %r" % (name,))
class Person(Private):
def __init__(self, parent = None):
Private.__init__(self, ["name", "age", "peers", "parent"])
self.parent = parent
def new_child(self):
return Person(self)
dad = Person()
dad.name = "Jason"
dad.age = 23
kid = dad.new_child()
kid.name = "Rachel"
kid.age = 2
print "Kid's parent is", kid.parent.name
node.NEXT = node
class Node:
def __init__(self, value = None):
self.next = self
self.prev = self
self.value = value
class Ring:
def __init__(self):
self.ring = None
self.count = 0
def __str__(self):
s = "#%d: " % self.count
x = self.ring
if x is None:
return s
values = []
while True:
values.append(x.value)
x = x.next
if x is self.ring:
break
return s + " -> ".join(map(str, values)) + " ->"
def search(self, value):
node = self.ring
while True:
if node.value == value:
return node
node = node.next
if node is self.ring:
break
def insert_value(self, value):
node = Node(value)
if self.ring is not None:
node.prev, node.next = self.ring.prev, self.ring
self.ring.prev.next = self.ring.prev = node
self.ring = node
self.count += 1
def delete_value(self, value):
node = self.search(value)
if node is not None:
self.delete_node(node)
def delete_node(self, node):
if node is node.next:
node.next = node.prev = None
self.ring = None
else:
node.prev.next, node.next.prev = node.next, node.prev
if node is self.ring:
self.ring = node.next
self.count -= 1
def __del__(self):
while self.ring is not None:
self.delete_node(self.ring)
COUNT = 1000
for rep in range(20):
r = Ring()
for i in range(COUNT):
r.insert_value(i)
import UserString
class MyString(UserString.UserString):
def __cmp__(self, other):
return cmp(self.data.upper(), other.upper())
class Person:
def __init__(self, name, idnum):
self.name = name
self.idnum = idnum
def __str__(self):
return "%s (%05d)" % (self.name.lower().capitalize(), self.idnum)
class TimeNumber:
def __init__(self, hours, minutes, seconds):
assert minutes < 60 and seconds < 60
self.hours = hours
self.minutes = minutes
self.seconds = seconds
def __str__(self):
return "%d:%02d:%02d" % (self.hours, self.minutes, self.seconds)
def __add__(self, other):
seconds = self.seconds + other.seconds
minutes = self.minutes + other.minutes
hours = self.hours + other.hours
if seconds >= 60:
seconds %= 60
minutes += 1
if minutes >= 60:
minutes %= 60
hours += 1
return TimeNumber(hours, minutes, seconds)
def __sub__(self, other):
raise NotImplementedError
def __mul__(self, other):
raise NotImplementedError
def __div__(self, other):
raise NotImplementedError
t1 = TimeNumber(0, 58, 59)
sec = TimeNumber(0, 0, 1)
min = TimeNumber(0, 1, 0)
print t1 + sec + min + min
class StrNum:
def __init__(self, value):
self.value = value
def __cmp__(self, other): return cmp(self.value, other.value)
def __str__(self): return self.value
def __nonzero__(self, other): return bool(self.value)
def __int__(self, other): return int(self.value)
def __add__(self, other): return StrNum(self.value + other.value)
def __radd__(self, other): return StrNum(other.value + self.value)
def __mul__(self, other): return StrNum(self.value * other)
def __rmul__(self, other): return StrNum(self.value * other)
def demo():
x = StrNum("Red")
y = StrNum("Black")
z = x + y
r = z * 3
print "values are %s, %s, %s, and %s" % (x, y, z, r)
if x < y:
s = "LT"
else:
s = "GE"
print x, "is", s, y
if __name__ == "__main__":
demo()
import re
_places_re = re.compile(r"\.(\d+)")
default_places = 0
class FixNum:
def __init__(self, value, places = None):
self.value = value
if places is None:
m = _places_re.search(str(value))
if m:
places = int(m.group(1))
else:
places = default_places
self.places = places
def __add__(self, other):
return FixNum(self.value + other.value,
max(self.places, other.places))
def __mul__(self, other):
return FixNum(self.value * other.value,
max(self.places, other.places))
def __div__(self, other):
return FixNum((self.value+0.0) / other.value,
max(self.places, other.places))
def __str__(self):
return "STR%s: %.*f" % (self.__class__.__name__,
self.places, self.value)
def __int__(self):
return int(self.value)
def __float__(self):
return self.value
def demo():
x = FixNum(40)
y = FixNum(12, 0)
print "sum of", x, "and", y, "is", x+y
print "product of", x, "and", y, "is", x*y
z = x/y
print "%s has %d places" % (z, z.places)
if not z.places:
z.places = 2
print "div of", x, "by", y, "is", z
print "square of that is ", z*z
if __name__ == "__main__":
demo()
import itertools
class ValueRing(object):
def __init__(self, colours):
self.colourcycle = itertools.cycle(colours)
def next_colour(self):
return self.colourcycle.next()
colour = property(next_colour)
vr = ValueRing(["red", "blue"])
for i in range(6):
print vr.colour,
print
x = vr.colour
print x, x, x
class AppendDict(dict):
def __setitem__(self, key, val):
if key in self:
self[key].append(val)
else:
super(AppendDict, self).__setitem__(key, [val])
tab = AppendDict()
tab["beer"] = "guinness"
tab["food"] = "potatoes"
tab["food"] = "peas"
for key, val in tab.items():
print key, "=>", val
class CaselessDict(dict):
def __setitem__(self, key, val):
super(CaselessDict, self).__setitem__(key.lower(), val)
def __getitem__(self, key):
return super(CaselessDict, self).__getitem__(key.lower())
tab = CaselessDict()
tab["VILLAIN"] = "big "
tab["herOine"] = "red riding hood"
tab["villain"] = "bad wolf"
for key, val in tab.items():
print key, "is", val
class RevDict(dict):
def __setitem__(self, key, val):
super(RevDict, self).__setitem__(key, val)
super(RevDict, self).__setitem__(val, key)
tab = RevDict()
tab["red"] = "rojo"
tab["blue"] = "azul"
tab["green"] = "verde"
tab["evil"] = ("No Way!", "Way!")
for key, val in tab.items():
print key, "is", val
import itertools
for elem in itertools.count():
print "Got", elem
tee = FileDispatcher(sys.stderr, sys.stdout)
import anydbm
filename = "test.db"
try:
db = anydbm.open(filename)
except anydbm, err:
print "Can't open %s: %s!" % (filename, err)
db["key"] = "value" if "key" in db: val = db.pop("key") db.close()
import sys, os, anydbm, re
db_file = '/tmp/userstats.db'
try:
db = anydbm.open(db_file,'c') except:
print "Can't open db %s: %s!" % (db_file, sys.exc_info()[1])
sys.exit(1)
if len(sys.argv) > 1:
if sys.argv[1] == 'ALL':
userlist = db.keys()
else:
userlist = sys.argv[1:]
userlist.sort()
for user in userlist:
if db.has_key(user):
print "%s\t%s" % (user, db[user])
else:
print "%s\t%s" % (user, 0)
else:
who = os.popen('who').readlines() if len(who)<1:
print "error running who" sys.exit(1)
user_re = re.compile("^(\S+)")
for line in who:
fnd = user_re.search(line)
if not fnd:
print "Bad line from who: %s" % line
sys.exit(1)
user = fnd.groups()[0]
if not db.has_key(user):
db[user] = "0"
db[user] = str(int(db[user])+1) db.close()
import anydbm
try:
db = anydbm.open(FILENAME,'w') except anydbm.error, err:
print "Can't open db %s: %s!" % (filename, err)
raise SystemExit(1)
db.clear()
db.close()
try:
db = anydbm.open(filename,'n') except anydbm.error, err:
print "Can't open db %s: %s!" % (filename, err)
raise SystemExit(1)
db.close()
import os
try:
os.remove(FILENAME)
except OSError, err:
print "Couldn't remove %s to empty the database: %s!" % (FILENAME,
err)
raise SystemExit
try:
db = anydbm.open(FILENAME,'n') except anydbm.error, err:
print "Couldn't create %s database: %s!" % (FILENAME, err)
raise SystemExit
import sys
import dbm, gdbm
if len(sys.argv)<3:
print "usage: db2gdbm infile outfile"
sys.exit(1)
(infile, outfile) = sys.argv[1:]
try:
db_in = dbm.open(infile)
except:
print "Can't open infile %s: %s!" % (infile, sys.exc_info()[1])
sys.exit(1)
try:
db_out = dbm.open(outfile,"n")
except:
print "Can't open outfile %s: %s!" % (outfile, sys.exc_info()[1])
sys.exit(1)
for k in db_in.keys():
db_out[k] = db_in[k]
db_out.close()
db_in.close()
OUTPUT.update(INPUT1)
OUTPUT.update(INPUT2)
OUTPUT = anydbm.open("OUT","n")
for INPUT in (INPUT1, INPUT2, INPUT1):
for key, value in INPUT.iteritems():
if OUTPUT.has_key(key):
print "key %s already present: %s, new: %s" % (
key, OUTPUT[key], value )
else:
OUTPUT[key] = value
import dbhash
dbhash.open("mydb.db", "cl")
import shelve
db = shelve.open("celebrities.db")
name1 = "Greg Stein"
name2 = "Greg Ward"
db[name1] = ["of ViewCVS fame", "gstein@lyra.org"]
db[name2] = ["of Distutils fame", "gward@python.net"]
greg1 = db[name1]
greg2 = db[name2]
print "Two Gregs: %x %x" % (id(greg1), id(greg2))
if greg1 == greg2:
print "You're having runtime fun with one Greg made two."
else:
print "No two Gregs are ever alike."
entry = db[name1]
entry[0] = "of Subversion fame"
db[name1] = entry
db = shelve.open("celebrities.db", writeback=True)
db[name2][0] = "of Optik fame"
db.sync()
import os as _os, shelve as _shelve
_fname = "persist.db"
if not _os.path.exists(_fname):
var1 = "foo"
var2 = "bar"
_d = _shelve.open("persist.db")
globals().update(_d)
print "var1 is %s; var2 is %s"%(var1, var2)
var1 = raw_input("New var1: ")
var2 = raw_input("New var2: ")
for key, val in globals().items():
if not key.startswith("_"):
_d[key] = val
import dbmodule
dbconn = dbmodule.connect(arguments...)
cursor = dbconn.cursor()
cursor.execute(sql)
while True:
row = cursor.fetchone()
if row is None:
break
...
cursor.close()
dbconn.close()
import MySQLdb
import pwd
dbconn = MySQLdb.connect(db='dbname', host='mysqlserver.domain.com',
port=3306, user='user', passwd='password')
cursor = dbconn.cursor()
cursor.execute("CREATE TABLE users (uid INT, login CHAR(8))")
sql_fmt = "INSERT INTO users VALUES( %s, %s )"
for userent in pwd.getpwall():
cursor.execute(sql_fmt, (userent.pw_uid, userent.pw_name))
cursor.execute("SELECT * FROM users WHERE uid < 50")
for row in cursor.fetchall():
print ", ".join(map(str, row))
cursor.execute("DROP TABLE users")
cursor.close()
dbconn.close()
import sys
import getopt
argv = sys.argv[1:]
opts, rest = getopt.getopt(argv, "vDo:")
opts, rest = getopt.getopt(argv, "v:D:o:")
opts, rest = getopt.getopt(argv, "", [
"verbose", "Debug", "output="])
import sys
def is_interactive_python():
try:
ps = sys.ps1
except:
return False
return True
import sys
def is_interactive():
return sys.stdin.isatty()
is_interactive = sys.stdin.isatty
import posix
def is_interactive_posix():
tty = open("/dev/tty")
tpgrp = posix.tcgetpgrp(tty.fileno())
pgrp = posix.getpgrp()
tty.close()
return (tpgrp == pgrp)
print "is python shell:", is_interactive_python()
print "is a tty:", is_interactive()
print "has no tty:", is_interactive_posix()
if is_interactive():
while True:
try:
ln = raw_input("Prompt:")
except:
break
print "you typed:", ln
import os
os.system("clear")
clear = os.popen("clear").read()
print clear
sys.stdout.write(clear)
import struct, fcntl, termios, sys
s = struct.pack("HHHH", 0, 0, 0, 0)
hchar, wchar = struct.unpack("HHHH", fcntl.ioctl(sys.stdout.fileno(),
termios.TIOCGWINSZ, s))[:2]
import curses
(hchar,wchar) = curses.getmaxyx()
import struct, fcntl, termios, sys
width = struct.unpack("HHHH", fcntl.ioctl(sys.stdout.fileno(),
termios.TIOCGWINSZ,
struct.pack("HHHH", 0, 0, 0, 0)))[1]
if width<10:
print "You must have at least 10 characters"
raise SystemExit
max_value = 0
for v in values:
max_value = max(max_value,v)
ratio = (width-10)/max_value for v in values:
print "%8.1f %s" % (v, "*"*(v*ratio))
RED = '\033[31m'
RESET = '\033[0;0m'
BLINK = '\033[05m'
NOBLINK = '\033[25m'
print RED+"DANGER, Will Robinson!"+RESET
print "This is just normal text"
print "Will ``"+BLINK+"Do you hurt yet?"+NOBLINK+"'' and back"
class _Getch:
"""Gets a single character from standard input. Doesn't echo to screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self):
return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
print "Press keys to see their ASCII values. Use Ctrl-C to quit.\n"
try:
while True:
char = ord(getch())
if char == 3:
break
print " Decimal: %3d Octal: %3o Hex: x%02x" % (char, char, char)
except KeyboardError:
pass
print "\aWake up!\n";
import msvcrt
if msvcrt.kbhit():
c = msvcrt.getch
import getpass
import pwd
import crypt
password = getpass.getpass('Enter your password: ')
username = getpass.getuser()
encrypted = pwd.getpwnam(username).pw_passwd
if not encrypted or encrypted == 'x':
print "Cannot verify password"
elif crypt.crypt(password, encrypted) != encrypted:
print "You are not", username
else:
print "Welcome,", username
import readline
readline.add_history("fake line")
line = raw_input()
import os
import readline
while True:
try:
cmd = raw_input('$ ')
except EOFError:
break
status = os.system(cmd)
exit_value = status >> 8
signal_num = status & 127
dumped_core = status & 128 and "(core dumped)" or ""
print "Program terminated with status %d from signal %d%s\n" % (
exit_value, signal_num, dumped_core)
readline.add_history("some line!")
readline.remove_history_item(position)
line = readline.get_history_item(index)
import code, readline
code.InteractiveConsole().interact("code.InteractiveConsole")
import pexpect
try:
command = pexpect.spawn("program to run")
except pexpect.ExceptionPexpect:
pass
command.setlog(None)
import sys
command.setlog(sys.stdout)
fp = file("pexpect.log", "w")
command.setlog(fp)
command.expect("ftp>")
command.expect("Name.*:")
import re
regex = re.compile("Name.*:")
command.expect(regex)
try:
command.expect("Password:", 10)
except pexpect.TIMEOUT:
pass
command.timeout = 10
try:
command.expect("Password:")
except pexpect.TIMEOUT:
pass
command.sendline("get spam_and_ham")
print>>command, "get spam_and_ham"
command.close()
import signal
command.kill(signal.SIGKILL)
which = command.expect(["invalid", "success", "error", "boom"])
choices = ["invalid", "success", "error", "boom"]
choices.append(pexpect.TIMEOUT)
choices.append(pexpect.EOF)
which = command.expect(choices)
from Tkinter import *
def print_callback():
print "print_callback"
main = Tk()
menubar = Menu(main)
main.config(menu=menubar)
file_menu = Menu(menubar)
menubar.add_cascade(label="File", underline=1, menu=file_menu)
file_menu.add_command(label="Print", command=print_callback)
main.mainloop()
from Tkinter import *
class Application(Tk):
def print_callback(self):
print "print_callback"
def debug_callback(self):
print "debug:", self.debug.get()
print "debug level:", self.debug_level.get()
def createWidgets(self):
menubar = Menu(self)
self.config(menu=menubar)
file_menu = Menu(menubar)
menubar.add_cascade(label="File",
underline=1, menu=file_menu)
file_menu.add_command(label="Print",
command=self.print_callback)
file_menu.add_command(label="Quit Immediately",
command=sys.exit)
options_menu = Menu(menubar)
menubar.add_cascade(label="Options",
underline=0, menu=options_menu)
options_menu.add_checkbutton(
label="Create Debugging File",
variable=self.debug,
command=self.debug_callback,
onvalue=1, offvalue=0)
options_menu.add_separator()
options_menu.add_radiobutton(
label = "Level 1",
variable = self.debug_level,
value = 1
)
options_menu.add_radiobutton(
label = "Level 2",
variable = self.debug_level,
value = 2
)
options_menu.add_radiobutton(
label = "Level 3",
variable = self.debug_level,
value = 3
)
def __init__(self, master=None):
Tk.__init__(self, master)
self.debug = IntVar()
self.debug.set(0)
self.debug_level = IntVar()
self.debug_level.set(1)
self.createWidgets()
app = Application()
app.mainloop()
import popen2
(err_out, stdin) = popen2.popen4("program args")
lines = err_out.read()
(err_out, stdin) = popen2.popen4("program args")
lines = err_out.readlines()
(err_out, stdin) = popen2.popen4("program args")
output = []
while True:
line = err_out.readline()
if not line:
break
output.appen(line)
output = ''.join(output)
import os
myfile = "foo.txt"
status = os.system("vi %s" % myfile)
import os
os.system("cmd1 args | cmd2 | cmd3 >outfile")
os.system("cmd args <infile >outfile 2>errfile")
status = os.system("%s %s %s" % (program, arg1, arg2))
if status != 0:
print "%s exited funny: %s" % (program, status)
raise SystemExit
import os
import sys
import glob
args = glob.glob("*.data")
try:
os.execvp("archive", args)
except OSError, e:
print "Couldn't replace myself with archive: %s" % err
raise SystemExit
os.execvp("archive", ["accounting.data"])
import sys
import popen2
pipe = popen2.Popen4("program arguments")
pid = pipe.pid
for line in pipe.fromchild.readlines():
sys.stdout.write(line)
import popen2
pipe = popen2.Popen4("gzip > foo.gz")
pid = pipe.pid
pipe.tochild.write("Hello zipped world!\n")
pipe.tochild.close()
class OutputFilter(object):
def __init__(self, target, *args, **kwds):
self.target = target
self.setup(*args, **kwds)
self.textbuffer = ""
def setup(self, *args, **kwds):
pass
def write(self, data):
if data.endswith("\n"):
data = self.process(self.textbuffer + data)
self.textbuffer = ""
if data is not None:
self.target.write(data)
else:
self.textbuffer += data
def process(self, data):
return data
class HeadFilter(OutputFilter):
def setup(self, maxcount):
self.count = 0
self.maxcount = maxcount
def process(self, data):
if self.count < self.maxcount:
self.count += 1
return data
class NumberFilter(OutputFilter):
def setup(self):
self.count=0
def process(self, data):
self.count += 1
return "%s: %s"%(self.count, data)
class QuoteFilter(OutputFilter):
def process(self, data):
return "> " + data
import sys
f = HeadFilter(sys.stdout, 100)
for i in range(130):
print>>f, i
print
txt = """Welcome to Linux, version 2.0.33 on a i686
"The software required `Windows 95 or better',
so I installed Linux." """
f1 = NumberFilter(sys.stdout)
f2 = QuoteFilter(f1)
for line in txt.split("\n"):
print>>f2, line
print
f1 = QuoteFilter(sys.stdout)
f2 = NumberFilter(f1)
for line in txt.split("\n"):
print>>f2, line
# @@PLEAC@@_16.6
# This script accepts several filenames
# as argument. If the file is zipped, unzip
# it first. Then read each line if the file
import os
import sys
import popen2
for file in sys.argv[1:]:
if file.endswith(".gz") or file.endswith(".Z"):
(stdout, stdin) = popen2.popen2("gzip -dc '%s'" % file)
fd = stdout
else:
fd = open(file)
for line in fd:
# ....
sys.stdout.write(line)
fd.close()
#-----------------------------
#-----------------------------
# Ask for filename and open it
import sys
print "File, please?"
line = sys.stdin.readline()
file = line.strip() # chomp
open(file)
# @@PLEAC@@_16.7
# Execute foo_command and read the output
import popen2
(stdout_err, stdin) = popen2.popen4("foo_command")
for line in stdout_err.readlines():
# ....
# @@PLEAC@@_16.8
# Open command in a pipe
# which reads from stdin and writes to stdout
import popen2
pipe = popen2.Popen4("wc -l") # Unix command
pipe.tochild.write("line 1\nline 2\nline 3\n")
pipe.tochild.close()
output = pipe.fromchild.read()
# @@PLEAC@@_16.9
# popen3: get stdout and stderr of new process
# Attetion: This can lead to deadlock,
# since the buffer of stderr or stdout might get filled.
# You need to use select if you want to avoid this.
import popen2
(child_stdout, child_stdin, child_stderr) = popen2.popen3(...)
# @@PLEAC@@_16.10
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_16.11
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_16.12
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_16.13
#
# Print available signals and their value
# See "man signal" "man kill" on unix.
import signal
for name in dir(signal):
if name.startswith("SIG"):
value = getattr(signal, name)
print "%s=%s" % (name, value)
# @@PLEAC@@_16.14
# You can send signals to processes
# with os.kill(pid, signal)
# @@PLEAC@@_16.15
import signal
def get_sig_quit(signum, frame):
....
signal.signal(signal.SIGQUIT, get_sig_quit) # Install handler
signal.signal(signal.SIGINT, signal.SIG_IGN) # Ignore this signal
signal.signal(signal.SIGSTOP, signal.SIG_DFL) # Restore to default handling
# @@PLEAC@@_16.16
# Example of handler: User must Enter Name ctrl-c does not help
import sys
import signal
def ding(signum, frame):
print "\aEnter your name!"
return
signal.signal(signal.SIGINT, ding)
print "Please enter your name:"
name = ""
while not name:
try:
name = sys.stdin.readline().strip()
except:
pass
print "Hello: %s" % name
# @@PLEAC@@_16.17
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_16.18
import signal
# ignore signal INT
signal.signal(signal.SIGINT, signal.SIG_IGN)
# Install signal handler
def tsktsk(signum, frame):
print "..."
signal.signal(signal.SIGINT, tsktsk)
# @@PLEAC@@_16.19
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_16.20
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_16.21
import signal
def handler(signum, frame):
raise "timeout"
signal.signal(signal.SIGALRM, handler)
try:
signal.alarm(5) # signal.alarm(3600)
# long-time operation
while True:
print "foo"
signal.alarm(0)
except:
signal.alarm(0)
print "timed out"
else:
print "no time out"
# @@PLEAC@@_16.22
# @@INCOMPLETE@@
# @@INCOMPLETE@@
# @@PLEAC@@_17.0
# Socket Programming (tcp/ip and udp/ip)
import socket
# Convert human readable form to 32 bit value
packed_ip = socket.inet_aton("208.146.240.1")
packed_ip = socket.inet_aton("www.oreilly.com")
# Convert 32 bit value to ip adress
ip_adress = socket.inet_ntoa(packed_ip)
# Create socket object
socketobj = socket(family, type) # Example socket.AF_INT, socket.SOCK_STREAM
# Get socketname
socketobj.getsockname() # Example, get port adress of client
# @@PLEAC@@_17.1
# Example: Connect to a server (tcp)
# Connect to a smtp server at localhost and send an email.
# For real applications you should use smtplib.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 25)) # SMTP
print s.recv(1024)
s.send("mail from: <pleac@localhost>\n")
print s.recv(1024)
s.send("rcpt to: <guettli@localhost>\n")
print s.recv(1024)
s.send("data\n")
print s.recv(1024)
s.send("From: Python Lover\nSubject: Python is better then perl\n\nYES!\n.\n")
print s.recv(1024)
s.close()
# @@PLEAC@@_17.2
# Create a Server, calling handler for every client
# You can test it with "telnet localhost 1029"
from SocketServer import TCPServer
from SocketServer import BaseRequestHandler
class MyHandler(BaseRequestHandler):
def handle(self):
print "I got an request"
server = TCPServer(("127.0.0.1", 1029), MyHandler)
server.serve_forever()
# @@PLEAC@@_17.3
# This is the continuation of 17.2
import time
from SocketServer import TCPServer
from SocketServer import BaseRequestHandler
class MyHandler(BaseRequestHandler):
def handle(self):
# self.request is the socket object
print "%s I got an request from ip=%s port=%s" % (
time.strftime("%Y-%m-%d %H:%M:%S"),
self.client_address[0],
self.client_address[1]
)
self.request.send("What is your name?\n")
bufsize=1024
response=self.request.recv(bufsize).strip() # or recv(bufsize, flags)
data_to_send="Welcome %s!\n" % response
self.request.send(data_to_send) # or send(data, flags)
print "%s connection finnished" % self.client_address[0]
server = TCPServer(("127.0.0.1", 1028), MyHandler)
server.serve_forever()
# -----------------
# Using select
import select
import socket
in_list = []
in_list.append(mysocket)
in_list.append(myfile)
# ...
out_list = []
out_list.append(...)
except_list = []
except_list.append(...)
(in_, out_, exc_) = select.select(in_list, out_list, except_list, timeout)
for fd in in_:
print "Can read", fd
for fd in out_:
print "Can write", fd
for fd in exc_:
print "Exception on", fd
# Missing: setting TCP_NODELAY
# @@PLEAC@@_17.4
import socket
# Set up a UDP socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# send
MSG = 'Hello'
HOSTNAME = '127.0.0.1'
PORTNO = 10000
s.connect((HOSTNAME, PORTNO))
if len(MSG) != s.send(MSG):
# where to get error message "$!".
print "cannot send to %s(%d):" % (HOSTNAME,PORTNO)
raise SystemExit(1)
MAXLEN = 1024
(data,addr) = s.recvfrom(MAXLEN)
s.close()
print '%s(%d) said "%s"' % (addr[0],addr[1], data)
# download the following standalone program
#!/usr/bin/python
# clockdrift - compare another system's clock with this one
import socket
import struct
import sys
import time
if len(sys.argv)>1:
him = sys.argv[1]
else:
him = '127.1'
SECS_of_70_YEARS = 2208988800
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect((him,socket.getservbyname('time','udp')))
s.send('')
(ptime, src) = s.recvfrom(4)
host = socket.gethostbyaddr(src[0])
delta = struct.unpack("!L", ptime)[0] - SECS_of_70_YEARS - time.time()
print "Clock on %s is %d seconds ahead of this one." % (host[0], delta)
import socket
import sys
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.bind(('', server_port))
except socket.error, err:
print "Couldn't be a udp server on port %d : %s" % (
server_port, err)
raise SystemExit
while True:
datagram = s.recv(MAX_TO_READ)
if not datagram:
break
s.close()
import SocketServer
class handler(SocketServer.DatagramRequestHandler):
def handle(self):
s = SocketServer.UDPServer(('',10000), handler)
s.serve_forever()
import SocketServer
PORTNO = 5151
class handler(SocketServer.DatagramRequestHandler):
def handle(self):
newmsg = self.rfile.readline().rstrip()
print "Client %s said ``%s''" % (self.client_address[0], newmsg)
self.wfile.write(self.server.oldmsg)
self.server.oldmsg = newmsg
s = SocketServer.UDPServer(('',PORTNO), handler)
print "Awaiting UDP messages on port %d" % PORTNO
s.oldmsg = "This is the starting message."
s.serve_forever()
import socket
import sys
MAXLEN = 1024
PORTNO = 5151
TIMEOUT = 5
server_host = sys.argv[1]
msg = " ".join(sys.argv[2:])
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(TIMEOUT)
sock.connect((server_host, PORTNO))
sock.send(msg)
try:
msg = sock.recv(MAXLEN)
ipaddr, port = sock.getpeername()
hishost = socket.gethostbyaddr(ipaddr)
print "Server %s responded ``%s''" % ( hishost[0], msg)
except:
print "recv from %s failed (timeout or no server running)." % (
server_host )
sock.close()
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')
hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex('www.python.org')
socket.gethostbyname_ex('www.google.org')
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.shutdown(0) socket.shutdown(1) socket.shutdown(2)
server.send("my request\n") server.shutdown(1) answer = server.recv(1000)
import os
import sys
import time
import signal
def phoenix(signum, frame):
print "Restarting myself: %s %s" % (self, args)
os.execv(self, args)
self = os.path.abspath(sys.argv[0])
args = sys.argv[:]
signal.signal(signal.SIGHUP, phoenix)
while True:
print "work"
time.sleep(1)
import signal
config_file = "/usr/local/etc/myprog/server_conf.py"
def read_config():
execfile(config_file)
signal.signal(signal.SIGHUP, read_config)
import os
try:
os.chroot("/var/daemon")
except Exception:
print "Could not chroot"
raise SystemExit(1)
import os
pid = os.fork()
if pid:
print "I am the new child %s" % pid
raise SystemExit
else:
print "I am still the parent"
import os
id=os.setsid()
import time
import signal
time_to_die = 0
def sighandler(signum, frame):
print "time to die"
global time_to_die
time_to_die = 1
signal.signal(signal.SIGINT, sighandler)
signal.signal(signal.SIGTERM, sighandler)
signal.signal(signal.SIGHUP, sighandler)
while not time_to_die:
print "work"
time.sleep(1)
import socket
try:
host_info = socket.gethostbyname_ex(name)
except socket.gaierror, err:
print "Can't resolve hostname %s: %s" % (name, err[1])
import socket
try:
address = socket.gethostbyname(name)
except socket.gaierror, err:
print "Can't resolve hostname %s: %s" % (name, err[1])
try:
host_info = socket.gethostbyaddr(address)
except socket.gaierror, err:
print "Can't resolve address %s: %s" % (address, err[1])
import socket
try:
host_info = socket.gethostbyaddr(address)
except socket.gaierror, err:
print "Can't look up %s: %s" % (address, err[1])
raise SystemExit(1)
try:
host_info = socket.gethostbyname_ex(name)
except:
print "Can't look up %s: %s" % (name, err[1])
raise SystemExit(1)
found = address in host_info[2]
import sys
import dns
import dns.resolver
answers = dns.resolver.query(sys.argv[1], 'MX')
for rdata in answers:
print rdata.preference, rdata.exchange
import sys
import socket
name = sys.argv[1]
hent = socket.gethostbyname_ex(name)
print "%s aliases: %s => %s" % (
hent[0],
len(hent[1])==0 and "None" or ",".join(hent[1]),
",".join(hent[2]) )
import ftplib
ftp = ftplib.FTP("ftp.host.com")
ftp.login(username, password)
ftp.cwd(directory)
outfile = open(filename, "wb")
ftp.retrbinary("RETR %s" % filename, outfile.write)
outfile.close()
upfile = open(upfilename, "rb")
ftp.storbinary("STOR %s" % upfilename, upfile)
upfile.close()
ftp.quit()
import smtplib
from email.MIMEText import MIMEText
msg = MIMEText(body)
msg['From'] = from_address
msg['To'] = to_address
msg['Subject'] = subject
mailer = smtplib.SMTP()
mailer.connect()
mailer.sendmail(from_address, [to_address], msg.as_string())
import nntplib
server = nntplib.NNTP("news.example.com")
response, count, first, last, name = server.group("misc.test")
headers = server.head(first)
bodytext = server.body(first)
article = server.article(first)
f = file("article.txt")
server.post(f)
response, grouplist = server.list()
for group in grouplist:
name, last, first, flag = group
if flag == 'y':
pass
import poplib
pop = poplib.POP3("mail.example.com")
pop.user(username)
pop.pass_(password)
count, size = pop.stat()
for i in range(1, count+1):
reponse, message, octets = pop.retr(i)
pop.dele(i)
pop.quit()
import telnetlib
tn = telnetlib.Telnet(hostname)
tn.read_until("login: ")
tn.write(user + "\n")
tn.read_until("Password: ")
tn.write(password + "\n")
d = tn.expect([prompt,], 10)
tn.write("ls\n")
files = d[2].split()
print len(files), "files"
tn.write("exit\n")
print tn.read_all()
import cgi
form = cgi.FieldStorage()
value = form.getvalue("PARAM_NAME")
print "Content-Type: text/html"
print
print "<P>You typed: <TT>%s</TT></P>" % (
cgi.escape(value),
)
import cgi
form = cgi.FieldStorage()
who = form.getvalue("Name")
phone = form.getvalue("Number")
picks = form.getvalue("Choices")
picks = form.getlist("Choices")
import cgi
import datetime
time_format = "%a, %d %b %Y %H:%M:%S %Z"
print "Expires: %s" % (
(datetime.datetime.now()
+ datetime.timedelta(+3)).strftime(time_format)
)
print "Date: %s" % (datetime.datetime.now().strftime(time_format))
print "Content-Type: text/plain; charset=ISO-8859-1"
import cgitb
cgitb.enable()
import cgitb
cgitb.enable(display=False)
import cgitb
cgitb.enable(logdir="/var/local/cgi-logs/")
import getpass
print "Content-Type: text/plain\n"
print "Running as %s\n" % getpass.getuser()
LoadModule python_module libexec/mod_python.so
<Directory /some/directory/htdocs/test>
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
</Directory>
from mod_python import apache
def handler(req):
req.write("Hello World!")
return apache.OK
import os
os.system("command %s %s" % (input, " ".join(files)))
import re
cmd = "command %s %s" % (input, " ".join(files))
if re.search(r"[^a-zA-Z0-9._\-]", cmd):
print "rejected"
sys.exit(1)
os.system(cmd)
trans = string.maketrans(string.ascii_letters+string.digits+"-_.",
from nevow import tags as T
print T.ol[T.li['red'], T.li['blue'], T.li['green']]
names = 'Larry Moe Curly'.split()
print T.ul[ [T.li(type="disc")[name] for name in names] ]
print T.li["alpha"]
print T.li['alpha'], T.li['omega']
states = {
"Wisconsin": [ "Superior", "Lake Geneva", "Madison" ],
"Colorado": [ "Denver", "Fort Collins", "Boulder" ],
"Texas": [ "Plano", "Austin", "Fort Stockton" ],
"California": [ "Sebastopol", "Santa Rosa", "Berkeley" ],
}
print "<TABLE> <CAPTION>Cities I Have Known</CAPTION>";
print T.tr[T.th('State'), T.th('Cities')]
for k in sorted(states.keys()):
print T.tr[ [T.th(k)] + [T.td(city) for city in sorted(states[k])] ]
print "</TABLE>";
print T.table[
[T.caption['Cities I have Known'],
T.tr[T.th['State'], T.th['Cities']] ] +
[T.tr[ [T.th(k)] + [T.td(city) for city in sorted(states[k])]]
for k in sorted(states.keys())]]
import MySQLdb
import cgi
form = cgi.FieldStorage()
if 'limit' in form:
limit = int(form['limit'].value)
else:
limit = ''
print '<html><head><title>Salary Query</title></head><body>'
print T.h1['Search']
print '<form>'
print T.p['Enter minimum salary',
T.input(type="text", name="limit", value=limit)]
print T.input(type="submit")
print '</form>'
if limit:
dbconn = MySQLdb.connect(db='somedb', host='server.host.dom',
port=3306, user='username',
passwd='password')
cursor = dbconn.cursor()
cursor.execute("""
SELECT name, salary FROM employees
WHERE salary > %s""", (limit,))
print T.h1["Results"]
print "<TABLE BORDER=1>"
for row in cursor.fetchall():
print T.tr[ [T.td(cell) for cell in row] ]
print "</TABLE>\n";
cursor.close()
dbconn.close()
print '</body></html>'
url = "http://python.org/pypi"
print "Location: %s\n" % url
raise SystemExit
import Cookie
import time
c = Cookie.SimpleCookie()
c['filling'] = 'vanilla cr?me'
now = time.time()
future = now + 3*(60*60*24*30) expire_date = time.strftime('%a %d %b %Y %H:%M:%S GMT', future)
c['filling']['expires'] = expire_date
c['filling']['domain'] = '.python.org'
whither = "http://somewhere.python.org/nonesuch.html"
print 'Status: 302 Moved Temporarily'
print c
print 'Location:', whither
print
import os, re
dir = 'http://www.wins.uva.nl/%7Emes/jargon'
matches = [
(r'Mac', 'm/Macintrash.html'),
(r'Win(dows )?NT', 'e/evilandrude.html'),
(r'Win|MSIE|WebTV', 'm/MicroslothWindows.html'),
(r'Linux', 'l/Linux.html'),
(r'HP-UX', 'h/HP-SUX.html'),
(r'SunOS', 's/ScumOS.html'),
(None, 'a/AppendixB.html'),
]
for regex, page in matches:
if not regex: break
if re.search(regex, os.environ['HTTP_USER_AGENT']):
break
print 'Location: %s/%s\n' % (dir, page)
print 'Status: 204 No response'
print
import SocketServer
def adr_str(adr):
return "%s:%d" % adr
class RequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
print "client access from %s" % adr_str(self.client_address)
print self.request.recv(10000)
self.request.send("Content-Type: text/plain\n"
"Server: dymmyhttpd/1.0.0\n"
"\n...\n")
self.request.close()
adr = ('127.0.0.1', 8001)
print "Please contact me at <http://%s>" % adr_str(adr)
server = SocketServer.TCPServer(adr, RequestHandler)
server.serve_forever()
server.server_close()
import Cookie
cookies = Cookie.SimpleCookie()
cookies["preference-name"] = "whatever you'd like"
print cookies
import cgi
import os
import Cookie
import datetime
cookname = "favorite-ice-cream" fieldname = "flavor"
cookies = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE",""))
if cookies.has_key(cookname):
favorite = cookies[cookname].value
else:
favorite = "mint"
form = cgi.FieldStorage()
if not form.has_key(fieldname):
print "Content-Type: text/html"
print "\n"
print "<html><body>"
print "<h1>Hello Ice Cream</h1>"
print "<form>"
print 'Please select a flavor: <input type="text" name="%s" value="%s" />' % (
fieldname, favorite )
print "</form>"
print "<hr />"
print "</body></html>"
else:
favorite = form[fieldname].value
cookies[cookname] = favorite
expire = datetime.datetime.now() + datetime.timedelta(730)
cookies[cookname]["expires"] = expire.strftime("%a, %d %b %Y %H:00:00 GMT")
cookies[cookname]["path"] = "/"
print "Content-Type: text/html"
print cookies
print "\n"
print "<html><body>"
print "<h1>Hello Ice Cream</h1>"
print "<p>You chose as your favorite flavor \"%s\"</p>" % favorite
print "</body></html>"
import os, cgi, fcntl, cPickle
fh = open('/tmp/formlog', 'ab')
fcntl.flock(fh.fileno(), fcntl.LOCK_EX)
form = cgi.FieldStorage()
cPickle.dump((form, os.environ.copy()) fh)
fh.close()
import cgi, smtplib, sys
form = cgi.FieldStorage()
email = """\
From: %S
To: hisname@hishost.com
Subject: mailed form submission
""" % sys.argv[0]
for key in form:
values = form[key]
if not isinstance(values, list):
value = [values.value]
else:
value = [v.value for v in values]
for item in values:
email += '\n%s: %s' % (key, value)
server = smtplib.SMTP('localhost')
server.sendmail(sys.argv[0], ['hisname@hishost.com'], email)
server.quit()
import fcntl, cPickle
fh = open('/tmp/formlog', 'rb')
fcntl.flock(fh.fileno(), fcntl.LOCK_SH)
count = 0
while True:
try:
form, environ = cPickle.load(fh)
except EOFError:
break
if environ.get('REMOTE_HOST').endswith('perl.com'):
continue
if 'items requested' in form:
count += int(form['items requested'].value)
print 'Total orders:', count
import urllib
content = urllib.urlopen(url).read()
try:
import urllib
content = urllib.urlopen(url).read()
except IOError:
print "could not get %s" % url
import sys, urllib2, HTMLParser
if len(sys.argv)<=1:
print "usage: %s url" % sys.argv[0]
sys.exit(1)
raw_url = sys.argv[1]
class html(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self._data = {}
self._open_tags = []
def handle_starttag(self, tag, attrs):
self._open_tags.append(tag)
def handle_endtag(self, tag):
if len(self._open_tags)>0:
self._open_tags.pop()
def handle_data(self, data):
if len(self._open_tags)>0:
self._data[self._open_tags[-1]] = data
def __getattr__(self,attr):
if not self._data.has_key(attr):
return ""
return self._data[attr]
url = raw_url
print "%s =>\n\t" % url,
try:
response = urllib2.urlopen(url)
except:
print " %s" % sys.exc_info()[1].reason[1]
sys.exit(1)
data = response.read()
parser = html()
parser.feed(data)
parser.close() count = len(data.split("\n"))
bytes = len(data)
print "%s (%d lines, %d bytes)" % (parser.title,
count,
bytes)
import httplib
conn = httplib.HTTPConnection('www.perl.com')
conn.request('GET','/cgi-bin/cpan_mod?module=DB_File&readme=1')
r1 = conn.getresponse()
content = r1.read()
import urllib
params = urllib.urlencode({'module': 'DB_File', 'readme': 1})
content = urllib.urlopen('http://www.perl.com', params).read()
from HTMLParser import HTMLParser
import urllib
from sets import Set as set class myParser(HTMLParser):
def __init__(self, url):
self.baseUrl = url[:url.rfind('/')]
HTMLParser.__init__(self)
def reset(self):
self.urls = set()
HTMLParser.reset(self)
def handle_starttag(self, tag, attrs):
if tag == 'a':
if attrs[0][0] == 'href':
if attrs[0][1].find(':') == -1:
self.urls.add(self.baseUrl + '/' + attrs[0][1])
else:
self.urls.add(attrs[0][1])
url = 'http://www.perl.com/CPAN'
p = myParser(url)
s = urllib.urlopen(url)
data = s.read()
p.feed(data)
urllist = p.urls._data.keys()
urllist.sort()
print '\n'.join(urllist)
import sys
import re
re_quoted = re.compile(r"(?m)^(>.*?)$")
re_url = re.compile(r"<URL:(.*)>")
re_http = re.compile(r"(http:\S+)")
re_strong = re.compile(r"\*(\S+)\*")
re_em = re.compile(r"\b_(\S+)_\b")
for para in open(sys.argv[1]).read().split("\n\n"):
if para.startswith(" "):
print "<pre>\n%s\n</pre>" % para
else:
para = re_quoted.sub(r"\1<br />", para) para = re_url.sub(r'<a href="\1">\1</a>', para) para = re_http.sub(r'<a href="\1">\1</a>', para) para = re_strong.sub(r"<strong>\1</strong>",para) para = re_em.sub(r"<em>\1</em>",para) print "<p>\n%s\n</p>" % para
import sys, re
import htmlentitydefs
def encode_entities(s):
for k,v in htmlentitydefs.codepoint2name.items():
if k<256: s = s.replace(chr(k),"&%s;"%v)
return s
print "<table>"
text = sys.stdin.read()
text = encode_entities(text)
text = re.sub(r"(\n[ \t]+)"," . ",text) text = re.sub(r"(?m)^(\S+?:)\s*(.*?)$",
r'<tr><th align="left">\1</th><td>\2</td></tr>',
text);
print text
print "</table>"
import os
ascii = os.popen("lynx -dump " + filename).read()
import formatter
import htmllib
w = formatter.DumbWriter()
f = formatter.AbstractFormatter(w)
p = htmllib.HTMLParser(f)
p.feed(html)
p.close()
import re
plain_text = re.sub(r"<[^>]*>","",html_text)
import sys, HTMLParser
class html(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self._plaintext = ""
self._ignore = False
def handle_starttag(self, tag, attrs):
if tag == "script":
self._ignore = True
def handle_endtag(self, tag):
if tag == "script":
self._ignore = False
def handle_data(self, data):
if len(data)>0 and not self._ignore:
self._plaintext += data
def get_plaintext(self):
return self._plaintext
def error(self,msg):
pass
html_text = open(sys.argv[1]).read()
parser = html()
parser.feed(html_text)
parser.close() print parser.get_plaintext()
title_s = re.search(r"(?i)<title>\s*(.*?)\s*</title>", text)
title = title_s and title_s.groups()[0] or "NO TITLE"
import sys, urllib2, HTMLParser
if len(sys.argv)<=1:
print "usage: %s url ..." % sys.argv[0]
sys.exit(1)
class html(HTMLParser.HTMLParser):
def __init__(self):
HTMLParser.HTMLParser.__init__(self)
self._data = {}
self._open_tags = []
def handle_starttag(self, tag, attrs):
self._open_tags.append(tag)
def handle_endtag(self, tag):
if len(self._open_tags)>0:
self._open_tags.pop()
def handle_data(self, data):
if len(self._open_tags)>0:
self._data[self._open_tags[-1]] = data
def __getattr__(self,attr):
if not self._data.has_key(attr):
return ""
return self._data[attr]
def error(self,msg):
pass
for url in sys.argv[1:]:
print "%s: " % url,
try:
response = urllib2.urlopen(url)
except:
print " %s" % sys.exc_info()[1]
sys.exit(1)
parser = html()
parser.feed(response.read())
parser.close() print parser.title
import sys
import urllib
def valid(url):
try:
conn = urllib.urlopen(url)
return 1
except:
return 0
from HTMLParser import HTMLParser
from sets import Set as set class myParser(HTMLParser):
def __init__(self, url):
self.baseUrl = url[:url.rfind('/')]
HTMLParser.__init__(self)
def reset(self):
self.urls = set()
HTMLParser.reset(self)
def handle_starttag(self, tag, attrs):
if tag == 'a':
if attrs[0][0] == 'href':
if attrs[0][1].find(':') == -1:
self.urls.add(self.baseUrl + '/' + attrs[0][1])
else:
self.urls.add(attrs[0][1])
if len(sys.argv)<=1:
print "usage: %s <start_url>" % (sys.argv[0])
sys.exit(1)
base_url = sys.argv[1]
print base_url+":"
p = myParser(base_url)
s = urllib.urlopen(base_url)
data = s.read()
p.feed(data)
for link in p.urls._data.keys():
state = "UNKNOWN URL"
if link.startswith("http:"):
state = "BAD"
if valid(link):
state = "OK"
print " %s: %s" % (link, state)
import urllib
import time
import sys
Date = {}
while 1:
ln = sys.stdin.readline()
if not ln:
break
ln = ln.strip()
try:
u = urllib.urlopen(ln)
date = time.mktime(u.info().getdate("date"))
if not Date.has_key(date):
Date[date] = []
Date[date].append(ln)
except:
sys.stderr.write("%s: %s!\n" % (ln, sys.exc_info()[1]))
dates = Date.keys()
dates.sort() for d in dates:
print "%s %s" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(d)),
", ".join(Date[d]))
import re
def template(filename, fillings):
text = open(filename).read()
def repl(matchobj):
if fillings.has_key(matchobj.group(1)):
return str(fillings[matchobj.group(1)])
return ""
text = re.sub("%%(.+?)%%", repl, text)
return text
fields = { "username":"peter", "count":"23", "total": "1234"}
print template("/home/httpd/templates/simple.template", fields)
import MySQLdb
import cgi
import re
import sys
def template(filename, fillings):
text = open(filename).read()
def repl(matchobj):
if fillings.has_key(matchobj.group(1)):
return str(fillings[matchobj.group(1)])
return ""
text = re.sub("%%(.+?)%%", repl, text)
return text
fields = cgi.FieldStorage()
if not fields.has_key("user"):
print "Content-Type: text/plain\n"
print "No username"
sys.exit(1)
def get_userdata(username):
db = MySQLdb.connect(passwd="",db="connections", user="bert")
db.query("select count(duration) as count,"
+" sum(duration) as total from logins"
+" where username='%s'" % username)
res = db.store_result().fetch_row(maxrows=1,how=1)
res[0]["username"] = username
db.close()
return res[0]
print "Content-Type: text/html\n"
print template("report.tpl", get_userdata(fields["user"].value))
LOGFILE = [
'127.0.0.1 - - [04/Sep/2005:20:50:31 +0200] "GET /bus HTTP/1.1" 301 303\n',
'127.0.0.1 - - [04/Sep/2005:20:50:31 +0200] "GET /bus HTTP/1.1" 301 303 "-" "Opera/8.02 (X11; Linux i686; U; en)"\n',
'192.168.0.1 - - [04/Sep/2005:20:50:36 +0200] "GET /bus/libjs/layersmenu-library.js HTTP/1.1" 200 6228\n',
'192.168.0.1 - - [04/Sep/2005:20:50:36 +0200] "GET /bus/libjs/layersmenu-library.js HTTP/1.1" 200 6228 "http://localhost/bus/" "Opera/8.02 (X11; Linux i686; U; en)"\n',
]
import re
web_server_log_re = re.compile(r'^(\S+) (\S+) (\S+) \[([^:]+):(\d+:\d+:\d+) ([^\]]+)\] "(\S+) (.*?) (\S+)" (\S+) (\S+)$')
split_re = re.compile(r'''(?x) # allow nicer formatting (but requires escaping blanks)
^(?P<client>\S+)\s
(?P<identuser>\S+)\s
(?P<authuser>\S+)\s
\[
(?P<date>[^:]+):
(?P<time>[\d:]+)\s
(?P<tz>[^\]]+)
\]\s
"
(?P<method>\S+)\s
(?P<url>.*?)\s
(?P<protocol>\S+)
"\s
(?P<status>\S+)\s
(?P<bytes>\S+)
(?:
\s
"
(?P<referrer>[^"]+)
"\s
"
(?P<agent>[^"]+)
"
)?''')
for line in LOGFILE:
f = split_re.match(line)
if f:
print "agent = %s" % f.groupdict()['agent']