From 04e1ddf7e29af75c9b90da3153c77de6166d0ec7 Mon Sep 17 00:00:00 2001 From: murphy Date: Mon, 20 Apr 2009 21:10:33 +0000 Subject: Added test outputs for Python. --- test/scanners/python/pleac.expected.raydebug | 11119 +++++++++ test/scanners/python/pygments.expected.raydebug | 25595 +++++++++++++++++++++ test/scanners/python/unistring.expected.raydebug | 131 + test/scanners/python/unistring.in.py | 131 + 4 files changed, 36976 insertions(+) create mode 100644 test/scanners/python/pleac.expected.raydebug create mode 100644 test/scanners/python/pygments.expected.raydebug create mode 100644 test/scanners/python/unistring.expected.raydebug create mode 100644 test/scanners/python/unistring.in.py diff --git a/test/scanners/python/pleac.expected.raydebug b/test/scanners/python/pleac.expected.raydebug new file mode 100644 index 0000000..fc88237 --- /dev/null +++ b/test/scanners/python/pleac.expected.raydebug @@ -0,0 +1,11119 @@ +comment(# -*- python -*-) +comment(# vim:set ft=python:) + +comment(# @@PLEAC@@_NAME) +comment(# @@SKIP@@ Python) + +comment(# @@PLEAC@@_WEB) +comment(# @@SKIP@@ http://www.python.org) + +comment(# @@PLEAC@@_INTRO) +comment(# @@SKIP@@ The latest version of Python is 2.4 but users of 2.3 and 2.2 (and) +comment(# @@SKIP@@ in some cases earlier versions\) can use the code herein.) +comment(# @@SKIP@@ Users of 2.2 and 2.3 should install or copy code from utils.py ) +comment(# @@SKIP@@ (http://aima.cs.berkeley.edu/python/utils.py\)) +comment(# @@SKIP@@ [the first section provides compatability code with 2.4]) +comment(# @@SKIP@@ Users of 2.2 should install optik (http://optik.sourceforge.com\) ) +comment(# @@SKIP@@ [for optparse and textwrap]) +comment(# @@SKIP@@ Where a 2.3 or 2.4 feature is unable to be replicated, an effort) +comment(# @@SKIP@@ has been made to provide a backward-compatible version in addition) +comment(# @@SKIP@@ to one using modern idioms.) +comment(# @@SKIP@@ Examples which translate the original Perl closely but which are) +comment(# @@SKIP@@ unPythonic are prefixed with a comment stating "DON'T DO THIS".) +comment(# @@SKIP@@ In some cases, it may be useful to know the techniques in these, ) +comment(# @@SKIP@@ though it's a bad solution for the specific problem.) + +comment(# @@PLEAC@@_1.0) +comment(#-----------------------------) +ident(mystr) operator(=) string comment(# a newline character) +ident(mystr) operator(=) string comment(# two characters, \\ and n) +comment(#-----------------------------) +ident(mystr) operator(=) string comment(# literal single quote inside double quotes) +ident(mystr) operator(=) string comment(# literal double quote inside single quotes) +comment(#-----------------------------) +ident(mystr) operator(=) string comment(# escaped single quote) +ident(mystr) operator(=) string comment(# escaped double quote) +comment(#-----------------------------) +ident(mystr) operator(=) string +ident(mystr) operator(=) string +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.1) +comment(#-----------------------------) + +comment(# get a 5-char string, skip 3, then grab 2 8-char strings, then the rest) +comment(# Note that struct.unpack cannot use * for an unknown length.) +comment(# See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65224) +keyword(import) ident(struct) +operator(()ident(lead)operator(,) ident(s1)operator(,) ident(s2)operator(\))operator(,) ident(tail) operator(=) ident(struct)operator(.)ident(unpack)operator(()stringoperator(,) ident(data)operator([)operator(:)integer(24)operator(])operator(\))operator(,) ident(data)operator([)integer(24)operator(:)operator(]) + +comment(# split at five-char boundaries) +ident(fivers) operator(=) ident(struct)operator(.)ident(unpack)operator(()string operator(*) operator(()predefined(len)operator(()ident(data)operator(\))operator(//)integer(5)operator(\))operator(,) ident(data)operator(\)) +ident(fivers) operator(=) keyword(print) operator([)ident(x)operator([)ident(i)operator(*)integer(5)operator(:)ident(i)operator(*)integer(5)operator(+)integer(5)operator(]) keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(x)operator(\))operator(/)integer(5)operator(\))operator(]) + +comment(# chop string into individual characters) +ident(chars) operator(=) predefined(list)operator(()ident(data)operator(\)) +comment(#-----------------------------) +ident(mystr) operator(=) string +comment(# +012345678901234567890 Indexing forwards (left to right\)) +comment(# 109876543210987654321- Indexing backwards (right to left\)) +comment(# note that 0 means 10 or 20, etc. above) + +ident(first) operator(=) ident(mystr)operator([)integer(0)operator(]) comment(# "T") +ident(start) operator(=) ident(mystr)operator([)integer(5)operator(:)integer(7)operator(]) comment(# "is") +ident(rest) operator(=) ident(mystr)operator([)integer(13)operator(:)operator(]) comment(# "you have") +ident(last) operator(=) ident(mystr)operator([)operator(-)integer(1)operator(]) comment(# "e") +ident(end) operator(=) ident(mystr)operator([)operator(-)integer(4)operator(:)operator(]) comment(# "have") +ident(piece) operator(=) ident(mystr)operator([)operator(-)integer(8)operator(:)operator(-)integer(5)operator(]) comment(# "you") +comment(#-----------------------------) +comment(# Python strings are immutable.) +comment(# In general, you should just do piecemeal reallocation:) +ident(mystr) operator(=) string +ident(mystr) operator(=) ident(mystr)operator([)operator(:)integer(5)operator(]) operator(+) string operator(+) ident(mystr)operator([)integer(7)operator(:)operator(]) + +comment(# Or replace and reallocate) +ident(mystr) operator(=) string +ident(mystr) operator(=) ident(mystr)operator(.)ident(replace)operator(()stringoperator(,) stringoperator(\)) + +comment(# DON'T DO THIS: In-place modification could be done using character arrays) +keyword(import) ident(array) +ident(mystr) operator(=) ident(array)operator(.)ident(array)operator(()stringoperator(,) stringoperator(\)) +ident(mystr)operator([)integer(5)operator(:)integer(7)operator(]) operator(=) ident(array)operator(.)ident(array)operator(()stringoperator(,) stringoperator(\)) +comment(# mystr is now array('c', "This wasn't what you have"\)) + +comment(# DON'T DO THIS: It could also be done using MutableString ) +keyword(from) ident(UserString) keyword(import) ident(MutableString) +ident(mystr) operator(=) ident(MutableString)operator(()stringoperator(\)) +ident(mystr)operator([)operator(-)integer(12)operator(:)operator(]) operator(=) string +comment(# mystr is now "This is wondrous") +comment(#-----------------------------) +comment(# you can test simple substrings with "in" (for regex matching see ch.6\):) +keyword(if) ident(txt) keyword(in) ident(mystr)operator([)operator(-)integer(10)operator(:)operator(])operator(:) + keyword(print) stringoperator(%)ident(txt) + +comment(# Or use the startswith(\) and endswith(\) string methods:) +keyword(if) ident(mystr)operator(.)ident(startswith)operator(()ident(txt)operator(\))operator(:) + keyword(print) stringoperator(%)operator(()ident(mystr)operator(,) ident(txt)operator(\)) +keyword(if) ident(mystr)operator(.)ident(endswith)operator(()ident(txt)operator(\))operator(:) + keyword(print) stringoperator(%)operator(()ident(mystr)operator(,) ident(txt)operator(\)) + +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.2) +comment(#-----------------------------) +comment(# Introductory Note: quite a bit of this section is not terribly Pythonic) +comment(# as names must be set before being used. For instance, unless myvar has ) +comment(# been previously defined, these next lines will all raise NameError:) +ident(myvar) operator(=) ident(myvar) keyword(or) ident(some_default) +ident(myvar2) operator(=) ident(myvar) keyword(or) ident(some_default) +ident(myvar) operator(|=) ident(some_default) comment(# bitwise-or, not logical-or - for demo) + +comment(# The standard way of setting a default is often:) +ident(myvar) operator(=) ident(default_value) +keyword(if) ident(some_condition)operator(:) + keyword(pass) comment(# code which may set myvar to something else) + +comment(# if myvar is returned from a function and may be empty/None, then use:) +ident(myvar) operator(=) ident(somefunc)operator(()operator(\)) +keyword(if) keyword(not) ident(myvar)operator(:) + ident(myvar) operator(=) ident(default_value) + +comment(# If you want a default value that can be overridden by the person calling ) +comment(# your code, you can often wrap it in a function with a named parameter:) +keyword(def) ident(myfunc)operator(()ident(myvar)operator(=)stringoperator(\))operator(:) + keyword(return) ident(myvar) operator(+) string +keyword(print) ident(myfunc)operator(()operator(\))operator(,) ident(myfunc)operator(()stringoperator(\)) +comment(#=> ab cb) + +comment(# Note, though, that this won't work for mutable objects such as lists or) +comment(# dicts that are mutated in the function as the object is only created once ) +comment(# and repeated calls to the same function will return the same object. This) +comment(# can be desired behaviour however - see section 10.3, for instance.) +keyword(def) ident(myfunc)operator(()ident(myvar)operator(=)operator([)operator(])operator(\))operator(:) + ident(myvar)operator(.)ident(append)operator(()stringoperator(\)) + keyword(return) ident(myvar) +keyword(print) ident(myfunc)operator(()operator(\))operator(,) ident(myfunc)operator(()operator(\)) +comment(#=> ['x'] ['x', 'x']) + +comment(# You need to do:) +keyword(def) ident(myfunc)operator(()ident(myvar)operator(=)pre_constant(None)operator(\))operator(:) + keyword(if) ident(myvar) keyword(is) pre_constant(None)operator(:) + ident(myvar) operator(=) operator([)operator(]) + ident(myvar)operator(.)ident(append)operator(()stringoperator(\)) + keyword(return) ident(myvar) +keyword(print) ident(myfunc)operator(()operator(\))operator(,) ident(myfunc)operator(()operator(\)) +comment(#=> ['x'] ['x']) + +comment(#=== Perl Equivalencies start here) +comment(# use b if b is true, otherwise use c) +ident(a) operator(=) ident(b) keyword(or) ident(c) + +comment(# as that is a little tricksy, the following may be preferred:) +keyword(if) ident(b)operator(:) + ident(a) operator(=) ident(b) +keyword(else)operator(:) + ident(a) operator(=) ident(c) + +comment(# set x to y unless x is already true) +keyword(if) keyword(not) ident(x)operator(:) + ident(x) operator(=) ident(y) +comment(#-----------------------------) +comment(# use b if b is defined, else c) +keyword(try)operator(:) + ident(a) operator(=) ident(b) +keyword(except) exception(NameError)operator(:) + ident(a) operator(=) ident(c) +comment(#-----------------------------) +ident(foo) operator(=) ident(bar) keyword(or) string +comment(#-----------------------------) +comment(# To get a user (for both UNIX and Windows\), use:) +keyword(import) ident(getpass) +ident(user) operator(=) ident(getpass)operator(.)ident(getuser)operator(()operator(\)) + +comment(# DON'T DO THIS: find the user name on Unix systems ) +keyword(import) ident(os) +ident(user) operator(=) ident(os)operator(.)ident(environ)operator(.)ident(get)operator(()stringoperator(\)) +keyword(if) ident(user) keyword(is) pre_constant(None)operator(:) + ident(user) operator(=) ident(os)operator(.)ident(environ)operator(.)ident(get)operator(()stringoperator(\)) +comment(#-----------------------------) +keyword(if) keyword(not) ident(starting_point)operator(:) + ident(starting_point) operator(=) string +comment(#-----------------------------) +keyword(if) keyword(not) ident(a)operator(:) comment(# copy only if empty) + ident(a) operator(=) ident(b) + +keyword(if) ident(b)operator(:) comment(# assign b if nonempty, else c) + ident(a) operator(=) ident(b) +keyword(else)operator(:) + ident(a) operator(=) ident(c) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.3) +comment(#-----------------------------) +ident(v1)operator(,) ident(v2) operator(=) ident(v2)operator(,) ident(v1) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(temp) operator(=) ident(a) +ident(a) operator(=) ident(b) +ident(b) operator(=) ident(temp) +comment(#-----------------------------) +ident(a) operator(=) string +ident(b) operator(=) string +ident(a)operator(,) ident(b) operator(=) ident(b)operator(,) ident(a) comment(# the first shall be last -- and versa vice ) +comment(#-----------------------------) +ident(alpha)operator(,) ident(beta)operator(,) ident(production) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +ident(alpha)operator(,) ident(beta)operator(,) ident(production) operator(=) ident(beta)operator(,) ident(production)operator(,) ident(alpha) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.4) +comment(#-----------------------------) +ident(num) operator(=) predefined(ord)operator(()ident(char)operator(\)) +ident(char) operator(=) predefined(chr)operator(()ident(num)operator(\)) +comment(#-----------------------------) +ident(char) operator(=) string operator(%) ident(num) +keyword(print) string operator(%) operator(()ident(num)operator(,) ident(num)operator(\)) +keyword(print) string operator(%) operator({)stringoperator(:) ident(num)operator(}) +keyword(print) string operator(%) predefined(locals)operator(()operator(\)) +comment(#=> Number 101 is character e) +comment(#-----------------------------) +ident(ascii_character_numbers) operator(=) operator([)predefined(ord)operator(()ident(c)operator(\)) keyword(for) ident(c) keyword(in) stringoperator(]) +keyword(print) ident(ascii_character_numbers) +comment(#=> [115, 97, 109, 112, 108, 101]) + +ident(word) operator(=) stringoperator(.)ident(join)operator(()operator([)predefined(chr)operator(()ident(n)operator(\)) keyword(for) ident(n) keyword(in) ident(ascii_character_numbers)operator(])operator(\)) +ident(word) operator(=) stringoperator(.)ident(join)operator(()operator([)predefined(chr)operator(()ident(n)operator(\)) keyword(for) ident(n) keyword(in) operator([)integer(115)operator(,) integer(97)operator(,) integer(109)operator(,) integer(112)operator(,) integer(108)operator(,) integer(101)operator(])operator(])operator(\)) +keyword(print) ident(word) +comment(#=> sample) +comment(#-----------------------------) +ident(hal) operator(=) string +ident(ibm) operator(=) stringoperator(.)ident(join)operator(()operator([)predefined(chr)operator(()predefined(ord)operator(()ident(c)operator(\))operator(+)integer(1)operator(\)) keyword(for) ident(c) keyword(in) ident(hal)operator(])operator(\)) comment(# add one to each ASCII value) +keyword(print) ident(ibm) +comment(#=> IBM) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.5) +comment(#-----------------------------) +ident(mylist) operator(=) predefined(list)operator(()ident(mystr)operator(\)) +comment(#-----------------------------) +keyword(for) ident(char) keyword(in) ident(mystr)operator(:) + keyword(pass) comment(# do something with char) +comment(#-----------------------------) +ident(mystr) operator(=) string +ident(uniq) operator(=) predefined(sorted)operator(()predefined(set)operator(()ident(mystr)operator(\))operator(\)) +keyword(print) string operator(%) stringoperator(.)ident(join)operator(()ident(uniq)operator(\)) +comment(#=> unique chars are: ' adelnpy') +comment(#-----------------------------) +ident(ascvals) operator(=) operator([)predefined(ord)operator(()ident(c)operator(\)) keyword(for) ident(c) keyword(in) ident(mystr)operator(]) +keyword(print) stringoperator(%)operator(()predefined(sum)operator(()ident(ascvals)operator(\))operator(,) ident(mystr)operator(\)) +comment(#=> total is 1248 for 'an apple a day'.) +comment(#-----------------------------) +comment(# sysv checksum) +keyword(def) ident(checksum)operator(()ident(myfile)operator(\))operator(:) + ident(values) operator(=) operator([)predefined(ord)operator(()ident(c)operator(\)) keyword(for) ident(line) keyword(in) ident(myfile) keyword(for) ident(c) keyword(in) ident(line)operator(]) + keyword(return) predefined(sum)operator(()ident(values)operator(\))operator(%)operator(()integer(2)operator(**)integer(16)operator(\)) operator(-) integer(1) + +keyword(import) ident(fileinput) +keyword(print) ident(checksum)operator(()ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(\)) comment(# data from sys.stdin) + +comment(# Using a function means any iterable can be checksummed:) +keyword(print) ident(checksum)operator(()predefined(open)operator(()stringoperator(\)) comment(# data from file) +keyword(print) ident(checksum)operator(()stringoperator(\)) comment(# data from string) +comment(#-----------------------------) +comment(#!/usr/bin/python) +comment(# slowcat - emulate a s l o w line printer) +comment(# usage: slowcat [- DELAY] [files ...]) +keyword(import) ident(sys)operator(,) ident(select) +keyword(import) ident(re) +ident(DELAY) operator(=) integer(1) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,)ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(:) + ident(DELAY)operator(=)operator(-)predefined(int)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\)) + keyword(del) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +keyword(for) ident(ln) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(for) ident(c) keyword(in) ident(ln)operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(c)operator(\)) + ident(sys)operator(.)ident(stdout)operator(.)ident(flush)operator(()operator(\)) + ident(select)operator(.)ident(select)operator(()operator([)operator(])operator(,)operator([)operator(])operator(,)operator([)operator(])operator(,) float(0)float(.005) operator(*) ident(DELAY)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.6) +comment(#-----------------------------) +comment(# 2.3+ only) +ident(revchars) operator(=) ident(mystr)operator([)operator(:)operator(:)operator(-)integer(1)operator(]) comment(# extended slice - step is -1) +ident(revwords) operator(=) stringoperator(.)ident(join)operator(()ident(mystr)operator(.)ident(split)operator(()stringoperator(\))operator([)operator(:)operator(:)operator(-)integer(1)operator(])operator(\)) + +comment(# pre 2.3 version:) +ident(mylist) operator(=) predefined(list)operator(()ident(mystr)operator(\)) +ident(mylist)operator(.)ident(reverse)operator(()operator(\)) +ident(revbytes) operator(=) stringoperator(.)ident(join)operator(()ident(mylist)operator(\)) + +ident(mylist) operator(=) ident(mystr)operator(.)ident(split)operator(()operator(\)) +ident(mylist)operator(.)ident(reverse)operator(()operator(\)) +ident(revwords) operator(=) stringoperator(.)ident(join)operator(()ident(mylist)operator(\)) + +comment(# Alternative version using reversed(\):) +ident(revchars) operator(=) stringoperator(.)ident(join)operator(()predefined(reversed)operator(()ident(mystr)operator(\))operator(\)) +ident(revwords) operator(=) stringoperator(.)ident(join)operator(()predefined(reversed)operator(()ident(mystr)operator(.)ident(split)operator(()stringoperator(\))operator(\))operator(\)) + +comment(# reversed(\) makes an iterator, which means that the reversal) +comment(# happens as it is consumed. This means that "print reversed(mystr\)" is not) +comment(# the same as mystr[::-1]. Standard usage is:) +keyword(for) ident(char) keyword(in) predefined(reversed)operator(()ident(mystr)operator(\))operator(:) + keyword(pass) comment(# ... do something) +comment(#-----------------------------) +comment(# 2.3+ only) +ident(word) operator(=) string +ident(is_palindrome) operator(=) operator(()ident(word) operator(==) ident(word)operator([)operator(:)operator(:)operator(-)integer(1)operator(])operator(\)) +comment(#-----------------------------) +comment(# Generator version) +keyword(def) ident(get_palindromes)operator(()ident(fname)operator(\))operator(:) + keyword(for) ident(line) keyword(in) predefined(open)operator(()ident(fname)operator(\))operator(:) + ident(word) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + keyword(if) predefined(len)operator(()ident(word)operator(\)) operator(>) integer(5) keyword(and) ident(word) operator(==) ident(word)operator([)operator(:)operator(:)operator(-)integer(1)operator(])operator(:) + keyword(yield) ident(word) +ident(long_palindromes) operator(=) predefined(list)operator(()ident(get_palindromes)operator(()stringoperator(\))operator(\)) + +comment(# Simpler old-style version using 2.2 string reversal) +keyword(def) ident(rev_string)operator(()ident(mystr)operator(\))operator(:) + ident(mylist) operator(=) predefined(list)operator(()ident(mystr)operator(\)) + ident(mylist)operator(.)ident(reverse)operator(()operator(\)) + keyword(return) stringoperator(.)ident(join)operator(()ident(mylist)operator(\)) + +ident(long_palindromes)operator(=)operator([)operator(]) +keyword(for) ident(line) keyword(in) predefined(open)operator(()stringoperator(\))operator(:) + ident(word) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + keyword(if) predefined(len)operator(()ident(word)operator(\)) operator(>) integer(5) keyword(and) ident(word) operator(==) ident(rev_string)operator(()ident(word)operator(\))operator(:) + ident(long_palindromes)operator(.)ident(append)operator(()ident(word)operator(\)) +keyword(print) ident(long_palindromes) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.7) +comment(#-----------------------------) +ident(mystr)operator(.)ident(expandtabs)operator(()operator(\)) +ident(mystr)operator(.)ident(expandtabs)operator(()integer(4)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.8) +comment(#-----------------------------) +ident(text) operator(=) stringoperator(%)operator({)stringoperator(:)integer(24)operator(,) stringoperator(:)integer(80)operator(\)) +keyword(print) ident(text) +comment(#=> I am 24 high and 80 long) + +ident(rows)operator(,) ident(cols) operator(=) integer(24)operator(,) integer(80) +ident(text) operator(=) stringoperator(%)predefined(locals)operator(()operator(\)) +keyword(print) ident(text) +comment(#=> I am 24 high and 80 long) +comment(#-----------------------------) +keyword(import) ident(re) +keyword(print) ident(re)operator(.)ident(sub)operator(()stringoperator(,) keyword(lambda) ident(i)operator(:) predefined(str)operator(()integer(2) operator(*) predefined(int)operator(()ident(i)operator(.)ident(group)operator(()integer(0)operator(\))operator(\))operator(\))operator(,) stringoperator(\)) +comment(#=> I am 34 years old) +comment(#-----------------------------) +comment(# expand variables in text, but put an error message in) +comment(# if the variable isn't defined) +keyword(class) ident(SafeDict)operator(()predefined(dict)operator(\))operator(:) + keyword(def) ident(__getitem__)operator(()pre_constant(self)operator(,) ident(key)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(get)operator(()ident(key)operator(,) stringoperator(%)ident(key)operator(\)) + +ident(hi) operator(=) string +ident(text) operator(=) stringoperator(%)ident(SafeDict)operator(()predefined(locals)operator(()operator(\))operator(\)) +keyword(print) ident(text) +comment(#=> Hello and [No Variable: bye]!) + +comment(#If you don't need a particular error message, just use the Template class:) +keyword(from) ident(string) keyword(import) ident(Template) +ident(x) operator(=) ident(Template)operator(()stringoperator(\)) +ident(hi) operator(=) string +keyword(print) ident(x)operator(.)ident(safe_substitute)operator(()predefined(locals)operator(()operator(\))operator(\)) +comment(#=> Hello and $bye!) +keyword(print) ident(x)operator(.)ident(substitute)operator(()predefined(locals)operator(()operator(\))operator(\)) comment(# will throw a KeyError) + +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.9) +comment(#-----------------------------) +ident(mystr) operator(=) stringoperator(.)ident(upper)operator(()operator(\)) comment(# BO PEEP) +ident(mystr) operator(=) ident(mystr)operator(.)ident(lower)operator(()operator(\)) comment(# bo peep) +ident(mystr) operator(=) ident(mystr)operator(.)ident(capitalize)operator(()operator(\)) comment(# Bo peep) +comment(#-----------------------------) +ident(beast) operator(=) string +ident(caprest) operator(=) ident(beast)operator(.)ident(capitalize)operator(()operator(\))operator(.)ident(swapcase)operator(()operator(\)) comment(# pYTHON) +comment(#-----------------------------) +keyword(print) stringoperator(.)ident(title)operator(()operator(\)) +comment(#=> This Is A Long Line) +comment(#-----------------------------) +keyword(if) ident(a)operator(.)ident(upper)operator(()operator(\)) operator(==) ident(b)operator(.)ident(upper)operator(()operator(\))operator(:) + keyword(print) string +comment(#-----------------------------) +keyword(import) ident(random) +keyword(def) ident(randcase_one)operator(()ident(letter)operator(\))operator(:) + keyword(if) ident(random)operator(.)ident(randint)operator(()integer(0)operator(,)integer(5)operator(\))operator(:) comment(# True on 1, 2, 3, 4) + keyword(return) ident(letter)operator(.)ident(lower)operator(()operator(\)) + keyword(else)operator(:) + keyword(return) ident(letter)operator(.)ident(upper)operator(()operator(\)) + +keyword(def) ident(randcase)operator(()ident(myfile)operator(\))operator(:) + keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(yield) stringoperator(.)ident(join)operator(()ident(randcase_one)operator(()ident(letter)operator(\)) keyword(for) ident(letter) keyword(in) ident(line)operator([)operator(:)operator(-)integer(1)operator(])operator(\)) + +keyword(for) ident(line) keyword(in) ident(randcase)operator(()ident(myfile)operator(\))operator(:) + keyword(print) ident(line) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.10) +comment(#-----------------------------) +string operator(%) operator(()ident(n) operator(+) integer(1)operator(\)) +keyword(print) stringoperator(,) ident(n)operator(+)integer(1)operator(,) string +comment(#-----------------------------) +comment(#Python templates disallow in-string calculations (see PEP 292\)) +keyword(from) ident(string) keyword(import) ident(Template) + +ident(email_template) operator(=) ident(Template)operator(()stringoperator(\)) + +keyword(import) ident(random) +keyword(import) ident(datetime) + +ident(person) operator(=) operator({)stringoperator(:)stringoperator(,) + stringoperator(:) stringoperator(,) + string operator(:) integer(1234567890)operator(,) + string operator(:) integer(500)operator(+)ident(random)operator(.)ident(randint)operator(()integer(0)operator(,)integer(99)operator(\))operator(}) + +keyword(print) ident(email_template)operator(.)ident(substitute)operator(()ident(person)operator(,) ident(date)operator(=)ident(datetime)operator(.)ident(date)operator(.)ident(today)operator(()operator(\))operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.11) +comment(#-----------------------------) +comment(# indenting here documents) +comment(#) +comment(# in python multiline strings can be used as here documents) +ident(var) operator(=) string + +comment(# using regular expressions) +keyword(import) ident(re) +ident(re_leading_blanks) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(,)ident(re)operator(.)ident(MULTILINE)operator(\)) +ident(var1) operator(=) ident(re_leading_blanks)operator(.)ident(sub)operator(()stringoperator(,)ident(var)operator(\))operator([)operator(:)operator(-)integer(1)operator(]) + +comment(# using string methods ) +comment(# split into lines, use every line except first and last, left strip and rejoin.) +ident(var2) operator(=) stringoperator(.)ident(join)operator(()operator([)ident(line)operator(.)ident(lstrip)operator(()operator(\)) keyword(for) ident(line) keyword(in) ident(var)operator(.)ident(split)operator(()stringoperator(\))operator([)integer(1)operator(:)operator(-)integer(1)operator(])operator(])operator(\)) + +ident(poem) operator(=) string + +keyword(import) ident(textwrap) +keyword(print) ident(textwrap)operator(.)ident(dedent)operator(()ident(poem)operator(\))operator([)integer(1)operator(:)operator(-)integer(1)operator(]) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_1.12) +comment(#-----------------------------) +keyword(from) ident(textwrap) keyword(import) ident(wrap) +ident(output) operator(=) ident(wrap)operator(()ident(para)operator(,) + ident(initial_indent)operator(=)ident(leadtab) + ident(subsequent_indent)operator(=)ident(nexttab)operator(\)) +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# wrapdemo - show how textwrap works) + +ident(txt) operator(=) string + +keyword(from) ident(textwrap) keyword(import) ident(TextWrapper) + +ident(wrapper) operator(=) ident(TextWrapper)operator(()ident(width)operator(=)integer(20)operator(,) + ident(initial_indent)operator(=)stringoperator(*)integer(4)operator(,) + ident(subsequent_indent)operator(=)stringoperator(*)integer(2)operator(\)) + +keyword(print) string operator(*) integer(2) +keyword(print) ident(wrapper)operator(.)ident(fill)operator(()ident(txt)operator(\)) + +comment(#-----------------------------) +string + +comment(#-----------------------------) +comment(# merge multiple lines into one, then wrap one long line) + +keyword(from) ident(textwrap) keyword(import) ident(fill) +keyword(import) ident(fileinput) + +keyword(print) ident(fill)operator(()stringoperator(.)ident(join)operator(()ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(\))operator(\)) + +comment(#-----------------------------) +comment(# Term::ReadKey::GetTerminalSize(\) isn't in the Perl standard library. ) +comment(# It isn't in the Python standard library either. Michael Hudson's ) +comment(# recipe from python-list #530228 is shown here.) +comment(# (http://aspn.activestate.com/ASPN/Mail/Message/python-list/530228\)) +comment(# Be aware that this will work on Unix but not on Windows.) + +keyword(from) ident(termwrap) keyword(import) ident(wrap) +keyword(import) ident(struct)operator(,) ident(fcntl) +keyword(def) ident(getheightwidth)operator(()operator(\))operator(:) + ident(height)operator(,) ident(width) operator(=) ident(struct)operator(.)ident(unpack)operator(() + stringoperator(,) ident(fcntl)operator(.)ident(ioctl)operator(()integer(0)operator(,) ident(TERMIOS)operator(.)ident(TIOCGWINSZ) operator(,)stringoperator(*)integer(8)operator(\))operator(\))operator([)integer(0)operator(:)integer(2)operator(]) + keyword(return) ident(height)operator(,) ident(width) + +comment(# PERL <>, $/, $\\ emulation) +keyword(import) ident(fileinput) +keyword(import) ident(re) + +ident(_)operator(,) ident(width) operator(=) ident(getheightwidth)operator(()operator(\)) +keyword(for) ident(para) keyword(in) ident(re)operator(.)ident(split)operator(()stringoperator(,) stringoperator(.)ident(join)operator(()ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(\))operator(\))operator(:) + keyword(print) ident(fill)operator(()ident(para)operator(,) ident(width)operator(\)) + + +comment(# @@PLEAC@@_1.13) +comment(#-----------------------------) +ident(mystr) operator(=) string comment(#") +ident(re)operator(.)ident(sub)operator(()stringoperator(,) keyword(lambda) ident(i)operator(:) string operator(+) ident(i)operator(.)ident(group)operator(()integer(0)operator(\))operator(,) ident(mystr)operator(\)) +ident(re)operator(.)ident(sub)operator(()stringoperator(,) keyword(lambda) ident(i)operator(:) string operator(+) ident(i)operator(.)ident(group)operator(()integer(0)operator(\))operator(,) ident(mystr)operator(\)) +ident(re)operator(.)ident(sub)operator(()stringoperator(,) keyword(lambda) ident(i)operator(:) string operator(+) ident(i)operator(.)ident(group)operator(()integer(0)operator(\))operator(,) stringoperator(\)) comment(# no function like quotemeta?) + + +comment(# @@PLEAC@@_1.14) +comment(#-----------------------------) +ident(mystr) operator(=) ident(mystr)operator(.)ident(lstrip)operator(()operator(\)) comment(# left) +ident(mystr) operator(=) ident(mystr)operator(.)ident(rstrip)operator(()operator(\)) comment(# right) +ident(mystr) operator(=) ident(mystr)operator(.)ident(strip)operator(()operator(\)) comment(# both ends) + + +comment(# @@PLEAC@@_1.15) +comment(#-----------------------------) +keyword(import) ident(csv) +keyword(def) ident(parse_csv)operator(()ident(line)operator(\))operator(:) + ident(reader) operator(=) ident(csv)operator(.)ident(reader)operator(()operator([)ident(line)operator(])operator(,) ident(escapechar)operator(=)stringoperator(\)) + keyword(return) ident(reader)operator(.)ident(next)operator(()operator(\)) + +ident(line) operator(=) string comment(#") + +ident(fields) operator(=) ident(parse_csv)operator(()ident(line)operator(\)) + +keyword(for) ident(i)operator(,) ident(field) keyword(in) predefined(enumerate)operator(()ident(fields)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(i)operator(,) ident(field)operator(\)) + +comment(# pre-2.3 version of parse_csv) +keyword(import) ident(re) +keyword(def) ident(parse_csv)operator(()ident(text)operator(\))operator(:) + ident(pattern) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + ident(mylist) operator(=) operator([)stringoperator(.)ident(join)operator(()ident(elem)operator(\)) + keyword(for) ident(elem) keyword(in) ident(re)operator(.)ident(findall)operator(()ident(pattern)operator(,) ident(text)operator(\))operator(]) + keyword(if) ident(text)operator([)operator(-)integer(1)operator(]) operator(==) stringoperator(:) + ident(mylist) operator(+=) operator([)stringoperator(]) + keyword(return) ident(mylist) + +comment(# cvs.reader is meant to work for many lines, something like:) +comment(# (NB: in Python default, quotechar is *not* escaped by backslash,) +comment(# but doubled instead. That's what Excel does.\)) +keyword(for) ident(fields) keyword(in) ident(cvs)operator(.)ident(reader)operator(()ident(lines)operator(,) ident(dialect)operator(=)stringoperator(\))operator(:) + keyword(for) ident(num)operator(,) ident(field) keyword(in) predefined(enumerate)operator(()ident(fields)operator(\))operator(:) + keyword(print) ident(num)operator(,) stringoperator(,) ident(field) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.16) +comment(#-----------------------------) +keyword(def) ident(soundex)operator(()ident(name)operator(,) predefined(len)operator(=)integer(4)operator(\))operator(:) + string + + comment(# digits holds the soundex values for the alphabet) + ident(digits) operator(=) string + ident(sndx) operator(=) string + ident(fc) operator(=) string + + comment(# translate alpha chars in name to soundex digits) + keyword(for) ident(c) keyword(in) ident(name)operator(.)ident(upper)operator(()operator(\))operator(:) + keyword(if) ident(c)operator(.)ident(isalpha)operator(()operator(\))operator(:) + keyword(if) keyword(not) ident(fc)operator(:) + ident(fc) operator(=) ident(c) comment(# remember first letter) + ident(d) operator(=) ident(digits)operator([)predefined(ord)operator(()ident(c)operator(\))operator(-)predefined(ord)operator(()stringoperator(\))operator(]) + comment(# duplicate consecutive soundex digits are skipped) + keyword(if) keyword(not) ident(sndx) keyword(or) operator(()ident(d) operator(!=) ident(sndx)operator([)operator(-)integer(1)operator(])operator(\))operator(:) + ident(sndx) operator(+=) ident(d) + + comment(# replace first digit with first alpha character) + ident(sndx) operator(=) ident(fc) operator(+) ident(sndx)operator([)integer(1)operator(:)operator(]) + + comment(# remove all 0s from the soundex code) + ident(sndx) operator(=) ident(sndx)operator(.)ident(replace)operator(()stringoperator(,)stringoperator(\)) + + comment(# return soundex code padded to len characters) + keyword(return) operator(()ident(sndx) operator(+) operator(()predefined(len) operator(*) stringoperator(\))operator(\))operator([)operator(:)predefined(len)operator(]) + +ident(user) operator(=) predefined(raw_input)operator(()stringoperator(\)) +keyword(if) ident(user) operator(==) stringoperator(:) + keyword(raise) exception(SystemExit) + +ident(name_code) operator(=) ident(soundex)operator(()ident(user)operator(\)) +keyword(for) ident(line) keyword(in) predefined(open)operator(()stringoperator(\))operator(:) + ident(line) operator(=) ident(line)operator(.)ident(split)operator(()stringoperator(\)) + keyword(for) ident(piece) keyword(in) ident(line)operator([)integer(4)operator(])operator(.)ident(split)operator(()operator(\))operator(:) + keyword(if) ident(name_code) operator(==) ident(soundex)operator(()ident(piece)operator(\))operator(:) + keyword(print) string operator(%) ident(line)operator([)integer(0)operator(])operator(,) ident(line)operator([)integer(4)operator(])operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.17) +comment(#-----------------------------) +keyword(import) ident(sys)operator(,) ident(fileinput)operator(,) ident(re) + +ident(data) operator(=) string analyzed)content( +)content(built-in => builtin)content( +)content(chastized => chastised)content( +)content(commandline => command-line)content( +)content(de-allocate => deallocate)content( +)content(dropin => drop-in)content( +)content(hardcode => hard-code)content( +)content(meta-data => metadata)content( +)content(multicharacter => multi-character)content( +)content(multiway => multi-way)content( +)content(non-empty => nonempty)content( +)content(non-profit => nonprofit)content( +)content(non-trappable => nontrappable)content( +)content(pre-define => predefine)content( +)content(preextend => pre-extend)content( +)content(re-compiling => recompiling)content( +)content(reenter => re-enter)content( +)content(turnkey => turn-key)content( +)delimiter(""")> +ident(mydict) operator(=) operator({)operator(}) +keyword(for) ident(line) keyword(in) ident(data)operator(.)ident(split)operator(()stringoperator(\))operator(:) + keyword(if) keyword(not) ident(line)operator(.)ident(strip)operator(()operator(\))operator(:) + keyword(continue) + ident(k)operator(,) ident(v) operator(=) operator([)ident(word)operator(.)ident(strip)operator(()operator(\)) keyword(for) ident(word) keyword(in) ident(line)operator(.)ident(split)operator(()string)delimiter(")>operator(\))operator(]) + ident(mydict)operator([)ident(k)operator(]) operator(=) ident(v) +ident(pattern_text) operator(=) string operator(+) stringoperator(.)ident(join)operator(()operator([)ident(re)operator(.)ident(escape)operator(()ident(word)operator(\)) keyword(for) ident(word) keyword(in) ident(mydict)operator(.)ident(keys)operator(()operator(\))operator(])operator(\)) operator(+) string +ident(pattern) operator(=) ident(re)operator(.)ident(compile)operator(()ident(pattern_text)operator(\)) + +ident(args) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) +ident(verbose) operator(=) integer(0) +keyword(if) ident(args) keyword(and) ident(args)operator([)integer(0)operator(]) operator(==) stringoperator(:) + ident(verbose) operator(=) integer(1) + ident(args) operator(=) ident(args)operator([)integer(1)operator(:)operator(]) + +keyword(if) keyword(not) ident(args)operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) + +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()ident(args)operator(,) ident(inplace)operator(=)integer(1)operator(,) ident(backup)operator(=)stringoperator(\))operator(:) + ident(output) operator(=) string + ident(pos) operator(=) integer(0) + keyword(while) pre_constant(True)operator(:) + ident(match) operator(=) ident(pattern)operator(.)ident(search)operator(()ident(line)operator(,) ident(pos)operator(\)) + keyword(if) keyword(not) ident(match)operator(:) + ident(output) operator(+=) ident(line)operator([)ident(pos)operator(:)operator(]) + keyword(break) + ident(output) operator(+=) ident(line)operator([)ident(pos)operator(:)ident(match)operator(.)ident(start)operator(()integer(0)operator(\))operator(]) operator(+) ident(mydict)operator([)ident(match)operator(.)ident(group)operator(()integer(1)operator(\))operator(]) + ident(pos) operator(=) ident(match)operator(.)ident(end)operator(()integer(0)operator(\)) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(output)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_1.18) +comment(#-----------------------------) +comment(#!/usr/bin/python) +comment(# psgrep - print selected lines of ps output by) +comment(# compiling user queries into code.) +comment(#) +comment(# examples :) +comment(# psgrep "uid<10") +keyword(import) ident(sys)operator(,) ident(os)operator(,) ident(re) + +keyword(class) ident(PsLineMatch)operator(:) + comment(# each field from the PS header) + ident(fieldnames) operator(=) operator(()stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,) \ + stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(\)) + ident(numeric_fields) operator(=) operator(()stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(\)) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(_fields) operator(=) operator({)operator(}) + + keyword(def) ident(new_line)operator(()pre_constant(self)operator(,) ident(ln)operator(\))operator(:) + pre_constant(self)operator(.)ident(_ln) operator(=) ident(ln)operator(.)ident(rstrip)operator(()operator(\)) + comment(# ps header for option "wwaxl" (different than in the perl code\)) + string + comment(# because only the last entry might contain blanks, splitting) + comment(# is safe) + ident(data) operator(=) pre_constant(self)operator(.)ident(_ln)operator(.)ident(split)operator(()pre_constant(None)operator(,)integer(12)operator(\)) + keyword(for) ident(fn)operator(,) ident(elem) keyword(in) predefined(zip)operator(()pre_constant(self)operator(.)ident(fieldnames)operator(,) ident(data)operator(\))operator(:) + keyword(if) ident(fn) keyword(in) pre_constant(self)operator(.)ident(numeric_fields)operator(:) comment(# make numbers integer ) + pre_constant(self)operator(.)ident(_fields)operator([)ident(fn)operator(]) operator(=) predefined(int)operator(()ident(elem)operator(\)) + keyword(else)operator(:) + pre_constant(self)operator(.)ident(_fields)operator([)ident(fn)operator(]) operator(=) ident(elem) + + keyword(def) ident(set_query)operator(()pre_constant(self)operator(,) ident(args)operator(\))operator(:) + comment(# assume args: "uid==500", "command ~ ^wm") + ident(conds)operator(=)operator([)operator(]) + ident(m) operator(=) ident(re)operator(.)ident(compile)operator(()string]+\)(.+\))delimiter(")>operator(\)) + keyword(for) ident(a) keyword(in) ident(args)operator(:) + keyword(try)operator(:) + operator(()ident(field)operator(,)ident(op)operator(,)ident(val)operator(\)) operator(=) ident(m)operator(.)ident(match)operator(()ident(a)operator(\))operator(.)ident(groups)operator(()operator(\)) + keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(a)operator(\)) + keyword(raise) exception(SystemExit) + keyword(if) ident(field) keyword(in) pre_constant(self)operator(.)ident(numeric_fields)operator(:) + ident(conds)operator(.)ident(append)operator(()ident(a)operator(\)) + keyword(else)operator(:) + ident(conds)operator(.)ident(append)operator(()stringoperator(,)operator(()ident(field)operator(,)ident(op)operator(,)ident(val)operator(\))operator(\)) + pre_constant(self)operator(.)ident(_desirable) operator(=) predefined(compile)operator(()stringoperator(+)stringoperator(.)ident(join)operator(()ident(conds)operator(\))operator(+)stringoperator(,) string)delimiter(")>operator(,)stringoperator(\)) + + keyword(def) ident(is_desirable)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) predefined(eval)operator(()pre_constant(self)operator(.)ident(_desirable)operator(,) operator({)operator(})operator(,) pre_constant(self)operator(.)ident(_fields)operator(\)) + + keyword(def) ident(__str__)operator(()pre_constant(self)operator(\))operator(:) + comment(# to allow "print".) + keyword(return) pre_constant(self)operator(.)ident(_ln) + +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(<=)integer(1)operator(:) + keyword(print) string \ + operator(%) operator(()ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(,) stringoperator(.)ident(join)operator(()ident(PsLineMatch)operator(()operator(\))operator(.)ident(fieldnames)operator(\))operator(\)) + keyword(raise) exception(SystemExit) + +ident(psln) operator(=) ident(PsLineMatch)operator(()operator(\)) +ident(psln)operator(.)ident(set_query)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(\)) +ident(p) operator(=) ident(os)operator(.)ident(popen)operator(()stringoperator(\)) +keyword(print) ident(p)operator(.)ident(readline)operator(()operator(\))operator([)operator(:)operator(-)integer(1)operator(]) comment(# emit header line) +keyword(for) ident(ln) keyword(in) ident(p)operator(.)ident(readlines)operator(()operator(\))operator(:) + ident(psln)operator(.)ident(new_line)operator(()ident(ln)operator(\)) + keyword(if) ident(psln)operator(.)ident(is_desirable)operator(()operator(\))operator(:) + keyword(print) ident(psln) +ident(p)operator(.)ident(close)operator(()operator(\)) + +comment(# alternatively one could consider every argument being a string and) +comment(# support wildcards: "uid==500" "command~^wm" by means of re, but this) +comment(# does not show dynamic python code generation, although re.compile) +comment(# also precompiles.) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_2.1) +comment(#-----------------------------) +comment(# The standard way of validating numbers is to convert them and catch) +comment(# an exception on failure) + +keyword(try)operator(:) + ident(myfloat) operator(=) predefined(float)operator(()ident(mystr)operator(\)) + keyword(print) string +keyword(except) exception(TypeError)operator(:) + keyword(print) string + +keyword(try)operator(:) + ident(myint) operator(=) predefined(int)operator(()ident(mystr)operator(\)) + keyword(print) string +keyword(except) exception(TypeError)operator(:) + keyword(print) string + +comment(# DON'T DO THIS. Explicit checking is prone to errors:) +keyword(if) ident(mystr)operator(.)ident(isdigit)operator(()operator(\))operator(:) comment(# Fails on "+4") + keyword(print) string +keyword(else)operator(:) + keyword(print) string + +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(mystr)operator(\))operator(:) comment(# Fails on "- 1" ) + keyword(print) string +keyword(else)operator(:) + keyword(print) string + +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(mystr)operator(\))operator(:) comment(# Opaque, and fails on "- 1") + keyword(print) string +keyword(else)operator(:) + keyword(print) string + +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.2) +comment(#-----------------------------) +comment(# equal(num1, num2, accuracy\) : returns true if num1 and num2 are) +comment(# equal to accuracy number of decimal places) + +keyword(def) ident(equal)operator(()ident(num1)operator(,) ident(num2)operator(,) ident(accuracy)operator(\))operator(:) + keyword(return) predefined(abs)operator(()ident(num1) operator(-) ident(num2)operator(\)) operator(<) integer(10)operator(**)operator(()operator(-)ident(accuracy)operator(\)) +comment(#-----------------------------) +keyword(from) ident(__future__) keyword(import) ident(division) comment(# use / for float div and // for int div) + +ident(wage) operator(=) integer(536) comment(# $5.36/hour) +ident(week) operator(=) integer(40) operator(*) ident(wage) comment(# $214.40) +keyword(print) string operator(%) operator(()ident(week)operator(/)integer(100)operator(\)) +comment(#=> One week's wage is: $214.40) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.3) +comment(#-----------------------------) +ident(rounded) operator(=) predefined(round)operator(()ident(num)operator(\)) comment(# rounds to integer) +comment(#-----------------------------) +ident(a) operator(=) float(0)float(.255) +ident(b) operator(=) string operator(%) ident(a) +keyword(print) string operator(%) operator(()ident(a)operator(,) ident(b)operator(\)) +keyword(print) string operator(%) operator(()ident(a)operator(,) ident(a)operator(\)) +comment(#=> Unrounded: 0.255000) +comment(#=> Rounded: 0.26) +comment(#=> Unrounded: 0.255000) +comment(#=> Rounded: 0.26) +comment(#-----------------------------) +keyword(from) ident(math) keyword(import) ident(floor)operator(,) ident(ceil) + +keyword(print) string +ident(a) operator(=) operator([)float(3)float(.3)operator(,) float(3)float(.5)operator(,) float(3)float(.7)operator(,) operator(-)float(3)float(.3)operator(]) +keyword(for) ident(n) keyword(in) ident(a)operator(:) + keyword(print) string operator(%) operator(()ident(n)operator(,) predefined(int)operator(()ident(n)operator(\))operator(,) ident(floor)operator(()ident(n)operator(\))operator(,) ident(ceil)operator(()ident(n)operator(\))operator(\)) +comment(#=> number int floor ceil) +comment(#=> 3.3 3.0 3.0 4.0) +comment(#=> 3.5 3.0 3.0 4.0) +comment(#=> 3.7 3.0 3.0 4.0) +comment(#=> -3.3 -3.0 -4.0 -3.0) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.4) +comment(#-----------------------------) +comment(# To convert a string in any base up to base 36, use the optional arg to int(\):) +ident(num) operator(=) predefined(int)operator(()stringoperator(,) integer(2)operator(\)) comment(# num is 54) + +comment(# To convert an int to an string representation in another base, you could use) +comment(# :) +keyword(import) ident(baseconvert) +keyword(def) ident(dec2bin)operator(()ident(i)operator(\))operator(:) + keyword(return) ident(baseconvert)operator(.)ident(baseconvert)operator(()ident(i)operator(,) ident(baseconvert)operator(.)ident(BASE10)operator(,) ident(baseconvert)operator(.)ident(BASE2)operator(\)) + +ident(binstr) operator(=) ident(dec2bin)operator(()integer(54)operator(\)) comment(# binstr is 110110) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.5) +comment(#-----------------------------) +keyword(for) ident(i) keyword(in) predefined(range)operator(()ident(x)operator(,)ident(y)operator(\))operator(:) + keyword(pass) comment(# i is set to every integer from x to y, excluding y) + +keyword(for) ident(i) keyword(in) predefined(range)operator(()ident(x)operator(,) ident(y)operator(,) integer(7)operator(\))operator(:) + keyword(pass) comment(# i is set to every integer from x to y, stepsize = 7) + +keyword(print) stringoperator(,) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(0)operator(,)integer(3)operator(\))operator(:) + keyword(print) ident(i)operator(,) +keyword(print) + +keyword(print) stringoperator(,) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(3)operator(,)integer(5)operator(\))operator(:) + keyword(print) ident(i)operator(,) +keyword(print) + +comment(# DON'T DO THIS:) +keyword(print) stringoperator(,) +ident(i) operator(=) integer(5) +keyword(while) ident(i) operator(<=) integer(12)operator(:) + keyword(print) ident(i) + ident(i) operator(+=) integer(1) + +comment(#=> Infancy is: 0 1 2) +comment(#=> Toddling is: 3 4) +comment(#=> Childhood is: 5 6 7 8 9 10 11 12) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.6) +comment(#-----------------------------) +comment(# See http://www.faqts.com/knowledge_base/view.phtml/aid/4442) +comment(# for a module that does this) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.7) +comment(#-----------------------------) +keyword(import) ident(random) comment(# use help(random\) to see the (large\) list of funcs) + +ident(rand) operator(=) ident(random)operator(.)ident(randint)operator(()ident(x)operator(,) ident(y)operator(\)) +comment(#-----------------------------) +ident(rand) operator(=) ident(random)operator(.)ident(randint)operator(()integer(25)operator(,) integer(76)operator(\)) +keyword(print) ident(rand) +comment(#-----------------------------) +ident(elt) operator(=) ident(random)operator(.)ident(choice)operator(()ident(mylist)operator(\)) +comment(#-----------------------------) +keyword(import) ident(string) +ident(chars) operator(=) ident(string)operator(.)ident(letters) operator(+) ident(string)operator(.)ident(digits) operator(+) string +ident(password) operator(=) stringoperator(.)ident(join)operator(()operator([)ident(random)operator(.)ident(choice)operator(()ident(chars)operator(\)) keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(8)operator(\))operator(])operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.8) +comment(#-----------------------------) +comment(# Changes the default RNG) +ident(random)operator(.)ident(seed)operator(()operator(\)) + +comment(# Or you can create independent RNGs) +ident(gen1) operator(=) ident(random)operator(.)ident(Random)operator(()integer(6)operator(\)) +ident(gen2) operator(=) ident(random)operator(.)ident(Random)operator(()integer(6)operator(\)) +ident(gen3) operator(=) ident(random)operator(.)ident(Random)operator(()integer(10)operator(\)) +ident(a1)operator(,) ident(b1) operator(=) ident(gen1)operator(.)ident(random)operator(()operator(\))operator(,) ident(gen1)operator(.)ident(random)operator(()operator(\)) +ident(a2)operator(,) ident(b2) operator(=) ident(gen2)operator(.)ident(random)operator(()operator(\))operator(,) ident(gen2)operator(.)ident(random)operator(()operator(\)) +ident(a3)operator(,) ident(b3) operator(=) ident(gen3)operator(.)ident(random)operator(()operator(\))operator(,) ident(gen3)operator(.)ident(random)operator(()operator(\)) +comment(# a1 == a2 and b1 == b2) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.9) +comment(#-----------------------------) +comment(# see http://www.sbc.su.se/~per/crng/ or http://www.frohne.westhost.com/rv11reference.htm) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.10) +comment(#-----------------------------) +keyword(import) ident(random) +ident(mean) operator(=) integer(25) +ident(sdev) operator(=) integer(2) +ident(salary) operator(=) ident(random)operator(.)ident(gauss)operator(()ident(mean)operator(,) ident(sdev)operator(\)) +keyword(print) string operator(%) ident(salary) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.11) +comment(#-----------------------------) +ident(radians) operator(=) ident(math)operator(.)ident(radians)operator(()ident(degrees)operator(\)) +ident(degrees) operator(=) ident(math)operator(.)ident(degrees)operator(()ident(radians)operator(\)) + +comment(# pre-2.3:) +keyword(from) ident(__future__) keyword(import) ident(division) +keyword(import) ident(math) +keyword(def) ident(deg2rad)operator(()ident(degrees)operator(\))operator(:) + keyword(return) operator(()ident(degrees) operator(/) integer(180)operator(\)) operator(*) ident(math)operator(.)ident(pi) +keyword(def) ident(rad2deg)operator(()ident(radians)operator(\))operator(:) + keyword(return) operator(()ident(radians) operator(/) ident(math)operator(.)ident(pi)operator(\)) operator(*) integer(180) +comment(#-----------------------------) +comment(# Use deg2rad instead of math.radians if you have pre-2.3 Python.) +keyword(import) ident(math) +keyword(def) ident(degree_sine)operator(()ident(degrees)operator(\))operator(:) + ident(radians) operator(=) ident(math)operator(.)ident(radians)operator(()ident(degrees)operator(\)) + keyword(return) ident(math)operator(.)ident(sin)operator(()ident(radians)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.12) +comment(#-----------------------------) +keyword(import) ident(math) + +comment(# DON'T DO THIS. Use math.tan(\) instead.) +keyword(def) ident(tan)operator(()ident(theta)operator(\))operator(:) + keyword(return) ident(math)operator(.)ident(sin)operator(()ident(theta)operator(\)) operator(/) ident(math)operator(.)ident(cos)operator(()ident(theta)operator(\)) +comment(#----------------) +comment(# NOTE: this sets y to 16331239353195370.0) +keyword(try)operator(:) + ident(y) operator(=) ident(math)operator(.)ident(tan)operator(()ident(math)operator(.)ident(pi)operator(/)integer(2)operator(\)) +keyword(except) exception(ValueError)operator(:) + ident(y) operator(=) pre_constant(None) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.13) +comment(#-----------------------------) +keyword(import) ident(math) +ident(log_e) operator(=) ident(math)operator(.)ident(log)operator(()ident(VALUE)operator(\)) +comment(#-----------------------------) +ident(log_10) operator(=) ident(math)operator(.)ident(log10)operator(()ident(VALUE)operator(\)) +comment(#-----------------------------) +keyword(def) ident(log_base)operator(()ident(base)operator(,) ident(value)operator(\))operator(:) + keyword(return) ident(math)operator(.)ident(log)operator(()ident(value)operator(\)) operator(/) ident(math)operator(.)ident(log)operator(()ident(base)operator(\)) +comment(#-----------------------------) +comment(# log_base defined as above) +ident(answer) operator(=) ident(log_base)operator(()integer(10)operator(,) integer(10000)operator(\)) +keyword(print) stringoperator(,) ident(answer) +comment(#=> log10(10,000\) = 4.0) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.14) +comment(#-----------------------------) +comment(# NOTE: must have NumPy installed. See) +comment(# http://www.pfdubois.com/numpy/) + +keyword(import) ident(Numeric) +ident(a) operator(=) ident(Numeric)operator(.)ident(array)operator(() operator(()operator(()integer(3)operator(,) integer(2)operator(,) integer(3)operator(\))operator(,) + operator(()integer(5)operator(,) integer(9)operator(,) integer(8)operator(\)) operator(\))operator(,) stringoperator(\)) +ident(b) operator(=) ident(Numeric)operator(.)ident(array)operator(() operator(()operator(()integer(4)operator(,) integer(7)operator(\))operator(,) + operator(()integer(9)operator(,) integer(3)operator(\))operator(,) + operator(()integer(8)operator(,) integer(1)operator(\)) operator(\))operator(,) stringoperator(\)) +ident(c) operator(=) ident(Numeric)operator(.)ident(matrixmultiply)operator(()ident(a)operator(,) ident(b)operator(\)) + +keyword(print) ident(c) +comment(#=> [[ 54. 30.]) +comment(#=> [ 165. 70.]]) + +keyword(print) ident(a)operator(.)ident(shape)operator(,) ident(b)operator(.)ident(shape)operator(,) ident(c)operator(.)ident(shape) +comment(#=> (2, 3\) (3, 2\) (2, 2\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.15) +comment(#-----------------------------) +ident(a) operator(=) integer(3)operator(+)integer(5)ident(j) +ident(b) operator(=) integer(2)operator(-)integer(2)ident(j) +ident(c) operator(=) ident(a) operator(*) ident(b) +keyword(print) stringoperator(,) ident(c) +comment(#=> c = (16+4j\)) + +keyword(print) ident(c)operator(.)ident(real)operator(,) ident(c)operator(.)ident(imag)operator(,) ident(c)operator(.)ident(conjugate)operator(()operator(\)) +comment(#=> 16.0 4.0 (16-4j\)) +comment(#-----------------------------) +keyword(import) ident(cmath) +keyword(print) ident(cmath)operator(.)ident(sqrt)operator(()integer(3)operator(+)integer(4)ident(j)operator(\)) +comment(#=> (2+1j\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.16) +comment(#-----------------------------) +ident(number) operator(=) predefined(int)operator(()ident(hexadecimal)operator(,) integer(16)operator(\)) +ident(number) operator(=) predefined(int)operator(()ident(octal)operator(,) integer(8)operator(\)) +ident(s) operator(=) predefined(hex)operator(()ident(number)operator(\)) +ident(s) operator(=) predefined(oct)operator(()ident(number)operator(\)) + +ident(num) operator(=) predefined(raw_input)operator(()stringoperator(\))operator(.)ident(rstrip)operator(()operator(\)) +keyword(if) ident(num)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + ident(num) operator(=) predefined(int)operator(()ident(num)operator([)integer(2)operator(:)operator(])operator(,) integer(16)operator(\)) +keyword(elif) ident(num)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + ident(num) operator(=) predefined(int)operator(()ident(num)operator([)integer(1)operator(:)operator(])operator(,) integer(8)operator(\)) +keyword(else)operator(:) + ident(num) operator(=) predefined(int)operator(()ident(num)operator(\)) +keyword(print) string operator(%) operator({) stringoperator(:) ident(num) operator(}) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_2.17) +comment(#-----------------------------) +keyword(def) ident(commify)operator(()ident(amount)operator(\))operator(:) + ident(amount) operator(=) predefined(str)operator(()ident(amount)operator(\)) + ident(firstcomma) operator(=) predefined(len)operator(()ident(amount)operator(\))operator(%)integer(3) keyword(or) integer(3) comment(# set to 3 if would make a leading comma) + ident(first)operator(,) ident(rest) operator(=) ident(amount)operator([)operator(:)ident(firstcomma)operator(])operator(,) ident(amount)operator([)ident(firstcomma)operator(:)operator(]) + ident(segments) operator(=) operator([)ident(first)operator(]) operator(+) operator([)ident(rest)operator([)ident(i)operator(:)ident(i)operator(+)integer(3)operator(]) keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(0)operator(,) predefined(len)operator(()ident(rest)operator(\))operator(,) integer(3)operator(\))operator(]) + keyword(return) stringoperator(.)ident(join)operator(()ident(segments)operator(\)) + +keyword(print) ident(commify)operator(()integer(12345678)operator(\)) +comment(#=> 12,345,678) + +comment(# DON'T DO THIS. It works on 2.3+ only and is slower and less straightforward) +comment(# than the non-regex version above.) +keyword(import) ident(re) +keyword(def) ident(commify)operator(()ident(amount)operator(\))operator(:) + ident(amount) operator(=) predefined(str)operator(()ident(amount)operator(\)) + ident(amount) operator(=) ident(amount)operator([)operator(:)operator(:)operator(-)integer(1)operator(]) + ident(amount) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(amount)operator(\)) + keyword(return) ident(amount)operator([)operator(:)operator(:)operator(-)integer(1)operator(]) + +comment(# @@PLEAC@@_2.18) +comment(# Printing Correct Plurals) +comment(#-----------------------------) +keyword(def) ident(pluralise)operator(()ident(value)operator(,) ident(root)operator(,) ident(singular)operator(=)stringoperator(,) ident(plural)operator(=)stringoperator(\))operator(:) + keyword(if) ident(value) operator(==) integer(1)operator(:) + keyword(return) ident(root) operator(+) ident(singular) + keyword(else)operator(:) + keyword(return) ident(root) operator(+) ident(plural) + +keyword(print) stringoperator(,) ident(duration)operator(,) ident(pluralise)operator(()ident(duration)operator(,) stringoperator(\)) + +keyword(print) string operator(%) operator(()ident(duration)operator(,) + ident(pluralise)operator(()ident(duration)operator(,) stringoperator(\))operator(,) + ident(pluralise)operator(()ident(duration)operator(,) stringoperator(,) stringoperator(,) stringoperator(\))operator(\)) +comment(#-----------------------------) +keyword(import) ident(re) +keyword(def) ident(noun_plural)operator(()ident(word)operator(\))operator(:) + ident(endings) operator(=) operator([)operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(]) + keyword(for) ident(singular)operator(,) ident(plural) keyword(in) ident(endings)operator(:) + ident(ret)operator(,) ident(found) operator(=) ident(re)operator(.)ident(subn)operator(()stringoperator(%)ident(singular)operator(,) ident(plural)operator(,) ident(word)operator(\)) + keyword(if) ident(found)operator(:) + keyword(return) ident(ret) + +ident(verb_singular) operator(=) ident(noun_plural)operator(;) comment(# make function alias) +comment(#-----------------------------) + +comment(# @@PLEAC@@_2.19) +comment(# Program: Calculating Prime Factors) +comment(#-----------------------------) +comment(#% bigfact 8 9 96 2178) +comment(#8 2**3) +comment(#) +comment(#9 3**2) +comment(#) +comment(#96 2**5 3) +comment(#) +comment(#2178 2 3**2 11**2) +comment(#-----------------------------) +comment(#% bigfact 239322000000000000000000) +comment(#239322000000000000000000 2**19 3 5**18 39887 ) +comment(#) +comment(#) +comment(#% bigfact 25000000000000000000000000) +comment(#25000000000000000000000000 2**24 5**26) +comment(#-----------------------------) +keyword(import) ident(sys) + +keyword(def) ident(factorise)operator(()ident(num)operator(\))operator(:) + ident(factors) operator(=) operator({)operator(}) + ident(orig) operator(=) ident(num) + keyword(print) ident(num)operator(,) stringoperator(,) + + comment(# we take advantage of the fact that (i +1\)**2 = i**2 + 2*i +1) + ident(i)operator(,) ident(sqi) operator(=) integer(2)operator(,) integer(4) + keyword(while) ident(sqi) operator(<=) ident(num)operator(:) + keyword(while) keyword(not) ident(num)operator(%)ident(i)operator(:) + ident(num) operator(/=) ident(i) + ident(factors)operator([)ident(i)operator(]) operator(=) ident(factors)operator(.)ident(get)operator(()ident(i)operator(,) integer(0)operator(\)) operator(+) integer(1) + + ident(sqi) operator(+=) integer(2)operator(*)ident(i) operator(+) integer(1) + ident(i) operator(+=) integer(1) + + keyword(if) ident(num) operator(!=) integer(1) keyword(and) ident(num) operator(!=) ident(orig)operator(:) + ident(factors)operator([)ident(num)operator(]) operator(=) ident(factors)operator(.)ident(get)operator(()ident(num)operator(,) integer(0)operator(\)) operator(+) integer(1) + + keyword(if) keyword(not) ident(factors)operator(:) + keyword(print) string + + keyword(for) ident(factor) keyword(in) predefined(sorted)operator(()ident(factors)operator(\))operator(:) + keyword(if) ident(factor)operator(:) + ident(tmp) operator(=) predefined(str)operator(()ident(factor)operator(\)) + keyword(if) ident(factors)operator([)ident(factor)operator(])operator(>)integer(1)operator(:) ident(tmp) operator(+=) string operator(+) predefined(str)operator(()ident(factors)operator([)ident(factor)operator(])operator(\)) + keyword(print) ident(tmp)operator(,) + keyword(print) + +comment(#--------) +keyword(if) ident(__name__) operator(==) stringoperator(:) + keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\)) operator(==) integer(1)operator(:) + keyword(print) stringoperator(,) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(,) string + keyword(else)operator(:) + keyword(for) ident(strnum) keyword(in) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(:) + keyword(try)operator(:) + ident(num) operator(=) predefined(int)operator(()ident(strnum)operator(\)) + ident(factorise)operator(()ident(num)operator(\)) + keyword(except) exception(ValueError)operator(:) + keyword(print) ident(strnum)operator(,) string +comment(#-----------------------------) +comment(# A more Pythonic variant (which separates calculation from printing\):) +keyword(def) ident(format_factor)operator(()ident(base)operator(,) ident(exponent)operator(\))operator(:) + keyword(if) ident(exponent) operator(>) integer(1)operator(:) + keyword(return) stringoperator(%)operator(()ident(base)operator(,) ident(exponent)operator(\)) + keyword(return) predefined(str)operator(()ident(base)operator(\)) + +keyword(def) ident(factorise)operator(()ident(num)operator(\))operator(:) + ident(factors) operator(=) operator({)operator(}) + ident(orig) operator(=) ident(num) + + comment(# we take advantage of the fact that (i+1\)**2 = i**2 + 2*i +1) + ident(i)operator(,) ident(sqi) operator(=) integer(2)operator(,) integer(4) + keyword(while) ident(sqi) operator(<=) ident(num)operator(:) + keyword(while) keyword(not) ident(num)operator(%)ident(i)operator(:) + ident(num) operator(/=) ident(i) + ident(factors)operator([)ident(i)operator(]) operator(=) ident(factors)operator(.)ident(get)operator(()ident(i)operator(,) integer(0)operator(\)) operator(+) integer(1) + ident(sqi) operator(+=) integer(2)operator(*)ident(i) operator(+) integer(1) + ident(i) operator(+=) integer(1) + + keyword(if) ident(num) keyword(not) keyword(in) operator(()integer(1)operator(,) ident(orig)operator(\))operator(:) + ident(factors)operator([)ident(num)operator(]) operator(=) ident(factors)operator(.)ident(get)operator(()ident(num)operator(,) integer(0)operator(\)) operator(+) integer(1) + + keyword(if) keyword(not) ident(factors)operator(:) + keyword(return) operator([)stringoperator(]) + + ident(out) operator(=) operator([)ident(format_factor)operator(()ident(base)operator(,) ident(exponent)operator(\)) + keyword(for) ident(base)operator(,) ident(exponent) keyword(in) predefined(sorted)operator(()ident(factors)operator(.)ident(items)operator(()operator(\))operator(\))operator(]) + keyword(return) ident(out) + +keyword(def) ident(print_factors)operator(()ident(value)operator(\))operator(:) + keyword(try)operator(:) + ident(num) operator(=) predefined(int)operator(()ident(value)operator(\)) + keyword(if) ident(num) operator(!=) predefined(float)operator(()ident(value)operator(\))operator(:) + keyword(raise) exception(ValueError) + keyword(except) operator(()exception(ValueError)operator(,) exception(TypeError)operator(\))operator(:) + keyword(raise) exception(ValueError)operator(()stringoperator(\)) + ident(factors) operator(=) ident(factorise)operator(()ident(num)operator(\)) + keyword(print) ident(num)operator(,) stringoperator(,) stringoperator(.)ident(join)operator(()ident(factors)operator(\)) + +comment(# @@PLEAC@@_3.0) +comment(#----------------------------- ) +comment(#introduction) +comment(# There are three common ways of manipulating dates in Python) +comment(# mxDateTime - a popular third-party module (not discussed here\) ) +comment(# time - a fairly low-level standard library module ) +comment(# datetime - a new library module for Python 2.3 and used for most of these samples ) +comment(# (I will use full names to show which module they are in, but you can also use) +comment(# from datetime import datetime, timedelta and so on for convenience\) ) + +keyword(import) ident(time) +keyword(import) ident(datetime) + +keyword(print) stringoperator(,) ident(time)operator(.)ident(localtime)operator(()operator(\))operator([)integer(7)operator(])operator(,) string +comment(# Today is day 218 of the current year) + +ident(today) operator(=) ident(datetime)operator(.)ident(date)operator(.)ident(today)operator(()operator(\)) +keyword(print) stringoperator(,) ident(today)operator(.)ident(timetuple)operator(()operator(\))operator([)integer(7)operator(])operator(,) stringoperator(,) ident(today)operator(.)ident(year) +comment(# Today is day 218 of 2003) + +keyword(print) stringoperator(,) ident(today)operator(.)ident(strftime)operator(()stringoperator(\))operator(,) string +comment(# Today is day 218 of the current year) + + +comment(# @@PLEAC@@_3.1) +comment(#----------------------------- ) +comment(# Finding todays date) + +ident(today) operator(=) ident(datetime)operator(.)ident(date)operator(.)ident(today)operator(()operator(\)) +keyword(print) stringoperator(,) ident(today) +comment(#=> The date is 2003-08-06) + +comment(# the function strftime(\) (string-format time\) produces nice formatting) +comment(# All codes are detailed at http://www.python.org/doc/current/lib/module-time.html) +keyword(print) ident(t)operator(.)ident(strftime)operator(()stringoperator(\)) +comment(#=> four-digit year: 2003, two-digit year: 03, month: 08, day: 06) + + +comment(# @@PLEAC@@_3.2) +comment(#----------------------------- ) +comment(# Converting DMYHMS to Epoch Seconds) +comment(# To work with Epoch Seconds, you need to use the time module) + +comment(# For the local timezone) +ident(t) operator(=) ident(datetime)operator(.)ident(datetime)operator(.)ident(now)operator(()operator(\)) +keyword(print) stringoperator(,) ident(time)operator(.)ident(mktime)operator(()ident(t)operator(.)ident(timetuple)operator(()operator(\))operator(\)) +comment(#=> Epoch Seconds: 1060199000.0) + +comment(# For UTC) +ident(t) operator(=) ident(datetime)operator(.)ident(datetime)operator(.)ident(utcnow)operator(()operator(\)) +keyword(print) stringoperator(,) ident(time)operator(.)ident(mktime)operator(()ident(t)operator(.)ident(timetuple)operator(()operator(\))operator(\)) +comment(#=> Epoch Seconds: 1060195503.0) + + +comment(# @@PLEAC@@_3.3) +comment(#----------------------------- ) +comment(# Converting Epoch Seconds to DMYHMS) + +ident(now) operator(=) ident(datetime)operator(.)ident(datetime)operator(.)ident(fromtimestamp)operator(()ident(EpochSeconds)operator(\)) +comment(#or use datetime.datetime.utcfromtimestamp(\)) +keyword(print) ident(now) +comment(#=> datetime.datetime(2003, 8, 6, 20, 43, 20\)) +keyword(print) ident(now)operator(.)ident(ctime)operator(()operator(\)) +comment(#=> Wed Aug 6 20:43:20 2003) + +comment(# or with the time module) +ident(oldtimetuple) operator(=) ident(time)operator(.)ident(localtime)operator(()ident(EpochSeconds)operator(\)) +comment(# oldtimetuple contains (year, month, day, hour, minute, second, weekday, yearday, daylightSavingAdjustment\) ) +keyword(print) ident(oldtimetuple) +comment(#=> (2003, 8, 6, 20, 43, 20, 2, 218, 1\)) + + +comment(# @@PLEAC@@_3.4) +comment(#----------------------------- ) +comment(# Adding to or Subtracting from a Date) +comment(# Use the rather nice datetime.timedelta objects) + +ident(now) operator(=) ident(datetime)operator(.)ident(date)operator(()integer(2003)operator(,) integer(8)operator(,) integer(6)operator(\)) +ident(difference1) operator(=) ident(datetime)operator(.)ident(timedelta)operator(()ident(days)operator(=)integer(1)operator(\)) +ident(difference2) operator(=) ident(datetime)operator(.)ident(timedelta)operator(()ident(weeks)operator(=)operator(-)integer(2)operator(\)) + +keyword(print) stringoperator(,) ident(now) operator(+) ident(difference1) +comment(#=> One day in the future is: 2003-08-07) + +keyword(print) stringoperator(,) ident(now) operator(+) ident(difference2) +comment(#=> Two weeks in the past is: 2003-07-23) + +keyword(print) ident(datetime)operator(.)ident(date)operator(()integer(2003)operator(,) integer(8)operator(,) integer(6)operator(\)) operator(-) ident(datetime)operator(.)ident(date)operator(()integer(2000)operator(,) integer(8)operator(,) integer(6)operator(\)) +comment(#=> 1095 days, 0:00:00) + +comment(#----------------------------- ) +ident(birthtime) operator(=) ident(datetime)operator(.)ident(datetime)operator(()integer(1973)operator(,) oct(01)operator(,) integer(18)operator(,) integer(3)operator(,) integer(45)operator(,) integer(50)operator(\)) comment(# 1973-01-18 03:45:50) + +ident(interval) operator(=) ident(datetime)operator(.)ident(timedelta)operator(()ident(seconds)operator(=)integer(5)operator(,) ident(minutes)operator(=)integer(17)operator(,) ident(hours)operator(=)integer(2)operator(,) ident(days)operator(=)integer(55)operator(\)) +ident(then) operator(=) ident(birthtime) operator(+) ident(interval) + +keyword(print) stringoperator(,) ident(then)operator(.)ident(ctime)operator(()operator(\)) +comment(#=> Then is Wed Mar 14 06:02:55 1973) + +keyword(print) stringoperator(,) ident(then)operator(.)ident(strftime)operator(()stringoperator(\)) +comment(#=> Then is Wednesday March 14 06:02:55 AM 1973) + +comment(#-----------------------------) +ident(when) operator(=) ident(datetime)operator(.)ident(datetime)operator(()integer(1973)operator(,) integer(1)operator(,) integer(18)operator(\)) operator(+) ident(datetime)operator(.)ident(timedelta)operator(()ident(days)operator(=)integer(55)operator(\)) +keyword(print) stringoperator(,) ident(when)operator(.)ident(strftime)operator(()stringoperator(\))operator(.)ident(lstrip)operator(()stringoperator(\)) +comment(#=> Nat was 55 days old on: 3/14/1973) + + +comment(# @@PLEAC@@_3.5) +comment(#----------------------------- ) +comment(# Dates produce timedeltas when subtracted.) + +ident(diff) operator(=) ident(date2) operator(-) ident(date1) +ident(diff) operator(=) ident(datetime)operator(.)ident(date)operator(()ident(year1)operator(,) ident(month1)operator(,) ident(day1)operator(\)) operator(-) ident(datetime)operator(.)ident(date)operator(()ident(year2)operator(,) ident(month2)operator(,) ident(day2)operator(\)) +comment(#----------------------------- ) + +ident(bree) operator(=) ident(datetime)operator(.)ident(datetime)operator(()integer(1981)operator(,) integer(6)operator(,) integer(16)operator(,) integer(4)operator(,) integer(35)operator(,) integer(25)operator(\)) +ident(nat) operator(=) ident(datetime)operator(.)ident(datetime)operator(()integer(1973)operator(,) integer(1)operator(,) integer(18)operator(,) integer(3)operator(,) integer(45)operator(,) integer(50)operator(\)) + +ident(difference) operator(=) ident(bree) operator(-) ident(nat) +keyword(print) stringoperator(,) ident(difference)operator(,) string +comment(#=> There were 3071 days, 0:49:35 between Nat and Bree) + +ident(weeks)operator(,) ident(days) operator(=) predefined(divmod)operator(()ident(difference)operator(.)ident(days)operator(,) integer(7)operator(\)) + +ident(minutes)operator(,) ident(seconds) operator(=) predefined(divmod)operator(()ident(difference)operator(.)ident(seconds)operator(,) integer(60)operator(\)) +ident(hours)operator(,) ident(minutes) operator(=) predefined(divmod)operator(()ident(minutes)operator(,) integer(60)operator(\)) + +keyword(print) string operator(%) operator(()ident(weeks)operator(,) ident(days)operator(,) ident(hours)operator(,) ident(minutes)operator(,) ident(seconds)operator(\)) +comment(#=> 438 weeks, 5 days, 0:49:35) + +comment(#----------------------------- ) +keyword(print) stringoperator(,) ident(difference)operator(.)ident(days)operator(,) string +comment(#=> There were 3071 days between bree and nat) + + +comment(# @@PLEAC@@_3.6) +comment(#----------------------------- ) +comment(# Day in a Week/Month/Year or Week Number) + +ident(when) operator(=) ident(datetime)operator(.)ident(date)operator(()integer(1981)operator(,) integer(6)operator(,) integer(16)operator(\)) + +keyword(print) string +keyword(print) ident(when)operator(.)ident(strftime)operator(()stringoperator(\)) +keyword(print) ident(when)operator(.)ident(strftime)operator(()stringoperator(\)) + +comment(#=> 16/6/1981 was:) +comment(#=> Day 2 of the week (a Tuesday\). Day 16 of the month (June\).) +comment(#=> Day 167 of the year (1981\), in week 24 of the year.) + + +comment(# @@PLEAC@@_3.7) +comment(#----------------------------- ) +comment(# Parsing Dates and Times from Strings) + +ident(time)operator(.)ident(strptime)operator(()stringoperator(\)) +comment(# (1981, 6, 16, 20, 18, 3, 1, 167, -1\)) + +ident(time)operator(.)ident(strptime)operator(()stringoperator(,) stringoperator(\)) +comment(# (1981, 6, 16, 0, 0, 0, 1, 167, -1\)) +comment(# strptime(\) can use any of the formatting codes from time.strftime(\)) + +comment(# The easiest way to convert this to a datetime seems to be; ) +ident(now) operator(=) ident(datetime)operator(.)ident(datetime)operator(()operator(*)ident(time)operator(.)ident(strptime)operator(()stringoperator(,) stringoperator(\))operator([)integer(0)operator(:)integer(5)operator(])operator(\)) +comment(# the '*' operator unpacks the tuple, producing the argument list.) + + +comment(# @@PLEAC@@_3.8) +comment(#----------------------------- ) +comment(# Printing a Date) +comment(# Use datetime.strftime(\) - see helpfiles in distro or at python.org) + +keyword(print) ident(datetime)operator(.)ident(datetime)operator(.)ident(now)operator(()operator(\))operator(.)ident(strftime)operator(()stringoperator(\)) +comment(#=> The date is Friday (Fri\) 08/08/2003) + +comment(# @@PLEAC@@_3.9) +comment(#----------------------------- ) +comment(# High Resolution Timers) + +ident(t1) operator(=) ident(time)operator(.)ident(clock)operator(()operator(\)) +comment(# Do Stuff Here) +ident(t2) operator(=) ident(time)operator(.)ident(clock)operator(()operator(\)) +keyword(print) ident(t2) operator(-) ident(t1) + +comment(# 2.27236813618) +comment(# Accuracy will depend on platform and OS,) +comment(# but time.clock(\) uses the most accurate timer it can) + +ident(time)operator(.)ident(clock)operator(()operator(\))operator(;) ident(time)operator(.)ident(clock)operator(()operator(\)) +comment(# 174485.51365466841) +comment(# 174485.55702610247) + +comment(#----------------------------- ) +comment(# Also useful;) +keyword(import) ident(timeit) +ident(code) operator(=) string +predefined(eval)operator(()ident(code)operator(\)) +comment(# [0, 2, 4, 6, 8]) + +ident(t) operator(=) ident(timeit)operator(.)ident(Timer)operator(()ident(code)operator(\)) +keyword(print) stringoperator(,) ident(t)operator(.)ident(timeit)operator(()integer(10000)operator(\))operator(,) string +keyword(print) stringoperator(,) ident(t)operator(.)ident(timeit)operator(()operator(\))operator(,) string + +comment(# 10,000 repeats of that code takes: 0.128238644856 seconds) +comment(# 1,000,000 repeats of that code takes: 12.5396490336 seconds) + +comment(#----------------------------- ) +keyword(import) ident(timeit) +ident(code) operator(=) string +ident(t) operator(=) ident(timeit)operator(.)ident(Timer)operator(()ident(code)operator(\)) + +keyword(print) string +keyword(print) stringoperator(,) ident(t)operator(.)ident(timeit)operator(()integer(1000)operator(\)) operator(/) integer(1000) +comment(# Time taken: 5.24391507859) + + +comment(# @@PLEAC@@_3.10) +comment(#----------------------------- ) +comment(# Short Sleeps) + +ident(seconds) operator(=) float(3)float(.1) +ident(time)operator(.)ident(sleep)operator(()ident(seconds)operator(\)) +keyword(print) string + +comment(# @@PLEAC@@_3.11) +comment(#----------------------------- ) +comment(# Program HopDelta) +comment(# Save a raw email to disk and run "python hopdelta.py FILE") +comment(# and it will process the headers and show the time taken) +comment(# for each server hop (nb: if server times are wrong, negative dates) +comment(# might appear in the output\).) + +keyword(import) ident(datetime)operator(,) ident(email)operator(,) ident(email)operator(.)ident(Utils) +keyword(import) ident(os)operator(,) ident(sys)operator(,) ident(time) + +keyword(def) ident(extract_date)operator(()ident(hop)operator(\))operator(:) + comment(# According to RFC822, the date will be prefixed with) + comment(# a semi-colon, and is the last part of a received) + comment(# header.) + ident(date_string) operator(=) ident(hop)operator([)ident(hop)operator(.)ident(find)operator(()stringoperator(\))operator(+)integer(2)operator(:)operator(]) + ident(date_string) operator(=) ident(date_string)operator(.)ident(strip)operator(()operator(\)) + ident(time_tuple) operator(=) ident(email)operator(.)ident(Utils)operator(.)ident(parsedate)operator(()ident(date_string)operator(\)) + + comment(# convert time_tuple to datetime) + ident(EpochSeconds) operator(=) ident(time)operator(.)ident(mktime)operator(()ident(time_tuple)operator(\)) + ident(dt) operator(=) ident(datetime)operator(.)ident(datetime)operator(.)ident(fromtimestamp)operator(()ident(EpochSeconds)operator(\)) + keyword(return) ident(dt) + +keyword(def) ident(process)operator(()ident(filename)operator(\))operator(:) + comment(# Main email file processing) + comment(# read the headers and process them) + ident(f) operator(=) predefined(file)operator(()ident(filename)operator(,) stringoperator(\)) + ident(msg) operator(=) ident(email)operator(.)ident(message_from_file)operator(()ident(f)operator(\)) + + ident(hops) operator(=) ident(msg)operator(.)ident(get_all)operator(()stringoperator(\)) + + comment(# in reverse order, get the server(s\) and date/time involved) + ident(hops)operator(.)ident(reverse)operator(()operator(\)) + ident(results) operator(=) operator([)operator(]) + keyword(for) ident(hop) keyword(in) ident(hops)operator(:) + ident(hop) operator(=) ident(hop)operator(.)ident(lower)operator(()operator(\)) + + keyword(if) ident(hop)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) comment(# 'Received: by' line) + ident(sender) operator(=) string + ident(receiver) operator(=) ident(hop)operator([)integer(3)operator(:)ident(hop)operator(.)ident(find)operator(()stringoperator(,)integer(3)operator(\))operator(]) + ident(date) operator(=) ident(extract_date)operator(()ident(hop)operator(\)) + + keyword(else)operator(:) comment(# 'Received: from' line) + ident(sender) operator(=) ident(hop)operator([)integer(5)operator(:)ident(hop)operator(.)ident(find)operator(()stringoperator(,)integer(5)operator(\))operator(]) + ident(by) operator(=) ident(hop)operator(.)ident(find)operator(()stringoperator(\))operator(+)integer(3) + ident(receiver) operator(=) ident(hop)operator([)ident(by)operator(:)ident(hop)operator(.)ident(find)operator(()stringoperator(,) ident(by)operator(\))operator(]) + ident(date) operator(=) ident(extract_date)operator(()ident(hop)operator(\)) + + ident(results)operator(.)ident(append)operator(()operator(()ident(sender)operator(,) ident(receiver)operator(,) ident(date)operator(\))operator(\)) + ident(output)operator(()ident(results)operator(\)) + +keyword(def) ident(output)operator(()ident(results)operator(\))operator(:) + keyword(print) string + keyword(print) + ident(previous_dt) operator(=) ident(delta) operator(=) integer(0) + keyword(for) operator(()ident(sender)operator(,) ident(receiver)operator(,) ident(date)operator(\)) keyword(in) ident(results)operator(:) + keyword(if) ident(previous_dt)operator(:) + ident(delta) operator(=) ident(date) operator(-) ident(previous_dt) + + keyword(print) string operator(%) operator(()ident(sender)operator(,) + ident(receiver)operator(,) + ident(date)operator(.)ident(strftime)operator(()stringoperator(\))operator(,) + ident(delta)operator(\)) + keyword(print) + ident(previous_dt) operator(=) ident(date) + +keyword(def) ident(main)operator(()operator(\))operator(:) + comment(# Perform some basic argument checking) + keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\)) operator(!=) integer(2)operator(:) + keyword(print) string + + keyword(else)operator(:) + ident(filename) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) + keyword(if) ident(os)operator(.)ident(path)operator(.)ident(isfile)operator(()ident(filename)operator(\))operator(:) + ident(process)operator(()ident(filename)operator(\)) + keyword(else)operator(:) + keyword(print) ident(filename)operator(,) string + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(main)operator(()operator(\)) + + +comment(# @@PLEAC@@_4.0) +comment(#-----------------------------) +comment(# Python does not automatically flatten lists, in other words) +comment(# in the following, non-nested contains four elements and) +comment(# nested contains three elements, the third element of which) +comment(# is itself a list containing two elements:) +ident(non_nested) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(]) +ident(nested) operator(=) operator([)stringoperator(,) stringoperator(,) operator([)stringoperator(,) stringoperator(])operator(]) +comment(#-----------------------------) +ident(tune) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.1) +comment(#-----------------------------) +ident(a) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +ident(a) operator(=) stringoperator(.)ident(split)operator(()operator(\)) + +ident(text) operator(=) string +ident(lines) operator(=) operator([)ident(line)operator(.)ident(lstrip)operator(()operator(\)) keyword(for) ident(line) keyword(in) ident(text)operator(.)ident(strip)operator(()operator(\))operator(.)ident(split)operator(()stringoperator(\))operator(]) +comment(#-----------------------------) +ident(biglist) operator(=) operator([)ident(line)operator(.)ident(rstrip)operator(()operator(\)) keyword(for) ident(line) keyword(in) predefined(open)operator(()stringoperator(\))operator(]) +comment(#-----------------------------) +ident(banner) operator(=) string +ident(banner) operator(=) string +comment(#-----------------------------) +ident(name) operator(=) string +ident(banner) operator(=) string operator(+) ident(name) operator(+) string +ident(banner) operator(=) string operator(%) ident(name) +comment(#-----------------------------) +ident(his_host) operator(=) string +keyword(import) ident(os) +ident(host_info) operator(=) ident(os)operator(.)ident(popen)operator(()string operator(+) ident(his_host)operator(\))operator(.)ident(read)operator(()operator(\)) + +comment(# NOTE: not really relevant to Python (no magic '$$' variable\)) +ident(python_info) operator(=) ident(os)operator(.)ident(popen)operator(()string operator(%) ident(os)operator(.)ident(getpid)operator(()operator(\))operator(\))operator(.)ident(read)operator(()operator(\)) +ident(shell_info) operator(=) ident(os)operator(.)ident(popen)operator(()stringoperator(\))operator(.)ident(read)operator(()operator(\)) +comment(#-----------------------------) +comment(# NOTE: not really relevant to Python (no automatic interpolation\)) +ident(banner) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +ident(banner) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +comment(#-----------------------------) +ident(brax) operator(=) string { } [ ] )delimiter(""")>operator(.)ident(split)operator(()operator(\)) comment(#""") +ident(brax) operator(=) predefined(list)operator(()string{}[])delimiter(""")>operator(\)) comment(#""") +ident(rings) operator(=) stringoperator(.)ident(split)operator(()operator(\)) comment(#''') +ident(tags) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +ident(sample) operator(=) stringoperator(.)ident(split)operator(()operator(\)) + +comment(#-----------------------------) +ident(banner) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +comment(#-----------------------------) +ident(ships) operator(=) stringoperator(.)ident(split)operator(()operator(\)) comment(# WRONG (only three ships\)) +ident(ships) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) comment(# right) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.2) +comment(#-----------------------------) +keyword(def) ident(commify_series)operator(()ident(args)operator(\))operator(:) + ident(n) operator(=) predefined(len)operator(()ident(args)operator(\)) + keyword(if) ident(n) operator(==) integer(0)operator(:) + keyword(return) string + keyword(elif) ident(n) operator(==) integer(1)operator(:) + keyword(return) ident(args)operator([)integer(0)operator(]) + keyword(elif) ident(n) operator(==) integer(2)operator(:) + keyword(return) ident(args)operator([)integer(0)operator(]) operator(+) string operator(+) ident(args)operator([)integer(1)operator(]) + keyword(return) stringoperator(.)ident(join)operator(()ident(args)operator([)operator(:)operator(-)integer(1)operator(])operator(\)) operator(+) string operator(+) ident(args)operator([)operator(-)integer(1)operator(]) + +ident(commify_series)operator(()operator([)operator(])operator(\)) +ident(commify_series)operator(()operator([)stringoperator(])operator(\)) +ident(commify_series)operator(()operator([)stringoperator(,) stringoperator(])operator(\)) +ident(commify_series)operator(()operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(\)) +comment(#-----------------------------) +ident(mylist) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +keyword(print) stringoperator(,) ident(mylist)operator(,) string +keyword(print) stringoperator(,) stringoperator(.)ident(join)operator(()ident(mylist)operator(\))operator(,) string +comment(#=> I have ['red', 'yellow', 'green'] marbles.) +comment(#=> I have red yellow green marbles.) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# commify_series - show proper comma insertion in list output) +ident(data) operator(=) operator(() + operator(() stringoperator(,) operator(\))operator(,) + operator(() stringoperator(.)ident(split)operator(()operator(\)) operator(\))operator(,) + operator(() stringoperator(.)ident(split)operator(()operator(\)) operator(\))operator(,) + operator(() stringoperator(,) stringoperator(,) string operator(\))operator(,) + operator(() stringoperator(,) stringoperator(,) stringoperator(,) string operator(\))operator(,) + operator(() stringoperator(,) string operator(\))operator(,) + operator(() stringoperator(,) + stringoperator(,) + string operator(\))operator(,) + operator(\)) + +keyword(def) ident(commify_series)operator(()ident(terms)operator(\))operator(:) + keyword(for) ident(term) keyword(in) ident(terms)operator(:) + keyword(if) string keyword(in) ident(term)operator(:) + ident(sepchar) operator(=) string + keyword(break) + keyword(else)operator(:) + ident(sepchar) operator(=) string + + ident(n) operator(=) predefined(len)operator(()ident(terms)operator(\)) + keyword(if) ident(n) operator(==) integer(0)operator(:) + keyword(return) string + keyword(elif) ident(n) operator(==) integer(1)operator(:) + keyword(return) ident(terms)operator([)integer(0)operator(]) + keyword(elif) ident(n) operator(==) integer(2)operator(:) + keyword(return) stringoperator(.)ident(join)operator(()ident(terms)operator(\)) + keyword(return) string operator(%) operator(()ident(sepchar)operator(.)ident(join)operator(()ident(terms)operator([)operator(:)operator(-)integer(1)operator(])operator(\))operator(,) ident(sepchar)operator(,) ident(terms)operator([)operator(-)integer(1)operator(])operator(\)) + +keyword(for) ident(item) keyword(in) ident(data)operator(:) + keyword(print) string operator(%) ident(commify_series)operator(()ident(item)operator(\)) + +comment(#=> The list is: just one thing.) +comment(#=> The list is: Mutt and Jeff.) +comment(#=> The list is: Peter, Paul, and Mary.) +comment(#=> The list is: To our parents, Mother Theresa, and God.) +comment(#=> The list is: pastrami, ham and cheese, peanut butter and jelly, and tuna.) +comment(#=> The list is: recycle tired, old phrases and ponder big, happy thoughts.) +comment(#=> The list is: recycle tired, old phrases; ponder big, happy thoughts; and) +comment(# sleep and dream peacefully.) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.3) +comment(#-----------------------------) +comment(# Python allocates more space than is necessary every time a list needs to) +comment(# grow and only shrinks lists when more than half the available space is) +comment(# unused. This means that adding or removing an element will in most cases) +comment(# not force a reallocation.) + +keyword(del) ident(mylist)operator([)ident(size)operator(:)operator(]) comment(# shrink mylist) +ident(mylist) operator(+=) operator([)pre_constant(None)operator(]) operator(*) ident(size) comment(# grow mylist by appending 'size' None elements) + +comment(# To add an element to the end of a list, use the append method:) +ident(mylist)operator(.)ident(append)operator(()integer(4)operator(\)) + +comment(# To insert an element, use the insert method:) +ident(mylist)operator(.)ident(insert)operator(()integer(0)operator(,) integer(10)operator(\)) comment(# Insert 10 at the beginning of the list) + +comment(# To extend one list with the contents of another, use the extend method:) +ident(list2) operator(=) operator([)integer(1)operator(,)integer(2)operator(,)integer(3)operator(]) +ident(mylist)operator(.)ident(extend)operator(()ident(list2)operator(\)) + +comment(# To insert the contents of one list into another, overwriting zero or ) +comment(# more elements, specify a slice:) +ident(mylist)operator([)integer(1)operator(:)integer(1)operator(]) operator(=) ident(list2) comment(# Don't overwrite anything; grow mylist if needed) +ident(mylist)operator([)integer(2)operator(:)integer(3)operator(]) operator(=) ident(list2) comment(# Overwrite mylist[2] and grow mylist if needed) + +comment(# To remove one element from the middle of a list:) +comment(# To remove elements from the middle of a list:) +keyword(del) ident(mylist)operator([)ident(idx1)operator(:)ident(idx2)operator(]) comment(# 0 or more) +ident(x) operator(=) ident(mylist)operator(.)ident(pop)operator(()ident(idx)operator(\)) comment(# remove mylist[idx] and assign it to x) + +comment(# You cannot assign to or get a non-existent element:) +comment(# >>> x = []) +comment(# >>> x[4] = 5) +comment(#) +comment(# Traceback (most recent call last\):) +comment(# File "", line 1, in -toplevel-) +comment(# x[4] = 5) +comment(# IndexError: list assignment index out of range) +comment(#) +comment(# >>> print x[1000]) +comment(#) +comment(# Traceback (most recent call last\):) +comment(# File "", line 1, in -toplevel-) +comment(# print x[1000]) +comment(# IndexError: list index out of range) +comment(#-----------------------------) +keyword(def) ident(what_about_that_list)operator(()ident(terms)operator(\))operator(:) + keyword(print) stringoperator(,) predefined(len)operator(()ident(terms)operator(\))operator(,) string + keyword(print) stringoperator(,) predefined(len)operator(()ident(terms)operator(\))operator(-)integer(1)operator(,) string + keyword(print) string operator(%) ident(terms)operator([)integer(3)operator(]) + +ident(people) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +ident(what_about_that_list)operator(()ident(people)operator(\)) +comment(#-----------------------------) +comment(#=> The list now has 4 elements.) +comment(#=> The index of the last element is 3 (or -1\).) +comment(#=> Element #3 is Young.) +comment(#-----------------------------) +ident(people)operator(.)ident(pop)operator(()operator(\)) +ident(what_about_that_list)operator(()ident(people)operator(\)) +comment(#-----------------------------) +ident(people) operator(+=) operator([)pre_constant(None)operator(]) operator(*) operator(()integer(10000) operator(-) predefined(len)operator(()ident(people)operator(\))operator(\)) +comment(#-----------------------------) +comment(#>>> people += [None] * (10000 - len(people\)\)) +comment(#>>> what_about_that_list(people\)) +comment(#The list now has 10000 elements.) +comment(#The index of the last element is 9999 (or -1\).) +comment(#Element #3 is None.) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.4) +comment(#-----------------------------) +keyword(for) ident(item) keyword(in) ident(mylist)operator(:) + keyword(pass) comment(# do something with item) +comment(#-----------------------------) +keyword(for) ident(user) keyword(in) ident(bad_users)operator(:) + ident(complain)operator(()ident(user)operator(\)) +comment(#-----------------------------) +keyword(import) ident(os) +keyword(for) operator(()ident(key)operator(,) ident(val)operator(\)) keyword(in) predefined(sorted)operator(()ident(os)operator(.)ident(environ)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(key)operator(,) ident(val)operator(\)) +comment(#-----------------------------) +keyword(for) ident(user) keyword(in) ident(all_users)operator(:) + ident(disk_space) operator(=) ident(get_usage)operator(()ident(user)operator(\)) comment(# find out how much disk space in use) + keyword(if) ident(disk_space) operator(>) ident(MAX_QUOTA)operator(:) comment(# if it's more than we want ...) + ident(complain)operator(()ident(user)operator(\)) comment(# ... then object vociferously) +comment(#-----------------------------) +keyword(import) ident(os) +keyword(for) ident(line) keyword(in) ident(os)operator(.)ident(popen)operator(()stringoperator(\))operator(:) + keyword(if) string keyword(in) ident(line)operator(:) + keyword(print) ident(line)operator(,) comment(# or print line[:-1]) + +comment(# or:) +keyword(print) stringoperator(.)ident(join)operator(()operator([)ident(line) keyword(for) ident(line) keyword(in) ident(os)operator(.)ident(popen)operator(()stringoperator(\)) + keyword(if) string keyword(in) ident(line)operator(])operator(\))operator(,) + +comment(#-----------------------------) +keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(for) ident(word) keyword(in) ident(line)operator(.)ident(split)operator(()operator(\))operator(:) comment(# Split on whitespace) + keyword(print) ident(word)operator([)operator(:)operator(:)operator(-)integer(1)operator(])operator(,) comment(# reverse word) + keyword(print) + +comment(# pre 2.3:) +keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(for) ident(word) keyword(in) ident(line)operator(.)ident(split)operator(()operator(\))operator(:) comment(# Split on whitespace) + ident(chars) operator(=) predefined(list)operator(()ident(word)operator(\)) comment(# Turn the string into a list of characters) + ident(chars)operator(.)ident(reverse)operator(()operator(\)) + keyword(print) stringoperator(.)ident(join)operator(()ident(chars)operator(\))operator(,) + keyword(print) +comment(#-----------------------------) +keyword(for) ident(item) keyword(in) ident(mylist)operator(:) + keyword(print) stringoperator(,) ident(item) +comment(#-----------------------------) +comment(# NOTE: you can't modify in place the way Perl does:) +comment(# data = [1, 2, 3]) +comment(# for elem in data:) +comment(# elem -= 1) +comment(#print data) +comment(#=>[1, 2, 3]) + +ident(data) operator(=) operator([)integer(1)operator(,) integer(2)operator(,) integer(3)operator(]) +ident(data) operator(=) operator([)ident(i)operator(-)integer(1) keyword(for) ident(i) keyword(in) ident(data)operator(]) +keyword(print) ident(data) +comment(#=>[0, 1, 2]) + +comment(# or) +keyword(for) ident(i)operator(,) ident(elem) keyword(in) predefined(enumerate)operator(()ident(data)operator(\))operator(:) + ident(data)operator([)ident(i)operator(]) operator(=) ident(elem) operator(-) integer(1) +comment(#-----------------------------) +comment(# NOTE: strings are immutable in Python so this doesn't translate well.) +ident(s) operator(=) ident(s)operator(.)ident(strip)operator(()operator(\)) +ident(data) operator(=) operator([)ident(s)operator(.)ident(strip)operator(()operator(\)) keyword(for) ident(s) keyword(in) ident(data)operator(]) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(mydict)operator(.)ident(items)operator(()operator(\))operator(:) + ident(mydict)operator([)ident(k)operator(]) operator(=) ident(v)operator(.)ident(strip)operator(()operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.5) +comment(#-----------------------------) +ident(fruits) operator(=) operator([)stringoperator(,) stringoperator(]) +keyword(for) ident(fruit) keyword(in) ident(fruits)operator(:) + keyword(print) ident(fruit)operator(,) string +comment(#=> Apple tastes good in a pie.) +comment(#=> Blackberry tastes good in a pie.) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(fruits)operator(\))operator(\))operator(:) + keyword(print) ident(fruits)operator([)ident(i)operator(])operator(,) string + +comment(# If you must explicitly index, use enumerate(\):) +keyword(for) ident(i)operator(,) ident(fruit) keyword(in) predefined(enumerate)operator(()ident(fruits)operator(\))operator(:) + keyword(print) stringoperator(%)operator(()ident(i)operator(+)integer(1)operator(,) ident(fruit)operator(\)) +comment(#-----------------------------) +ident(rogue_cats) operator(=) operator([)stringoperator(,) stringoperator(]) +ident(namedict) operator(=) operator({) stringoperator(:) ident(rogue_cats) operator(}) +keyword(for) ident(cat) keyword(in) ident(namedict)operator([)stringoperator(])operator(:) + keyword(print) ident(cat)operator(,) string +keyword(print) string +comment(#-----------------------------) +comment(# As noted before, if you need an index, use enumerate(\) and not this:) +keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(namedict)operator([)stringoperator(])operator(\))operator(\))operator(:) + keyword(print) ident(namedict)operator([)stringoperator(])operator([)ident(i)operator(])operator(,) string +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.6) +comment(#-----------------------------) +ident(uniq) operator(=) predefined(list)operator(()predefined(set)operator(()ident(mylist)operator(\))operator(\)) +comment(#-----------------------------) +comment(# See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259174) +comment(# for a more heavyweight version of a bag) +ident(seen) operator(=) operator({)operator(}) +keyword(for) ident(item) keyword(in) ident(mylist)operator(:) + ident(seen)operator([)ident(item)operator(]) operator(=) ident(seen)operator(.)ident(get)operator(()ident(item)operator(,) integer(0)operator(\)) operator(+) integer(1) + +ident(uniq) operator(=) ident(seen)operator(.)ident(keys)operator(()operator(\)) +comment(#-----------------------------) +ident(seen) operator(=) operator({)operator(}) +ident(uniq) operator(=) operator([)operator(]) +keyword(for) ident(item) keyword(in) ident(mylist)operator(:) + ident(count) operator(=) ident(seen)operator(.)ident(get)operator(()ident(item)operator(,) integer(0)operator(\)) + keyword(if) ident(count) operator(==) integer(0)operator(:) + ident(uniq)operator(.)ident(append)operator(()ident(item)operator(\)) + ident(seen)operator([)ident(item)operator(]) operator(=) ident(count) operator(+) integer(1) +comment(#-----------------------------) +comment(# generate a list of users logged in, removing duplicates) +keyword(import) ident(os) +ident(usernames) operator(=) operator([)ident(line)operator(.)ident(split)operator(()operator(\))operator([)integer(0)operator(]) keyword(for) ident(line) keyword(in) ident(os)operator(.)ident(popen)operator(()stringoperator(\))operator(]) +ident(uniq) operator(=) predefined(sorted)operator(()predefined(set)operator(()ident(usernames)operator(\))operator(\)) +keyword(print) stringoperator(,) stringoperator(.)ident(join)operator(()ident(uniq)operator(\)) + +comment(# DON'T DO THIS:) +keyword(import) ident(os) +ident(ucnt) operator(=) operator({)operator(}) +keyword(for) ident(line) keyword(in) ident(os)operator(.)ident(popen)operator(()stringoperator(\))operator(:) + ident(username) operator(=) ident(line)operator(.)ident(split)operator(()operator(\))operator([)integer(0)operator(]) comment(# Get the first word) + ident(ucnt)operator([)ident(username)operator(]) operator(=) ident(ucnt)operator(.)ident(get)operator(()ident(username)operator(,) integer(0)operator(\)) operator(+) integer(1) comment(# record the users' presence) + +comment(# extract and print unique keys) +ident(users) operator(=) ident(ucnt)operator(.)ident(keys)operator(()operator(\)) +ident(users)operator(.)ident(sort)operator(()operator(\)) +keyword(print) stringoperator(,) stringoperator(.)ident(join)operator(()ident(users)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.7) +comment(#-----------------------------) +comment(# assume a_list and b_list are already loaded) +ident(aonly) operator(=) operator([)ident(item) keyword(for) ident(item) keyword(in) ident(a_list) keyword(if) ident(item) keyword(not) keyword(in) ident(b_list)operator(]) + +comment(# A slightly more complex Pythonic version using sets - if you had a few) +comment(# lists, subtracting sets would be clearer than the listcomp version above) +ident(a_set) operator(=) predefined(set)operator(()ident(a_list)operator(\)) +ident(b_set) operator(=) predefined(set)operator(()ident(b_list)operator(\)) +ident(aonly) operator(=) predefined(list)operator(()ident(a_set) operator(-) ident(b_set)operator(\)) comment(# Elements in a_set but not in b_set) + +comment(# DON'T DO THIS.) +ident(seen) operator(=) operator({)operator(}) comment(# lookup table to test membership of B) +ident(aonly) operator(=) operator([)operator(]) comment(# answer) + +comment(# build lookup table) +keyword(for) ident(item) keyword(in) ident(b_list)operator(:) + ident(seen)operator([)ident(item)operator(]) operator(=) integer(1) + +comment(# find only elements in a_list and not in b_list) +keyword(for) ident(item) keyword(in) ident(a_list)operator(:) + keyword(if) keyword(not) ident(item) keyword(not) keyword(in) ident(seen)operator(:) + comment(# it's not in 'seen', so add to 'aonly') + ident(aonly)operator(.)ident(append)operator(()ident(item)operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS. There's lots of ways not to do it.) +ident(seen) operator(=) operator({)operator(}) comment(# lookup table) +ident(aonly) operator(=) operator([)operator(]) comment(# answer) + +comment(# build lookup table - unnecessary and poor Python style) +operator([)ident(seen)operator(.)ident(update)operator(()operator({)ident(x)operator(:) integer(1)operator(})operator(\)) keyword(for) ident(x) keyword(in) ident(b_list)operator(]) + +ident(aonly) operator(=) operator([)ident(item) keyword(for) ident(item) keyword(in) ident(a_list) keyword(if) ident(item) keyword(not) keyword(in) ident(seen)operator(]) + +comment(#-----------------------------) +ident(aonly) operator(=) predefined(list)operator(()predefined(set)operator(()ident(a_list)operator(\))operator(\)) + +comment(# DON'T DO THIS.) +ident(seen) operator(=) operator({)operator(}) +ident(aonly) operator(=) operator([)operator(]) +keyword(for) ident(item) keyword(in) ident(a_list)operator(:) + keyword(if) ident(item) keyword(not) keyword(in) ident(seen)operator(:) + ident(aonly)operator(.)ident(append)operator(()ident(item)operator(\)) + ident(seen)operator([)ident(item)operator(]) operator(=) integer(1) comment(# mark as seen) +comment(#-----------------------------) +ident(mydict)operator([)stringoperator(]) operator(=) integer(1) +ident(mydict)operator([)stringoperator(]) operator(=) integer(2) +comment(#-----------------------------) +ident(mydict)operator([)operator(()stringoperator(,) stringoperator(\))operator(]) operator(=) operator(()integer(1)operator(,)integer(2)operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(seen) operator(=) predefined(dict)operator(.)ident(fromkeys)operator(()ident(B)operator(.)ident(keys)operator(()operator(\))operator(\)) + +comment(# DON'T DO THIS pre-2.3:) +ident(seen) operator(=) operator({)operator(}) +keyword(for) ident(term) keyword(in) ident(B)operator(:) + ident(seen)operator([)ident(term)operator(]) operator(=) pre_constant(None) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(seen) operator(=) operator({)operator(}) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(B)operator(:) + ident(seen)operator([)ident(k)operator(]) operator(=) integer(1) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.8) +comment(#-----------------------------) +ident(a) operator(=) operator(()integer(1)operator(,) integer(3)operator(,) integer(5)operator(,) integer(6)operator(,) integer(7)operator(,) integer(8)operator(\)) +ident(b) operator(=) operator(()integer(2)operator(,) integer(3)operator(,) integer(5)operator(,) integer(7)operator(,) integer(9)operator(\)) + +ident(a_set) operator(=) predefined(set)operator(()ident(a)operator(\)) +ident(b_set) operator(=) predefined(set)operator(()ident(b)operator(\)) + +ident(union) operator(=) ident(a_set) operator(|) ident(b_set) comment(# or a_set.union(b_set\)) +ident(isect) operator(=) ident(a_set) operator(&) ident(b_set) comment(# or a_set.intersection(b_set\) ) +ident(diff) operator(=) ident(a_set) operator(^) ident(b_set) comment(# or a_set.symmetric_difference(b_set\)) + + +comment(# DON'T DO THIS:) +ident(union_list) operator(=) operator([)operator(])operator(;) ident(isect_list) operator(=) operator([)operator(])operator(;) ident(diff) operator(=) operator([)operator(]) +ident(union_dict) operator(=) operator({)operator(})operator(;) ident(isect_dict) operator(=) operator({)operator(}) +ident(count) operator(=) operator({)operator(}) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +keyword(for) ident(e) keyword(in) ident(a)operator(:) + ident(union_dict)operator([)ident(e)operator(]) operator(=) integer(1) + +keyword(for) ident(e) keyword(in) ident(b)operator(:) + keyword(if) ident(union_dict)operator(.)ident(has_key)operator(()ident(e)operator(\))operator(:) + ident(isect_dict)operator([)ident(e)operator(]) operator(=) integer(1) + ident(union_dict)operator([)ident(e)operator(]) operator(=) integer(1) + +ident(union_list) operator(=) ident(union_dict)operator(.)ident(keys)operator(()operator(\)) +ident(isect_list) operator(=) ident(isect_dict)operator(.)ident(keys)operator(()operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +keyword(for) ident(e) keyword(in) ident(a) operator(+) ident(b)operator(:) + keyword(if) ident(union)operator(.)ident(get)operator(()ident(e)operator(,) integer(0)operator(\)) operator(==) integer(0)operator(:) + ident(isect)operator([)ident(e)operator(]) operator(=) integer(1) + ident(union)operator([)ident(e)operator(]) operator(=) integer(1) + +ident(union) operator(=) ident(union)operator(.)ident(keys)operator(()operator(\)) +ident(isect) operator(=) ident(isect)operator(.)ident(keys)operator(()operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(count) operator(=) operator({)operator(}) +keyword(for) ident(e) keyword(in) ident(a) operator(+) ident(b)operator(:) + ident(count)operator([)ident(e)operator(]) operator(=) ident(count)operator(.)ident(get)operator(()ident(e)operator(,) integer(0)operator(\)) operator(+) integer(1) + +ident(union) operator(=) operator([)operator(])operator(;) ident(isect) operator(=) operator([)operator(])operator(;) ident(diff) operator(=) operator([)operator(]) + +keyword(for) ident(e) keyword(in) ident(count)operator(.)ident(keys)operator(()operator(\))operator(:) + ident(union)operator(.)ident(append)operator(()ident(e)operator(\)) + keyword(if) ident(count)operator([)ident(e)operator(]) operator(==) integer(2)operator(:) + ident(isect)operator(.)ident(append)operator(()ident(e)operator(\)) + keyword(else)operator(:) + ident(diff)operator(.)ident(append)operator(()ident(e)operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(isect) operator(=) operator([)operator(])operator(;) ident(diff) operator(=) operator([)operator(])operator(;) ident(union) operator(=) operator([)operator(]) +ident(count) operator(=) operator({)operator(}) +keyword(for) ident(e) keyword(in) ident(a) operator(+) ident(b)operator(:) + ident(count)operator([)ident(e)operator(]) operator(=) ident(count)operator(.)ident(get)operator(()ident(e)operator(,) integer(0)operator(\)) operator(+) integer(1) + +keyword(for) ident(e)operator(,) ident(num) keyword(in) ident(count)operator(.)ident(items)operator(()operator(\))operator(:) + ident(union)operator(.)ident(append)operator(()ident(e)operator(\)) + operator([)pre_constant(None)operator(,) ident(diff)operator(,) ident(isect)operator(])operator([)ident(num)operator(])operator(.)ident(append)operator(()ident(e)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.9) +comment(#-----------------------------) +comment(# "append" for a single term and) +comment(# "extend" for many terms) +ident(mylist1)operator(.)ident(extend)operator(()ident(mylist2)operator(\)) +comment(#-----------------------------) +ident(mylist1) operator(=) ident(mylist1) operator(+) ident(mylist2) +ident(mylist1) operator(+=) ident(mylist2) +comment(#-----------------------------) +ident(members) operator(=) operator([)stringoperator(,) stringoperator(]) +ident(initiates) operator(=) operator([)stringoperator(,) stringoperator(]) +ident(members)operator(.)ident(extend)operator(()ident(initiates)operator(\)) +comment(# members is now ["Time", "Flies", "An", "Arrow"]) +comment(#-----------------------------) +ident(members)operator([)integer(2)operator(:)operator(]) operator(=) operator([)stringoperator(]) operator(+) ident(initiates) +keyword(print) stringoperator(.)ident(join)operator(()ident(members)operator(\)) +ident(members)operator([)operator(:)integer(1)operator(]) operator(=) operator([)stringoperator(]) comment(# or members[1] = "Fruit") +ident(members)operator([)operator(-)integer(2)operator(:)operator(]) operator(=) operator([)stringoperator(,) stringoperator(]) +keyword(print) stringoperator(.)ident(join)operator(()ident(members)operator(\)) +comment(#-----------------------------) +comment(#=> Time Flies Like An Arrow) +comment(#=> Fruit Flies Like A Banana) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.10) +comment(#-----------------------------) +comment(# reverse mylist into revlist) + +ident(revlist) operator(=) ident(mylist)operator([)operator(:)operator(:)operator(-)integer(1)operator(]) + +comment(# or) +ident(revlist) operator(=) predefined(list)operator(()predefined(reversed)operator(()ident(mylist)operator(\))operator(\)) + +comment(# or pre-2.3) +ident(revlist) operator(=) ident(mylist)operator([)operator(:)operator(]) comment(# shallow copy) +ident(revlist)operator(.)ident(reverse)operator(()operator(\)) +comment(#-----------------------------) +keyword(for) ident(elem) keyword(in) predefined(reversed)operator(()ident(mylist)operator(\))operator(:) + keyword(pass) comment(# do something with elem) + +comment(# or) +keyword(for) ident(elem) keyword(in) ident(mylist)operator([)operator(:)operator(:)operator(-)integer(1)operator(])operator(:) + keyword(pass) comment(# do something with elem) + +comment(# if you need the index and the list won't take too much memory:) +keyword(for) ident(i)operator(,) ident(elem) keyword(in) predefined(reversed)operator(()predefined(list)operator(()predefined(enumerate)operator(()ident(mylist)operator(\))operator(\))operator(\))operator(:) + keyword(pass) + +comment(# If you absolutely must explicitly index:) +keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(mylist)operator(\))operator(-)integer(1)operator(,) operator(-)integer(1)operator(,) operator(-)integer(1)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +ident(descending) operator(=) predefined(sorted)operator(()ident(users)operator(,) ident(reverse)operator(=)pre_constant(True)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.11) +comment(#-----------------------------) +comment(# remove n elements from the front of mylist) +ident(mylist)operator([)operator(:)ident(n)operator(]) operator(=) operator([)operator(]) comment(# or del mylist[:n]) + +comment(# remove n elements from front of mylist, saving them into front) +ident(front)operator(,) ident(mylist)operator([)operator(:)ident(n)operator(]) operator(=) ident(mylist)operator([)operator(:)ident(n)operator(])operator(,) operator([)operator(]) + +comment(# remove 1 element from the front of mylist, saving it in front:) +ident(front) operator(=) ident(mylist)operator(.)ident(pop)operator(()integer(0)operator(\)) + +comment(# remove n elements from the end of mylist) +ident(mylist)operator([)operator(-)ident(n)operator(:)operator(]) operator(=) operator([)operator(]) comment(# or del mylist[-n:]) + +comment(# remove n elements from the end of mylist, saving them in end) +ident(end)operator(,) ident(mylist)operator([)operator(-)ident(n)operator(:)operator(]) operator(=) ident(mylist)operator([)operator(-)ident(n)operator(:)operator(])operator(,) operator([)operator(]) + +comment(# remove 1 element from the end of mylist, saving it in end:) +ident(end) operator(=) ident(mylist)operator(.)ident(pop)operator(()operator(\)) + +comment(#-----------------------------) +keyword(def) ident(shift2)operator(()ident(terms)operator(\))operator(:) + ident(front) operator(=) ident(terms)operator([)operator(:)integer(2)operator(]) + ident(terms)operator([)operator(:)integer(2)operator(]) operator(=) operator([)operator(]) + keyword(return) ident(front) + +keyword(def) ident(pop2)operator(()ident(terms)operator(\))operator(:) + ident(back) operator(=) ident(terms)operator([)operator(-)integer(2)operator(:)operator(]) + ident(terms)operator([)operator(-)integer(2)operator(:)operator(]) operator(=) operator([)operator(]) + keyword(return) ident(back) +comment(#-----------------------------) +ident(friends) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +ident(this)operator(,) ident(that) operator(=) ident(shift2)operator(()ident(friends)operator(\)) +comment(# 'this' contains Peter, 'that' has Paul, and) +comment(# 'friends' has Mary, Jim, and Tim) + +ident(beverages) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +ident(pair) operator(=) ident(pop2)operator(()ident(beverages)operator(\)) +comment(# pair[0] contains Sprite, pair[1] has Fresca,) +comment(# and 'beverages' has (Dew, Jolt, Cola\)) + +comment(# In general you probably shouldn't do things that way because it's ) +comment(# not clear from these calls that the lists are modified.) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.12) +keyword(for) ident(item) keyword(in) ident(mylist)operator(:) + keyword(if) ident(criterion)operator(:) + keyword(pass) comment(# do something with matched item) + keyword(break) +keyword(else)operator(:) + keyword(pass) comment(# unfound) +comment(#-----------------------------) +keyword(for) ident(idx)operator(,) ident(elem) keyword(in) predefined(enumerate)operator(()ident(mylist)operator(\))operator(:) + keyword(if) ident(criterion)operator(:) + keyword(pass) comment(# do something with elem found at mylist[idx]) + keyword(break) +keyword(else)operator(:) + keyword(pass) comment(## unfound) +comment(#-----------------------------) +comment(# Assuming employees are sorted high->low by wage.) +keyword(for) ident(employee) keyword(in) ident(employees)operator(:) + keyword(if) ident(employee)operator(.)ident(category) operator(==) stringoperator(:) + ident(highest_engineer) operator(=) ident(employee) + keyword(break) + +keyword(print) stringoperator(,) ident(highest_engineer)operator(.)ident(name) +comment(#-----------------------------) +comment(# If you need the index, use enumerate:) +keyword(for) ident(i)operator(,) ident(employee) keyword(in) predefined(enumerate)operator(()ident(employees)operator(\))operator(:) + keyword(if) ident(employee)operator(.)ident(category) operator(==) stringoperator(:) + ident(highest_engineer) operator(=) ident(employee) + keyword(break) +keyword(print) string operator(%) operator(()ident(i)operator(,) ident(highest_engineer)operator(.)ident(name)operator(\)) + + +comment(# The following is rarely appropriate:) +keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(mylist)operator(\))operator(\))operator(:) + keyword(if) ident(criterion)operator(:) + keyword(pass) comment(# do something) + keyword(break) +keyword(else)operator(:) + keyword(pass) comment(## not found) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_4.13) +ident(matching) operator(=) operator([)ident(term) keyword(for) ident(term) keyword(in) ident(mylist) keyword(if) ident(test)operator(()ident(term)operator(\))operator(]) +comment(#-----------------------------) +ident(matching) operator(=) operator([)operator(]) +keyword(for) ident(term) keyword(in) ident(mylist)operator(:) + keyword(if) ident(test)operator(()ident(term)operator(\))operator(:) + ident(matching)operator(.)ident(append)operator(()ident(term)operator(\)) +comment(#-----------------------------) +ident(bigs) operator(=) operator([)ident(num) keyword(for) ident(num) keyword(in) ident(nums) keyword(if) ident(num) operator(>) integer(1000000)operator(]) +ident(pigs) operator(=) operator([)ident(user) keyword(for) operator(()ident(user)operator(,) ident(val)operator(\)) keyword(in) ident(users)operator(.)ident(items)operator(()operator(\)) keyword(if) ident(val) operator(>) float(1)ident(e7)operator(]) +comment(#-----------------------------) +keyword(import) ident(os) +ident(matching) operator(=) operator([)ident(line) keyword(for) ident(line) keyword(in) ident(os)operator(.)ident(popen)operator(()stringoperator(\)) + keyword(if) ident(line)operator(.)ident(startswith)operator(()stringoperator(\))operator(]) +comment(#-----------------------------) +ident(engineers) operator(=) operator([)ident(employee) keyword(for) ident(employee) keyword(in) ident(employees) + keyword(if) ident(employee)operator(.)ident(position) operator(==) stringoperator(]) +comment(#-----------------------------) +ident(secondary_assistance) operator(=) operator([)ident(applicant) keyword(for) ident(applicant) keyword(in) ident(applicants) + keyword(if) integer(26000) operator(<=) ident(applicant)operator(.)ident(income) operator(<) integer(30000)operator(]) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.14) +ident(sorted_list) operator(=) predefined(sorted)operator(()ident(unsorted_list)operator(\)) +comment(#-----------------------------) +comment(# pids is an unsorted list of process IDs) +keyword(import) ident(os)operator(,) ident(signal)operator(,) ident(time) +keyword(for) ident(pid) keyword(in) predefined(sorted)operator(()ident(pids)operator(\))operator(:) + keyword(print) ident(pid) + +ident(pid) operator(=) predefined(raw_input)operator(()stringoperator(\)) +keyword(try)operator(:) + ident(pid) operator(=) predefined(int)operator(()ident(pid)operator(\)) +keyword(except) exception(ValueError)operator(:) + keyword(raise) exception(SystemExit)operator(()stringoperator(\)) +ident(os)operator(.)ident(kill)operator(()ident(pid)operator(,) ident(signal)operator(.)ident(SIGTERM)operator(\)) +ident(time)operator(.)ident(sleep)operator(()integer(2)operator(\)) +keyword(try)operator(:) + ident(os)operator(.)ident(kill)operator(()ident(pid)operator(,) ident(signal)operator(.)ident(SIGKILL)operator(\)) +keyword(except) exception(OSError)operator(,) ident(err)operator(:) + keyword(if) ident(err)operator(.)ident(errno) operator(!=) integer(3)operator(:) comment(# was it already killed?) + keyword(raise) +comment(#-----------------------------) +ident(descending) operator(=) predefined(sorted)operator(()ident(unsorted_list)operator(,) ident(reverse)operator(=)pre_constant(True)operator(\)) +comment(#-----------------------------) +ident(allnums) operator(=) operator([)integer(4)operator(,) integer(19)operator(,) integer(8)operator(,) integer(3)operator(]) +ident(allnums)operator(.)ident(sort)operator(()ident(reverse)operator(=)pre_constant(True)operator(\)) comment(# inplace) +comment(#-----------------------------) +comment(# pre 2.3) +ident(allnums)operator(.)ident(sort)operator(()operator(\)) comment(# inplace) +ident(allnums)operator(.)ident(reverse)operator(()operator(\)) comment(# inplace) +comment(#or) +ident(allnums) operator(=) predefined(sorted)operator(()ident(allnums)operator(,) ident(reverse)operator(=)pre_constant(True)operator(\)) comment(# reallocating) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.15) +ident(ordered) operator(=) predefined(sorted)operator(()ident(unordered)operator(,) predefined(cmp)operator(=)ident(compare)operator(\)) +comment(#-----------------------------) +ident(ordered) operator(=) predefined(sorted)operator(()ident(unordered)operator(,) ident(key)operator(=)ident(compute)operator(\)) + +comment(# ...which is somewhat equivalent to: ) +ident(precomputed) operator(=) operator([)operator(()ident(compute)operator(()ident(x)operator(\))operator(,) ident(x)operator(\)) keyword(for) ident(x) keyword(in) ident(unordered)operator(]) +ident(precomputed)operator(.)ident(sort)operator(()keyword(lambda) ident(a)operator(,) ident(b)operator(:) predefined(cmp)operator(()ident(a)operator([)integer(0)operator(])operator(,) ident(b)operator([)integer(0)operator(])operator(\))operator(\)) +ident(ordered) operator(=) operator([)ident(v) keyword(for) ident(k)operator(,)ident(v) keyword(in) ident(precomputed)operator(.)ident(items)operator(()operator(\))operator(]) +comment(#-----------------------------) +comment(# DON'T DO THIS.) +keyword(def) ident(functional_sort)operator(()ident(mylist)operator(,) ident(function)operator(\))operator(:) + ident(mylist)operator(.)ident(sort)operator(()ident(function)operator(\)) + keyword(return) ident(mylist) + +ident(ordered) operator(=) operator([)ident(v) keyword(for) ident(k)operator(,)ident(v) keyword(in) ident(functional_sort)operator(()operator([)operator(()ident(compute)operator(()ident(x)operator(\))operator(,) ident(x)operator(\)) keyword(for) ident(x) keyword(in) ident(unordered)operator(])operator(,) + keyword(lambda) ident(a)operator(,) ident(b)operator(:) predefined(cmp)operator(()ident(a)operator([)integer(0)operator(])operator(,) ident(b)operator([)integer(0)operator(])operator(\))operator(\))operator(]) +comment(#-----------------------------) +ident(ordered) operator(=) predefined(sorted)operator(()ident(employees)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) ident(x)operator(.)ident(name)operator(\)) +comment(#-----------------------------) +keyword(for) ident(employee) keyword(in) predefined(sorted)operator(()ident(employees)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) ident(x)operator(.)ident(name)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(employee)operator(.)ident(name)operator(,) ident(employee)operator(.)ident(salary)operator(\)) +comment(#-----------------------------) +ident(sorted_employees) operator(=) predefined(sorted)operator(()ident(employees)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) ident(x)operator(.)ident(name)operator(\))operator(:) +keyword(for) ident(employee) keyword(in) ident(sorted_employees)operator(:) + keyword(print) string operator(%) operator(()ident(employee)operator(.)ident(name)operator(,) ident(employee)operator(.)ident(salary)operator(\)) + +comment(# load bonus) +keyword(for) ident(employee) keyword(in) ident(sorted_employees)operator(:) + keyword(if) ident(bonus)operator(()ident(employee)operator(.)ident(ssn)operator(\))operator(:) + keyword(print) ident(employee)operator(.)ident(name)operator(,) string +comment(#-----------------------------) +ident(sorted_employees) operator(=) predefined(sorted)operator(()ident(employees)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) operator(()ident(x)operator(.)ident(name)operator(,) ident(x)operator(.)ident(age)operator(\))operator(\))operator(:) +comment(#-----------------------------) +comment(# NOTE: Python should allow access to the pwd fields by name) +comment(# as well as by position.) +keyword(import) ident(pwd) +comment(# fetch all users) +ident(users) operator(=) ident(pwd)operator(.)ident(getpwall)operator(()operator(\)) +keyword(for) ident(user) keyword(in) predefined(sorted)operator(()ident(users)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) ident(x)operator([)integer(0)operator(])operator(\))operator(:) + keyword(print) ident(user)operator([)integer(0)operator(]) +comment(#-----------------------------) +ident(sorted_list) operator(=) predefined(sorted)operator(()ident(names)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) ident(x)operator([)operator(:)integer(1)operator(])operator(\)) +comment(#-----------------------------) +ident(sorted_list) operator(=) predefined(sorted)operator(()ident(strings)operator(,) ident(key)operator(=)predefined(len)operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS.) +ident(temp) operator(=) operator([)operator(()predefined(len)operator(()ident(s)operator(\))operator(,) ident(s)operator(\)) keyword(for) ident(s) keyword(in) ident(strings)operator(]) +ident(temp)operator(.)ident(sort)operator(()keyword(lambda) ident(a)operator(,) ident(b)operator(:) predefined(cmp)operator(()ident(a)operator([)integer(0)operator(])operator(,) ident(b)operator([)integer(0)operator(])operator(\))operator(\)) +ident(sorted_list) operator(=) operator([)ident(x)operator([)integer(1)operator(]) keyword(for) ident(x) keyword(in) ident(temp)operator(]) +comment(#-----------------------------) +comment(# DON'T DO THIS.) +keyword(def) ident(functional_sort)operator(()ident(mylist)operator(,) ident(function)operator(\))operator(:) + ident(mylist)operator(.)ident(sort)operator(()ident(function)operator(\)) + keyword(return) ident(mylist) + +ident(sorted_fields) operator(=) operator([)ident(v) keyword(for) ident(k)operator(,)ident(v) keyword(in) ident(functional_sort)operator(() + operator([)operator(()predefined(int)operator(()ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(x)operator(\))operator(.)ident(group)operator(()integer(1)operator(\))operator(\))operator(,) ident(x)operator(\)) keyword(for) ident(x) keyword(in) ident(fields)operator(])operator(,) + keyword(lambda) ident(a)operator(,) ident(b)operator(:) predefined(cmp)operator(()ident(a)operator([)integer(0)operator(])operator(,) ident(b)operator([)integer(0)operator(])operator(\))operator(\))operator(]) +comment(#-----------------------------) +ident(entries) operator(=) operator([)ident(line)operator([)operator(:)operator(-)integer(1)operator(])operator(.)ident(split)operator(()operator(\)) keyword(for) ident(line) keyword(in) predefined(open)operator(()stringoperator(\))operator(]) + +keyword(for) ident(entry) keyword(in) predefined(sorted)operator(()ident(entries)operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:) operator(()ident(x)operator([)integer(3)operator(])operator(,) ident(x)operator([)integer(2)operator(])operator(,) ident(x)operator([)integer(0)operator(])operator(\))operator(\))operator(:) + keyword(print) ident(entry) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.16) +comment(#-----------------------------) +keyword(import) ident(itertools) +keyword(for) ident(process) keyword(in) ident(itertools)operator(.)ident(cycle)operator(()operator([)integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(])operator(\))operator(:) + keyword(print) stringoperator(,) ident(process) + ident(time)operator(.)ident(sleep)operator(()integer(1)operator(\)) + +comment(# pre 2.3:) +keyword(import) ident(time) +keyword(class) ident(Circular)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(assert) predefined(len)operator(()ident(data)operator(\)) operator(>=) integer(1)operator(,) string + pre_constant(self)operator(.)ident(data) operator(=) ident(data) + + keyword(def) ident(__iter__)operator(()pre_constant(self)operator(\))operator(:) + keyword(while) pre_constant(True)operator(:) + keyword(for) ident(elem) keyword(in) pre_constant(self)operator(.)ident(data)operator(:) + keyword(yield) ident(elem) + +ident(circular) operator(=) ident(Circular)operator(()operator([)integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(])operator(\)) + +keyword(for) ident(process) keyword(in) ident(circular)operator(:) + keyword(print) stringoperator(,) ident(process) + ident(time)operator(.)ident(sleep)operator(()integer(1)operator(\)) + +comment(# DON'T DO THIS. All those pops and appends mean that the list needs to be ) +comment(# constantly reallocated. This is rather bad if your list is large:) +keyword(import) ident(time) +keyword(class) ident(Circular)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(assert) predefined(len)operator(()ident(data)operator(\)) operator(>=) integer(1)operator(,) string + pre_constant(self)operator(.)ident(data) operator(=) ident(data) + + keyword(def) predefined(next)operator(()pre_constant(self)operator(\))operator(:) + ident(head) operator(=) pre_constant(self)operator(.)ident(data)operator(.)ident(pop)operator(()integer(0)operator(\)) + pre_constant(self)operator(.)ident(data)operator(.)ident(append)operator(()ident(head)operator(\)) + keyword(return) ident(head) + +ident(circular) operator(=) ident(Circular)operator(()operator([)integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(])operator(\)) +keyword(while) pre_constant(True)operator(:) + ident(process) operator(=) ident(circular)operator(.)ident(next)operator(()operator(\)) + keyword(print) stringoperator(,) ident(process) + ident(time)operator(.)ident(sleep)operator(()integer(1)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.17) +comment(#-----------------------------) +comment(# generate a random permutation of mylist in place) +keyword(import) ident(random) +ident(random)operator(.)ident(shuffle)operator(()ident(mylist)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.18) +comment(#-----------------------------) +keyword(import) ident(sys) + +keyword(def) ident(make_columns)operator(()ident(mylist)operator(,) ident(screen_width)operator(=)integer(78)operator(\))operator(:) + keyword(if) ident(mylist)operator(:) + ident(maxlen) operator(=) predefined(max)operator(()operator([)predefined(len)operator(()ident(elem)operator(\)) keyword(for) ident(elem) keyword(in) ident(mylist)operator(])operator(\)) + ident(maxlen) operator(+=) integer(1) comment(# to make extra space) + + ident(cols) operator(=) predefined(max)operator(()integer(1)operator(,) ident(screen_width)operator(/)ident(maxlen)operator(\)) + ident(rows) operator(=) integer(1) operator(+) predefined(len)operator(()ident(mylist)operator(\))operator(/)ident(cols) + + comment(# pre-create mask for faster computation) + ident(mask) operator(=) string operator(%) operator(()ident(maxlen)operator(-)integer(1)operator(\)) + + keyword(for) ident(n) keyword(in) predefined(range)operator(()ident(rows)operator(\))operator(:) + ident(row) operator(=) operator([)ident(mask)operator(%)ident(elem) + keyword(for) ident(elem) keyword(in) ident(mylist)operator([)ident(n)operator(:)operator(:)ident(rows)operator(])operator(]) + keyword(yield) stringoperator(.)ident(join)operator(()ident(row)operator(\))operator(.)ident(rstrip)operator(()operator(\)) + +keyword(for) ident(row) keyword(in) ident(make_columns)operator(()ident(sys)operator(.)ident(stdin)operator(.)ident(readlines)operator(()operator(\))operator(,) ident(screen_width)operator(=)integer(50)operator(\))operator(:) + keyword(print) ident(row) + + +comment(# A more literal translation) +keyword(import) ident(sys) + +comment(# subroutine to check whether at last item on line) +keyword(def) ident(EOL)operator(()ident(item)operator(\))operator(:) + keyword(return) operator(()ident(item)operator(+)integer(1)operator(\)) operator(%) ident(cols) operator(==) integer(0) + +comment(# Might not be portable to non-linux systems) +keyword(def) ident(getwinsize)operator(()operator(\))operator(:) + comment(# Use the curses module if installed) + keyword(try)operator(:) + keyword(import) ident(curses) + ident(stdscr) operator(=) ident(curses)operator(.)ident(initscr)operator(()operator(\)) + ident(rows)operator(,) ident(cols) operator(=) ident(stdscr)operator(.)ident(getmaxyx)operator(()operator(\)) + keyword(return) ident(cols) + keyword(except) exception(ImportError)operator(:) + keyword(pass) + + comment(# Nope, so deal with ioctl directly. What value for TIOCGWINSZ?) + keyword(try)operator(:) + keyword(import) ident(termios) + ident(TIOCGWINSZ) operator(=) ident(termios)operator(.)ident(TIOCGWINSZ) + keyword(except) exception(ImportError)operator(:) + ident(TIOCGWINSZ) operator(=) hex(0x40087468) comment(# This is Linux specific) + + keyword(import) ident(struct)operator(,) ident(fcntl) + ident(s) operator(=) ident(struct)operator(.)ident(pack)operator(()stringoperator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(\)) + keyword(try)operator(:) + ident(x) operator(=) ident(fcntl)operator(.)ident(ioctl)operator(()ident(sys)operator(.)ident(stdout)operator(.)ident(fileno)operator(()operator(\))operator(,) ident(TIOCGWINSZ)operator(,) ident(s)operator(\)) + keyword(except) exception(IOError)operator(:) + keyword(return) integer(80) + ident(rows)operator(,) ident(cols) operator(=) ident(struct)operator(.)ident(unpack)operator(()stringoperator(,) ident(x)operator(\))operator([)operator(:)integer(2)operator(]) + keyword(return) ident(cols) + +ident(cols) operator(=) ident(getwinsize)operator(()operator(\)) + +ident(data) operator(=) operator([)ident(s)operator(.)ident(rstrip)operator(()operator(\)) keyword(for) ident(s) keyword(in) ident(sys)operator(.)ident(stdin)operator(.)ident(readlines)operator(()operator(\))operator(]) +keyword(if) keyword(not) ident(data)operator(:) + ident(maxlen) operator(=) integer(1) +keyword(else)operator(:) + ident(maxlen) operator(=) predefined(max)operator(()predefined(map)operator(()predefined(len)operator(,) ident(data)operator(\))operator(\)) + +ident(maxlen) operator(+=) integer(1) comment(# to make extra space) + +comment(# determine boundaries of screen) +ident(cols) operator(=) operator(()ident(cols) operator(/) ident(maxlen)operator(\)) keyword(or) integer(1) +ident(rows) operator(=) operator(()predefined(len)operator(()ident(data)operator(\))operator(+)ident(cols)operator(\)) operator(/) ident(cols) + +comment(# pre-create mask for faster computation) +ident(mask) operator(=) string operator(%) operator(()ident(maxlen)operator(-)integer(1)operator(\)) + +comment(# now process each item, picking out proper piece for this position) +keyword(for) ident(item) keyword(in) predefined(range)operator(()ident(rows) operator(*) ident(cols)operator(\))operator(:) + ident(target) operator(=) operator(()ident(item) operator(%) ident(cols)operator(\)) operator(*) ident(rows) operator(+) operator(()ident(item)operator(/)ident(cols)operator(\)) + keyword(if) ident(target) operator(<) predefined(len)operator(()ident(data)operator(\))operator(:) + ident(piece) operator(=) ident(mask) operator(%) ident(data)operator([)ident(target)operator(]) + keyword(else)operator(:) + ident(piece) operator(=) ident(mask) operator(%) string + keyword(if) ident(EOL)operator(()ident(item)operator(\))operator(:) + ident(piece) operator(=) ident(piece)operator(.)ident(rstrip)operator(()operator(\)) comment(# don't blank-pad to EOL) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(piece)operator(\)) + keyword(if) ident(EOL)operator(()ident(item)operator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()stringoperator(\)) + +keyword(if) ident(EOL)operator(()ident(item)operator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()stringoperator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_4.19) +comment(#-----------------------------) +keyword(def) ident(factorial)operator(()ident(n)operator(\))operator(:) + ident(s) operator(=) integer(1) + keyword(while) ident(n)operator(:) + ident(s) operator(*=) ident(n) + ident(n) operator(-=) integer(1) + keyword(return) ident(s) +comment(#-----------------------------) +keyword(def) ident(permute)operator(()ident(alist)operator(,) ident(blist)operator(=)operator([)operator(])operator(\))operator(:) + keyword(if) keyword(not) ident(alist)operator(:) + keyword(yield) ident(blist) + keyword(for) ident(i)operator(,) ident(elem) keyword(in) predefined(enumerate)operator(()ident(alist)operator(\))operator(:) + keyword(for) ident(elem) keyword(in) ident(permute)operator(()ident(alist)operator([)operator(:)ident(i)operator(]) operator(+) ident(alist)operator([)ident(i)operator(+)integer(1)operator(:)operator(])operator(,) ident(blist) operator(+) operator([)ident(elem)operator(])operator(\))operator(:) + keyword(yield) ident(elem) + +keyword(for) ident(permutation) keyword(in) ident(permute)operator(()predefined(range)operator(()integer(4)operator(\))operator(\))operator(:) + keyword(print) ident(permutation) +comment(#-----------------------------) +comment(# DON'T DO THIS) +keyword(import) ident(fileinput) + +comment(# Slightly modified from) +comment(# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66463) +keyword(def) ident(print_list)operator(()ident(alist)operator(,) ident(blist)operator(=)operator([)operator(])operator(\))operator(:) + keyword(if) keyword(not) ident(alist)operator(:) + keyword(print) stringoperator(.)ident(join)operator(()ident(blist)operator(\)) + keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(alist)operator(\))operator(\))operator(:) + ident(blist)operator(.)ident(append)operator(()ident(alist)operator(.)ident(pop)operator(()ident(i)operator(\))operator(\)) + ident(print_list)operator(()ident(alist)operator(,) ident(blist)operator(\)) + ident(alist)operator(.)ident(insert)operator(()ident(i)operator(,) ident(blist)operator(.)ident(pop)operator(()operator(\))operator(\)) + +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(words) operator(=) ident(line)operator(.)ident(split)operator(()operator(\)) + ident(print_list)operator(()ident(words)operator(\)) +comment(#-----------------------------) +keyword(class) ident(FactorialMemo)operator(()predefined(list)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(append)operator(()integer(1)operator(\)) + + keyword(def) ident(__call__)operator(()pre_constant(self)operator(,) ident(n)operator(\))operator(:) + keyword(try)operator(:) + keyword(return) pre_constant(self)operator([)ident(n)operator(]) + keyword(except) exception(IndexError)operator(:) + ident(ret) operator(=) ident(n) operator(*) pre_constant(self)operator(()ident(n)operator(-)integer(1)operator(\)) + pre_constant(self)operator(.)ident(append)operator(()ident(ret)operator(\)) + keyword(return) ident(ret) + +ident(factorial) operator(=) ident(FactorialMemo)operator(()operator(\)) + +keyword(import) ident(sys) +keyword(import) ident(time) +ident(sys)operator(.)ident(setrecursionlimit)operator(()integer(10000)operator(\)) + +ident(start) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) +ident(factorial)operator(()integer(2000)operator(\)) +ident(f1) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) operator(-) ident(start) +ident(factorial)operator(()integer(2100)operator(\)) comment(# First 2000 values are cached already) +ident(f2) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) operator(-) ident(f1) operator(-) ident(start) +keyword(print) stringoperator(,) ident(f1) +keyword(print) stringoperator(,) ident(f2) +comment(#-----------------------------) + +keyword(class) ident(MemoizedPermutations)operator(()predefined(list)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(alist)operator(\))operator(:) + pre_constant(self)operator(.)ident(permute)operator(()ident(alist)operator(,) operator([)operator(])operator(\)) + + keyword(def) ident(permute)operator(()pre_constant(self)operator(,) ident(alist)operator(,) ident(blist)operator(\))operator(:) + keyword(if) keyword(not) ident(alist)operator(:) + pre_constant(self)operator(.)ident(append)operator(()ident(blist)operator(\)) + keyword(for) ident(i)operator(,) ident(elem) keyword(in) predefined(enumerate)operator(()ident(alist)operator(\))operator(:) + pre_constant(self)operator(.)ident(permute)operator(()ident(alist)operator([)operator(:)ident(i)operator(]) operator(+) ident(alist)operator([)ident(i)operator(+)integer(1)operator(:)operator(])operator(,) ident(blist) operator(+) operator([)ident(elem)operator(])operator(\)) + + keyword(def) ident(__call__)operator(()pre_constant(self)operator(,) ident(seq)operator(,) ident(idx)operator(\))operator(:) + keyword(return) operator([)ident(seq)operator([)ident(n)operator(]) keyword(for) ident(n) keyword(in) pre_constant(self)operator([)ident(idx)operator(])operator(]) + + +ident(p5) operator(=) ident(MemoizedPermutations)operator(()predefined(range)operator(()integer(5)operator(\))operator(\)) + +ident(words) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +keyword(print) ident(p5)operator(()ident(words)operator(,) integer(17)operator(\)) +keyword(print) ident(p5)operator(()ident(words)operator(,) integer(81)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_5.0) +comment(#-----------------------------) +comment(# dictionaries) +ident(age) operator(=) operator({)stringoperator(:) integer(24)operator(,) + stringoperator(:) integer(24)operator(,) + stringoperator(:) integer(17)operator(}) +comment(#-----------------------------) +ident(age) operator(=) operator({)operator(}) +ident(age)operator([)stringoperator(]) operator(=) integer(24) +ident(age)operator([)stringoperator(]) operator(=) integer(25) +ident(age)operator([)stringoperator(]) operator(=) integer(17) +comment(#-----------------------------) +ident(food_color) operator(=) operator({)stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) string + operator(}) +comment(#-----------------------------) +comment(# NOTE: keys must be quoted in Python) + + +comment(# @@PLEAC@@_5.1) +ident(mydict)operator([)ident(key)operator(]) operator(=) ident(value) +comment(#-----------------------------) +comment(# food_color defined per the introduction) +ident(food_color)operator([)stringoperator(]) operator(=) string +keyword(print) string +keyword(for) ident(food) keyword(in) ident(food_color)operator(:) + keyword(print) ident(food) + +comment(#=> Known foods:) +comment(#=> Raspberry) +comment(#=> Carrot) +comment(#=> Lemon) +comment(#=> Apple) +comment(#=> Banana) +comment(#-----------------------------) + +comment(# @@PLEAC@@_5.2) +comment(# does mydict have a value for key?) +keyword(if) ident(key) keyword(in) ident(mydict)operator(:) + keyword(pass) comment(# it exists) +keyword(else)operator(:) + keyword(pass) comment(# it doesn't) + +comment(#-----------------------------) +comment(# food_color per the introduction) +keyword(for) ident(name) keyword(in) operator(()stringoperator(,) stringoperator(\))operator(:) + keyword(if) ident(name) keyword(in) ident(food_color)operator(:) + keyword(print) ident(name)operator(,) string + keyword(else)operator(:) + keyword(print) ident(name)operator(,) string + +comment(#=> Banana is a food.) +comment(#=> Martini is a drink.) +comment(#-----------------------------) +ident(age) operator(=) operator({)operator(}) +ident(age)operator([)stringoperator(]) operator(=) integer(3) +ident(age)operator([)stringoperator(]) operator(=) integer(0) +ident(age)operator([)stringoperator(]) operator(=) pre_constant(None) + +keyword(for) ident(thing) keyword(in) operator(()stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(\))operator(:) + keyword(print) operator(()stringoperator(%)ident(thing)operator(\))operator(,) + keyword(if) ident(thing) keyword(in) ident(age)operator(:) + keyword(print) stringoperator(,) + keyword(if) ident(age)operator([)ident(thing)operator(]) keyword(is) keyword(not) pre_constant(None)operator(:) + keyword(print) stringoperator(,) + keyword(if) ident(age)operator([)ident(thing)operator(])operator(:) + keyword(print) stringoperator(,) + keyword(print) +comment(#=> Toddler: Exists Defined True) +comment(#=> Unborn: Exists Defined) +comment(#=> Phantasm: Exists) +comment(#=> Relic:) +comment(#-----------------------------) +comment(# Get file sizes for the requested filenames) +keyword(import) ident(fileinput)operator(,) ident(os) +ident(size) operator(=) operator({)operator(}) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(filename) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + keyword(if) ident(filename) keyword(in) ident(size)operator(:) + keyword(continue) + ident(size)operator([)ident(filename)operator(]) operator(=) ident(os)operator(.)ident(path)operator(.)ident(getsize)operator(()ident(filename)operator(\)) + + +comment(# @@PLEAC@@_5.3) +comment(# remove key and its value from mydict) +keyword(del) ident(mydict)operator([)ident(key)operator(]) +comment(#-----------------------------) +comment(# food_color as per Introduction) +keyword(def) ident(print_foods)operator(()operator(\))operator(:) + ident(foods) operator(=) ident(food_color)operator(.)ident(keys)operator(()operator(\)) + + keyword(print) stringoperator(,) stringoperator(.)ident(join)operator(()ident(foods)operator(\)) + keyword(print) stringoperator(,) + + keyword(for) ident(food) keyword(in) ident(foods)operator(:) + ident(color) operator(=) ident(food_color)operator([)ident(food)operator(]) + keyword(if) ident(color) keyword(is) keyword(not) pre_constant(None)operator(:) + keyword(print) ident(color)operator(,) + keyword(else)operator(:) + keyword(print) stringoperator(,) + keyword(print) + +keyword(print) string +ident(print_foods)operator(()operator(\)) + +keyword(print) string +ident(food_color)operator([)stringoperator(]) operator(=) pre_constant(None) +ident(print_foods)operator(()operator(\)) + +keyword(print) string +keyword(del) ident(food_color)operator([)stringoperator(]) +ident(print_foods)operator(()operator(\)) + +comment(#=> Initially:) +comment(#=> Keys: Carrot Lemon Apple Banana) +comment(#=> Values: orange yellow red yellow) +comment(#=> ) +comment(#=> With Banana set to None) +comment(#=> Keys: Carrot Lemon Apple Banana) +comment(#=> Values: orange yellow red (undef\)) +comment(#=> ) +comment(#=> With Banana deleted) +comment(#=> Keys: Carrot Lemon Apple) +comment(#=> Values: orange yellow red) +comment(#-----------------------------) +keyword(for) ident(key) keyword(in) operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(:) + keyword(del) ident(food_color)operator([)ident(key)operator(]) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_5.4) +comment(#-----------------------------) +keyword(for) ident(key)operator(,) ident(value) keyword(in) ident(mydict)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(pass) comment(# do something with key and value) + +comment(# If mydict is large, use iteritems(\) instead) +keyword(for) ident(key)operator(,) ident(value) keyword(in) ident(mydict)operator(.)ident(iteritems)operator(()operator(\))operator(:) + keyword(pass) comment(# do something with key and value) + +comment(#-----------------------------) +comment(# DON'T DO THIS:) +keyword(for) ident(key) keyword(in) ident(mydict)operator(.)ident(keys)operator(()operator(\))operator(:) + ident(value) operator(=) ident(mydict)operator([)ident(key)operator(]) + comment(# do something with key and value) +comment(#-----------------------------) +comment(# food_color per the introduction) +keyword(for) ident(food)operator(,) ident(color) keyword(in) ident(food_color)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(color)operator(\)) + +comment(# DON'T DO THIS:) +keyword(for) ident(food) keyword(in) ident(food_color)operator(:) + ident(color) operator(=) ident(food_color)operator([)ident(food)operator(]) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(color)operator(\)) + +comment(#-----------------------------) +keyword(print) string operator(%) predefined(vars)operator(()operator(\)) +comment(#-----------------------------) +keyword(for) ident(food)operator(,) ident(color) keyword(in) predefined(sorted)operator(()ident(food_color)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(color)operator(\)) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# countfrom - count number of messages from each sender) + +keyword(import) ident(sys) +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\)) operator(>) integer(1)operator(:) + ident(infile) operator(=) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\)) +keyword(else)operator(:) + ident(infile) operator(=) ident(sys)operator(.)ident(stdin) + +ident(counts) operator(=) operator({)operator(}) +keyword(for) ident(line) keyword(in) ident(infile)operator(:) + keyword(if) ident(line)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + ident(name) operator(=) ident(line)operator([)integer(6)operator(:)operator(-)integer(1)operator(]) + ident(counts)operator([)ident(name)operator(]) operator(=) ident(counts)operator(.)ident(get)operator(()ident(name)operator(,) integer(0)operator(\)) operator(+) integer(1) + +keyword(for) operator(()ident(name)operator(,) ident(count)operator(\)) keyword(in) predefined(sorted)operator(()ident(counts)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(name)operator(,) ident(count)operator(\)) + +comment(#-----------------------------) + + +comment(# @@PLEAC@@_5.5) +keyword(for) ident(key)operator(,) ident(val) keyword(in) ident(mydict)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) ident(key)operator(,) string)delimiter(")>operator(,) ident(val) +comment(#-----------------------------) +keyword(print) stringoperator(.)ident(join)operator(()operator([)operator(()string %s)delimiter(")> operator(%) ident(item)operator(\)) keyword(for) ident(item) keyword(in) ident(mydict)operator(.)ident(items)operator(()operator(\))operator(])operator(\)) +comment(#-----------------------------) +keyword(print) ident(mydict) +comment(#=> {'firstname': 'Andrew', 'login': 'dalke', 'state': 'New Mexico', 'lastname': 'Dalke'}) +comment(#-----------------------------) +keyword(import) ident(pprint) +ident(pprint)operator(.)ident(pprint)operator(()predefined(dict)operator(\)) +comment(#=> {'firstname': 'Andrew',) +comment(#=> 'lastname': 'Dalke',) +comment(#=> 'login': 'dalke',) +comment(#=> 'state': 'New Mexico'}) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_5.6) +comment(#-----------------------------) +keyword(class) ident(SequenceDict)operator(()predefined(dict)operator(\))operator(:) + string + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(\))operator(:) + pre_constant(self)operator(.)ident(_keys)operator(=)operator({)operator(}) comment(# key --> id) + pre_constant(self)operator(.)ident(_ids)operator(=)operator({)operator(}) comment(# id --> key) + pre_constant(self)operator(.)ident(_next_id)operator(=)integer(0) + + keyword(def) ident(__setitem__)operator(()pre_constant(self)operator(,) ident(key)operator(,) ident(value)operator(\))operator(:) + pre_constant(self)operator(.)ident(_keys)operator([)ident(key)operator(])operator(=)pre_constant(self)operator(.)ident(_next_id) + pre_constant(self)operator(.)ident(_ids)operator([)pre_constant(self)operator(.)ident(_next_id)operator(])operator(=)ident(key) + pre_constant(self)operator(.)ident(_next_id)operator(+=)integer(1) + keyword(return) predefined(dict)operator(.)ident(__setitem__)operator(()pre_constant(self)operator(,) ident(key)operator(,) ident(value)operator(\)) + + keyword(def) ident(__delitem__)operator(()pre_constant(self)operator(,) ident(key)operator(\))operator(:) + predefined(id)operator(=)pre_constant(self)operator(.)ident(_keys)operator([)ident(key)operator(]) + keyword(del)operator(()pre_constant(self)operator(.)ident(_keys)operator([)ident(key)operator(])operator(\)) + keyword(del)operator(()pre_constant(self)operator(.)ident(_ids)operator([)predefined(id)operator(])operator(\)) + keyword(return) predefined(dict)operator(.)ident(__delitem__)operator(()pre_constant(self)operator(,) ident(key)operator(\)) + + keyword(def) ident(values)operator(()pre_constant(self)operator(\))operator(:) + ident(values)operator(=)operator([)operator(]) + ident(ids)operator(=)predefined(list)operator(()pre_constant(self)operator(.)ident(_ids)operator(.)ident(items)operator(()operator(\))operator(\)) + ident(ids)operator(.)ident(sort)operator(()operator(\)) + keyword(for) predefined(id)operator(,) ident(key) keyword(in) ident(ids)operator(:) + ident(values)operator(.)ident(append)operator(()pre_constant(self)operator([)ident(key)operator(])operator(\)) + keyword(return) ident(values) + + keyword(def) ident(items)operator(()pre_constant(self)operator(\))operator(:) + ident(items)operator(=)operator([)operator(]) + ident(ids)operator(=)predefined(list)operator(()pre_constant(self)operator(.)ident(_ids)operator(.)ident(items)operator(()operator(\))operator(\)) + ident(ids)operator(.)ident(sort)operator(()operator(\)) + keyword(for) predefined(id)operator(,) ident(key) keyword(in) ident(ids)operator(:) + ident(items)operator(.)ident(append)operator(()operator(()ident(key)operator(,) pre_constant(self)operator([)ident(key)operator(])operator(\))operator(\)) + keyword(return) ident(items) + + keyword(def) ident(keys)operator(()pre_constant(self)operator(\))operator(:) + ident(ids)operator(=)predefined(list)operator(()pre_constant(self)operator(.)ident(_ids)operator(.)ident(items)operator(()operator(\))operator(\)) + ident(ids)operator(.)ident(sort)operator(()operator(\)) + ident(keys)operator(=)operator([)operator(]) + keyword(for) predefined(id)operator(,) ident(key) keyword(in) ident(ids)operator(:) + ident(keys)operator(.)ident(append)operator(()ident(key)operator(\)) + keyword(return) ident(keys) + + keyword(def) ident(update)operator(()pre_constant(self)operator(,) ident(d)operator(\))operator(:) + keyword(for) ident(key)operator(,) ident(value) keyword(in) ident(d)operator(.)ident(items)operator(()operator(\))operator(:) + pre_constant(self)operator([)ident(key)operator(])operator(=)ident(value) + + keyword(def) ident(clear)operator(()pre_constant(self)operator(\))operator(:) + predefined(dict)operator(.)ident(clear)operator(()pre_constant(self)operator(\)) + pre_constant(self)operator(.)ident(_keys)operator(=)operator({)operator(}) + pre_constant(self)operator(.)ident(_ids)operator(=)operator({)operator(}) + pre_constant(self)operator(.)ident(_next_id)operator(=)integer(0) + +keyword(def) ident(testSequenceDict)operator(()operator(\))operator(:) + ident(sd)operator(=)ident(SequenceDict)operator(()operator(\)) + + comment(# First Test) + ident(sd)operator([)integer(3)operator(])operator(=)string + ident(sd)operator([)integer(2)operator(])operator(=)string + ident(sd)operator([)integer(1)operator(])operator(=)string + keyword(print) ident(sd)operator(.)ident(keys)operator(()operator(\)) + keyword(print) ident(sd)operator(.)ident(items)operator(()operator(\)) + keyword(print) ident(sd)operator(.)ident(values)operator(()operator(\)) + + keyword(del)operator(()ident(sd)operator([)integer(1)operator(])operator(\)) + keyword(del)operator(()ident(sd)operator([)integer(2)operator(])operator(\)) + keyword(del)operator(()ident(sd)operator([)integer(3)operator(])operator(\)) + + keyword(print) ident(sd)operator(.)ident(keys)operator(()operator(\))operator(,) ident(sd)operator(.)ident(items)operator(()operator(\))operator(,) ident(sd)operator(.)ident(values)operator(()operator(\)) + keyword(print) ident(sd)operator(.)ident(_ids)operator(,) ident(sd)operator(.)ident(_keys) + + keyword(print) string + comment(# Second Test) + ident(sd)operator([)stringoperator(])operator(=)string + ident(sd)operator([)stringoperator(])operator(=)string + ident(sd)operator(.)ident(update)operator(()operator({)stringoperator(:) stringoperator(})operator(\)) + keyword(print) ident(sd)operator(.)ident(keys)operator(()operator(\)) + keyword(print) ident(sd)operator(.)ident(items)operator(()operator(\)) + keyword(print) ident(sd)operator(.)ident(values)operator(()operator(\)) + + keyword(del)operator(()ident(sd)operator([)stringoperator(])operator(\)) + keyword(del)operator(()ident(sd)operator([)stringoperator(])operator(\)) + keyword(del)operator(()ident(sd)operator([)stringoperator(])operator(\)) + + keyword(print) ident(sd)operator(.)ident(keys)operator(()operator(\))operator(,) ident(sd)operator(.)ident(items)operator(()operator(\))operator(,) ident(sd)operator(.)ident(values)operator(()operator(\)) + keyword(print) ident(sd)operator(.)ident(_ids)operator(,) ident(sd)operator(.)ident(_keys) + +keyword(def) ident(likePerlCookbook)operator(()operator(\))operator(:) + ident(food_color)operator(=)ident(SequenceDict)operator(()operator(\)) + ident(food_color)operator([)stringoperator(])operator(=)stringoperator(;) + ident(food_color)operator([)stringoperator(])operator(=)stringoperator(;) + ident(food_color)operator([)stringoperator(])operator(=)string + keyword(print) string + keyword(for) ident(food)operator(,) ident(color) keyword(in) ident(food_color)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(color)operator(\)) + +keyword(if) ident(__name__)operator(==)stringoperator(:) + comment(#testSequenceDict(\)) + ident(likePerlCookbook)operator(()operator(\)) + + +comment(# @@PLEAC@@_5.7) +keyword(import) ident(os) +ident(ttys) operator(=) operator({)operator(}) + +ident(who) operator(=) ident(os)operator(.)ident(popen)operator(()stringoperator(\)) + +keyword(for) ident(line) keyword(in) ident(who)operator(:) + ident(user)operator(,) ident(tty) operator(=) ident(line)operator(.)ident(split)operator(()operator(\))operator([)operator(:)integer(2)operator(]) + ident(ttys)operator(.)ident(setdefault)operator(()ident(user)operator(,) operator([)operator(])operator(\))operator(.)ident(append)operator(()ident(tty)operator(\)) + +keyword(for) operator(()ident(user)operator(,) ident(tty_list)operator(\)) keyword(in) predefined(sorted)operator(()ident(ttys)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + keyword(print) ident(user) operator(+) string operator(+) stringoperator(.)ident(join)operator(()ident(tty_list)operator(\)) +comment(#-----------------------------) +keyword(import) ident(pwd) +keyword(for) operator(()ident(user)operator(,) ident(tty_list)operator(\)) keyword(in) ident(ttys)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) ident(user) operator(+) stringoperator(,) predefined(len)operator(()ident(tty_list)operator(\))operator(,) string + keyword(for) ident(tty) keyword(in) predefined(sorted)operator(()ident(tty_list)operator(\))operator(:) + keyword(try)operator(:) + ident(uid) operator(=) ident(os)operator(.)ident(stat)operator(()string operator(+) ident(tty)operator(\))operator(.)ident(st_uid) + ident(user) operator(=) ident(pwd)operator(.)ident(getpwuid)operator(()ident(uid)operator(\))operator([)integer(0)operator(]) + keyword(except) ident(os)operator(.)ident(error)operator(:) + ident(user) operator(=) string + keyword(print) string operator(%) operator(()ident(tty)operator(,) ident(user)operator(\)) + +comment(# @@PLEAC@@_5.8) +comment(# lookup_dict maps keys to values) +ident(reverse) operator(=) predefined(dict)operator(()operator([)operator(()ident(val)operator(,) ident(key)operator(\)) keyword(for) operator(()ident(key)operator(,) ident(val)operator(\)) keyword(in) ident(lookup_dict)operator(.)ident(items)operator(()operator(\))operator(])operator(\)) +comment(#-----------------------------) +ident(surname) operator(=) operator({)stringoperator(:) stringoperator(,) stringoperator(:) stringoperator(}) +ident(first_name) operator(=) predefined(dict)operator(()operator([)operator(()ident(last)operator(,) ident(first)operator(\)) keyword(for) operator(()ident(first)operator(,) ident(last)operator(\)) keyword(in) ident(surname)operator(.)ident(items)operator(()operator(\))operator(])operator(\)) + +keyword(print) ident(first_name)operator([)stringoperator(]) +comment(#=> Mickey) +comment(#-----------------------------) +comment(#!/usr/bin/perl -w) +comment(# foodfind - find match for food or color) + +keyword(import) ident(sys) +keyword(if) keyword(not) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(:) + keyword(raise) exception(SystemExit)operator(()stringoperator(\)) +ident(given) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) + +ident(color_dict) operator(=) operator({)stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + operator(}) +ident(food_dict) operator(=) predefined(dict)operator(()operator([)operator(()ident(color)operator(,) ident(food)operator(\)) keyword(for) operator(()ident(food)operator(,) ident(color)operator(\)) keyword(in) ident(color_dict)operator(.)ident(items)operator(()operator(\))operator(])operator(\)) + +keyword(if) ident(given) keyword(in) ident(color_dict)operator(:) + keyword(print) ident(given)operator(,) stringoperator(,) ident(color_dict)operator([)ident(given)operator(]) +keyword(elif) ident(given) keyword(in) ident(food_dict)operator(:) + keyword(print) ident(food_dict)operator([)ident(given)operator(])operator(,) stringoperator(,) ident(given) +comment(#-----------------------------) +comment(# food_color as per the introduction) +ident(foods_with_color) operator(=) operator({)operator(}) +keyword(for) ident(food)operator(,) ident(color) keyword(in) ident(food_color)operator(.)ident(items)operator(()operator(\))operator(:) + ident(foods_with_color)operator(.)ident(setdefault)operator(()ident(color)operator(,) operator([)operator(])operator(\))operator(.)ident(append)operator(()ident(food)operator(\)) + +keyword(print) stringoperator(.)ident(join)operator(()ident(foods_with_color)operator([)stringoperator(])operator(\))operator(,) string +comment(#-----------------------------) + +comment(# @@PLEAC@@_5.9) +comment(#-----------------------------) +comment(# mydict is the hash to sort) +keyword(for) ident(key)operator(,) ident(value) keyword(in) predefined(sorted)operator(()ident(mydict)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + comment(# do something with key, value) +comment(#-----------------------------) +comment(# food_color as per section 5.8) +keyword(for) ident(food)operator(,) ident(color) keyword(in) predefined(sorted)operator(()ident(food_color)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(color)operator(\)) +comment(#-----------------------------) +comment(# NOTE: alternative version) +keyword(for) ident(item) keyword(in) predefined(sorted)operator(()ident(food_color)operator(.)ident(items)operator(()operator(\))operator(\))operator(:) + keyword(print) string operator(%) ident(item) +comment(#-----------------------------) +comment(# NOTE: alternative version showing a user-defined function) +keyword(def) ident(food_cmp)operator(()ident(x)operator(,) ident(y)operator(\))operator(:) + keyword(return) predefined(cmp)operator(()ident(x)operator(,) ident(y)operator(\)) + +keyword(for) ident(food)operator(,) ident(color) keyword(in) predefined(sorted)operator(()ident(food_color)operator(,) predefined(cmp)operator(=)ident(food_cmp)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(color)operator(\)) +comment(#-----------------------------) +keyword(def) ident(food_len_cmp)operator(()ident(x)operator(,) ident(y)operator(\))operator(:) + keyword(return) predefined(cmp)operator(()predefined(len)operator(()ident(x)operator(\))operator(,) predefined(len)operator(()ident(y)operator(\))operator(\)) + +keyword(for) ident(food) keyword(in) predefined(sorted)operator(()ident(food_color)operator(,) predefined(cmp)operator(=)ident(food_len_cmp)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(food_color)operator([)ident(food)operator(])operator(\)) + +comment(# In this instance, however, the following is both simpler and faster:) +keyword(for) ident(food) keyword(in) predefined(sorted)operator(()ident(food_color)operator(,) ident(key)operator(=)predefined(len)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(food)operator(,) ident(food_color)operator([)ident(food)operator(])operator(\)) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_5.10) +comment(#-----------------------------) +ident(merged) operator(=) operator({)operator(}) +ident(merged)operator(.)ident(update)operator(()ident(a_dict)operator(\)) +ident(merged)operator(.)ident(update)operator(()ident(b_dict)operator(\)) + +comment(#-----------------------------) +comment(# NOTE: alternative version) +ident(merged) operator(=) ident(a_dict)operator(.)ident(copy)operator(()operator(\)) +ident(merged)operator(.)ident(update)operator(()ident(b_dict)operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS:) + +ident(merged) operator(=) operator({)operator(}) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(a_dict)operator(.)ident(items)operator(()operator(\))operator(:) + ident(merged)operator([)ident(k)operator(]) operator(=) ident(v) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(b_dict)operator(.)ident(items)operator(()operator(\))operator(:) + ident(merged)operator([)ident(k)operator(]) operator(=) ident(v) +comment(#-----------------------------) +comment(# food_color as per section 5.8) +ident(drink_color) operator(=) operator({)stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(}) + +ident(ingested_color) operator(=) ident(drink_color)operator(.)ident(copy)operator(()operator(\)) +ident(ingested_color)operator(.)ident(update)operator(()ident(food_color)operator(\)) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(drink_color) operator(=) operator({)stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(}) + +ident(substance_color) operator(=) operator({)operator(}) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(food_color)operator(.)ident(items)operator(()operator(\))operator(:) + ident(substance_color)operator([)ident(k)operator(]) operator(=) ident(v) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(drink_color)operator(.)ident(items)operator(()operator(\))operator(:) + ident(substance_color)operator([)ident(k)operator(]) operator(=) ident(v) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(substance_color) operator(=) operator({)operator(}) +keyword(for) ident(mydict) keyword(in) operator(()ident(food_color)operator(,) ident(drink_color)operator(\))operator(:) + keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(mydict)operator(:) + ident(substance_color)operator([)ident(k)operator(]) operator(=) ident(v) +comment(#-----------------------------) +comment(# DON'T DO THIS:) +ident(substance_color) operator(=) operator({)operator(}) +keyword(for) ident(item) keyword(in) ident(food_color)operator(.)ident(items)operator(()operator(\)) operator(+) ident(drink_color)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(mydict)operator(:) + ident(substance_color)operator([)ident(k)operator(]) operator(=) ident(v) +comment(#-----------------------------) +ident(substance_color) operator(=) operator({)operator(}) +keyword(for) ident(mydict) keyword(in) operator(()ident(food_color)operator(,) ident(drink_color)operator(\))operator(:) + keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(mydict)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(if) ident(substance_color)operator(.)ident(has_key)operator(()ident(k)operator(\))operator(:) + keyword(print) stringoperator(,) ident(k)operator(,) string + keyword(continue) + ident(substance_color)operator([)ident(k)operator(]) operator(=) ident(v) + +comment(# I think it's a copy, in which case) +ident(all_colors) operator(=) ident(new_colors)operator(.)ident(copy)operator(()operator(\)) + + +comment(# @@PLEAC@@_5.11) +ident(common) operator(=) operator([)ident(k) keyword(for) ident(k) keyword(in) ident(dict1) keyword(if) ident(k) keyword(in) ident(dict2)operator(]) +comment(#-----------------------------) +ident(this_not_that) operator(=) operator([)ident(k) keyword(for) ident(k) keyword(in) ident(dict1) keyword(if) ident(k) keyword(not) keyword(in) ident(dict2)operator(]) +comment(#-----------------------------) +comment(# citrus_color is a dict mapping citrus food name to its color.) +ident(citrus_color) operator(=) operator({)stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(}) + +comment(# build up a list of non-citrus foods) +ident(non_citrus) operator(=) operator([)ident(k) keyword(for) ident(k) keyword(in) ident(food_color) keyword(if) ident(k) keyword(not) keyword(in) ident(citruscolor)operator(]) +comment(#-----------------------------) + +comment(# @@PLEAC@@_5.12) +comment(#-----------------------------) +comment(# references as keys of dictionaries is no pb in python) + +ident(name) operator(=) operator({)operator(}) +keyword(for) ident(filename) keyword(in) operator(()stringoperator(,) stringoperator(,) stringoperator(\))operator(:) + keyword(try)operator(:) + ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(\)) + keyword(except) exception(IOError)operator(:) + keyword(pass) + keyword(else)operator(:) + ident(names)operator([)ident(myfile)operator(]) operator(=) ident(filename) + +keyword(print) stringoperator(,) stringoperator(.)ident(join)operator(()ident(name)operator(.)ident(values)operator(()operator(\))operator(\)) +keyword(for) ident(f)operator(,) ident(fname) keyword(in) ident(name)operator(.)ident(items)operator(()operator(\))operator(:) + ident(f)operator(.)ident(seek)operator(()integer(0)operator(,) integer(2)operator(\)) comment(# seek to the end) + keyword(print) string operator(%) operator(()ident(fname)operator(,) ident(f)operator(.)ident(tell)operator(()operator(\))operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_5.13) +comment(# Python doesn't allow presizing of dicts, but hashing is efficient -) +comment(# it only re-sizes at intervals, not every time an item is added.) + +comment(# @@PLEAC@@_5.14) +ident(count) operator(=) operator({)operator(}) +keyword(for) ident(element) keyword(in) ident(mylist)operator(:) + ident(count)operator([)ident(element)operator(]) operator(=) ident(count)operator(.)ident(get)operator(()ident(element)operator(,) integer(0)operator(\)) operator(+) integer(1) + +comment(# @@PLEAC@@_5.15) +comment(#-----------------------------) +keyword(import) ident(fileinput) + +ident(father) operator(=) operator({)stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + operator(}) + +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(person) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + keyword(while) ident(person)operator(:) comment(# as long as we have people,) + keyword(print) ident(person)operator(,) comment(# print the current name) + ident(person) operator(=) ident(father)operator(.)ident(get)operator(()ident(person)operator(\)) comment(# set the person to the person's father) + keyword(print) + +comment(#-----------------------------) +keyword(import) ident(fileinput) + +ident(children) operator(=) operator({)operator(}) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(father)operator(.)ident(items)operator(()operator(\))operator(:) + ident(children)operator(.)ident(setdefault)operator(()ident(v)operator(,) operator([)operator(])operator(\))operator(.)ident(append)operator(()ident(k)operator(\)) + +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(person) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + ident(kids) operator(=) ident(children)operator(.)ident(get)operator(()ident(person)operator(,) operator([)stringoperator(])operator(\)) + keyword(print) ident(person)operator(,) stringoperator(,) stringoperator(.)ident(join)operator(()ident(kids)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(sys)operator(,) ident(re) +ident(pattern) operator(=) ident(re)operator(.)ident(compile)operator(()string]+\))delimiter(')>operator(\)) +ident(includes) operator(=) operator({)operator(}) +keyword(for) ident(filename) keyword(in) ident(filenames)operator(:) + keyword(try)operator(:) + ident(infile) operator(=) predefined(open)operator(()ident(filename)operator(\)) + keyword(except) exception(IOError)operator(,) ident(err)operator(:) + keyword(print)operator(>>)ident(sys)operator(.)ident(stderr)operator(,) ident(err) + keyword(continue) + keyword(for) ident(line) keyword(in) ident(infile)operator(:) + ident(match) operator(=) ident(pattern)operator(.)ident(match)operator(()ident(line)operator(\)) + keyword(if) ident(match)operator(:) + ident(includes)operator(.)ident(setdefault)operator(()ident(match)operator(.)ident(group)operator(()integer(1)operator(\))operator(,) operator([)operator(])operator(\))operator(.)ident(append)operator(()ident(filename)operator(\)) +comment(#-----------------------------) +comment(# list of files that don't include others) +ident(mydict) operator(=) operator({)operator(}) +keyword(for) ident(e) keyword(in) predefined(reduce)operator(()keyword(lambda) ident(a)operator(,)ident(b)operator(:) ident(a) operator(+) ident(b)operator(,) ident(includes)operator(.)ident(values)operator(()operator(\))operator(\))operator(:) + keyword(if) keyword(not) ident(includes)operator(.)ident(has_key)operator(()ident(e)operator(\))operator(:) + ident(mydict)operator([)ident(e)operator(]) operator(=) integer(1) +ident(include_free) operator(=) ident(mydict)operator(.)ident(keys)operator(()operator(\)) +ident(include_free)operator(.)ident(sort)operator(()operator(\)) + +comment(# @@PLEAC@@_5.16) +comment(#-----------------------------) +comment(#!/usr/bin/env python -w) +comment(# dutree - print sorted indented rendition of du output) +keyword(import) ident(os)operator(,) ident(sys) + +keyword(def) ident(get_input)operator(()ident(args)operator(\))operator(:) + comment(# NOTE: This is insecure - use only from trusted code!) + ident(cmd) operator(=) string operator(+) stringoperator(.)ident(join)operator(()ident(args)operator(\)) + ident(infile) operator(=) ident(os)operator(.)ident(popen)operator(()ident(cmd)operator(\)) + + ident(dirsize) operator(=) operator({)operator(}) + ident(kids) operator(=) operator({)operator(}) + keyword(for) ident(line) keyword(in) ident(infile)operator(:) + ident(size)operator(,) ident(name) operator(=) ident(line)operator([)operator(:)operator(-)integer(1)operator(])operator(.)ident(split)operator(()stringoperator(,) integer(1)operator(\)) + ident(dirsize)operator([)ident(name)operator(]) operator(=) predefined(int)operator(()ident(size)operator(\)) + ident(parent) operator(=) ident(os)operator(.)ident(path)operator(.)ident(dirname)operator(()ident(name)operator(\)) + ident(kids)operator(.)ident(setdefault)operator(()ident(parent)operator(,) operator([)operator(])operator(\))operator(.)ident(append)operator(()ident(name)operator(\)) + comment(# Remove the last field added, which is the root) + ident(kids)operator([)ident(parent)operator(])operator(.)ident(pop)operator(()operator(\)) + keyword(if) keyword(not) ident(kids)operator([)ident(parent)operator(])operator(:) + keyword(del) ident(kids)operator([)ident(parent)operator(]) + + keyword(return) ident(name)operator(,) ident(dirsize)operator(,) ident(kids) + +keyword(def) ident(getdots)operator(()ident(root)operator(,) ident(dirsize)operator(,) ident(kids)operator(\))operator(:) + ident(size) operator(=) ident(cursize) operator(=) ident(dirsize)operator([)ident(root)operator(]) + keyword(if) ident(kids)operator(.)ident(has_key)operator(()ident(root)operator(\))operator(:) + keyword(for) ident(kid) keyword(in) ident(kids)operator([)ident(root)operator(])operator(:) + ident(cursize) operator(-=) ident(dirsize)operator([)ident(kid)operator(]) + ident(getdots)operator(()ident(kid)operator(,) ident(dirsize)operator(,) ident(kids)operator(\)) + keyword(if) ident(size) operator(!=) ident(cursize)operator(:) + ident(dot) operator(=) ident(root) operator(+) string + ident(dirsize)operator([)ident(dot)operator(]) operator(=) ident(cursize) + ident(kids)operator([)ident(root)operator(])operator(.)ident(append)operator(()ident(dot)operator(\)) + +keyword(def) ident(output)operator(()ident(root)operator(,) ident(dirsize)operator(,) ident(kids)operator(,) ident(prefix) operator(=) stringoperator(,) ident(width) operator(=) integer(0)operator(\))operator(:) + ident(path) operator(=) ident(os)operator(.)ident(path)operator(.)ident(basename)operator(()ident(root)operator(\)) + ident(size) operator(=) ident(dirsize)operator([)ident(root)operator(]) + ident(fmt) operator(=) string operator(+) predefined(str)operator(()ident(width)operator(\)) operator(+) string + ident(line) operator(=) ident(fmt) operator(%) operator(()ident(size)operator(,) ident(path)operator(\)) + keyword(print) ident(prefix) operator(+) ident(line) + + ident(prefix) operator(+=) operator(()string operator(*) operator(()ident(width)operator(-)integer(1)operator(\))operator(\)) operator(+) string operator(+) operator(()string operator(*) predefined(len)operator(()ident(path)operator(\))operator(\)) + + keyword(if) ident(kids)operator(.)ident(has_key)operator(()ident(root)operator(\))operator(:) + ident(kid_list) operator(=) ident(kids)operator([)ident(root)operator(]) + ident(kid_list)operator(.)ident(sort)operator(()keyword(lambda) ident(x)operator(,) ident(y)operator(,) ident(dirsize)operator(=)ident(dirsize)operator(:) + predefined(cmp)operator(()ident(dirsize)operator([)ident(x)operator(])operator(,) ident(dirsize)operator([)ident(y)operator(])operator(\))operator(\)) + ident(width) operator(=) predefined(len)operator(()predefined(str)operator(()ident(dirsize)operator([)ident(kid_list)operator([)operator(-)integer(1)operator(])operator(])operator(\))operator(\)) + keyword(for) ident(kid) keyword(in) ident(kid_list)operator(:) + ident(output)operator(()ident(kid)operator(,) ident(dirsize)operator(,) ident(kids)operator(,) ident(prefix)operator(,) ident(width)operator(\)) + +keyword(def) ident(main)operator(()operator(\))operator(:) + ident(root)operator(,) ident(dirsize)operator(,) ident(kids) operator(=) ident(get_input)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(\)) + ident(getdots)operator(()ident(root)operator(,) ident(dirsize)operator(,) ident(kids)operator(\)) + ident(output)operator(()ident(root)operator(,) ident(dirsize)operator(,) ident(kids)operator(\)) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(main)operator(()operator(\)) + + +comment(# @@PLEAC@@_6.0) +comment(# Note: regexes are used less often in Python than in Perl as tasks are often) +comment(# covered by string methods, or specialised objects, modules, or packages.) + +keyword(import) ident(re) comment(# "re" is the regular expression module.) +ident(re)operator(.)ident(search)operator(()stringoperator(,)ident(meadow)operator(\)) comment(# returns a MatchObject is meadow contains "sheep".) +keyword(if) keyword(not) ident(re)operator(.)ident(search)operator(()stringoperator(,)ident(meadow)operator(\))operator(:) + keyword(print) string +comment(# replacing strings is not done by "re"gular expressions.) +ident(meadow) operator(=) ident(meadow)operator(.)ident(replace)operator(()stringoperator(,)stringoperator(\)) comment(# replace "old" with "new" and assign result.) +comment(#-----------------------------) +ident(re)operator(.)ident(search)operator(()stringoperator(,)ident(meadow)operator(\)) + +ident(meadow) operator(=) string + +ident(meadow) operator(=) string + +keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,)ident(meadow)operator(,)ident(re)operator(.)ident(I)operator(\)) operator(:) keyword(print) string +comment(#-----------------------------) +comment(# The tricky bit) +ident(mystr) operator(=) string +ident(re)operator(.)ident(sub)operator(()stringoperator(,)stringoperator(,)ident(mystr)operator(,)integer(1)operator(\)) comment(# gives 'egood food') + +ident(echo) ident(ababacaca) operator(|) ident(python) operator(-)ident(c) string +comment(#-----------------------------) +comment(# pattern matching modifiers) +comment(# assume perl code iterates over some file) +keyword(import) ident(re)operator(,) ident(fileinput) +keyword(for) ident(ln) operator(=) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(fnd) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(ln)operator(\)) + keyword(if) predefined(len)operator(()ident(fnd)operator(\)) operator(>) integer(0)operator(:) + keyword(print) string operator(%) operator(()ident(fnd)operator([)integer(0)operator(])operator(\)) +comment(# ----------------------------) +ident(digits) operator(=) string +ident(nonlap) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(digits)operator(\)) +ident(yeslap) operator(=) operator([)stringoperator(]) +keyword(print) stringoperator(,)stringoperator(.)ident(join)operator(()ident(nonlap)operator(\)) +keyword(print) stringoperator(,)stringoperator(.)ident(join)operator(()ident(yeslap)operator(\)) +comment(# ----------------------------) +ident(mystr) operator(=) string +ident(fnd) operator(=) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(mystr)operator(\)) +keyword(print) string operator(%) operator(()ident(mystr)operator([)operator(:)ident(fnd)operator(.)ident(start)operator(()operator(\))operator(])operator(,) ident(fnd)operator(.)ident(group)operator(()operator(\))operator(,) ident(mystr)operator([)ident(fnd)operator(.)ident(end)operator(()operator(\))operator(:)operator(])operator(\)) +comment(# (And \) (little lambs\) ( eat ivy\)) + + +comment(# @@PLEAC@@_6.1) +keyword(import) ident(re) +ident(dst) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,)stringoperator(,)ident(src)operator(\)) +comment(#-----------------------------) +comment(# strip to basename) +ident(basename) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,)stringoperator(,)ident(progname)operator(\)) + +comment(# Make All Words Title-Cased) +comment(# DON'T DO THIS - use str.title(\) instead) +keyword(def) ident(cap)operator(()ident(mo)operator(\))operator(:) keyword(return) ident(mo)operator(.)ident(group)operator(()operator(\))operator(.)ident(capitalize)operator(()operator(\)) +ident(re)operator(.)ident(sub)operator(()string)content(\\w)content(+\))delimiter(")>operator(,)ident(cap)operator(,)stringoperator(\)) + +comment(# /usr/man/man3/foo.1 changes to /usr/man/cat3/foo.1) +ident(manpage) operator(=) string +ident(catpage) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,)stringoperator(,)ident(manpage)operator(\)) +comment(#-----------------------------) +ident(bindirs) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +ident(libdirs) operator(=) operator([)ident(d)operator(.)ident(replace)operator(()stringoperator(,) stringoperator(\)) keyword(for) ident(d) keyword(in) ident(bindirs)operator(]) + +keyword(print) stringoperator(.)ident(join)operator(()ident(libdirs)operator(\)) +comment(#=> /usr/lib /lib /usr/local/lib) +comment(#-----------------------------) +comment(# strings are never modified in place.) +comment(#-----------------------------) + +comment(# @@PLEAC@@_6.2) +comment(##---------------------------) + +comment(# DON'T DO THIS. use line[:-1].isalpha(\) [this probably goes for the) +comment(# remainder of this section too!]) +keyword(import) ident(re) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,)ident(line)operator(\))operator(:) + keyword(print) string +comment(##---------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(line)operator(,) ident(re)operator(.)ident(LOCALE)operator(\))operator(:) + keyword(print) string +comment(##---------------------------) +keyword(import) ident(re) +keyword(import) ident(locale) + +keyword(try)operator(:) + ident(locale)operator(.)ident(setlocale)operator(()ident(locale)operator(.)ident(LC_ALL)operator(,) stringoperator(\)) +keyword(except)operator(:) + keyword(print) string + keyword(raise) exception(SystemExit) + +ident(DATA)operator(=)string + +keyword(for) ident(ln) keyword(in) ident(DATA)operator(.)ident(split)operator(()operator(\))operator(:) + ident(ln) operator(=) ident(ln)operator(.)ident(rstrip)operator(()operator(\)) + keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,)ident(ln)operator(,)ident(re)operator(.)ident(LOCALE)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(ln)operator(\)) + keyword(else)operator(:) + keyword(print) string operator(%) operator(()ident(ln)operator(\)) +comment(# although i dont think "coöperate" should be in canadian) +comment(##---------------------------) + +comment(# @@PLEAC@@_6.3) +comment(# Matching Words) +string comment(# as many non-whitespace bytes as possible) +string comment(# as many letters, apostrophes, and hyphens) + +comment(# string split is similar to splitting on "\\s+") +stringoperator(.)ident(split)operator(()operator(\)) + +string comment(# word boundaries ) +string comment(# might work too as on letters are allowed.) + +ident(re)operator(.)ident(search)operator(()stringoperator(,)stringoperator(\)) comment(# matches on thistle not on this) +ident(re)operator(.)ident(search)operator(()stringoperator(,)stringoperator(\)) comment(# does not match) + +comment(# @@PLEAC@@_6.4) +comment(#-----------------------------) +comment(#!/usr/bin/python) +comment(# resname - change all "foo.bar.com" style names in the input stream) +comment(# into "foo.bar.com [204.148.40.9]" (or whatever\) instead) + +keyword(import) ident(socket) comment(# load inet_addr) +keyword(import) ident(fileinput) +keyword(import) ident(re) + +ident(match) operator(=) ident(re)operator(.)ident(compile)operator(()string # capture hostname)content( +)content( (?: # these parens for grouping only)content( +)content( [)content(\\w)content(-]+ # hostname component)content( +)content( )content(\\.)content( # ant the domain dot)content( +)content( \) + # now repeat that whole thing a bunch of times)content( +)content( [A-Za-z] # next must be a letter)content( +)content( [)content(\\w)content(-] + # now trailing domain part)content( +)content( \) # end of hostname capture)content( +)content( )delimiter(""")>operator(,)ident(re)operator(.)ident(VERBOSE)operator(\)) comment(# for nice formatting) + +keyword(def) ident(repl)operator(()ident(match_obj)operator(\))operator(:) + ident(orig_hostname) operator(=) ident(match_obj)operator(.)ident(group)operator(()stringoperator(\)) + keyword(try)operator(:) + ident(addr) operator(=) ident(socket)operator(.)ident(gethostbyname)operator(()ident(orig_hostname)operator(\)) + keyword(except) ident(socket)operator(.)ident(gaierror)operator(:) + ident(addr) operator(=) string + keyword(return) string operator(%) operator(()ident(orig_hostname)operator(,) ident(addr)operator(\)) + +keyword(for) ident(ln) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(print) ident(match)operator(.)ident(sub)operator(()ident(repl)operator(,) ident(ln)operator(\)) +comment(#-----------------------------) +ident(re)operator(.)ident(sub)operator(()stringoperator(,) + keyword(lambda) ident(m)operator(:) predefined(eval)operator(()ident(m)operator(.)ident(group)operator(()integer(1)operator(\))operator(\))operator(,) comment(# replace with the value of the global variable) + ident(line) + operator(\)) +comment(##-----------------------------) +ident(re)operator(.)ident(sub)operator(()stringoperator(,) + keyword(lambda) ident(m)operator(:) predefined(eval)operator(()predefined(eval)operator(()ident(m)operator(.)ident(group)operator(()integer(1)operator(\))operator(\))operator(\))operator(,) comment(# replace with the value of *any* variable) + ident(line) + operator(\)) +comment(##-----------------------------) + +comment(# @@PLEAC@@_6.5) +keyword(import) ident(re) +ident(pond) operator(=) string +ident(fishes) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\)) +keyword(if) predefined(len)operator(()ident(fishes)operator(\))operator(>)integer(2)operator(:) + keyword(print) string operator(%) operator(()ident(fishes)operator([)integer(2)operator(])operator(\)) +comment(##-----------------------------) +ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\)) +comment(##-----------------------------) +ident(count) operator(=) integer(0) +keyword(for) ident(match_object) keyword(in) ident(re)operator(.)ident(finditer)operator(()stringoperator(,) ident(mystr)operator(\))operator(:) + ident(count) operator(+=) integer(1) comment(# or whatever you want to do here) + +comment(# "progressive" matching might be better if one wants match 5 from 50.) +comment(# to count use) +ident(count) operator(=) predefined(len)operator(()ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(mystr)operator(\))operator(\)) +ident(count) operator(=) predefined(len)operator(()ident(re)operator(.)ident(findall)operator(()stringoperator(,)stringoperator(\))operator(\)) + +comment(# "count" overlapping matches) +ident(count) operator(=) predefined(len)operator(()ident(re)operator(.)ident(findall)operator(()stringoperator(,)stringoperator(\))operator(\)) + +comment(# FASTEST non-overlapping might be str.count) +stringoperator(.)ident(count)operator(()stringoperator(\)) +comment(##-----------------------------) +ident(pond) operator(=) string +ident(colors) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\)) comment(# get all matches) +ident(color) operator(=) ident(colors)operator([)integer(2)operator(]) comment(# then the one we want) + +comment(# or without a temporary list) +ident(color) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\))operator([)integer(2)operator(]) comment(# just grab element 3) + +keyword(print) string operator(%) operator(()ident(color)operator(\)) +comment(##-----------------------------) +keyword(import) ident(re) + +ident(pond) operator(=) string +ident(matches) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\)) +ident(evens) operator(=) operator([)ident(fish) keyword(for) operator(()ident(i)operator(,) ident(fish)operator(\)) keyword(in) predefined(enumerate)operator(()ident(matches)operator(\)) keyword(if) ident(i)operator(%)integer(2)operator(]) +keyword(print) string operator(%) operator(()stringoperator(.)ident(join)operator(()ident(evens)operator(\))operator(\)) +comment(##-----------------------------) +ident(count) operator(=) integer(0) +keyword(def) ident(four_is_sushi)operator(()ident(match_obj)operator(\))operator(:) + keyword(global) ident(count) + ident(count) operator(+=) integer(1) + keyword(if) ident(count)operator(==)integer(4)operator(:) + keyword(return) string operator(%) operator(()ident(match_obj)operator(.)ident(group)operator(()integer(2)operator(\))operator(\)) + keyword(return) stringoperator(.)ident(join)operator(()ident(match_obj)operator(.)ident(groups)operator(()operator(\))operator(\)) + +ident(re)operator(.)ident(sub)operator(()stringoperator(,) + ident(four_is_sushi)operator(,) + ident(pond)operator(\)) +comment(# one fish two fish red fish sushi fish) +comment(##-----------------------------) +comment(# greedily) +ident(last_fish) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\)) +comment(##-----------------------------) +ident(pond) operator(=) string +ident(color) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(pond)operator(\))operator([)operator(-)integer(1)operator(]) +keyword(print) stringoperator(+)ident(color)operator(+)string +comment(# FASTER using string.) +ident(lastfish) operator(=) ident(pond)operator(.)ident(rfind)operator(()stringoperator(\)) +ident(color) operator(=) ident(pond)operator([)operator(:)ident(lastfish)operator(])operator(.)ident(split)operator(()operator(\))operator([)operator(-)integer(1)operator(]) +comment(##-----------------------------) +string + +ident(pond) operator(=) string +ident(fnd) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) + ident(pond)operator(\)) +keyword(if) predefined(len)operator(()ident(fnd)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(fnd)operator([)integer(0)operator(])operator(\)) +keyword(else)operator(:) + keyword(print) string + + +comment(# @@PLEAC@@_6.6) +comment(# Matching Multiple Lines) +comment(#) +comment(#!/usr/bin/python) +comment(# killtags - very bad html tag killer) +keyword(import) ident(re) +keyword(import) ident(sys) + +ident(text) operator(=) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\)) comment(# read the whole file) +ident(text) operator(=) ident(re)operator(.)ident(sub)operator(()string)delimiter(")>operator(,)stringoperator(,)ident(text)operator(\)) comment(# strip tags (terrible) +keyword(print) ident(text) +comment(## ----------------------------) +comment(#!/usr/bin/python) +comment(# headerfy: change certain chapter headers to html) +keyword(import) ident(sys)operator(,) ident(re) + +ident(match) operator(=) ident(re)operator(.)ident(compile)operator(()string # capture in g)content( +)content( Chapter # literal string)content( +)content( )content(\\s)content(+ # mandatory whitespace)content( +)content( )content(\\d)content(+ # decimal number)content( +)content( )content(\\s)content(* # optional whitespace)content( +)content( : # a real colon)content( +)content( . * # anything not a newline till end of line)content( +)content( \))content( +)content( )delimiter(""")>operator(\)) +ident(text) operator(=) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\)) comment(# read the whole file) +keyword(for) ident(paragraph) keyword(in) ident(text)operator(.)ident(split)operator(()stringoperator(\))operator(:) comment(# split on unix end of lines) + ident(p) operator(=) ident(match)operator(.)ident(sub)operator(()string)content(\\g)content()delimiter(")>operator(,)ident(paragraph)operator(\)) + keyword(print) ident(p) +comment(## ----------------------------) +comment(# the one liner does not run.) +comment(# python -c 'import sys,re; for p in open(sys.argv[1]\).read(\).split("\\n\\n"\): print re.sub(r"(?ms\)\\A(Chapter\\s+\\d+\\s*:.*\)","

\\g0

",p\)') +comment(## ----------------------------) +ident(match) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + comment(# s makes . span line boundaries) + comment(# m makes ^ match at the beginning of the string and at the beginning of each line) + +ident(chunk) operator(=) integer(0) +keyword(for) ident(paragraph) keyword(in) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(.)ident(split)operator(()stringoperator(\))operator(:) + ident(chunk) operator(+=) integer(1) + ident(fnd) operator(=) ident(match)operator(.)ident(findall)operator(()ident(paragraph)operator(\)) + keyword(if) ident(fnd)operator(:) + keyword(print) string>)delimiter(")> operator(%) operator(()ident(chunk)operator(,)ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(,)string>,<<)delimiter(")>operator(.)ident(join)operator(()ident(fnd)operator(\))operator(\)) +comment(## ----------------------------) + +comment(# @@PLEAC@@_6.7) +keyword(import) ident(sys) +comment(# Read the whole file and split) +ident(chunks) operator(=) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(.)ident(split)operator(()operator(\)) comment(# on whitespace) +ident(chunks) operator(=) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(.)ident(split)operator(()stringoperator(\)) comment(# on line ends) + +comment(# splitting on pattern) +keyword(import) ident(re) +ident(pattern) operator(=) string +ident(chunks) operator(=) ident(re)operator(.)ident(split)operator(()ident(pattern)operator(,) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(\)) +comment(##-----------------------------) +ident(chunks) operator(=) ident(re)operator(.)ident(split)operator(()stringoperator(,)predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(\)) +keyword(print) string operator(%) operator(()predefined(len)operator(()ident(chunks)operator(\))operator(\)) +comment(# without delimiters) +ident(chunks) operator(=) ident(re)operator(.)ident(split)operator(()stringoperator(,)predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(\)) + +comment(# with delimiters) +ident(chunks) operator(=) ident(re)operator(.)ident(split)operator(()stringoperator(,)predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(\)) + +comment(# with delimiters at chunkstart) +ident(chunks) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) + predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(\)) +comment(# [1] if "?:" is removed the result holds tuples: ('.Ch\\nchapter x','.Ch'\)) +comment(# which might be more usefull. ) + +comment(# @@PLEAC@@_6.8) +comment(##-----------------------------) +comment(# Python doesn't have perl's range operators) +comment(# If you want to only use a selected line range, use enumerate) +comment(# (though note that indexing starts at zero:) +keyword(for) ident(i)operator(,) ident(line) keyword(in) predefined(enumerate)operator(()ident(myfile)operator(\))operator(:) + keyword(if) ident(firstlinenum) operator(<=) ident(i) operator(<) ident(lastlinenum)operator(:) + ident(dosomethingwith)operator(()ident(line)operator(\)) + +comment(# Using patterned ranges is slightly trickier -) +comment(# You need to search for the first pattern then) +comment(# search for the next pattern:) +keyword(import) ident(re) +keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(if) ident(re)operator(.)ident(match)operator(()ident(pat1)operator(,) ident(line)operator(\))operator(:) + keyword(break) + +ident(dosomethingwith)operator(()ident(line)operator(\)) comment(# Only if pat1 can be on same line as pat2) + +keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(if) ident(re)operator(.)ident(match)operator(()ident(pat2)operator(,) ident(line)operator(\))operator(:) + keyword(break) + ident(dosomethingwith)operator(()ident(line)operator(\)) +comment(##-----------------------------) +comment(# If you need to extract ranges a lot, the following generator funcs) +comment(# may be useful:) +keyword(def) ident(extract_range)operator(()ident(myfile)operator(,) ident(start)operator(,) ident(finish)operator(\))operator(:) + keyword(for) ident(i)operator(,) ident(line) keyword(in) predefined(enumerate)operator(()ident(myfile)operator(\))operator(:) + keyword(if) ident(start) operator(<=) ident(i) operator(<) ident(finish)operator(:) + keyword(yield) ident(line) + keyword(elif) ident(i) operator(==) ident(finish)operator(:) + keyword(break) + +keyword(for) ident(line) keyword(in) ident(extract_range)operator(()predefined(open)operator(()stringoperator(\))operator(,) integer(3)operator(,) integer(5)operator(\))operator(:) + keyword(print) ident(line) + +keyword(def) ident(patterned_range)operator(()ident(myfile)operator(,) ident(startpat)operator(,) ident(endpat)operator(=)pre_constant(None)operator(\))operator(:) + ident(startpat) operator(=) ident(re)operator(.)ident(compile)operator(()ident(startpat)operator(\)) + keyword(if) ident(endpat) keyword(is) keyword(not) pre_constant(None)operator(:) + ident(endpat) operator(=) ident(re)operator(.)ident(compile)operator(()ident(endpat)operator(\)) + ident(in_range) operator(=) pre_constant(False) + keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(if) ident(re)operator(.)ident(match)operator(()ident(startpat)operator(,) ident(line)operator(\))operator(:) + ident(in_range) operator(=) pre_constant(True) + keyword(if) ident(in_range)operator(:) + keyword(yield) ident(line) + keyword(if) ident(endpat) keyword(is) keyword(not) pre_constant(None) keyword(and) ident(re)operator(.)ident(match)operator(()ident(endpat)operator(,) ident(line)operator(\))operator(:) + keyword(break) + +comment(# DO NOT DO THIS. Use the email module instead) +keyword(for) ident(line) keyword(in) ident(patterned_range)operator(()ident(msg)operator(,) stringoperator(,) stringoperator(\))operator(:) + keyword(pass) comment(#...) + + +comment(# @@PLEAC@@_6.9) +ident(tests) operator(=) operator(()operator(()stringoperator(,)stringoperator(\))operator(,) + operator(()stringoperator(,)stringoperator(\))operator(,) + operator(()stringoperator(,)stringoperator(\))operator(,) + operator(()stringoperator(,)stringoperator(\))operator(,) + operator(()stringoperator(,)stringoperator(\))operator(,) + operator(()stringoperator(,)stringoperator(\))operator(,) + operator(\)) + +comment(# The book says convert "*","?","[","]" all other characters will be quoted.) +comment(# The book uses "\\Q" which escapes any characters that would otherwise be) +comment(# treated as regular expression.) +comment(# Escaping every char fails as "\\s" is not "s" in a regex.) + +keyword(def) ident(glob2pat)operator(()ident(globstr)operator(\))operator(:) + ident(pat) operator(=) ident(globstr)operator(.)ident(replace)operator(()stringoperator(,)stringoperator(\)) + ident(pat) operator(=) ident(pat)operator(.)ident(replace)operator(()stringoperator(,)stringoperator(\))operator(.)ident(replace)operator(()stringoperator(,)stringoperator(\))operator(.)ident(replace)operator(()stringoperator(,)stringoperator(\)) + + keyword(return) stringoperator(+)ident(pat)operator(+)string + +keyword(for) ident(globstr)operator(,) ident(patstr) keyword(in) ident(tests)operator(:) + ident(g2p) operator(=) ident(glob2pat)operator(()ident(globstr)operator(\)) + keyword(if) ident(g2p) operator(!=) ident(patstr)operator(:) + keyword(print) ident(globstr)operator(,) stringoperator(,) ident(patstr)operator(,) stringoperator(,) ident(g2p) + + +comment(# @@PLEAC@@_6.10) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# popgrep1 - grep for abbreviations of places that say "pop") +comment(# version 1: slow but obvious way) +keyword(import) ident(fileinput) +keyword(import) ident(re) +ident(popstates) operator(=) operator([)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(]) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(for) ident(state) keyword(in) ident(popstates)operator(:) + keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(+)ident(state)operator(+)stringoperator(,)ident(line)operator(\))operator(:) + keyword(print) ident(line) + + + +comment(#-----------------------------) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# popgrep2 - grep for abbreviations of places that say "pop") +comment(# version 2: compile the patterns) +keyword(import) ident(fileinput) +keyword(import) ident(re) +ident(popstates) operator(=) operator([)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(]) +ident(state_re) operator(=) operator([)operator(]) +keyword(for) ident(state) keyword(in) ident(popstates)operator(:) + ident(state_re)operator(.)ident(append)operator(()ident(re)operator(.)ident(compile)operator(()stringoperator(+)ident(state)operator(+)stringoperator(\))operator(\)) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(for) ident(state) keyword(in) ident(state_re)operator(:) + keyword(if) ident(state)operator(.)ident(search)operator(()ident(line)operator(\))operator(:) + keyword(print) ident(line) + + +comment(#-----------------------------) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# popgrep3 - grep for abbreviations of places that say "pop") +comment(# version 3: compile a single pattern) +keyword(import) ident(fileinput) +keyword(import) ident(re) +ident(popstates) operator(=) operator([)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(]) +ident(state_re) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(+)stringoperator(.)ident(join)operator(()ident(popstates)operator(\))operator(+)stringoperator(\)) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(if) ident(state_re)operator(.)ident(search)operator(()ident(line)operator(\))operator(:) + keyword(print) ident(line) + + +comment(#-----------------------------) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# grepauth - print lines that mention both Tom and Nat) +keyword(import) ident(fileinput) +keyword(import) ident(re) + +keyword(def) ident(build_match_any)operator(()ident(words)operator(\))operator(:) + keyword(return) ident(re)operator(.)ident(compile)operator(()stringoperator(.)ident(join)operator(()ident(words)operator(\))operator(\)) +keyword(def) ident(uniq)operator(()ident(arr)operator(\))operator(:) + ident(seen) operator(=) operator({)operator(}) + keyword(for) ident(item) keyword(in) ident(arr)operator(:) + ident(seen)operator([)ident(item)operator(]) operator(=) ident(seen)operator(.)ident(get)operator(()ident(item)operator(,) integer(0)operator(\)) operator(+) integer(1) + keyword(return) ident(seen)operator(.)ident(keys)operator(()operator(\)) +keyword(def) ident(build_match_all)operator(()ident(words)operator(\))operator(:) + ident(r) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(.)ident(join)operator(()ident(words)operator(\))operator(\)) + ident(c) operator(=) keyword(lambda) ident(line)operator(:) predefined(len)operator(()ident(uniq)operator(()ident(r)operator(.)ident(findall)operator(()ident(line)operator(\))operator(\))operator(\))operator(>=)predefined(len)operator(()ident(words)operator(\)) + keyword(return) ident(c) + +predefined(any) operator(=) ident(build_match_any)operator(()operator(()stringoperator(,)stringoperator(\))operator(\)) +predefined(all) operator(=) ident(build_match_all)operator(()operator(()stringoperator(,)stringoperator(\))operator(\)) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(if) predefined(any)operator(.)ident(search)operator(()ident(line)operator(\))operator(:) + keyword(print) stringoperator(,) ident(line) + keyword(if) predefined(all)operator(()ident(line)operator(\))operator(:) + keyword(print) stringoperator(,) ident(line) + + + +comment(#-----------------------------) + + +comment(# @@PLEAC@@_6.11) +comment(# Testing for a Valid Pattern) + +keyword(import) ident(re) +keyword(while) pre_constant(True)operator(:) + ident(pat) operator(=) predefined(raw_input)operator(()stringoperator(\)) + keyword(try)operator(:) + ident(re)operator(.)ident(compile)operator(()ident(pat)operator(\)) + keyword(except) ident(re)operator(.)ident(error)operator(,) ident(err)operator(:) + keyword(print) stringoperator(,) ident(err) + keyword(continue) + keyword(break) + +comment(# ----) +keyword(def) ident(is_valid_pattern)operator(()ident(pat)operator(\))operator(:) + keyword(try)operator(:) + ident(re)operator(.)ident(compile)operator(()ident(pat)operator(\)) + keyword(except) ident(re)operator(.)ident(error)operator(:) + keyword(return) pre_constant(False) + keyword(return) pre_constant(True) + +comment(# ----) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# paragrep - trivial paragraph grepper) +comment(#) +comment(# differs from perl version in parano.) +comment(# python version displays paragraph in current file.) + +keyword(import) ident(sys)operator(,) ident(os)operator(.)ident(path)operator(,) ident(re) +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(<=)integer(1)operator(:) + keyword(print) string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +ident(pat) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +keyword(try)operator(:) + ident(pat_re) operator(=) ident(re)operator(.)ident(compile)operator(()ident(pat)operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(,) ident(pat)operator(,) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\)) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +keyword(for) ident(filename) keyword(in) predefined(filter)operator(()ident(os)operator(.)ident(path)operator(.)ident(isfile)operator(,)ident(sys)operator(.)ident(argv)operator([)integer(2)operator(:)operator(])operator(\))operator(:) + ident(parano) operator(=) integer(0) + keyword(for) ident(para) keyword(in) predefined(open)operator(()ident(filename)operator(\))operator(.)ident(read)operator(()operator(\))operator(.)ident(split)operator(()stringoperator(\))operator(:) + ident(parano) operator(+=) integer(1) + keyword(if) ident(pat_re)operator(.)ident(search)operator(()ident(para)operator(\))operator(:) + keyword(print) ident(filename)operator(,) ident(parano)operator(,) ident(para)operator(,) string + + + +comment(# ----) + +comment(# as we dont evaluate patterns the attack ::) +comment(#) +comment(# $pat = "You lose @{[ system('rm -rf *']} big here";) +comment(#) +comment(# does not work.) + + +comment(# @@PLEAC@@_6.12) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# localeg - demonstrates locale effects) +comment(#) +comment(# re must be told to respect locale either in the regexp) +comment(# "(?L\)" or as flag to the call (python 2.4\) "re.LOCALE".) + +keyword(import) ident(sys) +keyword(import) ident(re)operator(,) ident(string) +keyword(from) ident(locale) keyword(import) ident(LC_CTYPE)operator(,) ident(setlocale)operator(,) ident(getlocale) + +ident(name) operator(=) string +ident(locale) operator(=) operator({)string operator(:) stringoperator(,) string operator(:) stringoperator(}) +comment(# us-ascii is not supported on linux py23) +comment(# none works in activestate py24) + +keyword(try)operator(:) + ident(setlocale)operator(()ident(LC_CTYPE)operator(,) ident(locale)operator([)stringoperator(])operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) ident(locale)operator([)stringoperator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +ident(english_names) operator(=) operator([)operator(]) +keyword(for) ident(n) keyword(in) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(name)operator(\))operator(:) + ident(english_names)operator(.)ident(append)operator(()ident(n)operator(.)ident(capitalize)operator(()operator(\))operator(\)) + +keyword(try)operator(:) + ident(setlocale)operator(()ident(LC_CTYPE)operator(,) ident(locale)operator([)stringoperator(])operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) ident(locale)operator([)stringoperator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +ident(german_names) operator(=) predefined(map)operator(()ident(string)operator(.)ident(capitalize)operator(,) ident(re)operator(.)ident(findall)operator(()stringoperator(,)ident(name)operator(\))operator(\)) + +keyword(print) string operator(%) stringoperator(.)ident(join)operator(()ident(english_names)operator(\)) +keyword(print) string operator(%) stringoperator(.)ident(join)operator(()ident(german_names)operator(\)) + + +comment(# @@PLEAC@@_6.13) +comment(##-----------------------------) +keyword(import) ident(difflib) +ident(matchlist) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(]) +keyword(print) ident(difflib)operator(.)ident(get_close_matches)operator(()stringoperator(,) ident(matchlist)operator(\)) +comment(#=> ['lapel', 'apple', 'ape']) +comment(##-----------------------------) +comment(# Also see:) +comment(# http://www.personal.psu.edu/staff/i/u/iua1/python/apse/) +comment(# http://www.bio.cam.ac.uk/~mw263/pyagrep.html) + +comment(# @@PLEAC@@_6.14) +comment(##-----------------------------) +comment(# To search (potentially\) repeatedly for a pattern, use re.finditer(\):) + +comment(# DO NOT DO THIS. Split on commas and convert elems using int(\)) +ident(mystr) operator(=) string +keyword(for) ident(match) keyword(in) ident(re)operator(.)ident(finditer)operator(()stringoperator(,) ident(mystr)operator(\))operator(:) + ident(n) operator(=) ident(match)operator(.)ident(group)operator(()integer(0)operator(\)) + keyword(if) ident(n) operator(==) stringoperator(:) + keyword(break) comment(# '120' will never be matched) + keyword(print) stringoperator(,) ident(n) + +comment(# matches know their end position) +ident(mystr) operator(=) string +ident(x) operator(=) ident(re)operator(.)ident(finditer)operator(()stringoperator(,) ident(mystr)operator(\)) +keyword(for) ident(match) keyword(in) ident(x)operator(:) + ident(n) operator(=) ident(match)operator(.)ident(group)operator(()integer(0)operator(\)) + keyword(print) stringoperator(,) ident(n) + +ident(tail) operator(=) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(mystr)operator([)ident(match)operator(.)ident(end)operator(()operator(\))operator(:)operator(])operator(\)) +keyword(if) ident(tail)operator(:) + keyword(print) stringoperator(%)ident(tail)operator(.)ident(group)operator(()integer(0)operator(\)) + + +comment(# @@PLEAC@@_6.15) +comment(# Python's regexes are based on Perl's, so it has the non-greedy ) +comment(# '*?', '+?', and '??' versions of '*', '+', and '?'.) +comment(# DO NOT DO THIS. import htmllib, formatter, etc, instead) +comment(#-----------------------------) +comment(# greedy pattern) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()string)delimiter(")>operator(,) stringoperator(,) ident(txt)operator(\)) comment(# try to remove tags, very badly) + +comment(# non-greedy pattern) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()string)delimiter(")>operator(,) stringoperator(,) ident(txt)operator(\)) comment(# try to remove tags, still rather badly) +comment(#-----------------------------) +ident(txt) operator(=) stringthis and that are important Oh, me too!)delimiter(")> + +keyword(print) ident(re)operator(.)ident(findall)operator(()string(.*?\))delimiter(")>operator(,) ident(txt) +comment(##-----------------------------) +keyword(print) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +keyword(print) ident(re)operator(.)ident(findall)operator(()string((?:(?!|\).\)*\))delimiter(")>operator(,) ident(txt)operator(\)) +comment(##-----------------------------) +keyword(print) ident(re)operator(.)ident(findall)operator(()string((?:(?!<[ib]>\).\)*\))delimiter(")>operator(,) ident(txt)operator(\)) +comment(##-----------------------------) +keyword(print) ident(re)operator(.)ident(findall)operator(()string )content( +)content( [^<]* # stuff not possibly bad, and not possibly the end.)content( +)content( (?: # at this point, we can have '<' if not part of something bad)content( +)content( (?! \) # what we can't have)content( +)content( < # okay, so match the '<')content( +)content( [^<]* # and continue with more safe stuff)content( +)content( \) *)content( +)content( )content( +)content( )delimiter(""")>operator(,) ident(re)operator(.)ident(VERBOSE)operator(,) ident(txt)operator(\)) +comment(##-----------------------------) + +comment(# @@PLEAC@@_6.16) +comment(##-----------------------------) +ident(text) operator(=) string +ident(words) operator(=) ident(text)operator(.)ident(split)operator(()operator(\)) +keyword(for) ident(curr)operator(,) predefined(next) keyword(in) predefined(zip)operator(()ident(words)operator([)operator(:)operator(-)integer(1)operator(])operator(,) ident(words)operator([)integer(1)operator(:)operator(])operator(\))operator(:) + keyword(if) ident(curr)operator(.)ident(upper)operator(()operator(\)) operator(==) predefined(next)operator(.)ident(upper)operator(()operator(\))operator(:) + keyword(print) string operator(%) ident(curr) + +comment(# DON'T DO THIS) +keyword(import) ident(re) +ident(pat) operator(=) string +keyword(for) ident(match) keyword(in) ident(re)operator(.)ident(finditer)operator(()ident(pat)operator(,) ident(text)operator(,) ident(flags)operator(=)ident(re)operator(.)ident(VERBOSE)operator(|)ident(re)operator(.)ident(IGNORECASE)operator(\))operator(:) + keyword(print) string operator(%) ident(match)operator(.)ident(group)operator(()integer(1)operator(\)) +comment(##-----------------------------) +ident(a) operator(=) stringoperator(;) +ident(b) operator(=) stringoperator(;) + +ident(text) operator(=) ident(a)operator(+)stringoperator(+)ident(b) +ident(pat) operator(=) string +keyword(for) ident(match) keyword(in) ident(re)operator(.)ident(finditer)operator(()ident(pat)operator(,) ident(text)operator(\))operator(:) + ident(m1)operator(,) ident(m2)operator(,) ident(m3) operator(=) ident(match)operator(.)ident(groups)operator(()operator(\)) + keyword(print) ident(m2)operator(,) stringoperator(%)operator(()ident(m1)operator(,) ident(m2)operator(,) ident(m3)operator(\)) +comment(##-----------------------------) +ident(pat) operator(=) string +comment(##-----------------------------) +keyword(try)operator(:) + keyword(while) pre_constant(True)operator(:) + ident(factor) operator(=) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(n)operator(\))operator(.)ident(group)operator(()integer(1)operator(\)) + ident(n) operator(=) ident(re)operator(.)ident(sub)operator(()ident(factor)operator(,) stringoperator(,) ident(n)operator(\)) + keyword(print) predefined(len)operator(()ident(factor)operator(\)) +keyword(except) exception(AttributeError)operator(:) + keyword(print) predefined(len)operator(()ident(n)operator(\)) +comment(##-----------------------------) +keyword(def) ident(diaphantine)operator(()ident(n)operator(,) ident(x)operator(,) ident(y)operator(,) ident(z)operator(\))operator(:) + ident(pat) operator(=) stringoperator(%)operator(()ident(x)operator(-)integer(1)operator(,) ident(y)operator(-)integer(1)operator(,) ident(z)operator(-)integer(1)operator(\)) + ident(text) operator(=) stringoperator(*)ident(n) + keyword(try)operator(:) + ident(vals) operator(=) operator([)predefined(len)operator(()ident(v)operator(\)) keyword(for) ident(v) keyword(in) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) ident(text)operator(\))operator(.)ident(groups)operator(()operator(\))operator(]) + keyword(except) exception(ValueError)operator(:) + keyword(print) string + keyword(else)operator(:) + keyword(print) stringoperator(%)predefined(tuple)operator(()ident(vals)operator(\)) + +ident(diaphantine)operator(()ident(n)operator(=)integer(281)operator(,) ident(x)operator(=)integer(12)operator(,) ident(y)operator(=)integer(15)operator(,) ident(z)operator(=)integer(16)operator(\)) + +comment(# @@PLEAC@@_6.17) +comment(##-----------------------------) +comment(# Pass any of the following patterns to re.match(\), etc) +ident(pat) operator(=) string +ident(pat) operator(=) string +ident(pat) operator(=) string +ident(pat) operator(=) string +ident(pat) operator(=) string +comment(##-----------------------------) +keyword(if) keyword(not) ident(re)operator(.)ident(match)operator(()ident(pattern)operator(,) ident(text)operator(\))operator(:) + ident(something)operator(()operator(\)) +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()ident(pat1)operator(,) ident(text)operator(\)) keyword(and) ident(re)operator(.)ident(match)operator(()ident(pat2)operator(,) ident(text)operator(\))operator(:) + ident(something)operator(()operator(\)) +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()ident(pat1)operator(,) ident(text)operator(\)) keyword(or) ident(re)operator(.)ident(match)operator(()ident(pat2)operator(,) ident(text)operator(\))operator(:) + ident(something)operator(()operator(\)) +comment(##-----------------------------) +comment(# DON'T DO THIS.) +string +keyword(import) ident(sys)operator(,) ident(re) + +ident(pat) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +keyword(for) ident(line) keyword(in) ident(sys)operator(.)ident(stdin)operator(:) + keyword(if) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) ident(line)operator(\))operator(:) + keyword(print) ident(line)operator([)operator(:)operator(-)integer(1)operator(]) +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) stringoperator(\))operator(:) + ident(something)operator(()operator(\)) +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(s)operator(\)) keyword(and) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(s)operator(\))operator(:) + ident(something)operator(()operator(\)) +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) + ident(murray_hill)operator(,) + ident(re)operator(.)ident(DOTALL) operator(|) ident(re)operator(.)ident(VERBOSE)operator(\))operator(:) + keyword(print) string +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) stringoperator(\))operator(:) + ident(something)operator(()operator(\)) +comment(##-----------------------------) +ident(brand) operator(=) string +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) + ident(brand)operator(,) + ident(re)operator(.)ident(DOTALL) operator(|) ident(re)operator(.)ident(VERBOSE)operator(\))operator(:) + keyword(print) string +comment(##-----------------------------) +ident(x) operator(=) string +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(x)operator(\))operator(:) + keyword(print) string +comment(##-----------------------------) +keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) + ident(x)operator(,) + ident(re)operator(.)ident(VERBOSE) operator(|) ident(re)operator(.)ident(DOTALL)operator(\))operator(:) + keyword(print) stringoperator(;) +comment(##-----------------------------) + +comment(# @@PLEAC@@_6.18) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_6.19) +comment(##-----------------------------) +keyword(from) ident(email)operator(.)ident(_parseaddr) keyword(import) ident(AddressList) + +keyword(print) ident(AddressList)operator(()stringoperator(\))operator(.)ident(addresslist)operator([)integer(0)operator(]) + +keyword(print) ident(AddressList)operator(()stringoperator(\))operator(.)ident(addresslist)operator([)integer(0)operator(]) + +ident(name)operator(,) ident(address) operator(=) ident(AddressList)operator(()string)delimiter(")>operator(\))operator(.)ident(addresslist)operator([)integer(0)operator(]) +keyword(print) stringoperator(%)operator(()ident(name)operator(,) ident(address)operator(\)) + +comment(# @@PLEAC@@_6.20) +comment(##-----------------------------) +comment(# Assuming the strings all start with different letters, or you don't) +comment(# mind there being precedence, use the startswith string method:) + +keyword(def) ident(get_action)operator(()ident(answer)operator(\))operator(:) + ident(answer) operator(=) ident(answer)operator(.)ident(lower)operator(()operator(\)) + ident(actions) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(]) + keyword(for) ident(action) keyword(in) ident(actions)operator(:) + keyword(if) ident(action)operator(.)ident(startswith)operator(()ident(answer)operator(\))operator(:) + keyword(return) ident(action) + +keyword(print) stringoperator(%)ident(get_action)operator(()stringoperator(\)) +comment(#=> Action is list.) +comment(##-----------------------------) +comment(#DON'T DO THIS:) +keyword(import) ident(re) +ident(answer) operator(=) string +ident(answer) operator(=) ident(re)operator(.)ident(escape)operator(()ident(answer)operator(.)ident(strip)operator(()operator(\))operator(\)) +keyword(for) ident(action) keyword(in) operator(()stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(\))operator(:) + keyword(if) ident(re)operator(.)ident(match)operator(()ident(answer)operator(,) ident(action)operator(,) ident(flags)operator(=)ident(re)operator(.)ident(IGNORECASE)operator(\))operator(:) + keyword(print) stringoperator(%)ident(action)operator(.)ident(lower)operator(()operator(\)) +comment(##-----------------------------) +keyword(import) ident(re)operator(,) ident(sys) +keyword(def) ident(handle_cmd)operator(()ident(cmd)operator(\))operator(:) + ident(cmd) operator(=) ident(re)operator(.)ident(escape)operator(()ident(cmd)operator(.)ident(strip)operator(()operator(\))operator(\)) + keyword(for) ident(name)operator(,) ident(action) keyword(in) operator({)stringoperator(:) ident(invoke_editor)operator(,) + stringoperator(:) ident(deliver_message)operator(,) + stringoperator(:) keyword(lambda)operator(:) ident(system)operator(()ident(pager)operator(,) ident(myfile)operator(\))operator(,) + stringoperator(:) ident(sys)operator(.)ident(exit)operator(,) + operator(}) + keyword(if) ident(re)operator(.)ident(match)operator(()ident(cmd)operator(,) ident(name)operator(,) ident(flags)operator(=)ident(re)operator(.)ident(IGNORECASE)operator(\))operator(:) + ident(action)operator(()operator(\)) + keyword(break) + keyword(else)operator(:) + keyword(print) stringoperator(,) ident(cmd) +ident(handle_cmd)operator(()stringoperator(\)) + +comment(# @@PLEAC@@_6.21) +comment(##-----------------------------) +comment(# urlify - wrap HTML links around URL-like constructs) +keyword(import) ident(re)operator(,) ident(sys)operator(,) ident(fileinput) + +keyword(def) ident(urlify_string)operator(()ident(s)operator(\))operator(:) + ident(urls) operator(=) string + + ident(ltrs) operator(=) stringoperator(;) + ident(gunk) operator(=) string + ident(punc) operator(=) string + predefined(any) operator(=) ident(ltrs) operator(+) ident(gunk) operator(+) ident(punc) + + ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(%)predefined(locals)operator(()operator(\))operator(,) ident(re)operator(.)ident(VERBOSE) operator(|) ident(re)operator(.)ident(IGNORECASE)operator(\)) + keyword(return) ident(re)operator(.)ident(sub)operator(()ident(pat)operator(,) string)content(\\1)content()delimiter(")>operator(,) ident(s)operator(\)) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(print) ident(urlify_string)operator(()ident(line)operator(\)) + + +comment(# @@PLEAC@@_6.22) +comment(##-----------------------------) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_6.23) +comment(# The majority of regexes in this section are either partially) +comment(# or completely The Wrong Thing to Do.) +comment(##-----------------------------) +comment(# DON'T DO THIS. Use a Roman Numeral module, etc. (since) +comment(# you need one anyway to calculate values\)) +ident(pat) operator(=) string +ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) stringoperator(\)) +comment(##-----------------------------) +ident(txt) operator(=) string + +comment(# If the words are cleanly delimited just split and rejoin:) +ident(word1)operator(,) ident(word2)operator(,) ident(rest) operator(=) ident(txt)operator(.)ident(split)operator(()stringoperator(,) integer(2)operator(\)) +keyword(print) stringoperator(.)ident(join)operator(()operator([)ident(word2)operator(,) ident(word1)operator(,) ident(rest)operator(])operator(\)) + +comment(# Otherwise:) +ident(frompat) operator(=) string +ident(topat) operator(=) string +keyword(print) ident(re)operator(.)ident(sub)operator(()ident(frompat)operator(,) ident(topat)operator(,) ident(txt)operator(\)) + +comment(##-----------------------------) +keyword(print) predefined(str)operator(.)ident(split)operator(()stringoperator(\)) + +comment(# DON'T DO THIS) +ident(pat) operator(=) string +keyword(print) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) stringoperator(\))operator(.)ident(groups)operator(()operator(\)) +comment(##-----------------------------) +ident(line) operator(=) string +keyword(if) predefined(len)operator(()ident(line)operator(\)) operator(>) integer(80)operator(:) + ident(process)operator(()ident(line)operator(\)) + +comment(# DON'T DO THIS) +ident(pat) operator(=) string +keyword(if) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) ident(line)operator(\))operator(:) + ident(process)operator(()ident(line)operator(\)) +comment(##-----------------------------) +ident(dt) operator(=) ident(time)operator(.)ident(strptime)operator(()stringoperator(,) stringoperator(\)) + +comment(# DON'T DO THIS) +ident(pat) operator(=) string +ident(dt) operator(=) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) stringoperator(\))operator(.)ident(groups)operator(()operator(\)) +comment(##-----------------------------) +ident(txt) operator(=) string +keyword(print) ident(txt)operator(.)ident(replace)operator(()stringoperator(,) stringoperator(\)) +comment(# Alternatively for file operations use os.path, shutil, etc.) + +comment(# DON'T DO THIS) +keyword(print) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +keyword(import) ident(re) + +keyword(def) ident(unescape_hex)operator(()ident(matchobj)operator(\))operator(:) + keyword(return) predefined(chr)operator(()predefined(int)operator(()ident(matchobj)operator(.)ident(groups)operator(()integer(0)operator(\))operator([)integer(0)operator(])operator(,) integer(16)operator(\))operator(\)) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) ident(unescape_hex)operator(,) ident(txt)operator(\)) + +comment(# Assuming that the hex escaping is well-behaved, an alternative is:) +keyword(def) ident(unescape_hex)operator(()ident(seg)operator(\))operator(:) + keyword(return) predefined(chr)operator(()predefined(int)operator(()ident(seg)operator([)operator(:)integer(2)operator(])operator(,) integer(16)operator(\))operator(\)) operator(+) ident(seg)operator([)integer(2)operator(:)operator(]) + +ident(segs) operator(=) ident(txt)operator(.)ident(split)operator(()stringoperator(\)) +ident(txt) operator(=) ident(segs)operator([)integer(0)operator(]) operator(+) stringoperator(.)ident(join)operator(()ident(unescape_hex)operator(()ident(seg)operator(\)) keyword(for) ident(seg) keyword(in) ident(segs)operator([)integer(1)operator(:)operator(])operator(\)) +comment(##-----------------------------) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(,) ident(re)operator(.)ident(VERBOSE)operator(\)) +comment(##-----------------------------) +ident(txt)operator(.)ident(strip)operator(()operator(\)) + +comment(# DON'T DO THIS) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(\)) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +ident(txt)operator(.)ident(replace)operator(()stringoperator(,) stringoperator(\)) + +comment(# DON'T DO THIS) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(\)) +comment(##-----------------------------) +keyword(import) ident(socket) +ident(socket)operator(.)ident(inet_aton)operator(()ident(txt)operator(\)) comment(# Will raise an error if incorrect) + +comment(# DON'T DO THIS.) +ident(octseg) operator(=)string +ident(dot) operator(=) string +ident(pat) operator(=) string operator(+) ident(octseg) operator(+) ident(dot) operator(+) ident(octseg) operator(+) ident(dot) operator(+) ident(octseg) operator(+) ident(dot) operator(+) ident(octseg) operator(+) string + +keyword(if) keyword(not) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) ident(txt)operator(,) ident(re)operator(.)ident(VERBOSE)operator(\)) + keyword(raise) exception(ValueError) + +comment(# Defitely DON'T DO THIS.) +ident(pat) operator(=) string +comment(##-----------------------------) +ident(fname) operator(=) ident(os)operator(.)ident(path)operator(.)ident(basename)operator(()ident(path)operator(\)) + +comment(# DON'T DO THIS.) +ident(fname) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(path)operator(\)) +comment(##-----------------------------) +keyword(import) ident(os) +keyword(try)operator(:) + ident(tc) operator(=) ident(os)operator(.)ident(environ)operator([)stringoperator(]) +keyword(except) exception(KeyError)operator(:) + ident(cols) operator(=) integer(80) +keyword(else)operator(:) + ident(cols) operator(=) ident(re)operator(.)ident(match)operator(()stringoperator(\))operator(.)ident(groups)operator(()integer(1)operator(\)) +comment(##-----------------------------) +comment(# (not quite equivalent to the Perl version\)) +ident(name) operator(=) ident(os)operator(.)ident(path)operator(.)ident(basename)operator(()ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) + +comment(# DON'T DO THIS.) +ident(name) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) +comment(##-----------------------------) +keyword(if) ident(sys)operator(.)ident(platform) operator(!=) stringoperator(:) + keyword(raise) exception(SystemExit)operator(()stringoperator(\)) +comment(##-----------------------------) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(\)) + +comment(# In many cases you could just use:) +ident(txt) operator(=) ident(txt)operator(.)ident(replace)operator(()stringoperator(,) stringoperator(\)) +comment(##-----------------------------) +ident(nums) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +comment(# If the words are clearly delimited just use:) +ident(capwords) operator(=) operator([)ident(word) keyword(for) ident(word) keyword(in) ident(txt)operator(.)ident(split)operator(()operator(\)) keyword(if) ident(word)operator(.)ident(isupper)operator(()operator(\))operator(]) + +comment(# Otherwise) +ident(capwords) operator(=) operator([)ident(word) keyword(for) ident(word) keyword(in) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) keyword(if) ident(word)operator(.)ident(isupper)operator(()operator(\))operator(]) + +comment(# (probably\) DON'T DO THIS. ) +ident(capwords) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +comment(# If the words are clearly delimited just use:) +ident(lowords) operator(=) operator([)ident(word) keyword(for) ident(word) keyword(in) ident(txt)operator(.)ident(split)operator(()operator(\)) keyword(if) ident(word)operator(.)ident(islower)operator(()operator(\))operator(]) + +comment(# Otherwise) +ident(lowords) operator(=) operator([)ident(word) keyword(for) ident(word) keyword(in) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) keyword(if) ident(word)operator(.)ident(islower)operator(()operator(\))operator(]) + +comment(# (probably\) DON'T DO THIS. ) +ident(lowords) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +comment(# If the words are clearly delimited just use:) +ident(icwords) operator(=) operator([)ident(word) keyword(for) ident(word) keyword(in) ident(txt)operator(.)ident(split)operator(()operator(\)) keyword(if) ident(word)operator(.)ident(istitle)operator(()operator(\))operator(]) + +comment(# Otherwise) +ident(icwords) operator(=) operator([)ident(word) keyword(for) ident(word) keyword(in) ident(re)operator(.)ident(finditer)operator(()stringoperator(\)) keyword(if) ident(word)operator(.)ident(istitle)operator(()operator(\))operator(]) + +comment(# DON'T DO THIS. ) +ident(icwords) operator(=) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +comment(# DON'T DO THIS - use HTMLParser, etc.) +ident(links) operator(=) ident(re)operator(.)ident(findall)operator(()string]+?HREF)content(\\s)content(*=)content(\\s)content(*["']?([^'" >]+?\)[ '"]?>)delimiter(""")>operator(,) ident(txt)operator(\)) +comment(##-----------------------------) +ident(names) operator(=) ident(txt)operator(.)ident(split)operator(()operator(\)) +keyword(if) predefined(len)operator(()ident(names)operator(\)) operator(==) integer(3)operator(:) + ident(initial) operator(=) ident(names)operator([)integer(1)operator(])operator([)integer(0)operator(]) +keyword(else)operator(:) + ident(initial) operator(=) string + +comment(# DON'T DO THIS. ) +ident(pat) operator(=) string +keyword(try)operator(:) + ident(initial) operator(=) ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) ident(txt)operator(\))operator(.)ident(group)operator(()integer(1)operator(\)) +keyword(except) exception(AttributeError)operator(:) + ident(initial) operator(=) string +comment(##-----------------------------) +ident(txt) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(txt)operator(\)) +comment(##-----------------------------) +ident(sentences) operator(=) operator([)ident(elem)operator([)integer(0)operator(]) keyword(for) ident(elem) keyword(in) ident(re)operator(.)ident(findall)operator(()stringoperator(,) ident(s)operator(\))operator(]) +comment(##-----------------------------) +keyword(import) ident(time) +ident(dt) operator(=) ident(time)operator(.)ident(strptime)operator(()ident(txt)operator(,) stringoperator(\)) + +comment(# DON'T DO THIS.) +ident(year)operator(,) ident(month)operator(,) ident(day) operator(=) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(txt)operator(\))operator(.)ident(groups)operator(()operator(\)) +comment(##-----------------------------) +ident(pat) operator(=) string +ident(re)operator(.)ident(match)operator(()ident(pat)operator(,) ident(txt)operator(,) ident(re)operator(.)ident(VERBOSE)operator(\)) +comment(##-----------------------------) +ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(txt)operator(,) ident(re)operator(.)ident(IGNORECASE)operator(\)) +comment(##-----------------------------) +keyword(for) ident(line) keyword(in) predefined(file)operator(()ident(fname)operator(,) stringoperator(\))operator(:) comment(#Universal newlines) + ident(process)operator(()ident(line)operator(\)) + +comment(# DON'T DO THIS) +ident(lines) operator(=) operator([)ident(re)operator(.)ident(sub)operator(()stringoperator(,) stringoperator(,) ident(line)operator(\)) + keyword(for) ident(line) keyword(in) predefined(file)operator(()ident(fname)operator(\))operator(]) +comment(##-----------------------------) + + +comment(# @@PLEAC@@_7.0) +keyword(for) ident(line) keyword(in) predefined(open)operator(()stringoperator(\))operator(:) + keyword(if) ident(blue) keyword(in) ident(line)operator(:) + keyword(print) ident(line)operator([)operator(:)operator(-)integer(1)operator(]) +comment(#---------) +keyword(import) ident(sys)operator(,) ident(re) +ident(pattern) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +keyword(for) ident(line) keyword(in) ident(sys)operator(.)ident(stdin)operator(:) + keyword(if) keyword(not) ident(pattern)operator(.)ident(search)operator(()ident(line)operator(\))operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()stringoperator(\)) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()string operator(+) ident(line)operator(\)) +ident(sys)operator(.)ident(stdout)operator(.)ident(close)operator(()operator(\)) +comment(#---------) +ident(logfile) operator(=) predefined(open)operator(()stringoperator(,) stringoperator(\)) +comment(#---------) +ident(logfile)operator(.)ident(close)operator(()operator(\)) +comment(#---------) +keyword(print)operator(>>)ident(logfile)operator(,) string +keyword(print) string + +comment(# DONT DO THIS) +keyword(import) ident(sys) +ident(old_output)operator(,) ident(sys)operator(.)ident(stdout) operator(=) ident(sys)operator(.)ident(stdout)operator(,) ident(logfile) +keyword(print) string +ident(sys)operator(.)ident(stdout) operator(=) ident(old_output) +keyword(print) string +comment(#---------) + +comment(# @@PLEAC@@_7.1) +comment(# Python's open(\) function somewhat covers both perl's open(\) and ) +comment(# sysopen(\) as it has optional arguments for mode and buffering.) +ident(source) operator(=) predefined(open)operator(()ident(path)operator(\)) +ident(sink) operator(=) predefined(open)operator(()ident(path)operator(,) stringoperator(\)) +comment(#---------) +comment(# NOTE: almost no one uses the low-level os.open and os.fdopen) +comment(# commands, so their inclusion here is just silly. If ) +comment(# os.fdopen(os.open(...\)\) were needed often, it would be turned) +comment(# into its own function. Instead, I'll use 'fd' to hint that) +comment(# os.open returns a file descriptor) +keyword(import) ident(os) +ident(source_fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDONLY)operator(\)) +ident(source) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(fd)operator(\)) +ident(sink_fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(\)) +ident(sink) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(sink_fd)operator(\)) +comment(#---------) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(filename)operator(,) ident(os)operator(.)ident(O_WRONLY) operator(|) ident(os)operator(.)ident(O_CREAT)operator(\)) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +comment(#---------) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(name)operator(,) ident(flags)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(name)operator(,) ident(flags)operator(,) ident(mode)operator(\)) +comment(#---------) +ident(myfile) operator(=) predefined(open)operator(()ident(path)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDONLY)operator(\)) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(path)operator(,) stringoperator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_TRUNC)operator(|)ident(os)operator(.)ident(O_CREAT)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_TRUNC)operator(|)ident(os)operator(.)ident(O_CREAT)operator(,) oct(0600)operator(\)) +comment(#-----------------------------) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_EXCL)operator(|)ident(os)operator(.)ident(O_CREAT)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_EXCL)operator(|)ident(os)operator(.)ident(O_CREAT)operator(,) oct(0600)operator(\)) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(path)operator(,) stringoperator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_APPEND)operator(|)ident(os)operator(.)ident(O_CREAT)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_APPEND)operator(|)ident(s)operator(.)ident(O_CREAT)operator(,) oct(0600)operator(\)) +comment(#-----------------------------) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_WRONLY)operator(|)ident(os)operator(.)ident(O_APPEND)operator(\)) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(path)operator(,) stringoperator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDWR)operator(\)) +comment(#-----------------------------) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDWR)operator(|)ident(os)operator(.)ident(O_CREAT)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDWR)operator(|)ident(os)operator(.)ident(O_CREAT)operator(,) oct(0600)operator(\)) +comment(#-----------------------------) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDWR)operator(|)ident(os)operator(.)ident(O_EXCL)operator(|)ident(os)operator(.)ident(O_CREAT)operator(\)) +ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(path)operator(,) ident(os)operator(.)ident(O_RDWR)operator(|)ident(os)operator(.)ident(O_EXCL)operator(|)ident(os)operator(.)ident(O_CREAT)operator(,) oct(0600)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_7.2) +comment(# Nothing different needs to be done with Python) + +comment(# @@PLEAC@@_7.3) +keyword(import) ident(os) +ident(filename) operator(=) ident(os)operator(.)ident(path)operator(.)ident(expanduser)operator(()ident(filename)operator(\)) + +comment(# @@PLEAC@@_7.4) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(\)) comment(# raise an exception on error) + +keyword(try)operator(:) + ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(\)) +keyword(except) exception(IOError)operator(,) ident(err)operator(:) + keyword(raise) exception(AssertionError)operator(()string operator(%) + operator(()ident(filename)operator(,) ident(err)operator(.)ident(strerror)operator(\))operator(\)) + +comment(# @@PLEAC@@_7.5) +keyword(import) ident(tempfile) + +ident(myfile) operator(=) ident(tempfile)operator(.)ident(TemporaryFile)operator(()operator(\)) + +comment(#-----------------------------) +comment(# NOTE: The TemporaryFile(\) call is much more appropriate) +comment(# I would not suggest using this code for real work.) +keyword(import) ident(os)operator(,) ident(tempfile) + +keyword(while) pre_constant(True)operator(:) + ident(name) operator(=) ident(os)operator(.)ident(tmpnam)operator(()operator(\)) + keyword(try)operator(:) + ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(name)operator(,) ident(os)operator(.)ident(O_RDWR)operator(|)ident(os)operator(.)ident(O_CREAT)operator(|)ident(os)operator(.)ident(O_EXCL)operator(\)) + keyword(break) + keyword(except) ident(os)operator(.)ident(error)operator(:) + keyword(pass) +ident(myfile) operator(=) ident(tempfile)operator(.)ident(TemporaryFileWrapper)operator(()ident(os)operator(.)ident(fdopen)operator(()ident(fd)operator(\))operator(,) ident(name)operator(\)) + +comment(# now go on to use the file ...) +comment(#-----------------------------) +keyword(import) ident(os) +keyword(while) pre_constant(True)operator(:) + ident(tmpname) operator(=) ident(os)operator(.)ident(tmpnam)operator(()operator(\)) + ident(fd) operator(=) ident(os)operator(.)ident(open)operator(()ident(tmpnam)operator(,) ident(os)operator(.)ident(O_RDWR) operator(|) ident(os)operator(.)ident(O_CREAT) operator(|) ident(os)operator(.)ident(O_EXCL)operator(\)) + keyword(if) ident(fd)operator(:) + ident(tmpfile) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(fd)operator(\)) + keyword(break) + +ident(os)operator(.)ident(remove)operator(()ident(tmpnam)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(tempfile) + +ident(myfile) operator(=) ident(tempfile)operator(.)ident(TemporaryFile)operator(()ident(bufsize) operator(=) integer(0)operator(\)) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(10)operator(\))operator(:) + keyword(print)operator(>>)ident(myfile)operator(,) ident(i) +ident(myfile)operator(.)ident(seek)operator(()integer(0)operator(\)) +keyword(print) stringoperator(,) ident(myfile)operator(.)ident(read)operator(()operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_7.6) +ident(DATA) operator(=) string +keyword(for) ident(line) keyword(in) ident(DATA)operator(.)ident(split)operator(()stringoperator(\))operator(:) + keyword(pass) comment(# process the line) + +comment(# @@PLEAC@@_7.7) + +keyword(for) ident(line) keyword(in) ident(sys)operator(.)ident(stdin)operator(:) + keyword(pass) comment(# do something with the line) + +comment(# processing a list of files from commandline) +keyword(import) ident(fileinput) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(do) ident(something) keyword(with) ident(the) ident(line) + +comment(#-----------------------------) +keyword(import) ident(sys) + +keyword(def) ident(do_with)operator(()ident(myfile)operator(\))operator(:) + keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + keyword(print) ident(line)operator([)operator(:)operator(-)integer(1)operator(]) + +ident(filenames) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) +keyword(if) ident(filenames)operator(:) + keyword(for) ident(filename) keyword(in) ident(filenames)operator(:) + keyword(try)operator(:) + ident(do_with)operator(()predefined(open)operator(()ident(filename)operator(\))operator(\)) + keyword(except) exception(IOError)operator(,) ident(err)operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()string operator(%) operator(()ident(filename)operator(,) ident(err)operator(.)ident(strerror)operator(\))operator(\)) + keyword(continue) +keyword(else)operator(:) + ident(do_with)operator(()ident(sys)operator(.)ident(stdin)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(sys)operator(,) ident(glob) +ident(ARGV) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) keyword(or) ident(glob)operator(.)ident(glob)operator(()stringoperator(\)) +comment(#-----------------------------) +comment(# NOTE: the getopt module is the prefered mechanism for reading) +comment(# command line arguments) +keyword(import) ident(sys) +ident(args) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) +ident(chop_first) operator(=) integer(0) + +keyword(if) ident(args) keyword(and) ident(args)operator([)integer(0)operator(]) operator(==) stringoperator(:) + ident(chop_first) operator(+=) integer(1) + ident(args) operator(=) ident(args)operator([)integer(1)operator(:)operator(]) + +comment(# arg demo 2: Process optional -NUMBER flag) + +comment(# NOTE: You just wouldn't process things this way for Python,) +comment(# but I'm trying to preserve the same semantics.) + +keyword(import) ident(sys)operator(,) ident(re) +ident(digit_pattern) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + +ident(args) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) +keyword(if) ident(args)operator(:) + ident(match) operator(=) ident(digit_pattern)operator(.)ident(match)operator(()ident(args)operator([)integer(0)operator(])operator(\)) + keyword(if) ident(match)operator(:) + ident(columns) operator(=) predefined(int)operator(()ident(match)operator(.)ident(group)operator(()integer(1)operator(\))operator(\)) + ident(args) operator(=) ident(args)operator([)integer(1)operator(:)operator(]) + +comment(# NOTE: here's the more idiomatic way, which also checks) +comment(# for the "--" or a non "-" argument to stop processing) + +ident(args) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) +keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(args)operator(\))operator(\))operator(:) + ident(arg) operator(=) ident(args)operator([)ident(i)operator(]) + keyword(if) ident(arg) operator(==) string keyword(or) keyword(not) ident(arg)operator(.)ident(startwith)operator(()stringoperator(\))operator(:) + keyword(break) + keyword(if) ident(arg)operator([)integer(1)operator(:)operator(])operator(.)ident(isdigit)operator(()operator(\))operator(:) + ident(columns) operator(=) predefined(int)operator(()ident(arg)operator([)integer(1)operator(:)operator(])operator(\)) + keyword(continue) + + + +comment(# arg demo 3: Process clustering -a, -i, -n, or -u flags) +keyword(import) ident(sys)operator(,) ident(getopt) +keyword(try)operator(:) + ident(args)operator(,) ident(filenames) operator(=) ident(getopt)operator(.)ident(getopt)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(,) stringoperator(\)) +keyword(except) ident(getopt)operator(.)ident(error)operator(:) + keyword(raise) exception(SystemExit)operator(()string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) + +ident(append) operator(=) ident(ignore_ints) operator(=) ident(nostdout) operator(=) ident(unbuffer) operator(=) integer(0) +keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(args)operator(:) + keyword(if) ident(k) operator(==) stringoperator(:) ident(append) operator(+=) integer(1) + keyword(elif) ident(k) operator(==) stringoperator(:) ident(ignore_ints) operator(+=) integer(1) + keyword(elif) ident(k) operator(==) stringoperator(:) ident(nostdout) operator(+=) integer(1) + keyword(elif) ident(k) operator(==) stringoperator(:) ident(unbuffer) operator(+=) integer(1) + keyword(else)operator(:) + keyword(raise) exception(AssertionError)operator(()string operator(%) ident(k)operator(\)) + +comment(#-----------------------------) +comment(# Note: Idiomatic Perl get translated to idiomatic Python) +keyword(import) ident(fileinput) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()string operator(%) + operator(()ident(fileinput)operator(.)ident(filename)operator(()operator(\))operator(,) ident(fileinput)operator(.)ident(filelineno)operator(()operator(\))operator(,) ident(line)operator(\))operator(\)) +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# findlogin1 - print all lines containing the string "login") +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) comment(# loop over files on command line) + keyword(if) ident(line)operator(.)ident(find)operator(()stringoperator(\)) operator(!=) operator(-)integer(1)operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(line)operator(\)) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# lowercase - turn all lines into lowercase) +comment(### NOTE: I don't know how to do locales in Python) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) comment(# loop over files on command line) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(line)operator(.)ident(lower)operator(()operator(\))operator(\)) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# NOTE: The Perl code appears buggy, in that "Q__END__W" is considered) +comment(# to be a __END__ and words after the __END__ on the same line) +comment(# are included in the count!!!) +comment(# countchunks - count how many words are used.) +comment(# skip comments, and bail on file if __END__) +comment(# or __DATA__ seen.) +ident(chunks) operator(=) integer(0) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(for) ident(word) keyword(in) ident(line)operator(.)ident(split)operator(()operator(\))operator(:) + keyword(if) ident(word)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + keyword(continue) + keyword(if) ident(word) keyword(in) operator(()stringoperator(,) stringoperator(\))operator(:) + ident(fileinput)operator(.)ident(close)operator(()operator(\)) + keyword(break) + ident(chunks) operator(+=) integer(1) +keyword(print) stringoperator(,) ident(chunks)operator(,) string + + +comment(# @@PLEAC@@_7.8) +keyword(import) ident(shutil) + +ident(old) operator(=) predefined(open)operator(()stringoperator(\)) +ident(new) operator(=) predefined(open)operator(()stringoperator(,)stringoperator(\)) + +keyword(for) ident(line) keyword(in) ident(old)operator(:) + ident(new)operator(.)ident(writeline)operator(()ident(line)operator(\)) +ident(new)operator(.)ident(close)operator(()operator(\)) +ident(old)operator(.)ident(close)operator(()operator(\)) + +ident(shutil)operator(.)ident(copyfile)operator(()stringoperator(,) stringoperator(\)) +ident(shutil)operator(.)ident(copyfile)operator(()stringoperator(,) stringoperator(\)) + +comment(# insert lines at line 20:) +keyword(for) ident(i)operator(,) ident(line) keyword(in) predefined(enumerate)operator(()ident(old)operator(\))operator(:) + keyword(if) ident(i) operator(==) integer(20)operator(:) + keyword(print)operator(>>)ident(new)operator(,) string + keyword(print)operator(>>)ident(new)operator(,) string + keyword(print)operator(>>)ident(new)operator(,) ident(line) + + +comment(# or delete lines 20 through 30:) +keyword(for) ident(i)operator(,) ident(line) keyword(in) predefined(enumerate)operator(()ident(old)operator(\))operator(:) + keyword(if) integer(20) operator(<=) ident(i) operator(<=) integer(30)operator(:) + keyword(continue) + keyword(print)operator(>>)ident(new)operator(,) ident(line) + + +comment(# @@PLEAC@@_7.9) +comment(# modifying with "-i" commandline switch is a perl feature) +comment(# python has fileinput) +keyword(import) ident(fileinput)operator(,) ident(sys)operator(,) ident(time) +ident(today) operator(=) ident(time)operator(.)ident(strftime)operator(()stringoperator(,)ident(time)operator(.)ident(localtime)operator(()operator(\))operator(\)) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()ident(inplace)operator(=)integer(1)operator(,) ident(backup)operator(=)stringoperator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(line)operator(.)ident(replace)operator(()stringoperator(,)ident(today)operator(\))operator(\)) + +comment(# set up to iterate over the *.c files in the current directory,) +comment(# editing in place and saving the old file with a .orig extension.) +keyword(import) ident(glob)operator(,) ident(re) +ident(match) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +ident(files) operator(=) ident(fileinput)operator(.)ident(FileInput)operator(()ident(glob)operator(.)ident(glob)operator(()stringoperator(\))operator(,) ident(inplace)operator(=)integer(1)operator(,) ident(backup)operator(=)stringoperator(\)) +keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(files)operator(.)ident(readline)operator(()operator(\)) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()ident(line)operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + keyword(break) + keyword(if) ident(files)operator(.)ident(isfirstline)operator(()operator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()stringoperator(\)) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(match)operator(.)ident(sub)operator(()stringoperator(,)ident(line)operator(\))operator(\)) + + +comment(# @@PLEAC@@_7.10) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +ident(data) operator(=) ident(myfile)operator(.)ident(read)operator(()operator(\)) +comment(# change data here) +ident(myfile)operator(.)ident(seek)operator(()integer(0)operator(,) integer(0)operator(\)) +ident(myfile)operator(.)ident(write)operator(()ident(data)operator(\)) +ident(myfile)operator(.)ident(truncate)operator(()ident(myfile)operator(.)ident(tell)operator(()operator(\))operator(\)) +ident(myfile)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +ident(data) operator(=) operator([)ident(process)operator(()ident(line)operator(\)) keyword(for) ident(line) keyword(in) ident(myfile)operator(]) +ident(myfile)operator(.)ident(seek)operator(()integer(0)operator(,) integer(0)operator(\)) +ident(myfile)operator(.)ident(writelines)operator(()ident(data)operator(\)) +ident(myfile)operator(.)ident(truncate)operator(()ident(myfile)operator(.)ident(tell)operator(()operator(\))operator(\)) +ident(myfile)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_7.11) + +keyword(import) ident(fcntl) +ident(myfile) operator(=) predefined(open)operator(()ident(somepath)operator(,) stringoperator(\)) +ident(fcntl)operator(.)ident(flock)operator(()ident(myfile)operator(,) ident(fcntl)operator(.)ident(LOCK_EX)operator(\)) +comment(# update file, then...) +ident(myfile)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) +ident(fcntl)operator(.)ident(LOCK_SH) +ident(fcntl)operator(.)ident(LOCK_EX) +ident(fcntl)operator(.)ident(LOCK_NB) +ident(fcntl)operator(.)ident(LOCK_UN) +comment(#-----------------------------) +keyword(import) ident(warnings) +keyword(try)operator(:) + ident(fcntl)operator(.)ident(flock)operator(()ident(myfile)operator(,) ident(fcntl)operator(.)ident(LOCK_EX)operator(|)ident(fcntl)operator(.)ident(LOCK_NB)operator(\)) +keyword(except) exception(IOError)operator(:) + ident(warnings)operator(.)ident(warn)operator(()stringoperator(\)) + ident(fcntl)operator(.)ident(flock)operator(()ident(myfile)operator(,) ident(fcntl)operator(.)ident(LOCK_EX)operator(\)) +comment(#-----------------------------) +ident(fcntl)operator(.)ident(flock)operator(()ident(myfile)operator(,) ident(fcntl)operator(.)ident(LOCK_UN)operator(\)) +comment(#-----------------------------) +comment(# option "r+" instead "w+" stops python from truncating the file on opening) +comment(# when another process might well hold an advisory exclusive lock on it.) +ident(myfile) operator(=) predefined(open)operator(()ident(somepath)operator(,) stringoperator(\)) +ident(fcntl)operator(.)ident(flock)operator(()ident(myfile)operator(,) ident(fcntl)operator(.)ident(LOCK_EX)operator(\)) +ident(myfile)operator(.)ident(seek)operator(()integer(0)operator(,) integer(0)operator(\)) +ident(myfile)operator(.)ident(truncate)operator(()integer(0)operator(\)) +keyword(print)operator(>>)ident(myfile)operator(,) string comment(# or myfile.write("\\n"\)) +ident(myfile)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_7.12) +comment(# Python doesn't have command buffering. Files can have buffering set,) +comment(# when opened:) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(,) ident(buffering)operator(=)integer(0)operator(\)) comment(#Unbuffered) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(,) ident(buffering)operator(=)integer(1)operator(\)) comment(#Line buffered) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(,) ident(buffering)operator(=)integer(100)operator(\)) comment(#Use buffer of (approx\) 100 bytes) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(,) ident(buffering)operator(=)operator(-)integer(1)operator(\)) comment(#Use system default) + +ident(myfile)operator(.)ident(flush)operator(()operator(\)) comment(# Flush the I/O buffer) + +comment(# stdout is treated as a file. If you ever need to flush it, do so:) +keyword(import) ident(sys) +ident(sys)operator(.)ident(stdout)operator(.)ident(flush)operator(()operator(\)) + +comment(# DON'T DO THIS. Use urllib, etc.) +keyword(import) ident(socket) +ident(mysock) operator(=) ident(socket)operator(.)ident(socket)operator(()operator(\)) +ident(mysock)operator(.)ident(connect)operator(()operator(()stringoperator(,) integer(80)operator(\))operator(\)) +comment(# mysock.setblocking(True\)) +ident(mysock)operator(.)ident(send)operator(()stringoperator(\)) +ident(f) operator(=) ident(mysock)operator(.)ident(makefile)operator(()operator(\)) +keyword(print) string +keyword(for) ident(line) keyword(in) ident(f)operator(:) + keyword(print) ident(line)operator([)operator(:)operator(-)integer(1)operator(]) + +comment(# @@PLEAC@@_7.13) +keyword(import) ident(select) +keyword(while) pre_constant(True)operator(:) + ident(rlist)operator(,) ident(wlist)operator(,) ident(xlist) operator(=) ident(select)operator(.)ident(select)operator(()operator([)ident(file1)operator(,) ident(file2)operator(,) ident(file3)operator(])operator(,) operator([)operator(])operator(,) operator([)operator(])operator(,) integer(0)operator(\)) + keyword(for) ident(r) keyword(in) ident(rlist)operator(:) + keyword(pass) comment(# Do something with the file handle) + +comment(# @@PLEAC@@_7.14) +comment(# @@SKIP@@ Use select.poll(\) on Unix systems.) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_7.15) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_7.16) +comment(# NOTE: this is all much easier in Python) +keyword(def) ident(subroutine)operator(()ident(myfile)operator(\))operator(:) + keyword(print)operator(>>)ident(myfile)operator(,) string + +ident(variable) operator(=) ident(myfile) +ident(subroutine)operator(()ident(variable)operator(\)) + +comment(# @@PLEAC@@_7.17) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_7.18) +keyword(for) ident(myfile) keyword(in) ident(files)operator(:) + keyword(print)operator(>>)ident(myfile)operator(,) ident(stuff_to_print) + +comment(# NOTE: This is unix specific) +keyword(import) ident(os) +predefined(file) operator(=) ident(os)operator(.)ident(popen)operator(()string/dev/null)delimiter(")>operator(,) stringoperator(\)) +keyword(print)operator(>>)ident(myfile)operator(,) string + +comment(# NOTE: the "make STDOUT go to three files" is bad programming style) +keyword(import) ident(os)operator(,) ident(sys) +ident(sys)operator(.)ident(stdout)operator(.)ident(file) operator(=) ident(os)operator(.)ident(popen)operator(()stringoperator(,) stringoperator(\)) +keyword(print) string +ident(sys)operator(.)ident(stdout)operator(.)ident(close)operator(()operator(\)) + +comment(# You could use a utility object to redirect writes:) +keyword(class) ident(FileDispatcher)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(*)ident(files)operator(\))operator(:) + pre_constant(self)operator(.)ident(files) operator(=) ident(files) + + keyword(def) ident(write)operator(()pre_constant(self)operator(,) ident(msg)operator(\))operator(:) + keyword(for) ident(f) keyword(in) pre_constant(self)operator(.)ident(files)operator(:) + ident(f)operator(.)ident(write)operator(()ident(msg)operator(\)) + + keyword(def) ident(close)operator(()pre_constant(self)operator(\))operator(:) + keyword(for) ident(f) keyword(in) pre_constant(self)operator(.)ident(files)operator(:) + ident(f)operator(.)ident(close)operator(()operator(\)) + +ident(x) operator(=) predefined(open)operator(()stringoperator(,) stringoperator(\)) +ident(y) operator(=) predefined(open)operator(()stringoperator(,) stringoperator(\)) +ident(z) operator(=) predefined(open)operator(()stringoperator(,) stringoperator(\)) + +ident(fd) operator(=) ident(FileDispatcher)operator(()ident(x)operator(,) ident(y)operator(,) ident(z)operator(\)) +keyword(print)operator(>>)ident(fd)operator(,) string comment(# equiv to fd.write("Foo"\); fd.write("\\n"\)) +keyword(print)operator(>>)ident(fd)operator(,) string +ident(fd)operator(.)ident(close)operator(()operator(\)) + +comment(# @@PLEAC@@_7.19) +keyword(import) ident(os) +ident(myfile) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(fdnum)operator(\)) comment(# open the descriptor itself) +ident(myfile) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(os)operator(.)ident(dup)operator(()ident(fdnum)operator(\))operator(\)) comment(# open to a copy of the descriptor) + +comment(###) +ident(outcopy) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(os)operator(.)ident(dup)operator(()ident(sys)operator(.)ident(stdin)operator(.)ident(fileno)operator(()operator(\))operator(\))operator(,) stringoperator(\)) +ident(incopy) operator(=) ident(os)operator(.)ident(fdopen)operator(()ident(os)operator(.)ident(dup)operator(()ident(sys)operator(.)ident(stdin)operator(.)ident(fileno)operator(()operator(\))operator(\))operator(,) stringoperator(\)) + +comment(# @@PLEAC@@_7.20) +ident(original) operator(=) predefined(open)operator(()stringoperator(\)) +ident(alias) operator(=) ident(original) +ident(alias)operator(.)ident(close)operator(()operator(\)) +keyword(print) ident(original)operator(.)ident(closed) +comment(#=>True) + +keyword(import) ident(copy) + +ident(original) operator(=) predefined(open)operator(()stringoperator(\)) +ident(dupe) operator(=) ident(copy)operator(.)ident(copy)operator(()ident(original)operator(\)) +ident(dupe)operator(.)ident(close)operator(()operator(\)) +keyword(print) ident(original)operator(.)ident(closed) +comment(#=>False) + +comment(# DON'T DO THIS.) +keyword(import) ident(sys) +ident(oldstderr) operator(=) ident(sys)operator(.)ident(stderr) +ident(oldstdout) operator(=) ident(sys)operator(.)ident(stdout) + +ident(sys)operator(.)ident(stderr) operator(=) predefined(open)operator(()stringoperator(\)) +ident(sys)operator(.)ident(stdout) operator(=) predefined(open)operator(()stringoperator(\)) + +keyword(print) string comment(# Will be written to C:/stdoutfile.txt) +ident(sys)operator(.)ident(stdout)operator(.)ident(close)operator(()operator(\)) + +ident(sys)operator(.)ident(stdout) operator(=) ident(oldstdout) +ident(sys)operator(.)ident(stderr) operator(=) ident(oldstderr) + + +comment(# @@PLEAC@@_7.21) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_7.22) +comment(# On Windows:) +keyword(import) ident(msvcrt) +ident(myfile)operator(.)ident(seek)operator(()integer(5)operator(,) integer(0)operator(\)) +ident(msvcrt)operator(.)ident(locking)operator(()ident(myfile)operator(.)ident(fileno)operator(()operator(\))operator(,) ident(msvcrt)operator(.)ident(LK_NBLCK)operator(,) integer(3)operator(\)) + +comment(# On Unix:) +keyword(import) ident(fcntl) +ident(fcntl)operator(.)ident(lockf)operator(()ident(myfile)operator(.)ident(fileno)operator(()operator(\))operator(,) ident(fcntl)operator(.)ident(LOCK_EX) operator(|) ident(fcntl)operator(.)ident(LOCK_NB)operator(,) integer(3)operator(,) integer(5)operator(\)) + + +comment(# ^^PLEAC^^_8.0) +comment(#-----------------------------) +keyword(for) ident(line) keyword(in) ident(DATAFILE)operator(:) + ident(line) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + ident(size) operator(=) predefined(len)operator(()ident(line)operator(\)) + keyword(print) ident(size) comment(# output size of line) + +comment(#-----------------------------) +keyword(for) ident(line) keyword(in) ident(datafile)operator(:) + keyword(print) ident(length)operator(()ident(line)operator(.)ident(rstrip)operator(()operator(\))operator(\)) comment(# output size of line) +comment(#-----------------------------) +ident(lines) operator(=) ident(datafile)operator(.)ident(readlines)operator(()operator(\)) +comment(#-----------------------------) +ident(whole_file) operator(=) ident(myfile)operator(.)ident(read)operator(()operator(\)) +comment(#-----------------------------) +comment(## No direct equivalent in Python) +comment(#% perl -040 -e '$word = <>; print "First word is $word\\n";') +comment(#-----------------------------) +comment(## No direct equivalent in Python) +comment(#% perl -ne 'BEGIN { $/="%%\\n" } chomp; print if /Unix/i' fortune.dat) +comment(#-----------------------------) +keyword(print)operator(>>)ident(myfile)operator(,) stringoperator(,) stringoperator(,) string comment(# "One two three") +keyword(print) string comment(# Sent to default output file) +comment(#-----------------------------) +predefined(buffer) operator(=) ident(myfile)operator(.)ident(read)operator(()integer(4096)operator(\)) +ident(rv) operator(=) predefined(len)operator(()predefined(buffer)operator(\)) +comment(#-----------------------------) +ident(myfile)operator(.)ident(truncate)operator(()ident(length)operator(\)) +predefined(open)operator(()string operator(%) ident(os)operator(.)ident(getpid)operator(()operator(\))operator(,) stringoperator(\))operator(.)ident(truncate)operator(()ident(length)operator(\)) +comment(#-----------------------------) +ident(pos) operator(=) ident(myfile)operator(.)ident(tell)operator(()operator(\)) +keyword(print) stringoperator(,) ident(pos)operator(,) string +comment(#-----------------------------) +ident(logfile)operator(.)ident(seek)operator(()integer(0)operator(,) integer(2)operator(\)) comment(# Seek to the end) +ident(datafile)operator(.)ident(seek)operator(()ident(pos)operator(\)) comment(# Seek to a given byte) +ident(outfile)operator(.)ident(seek)operator(()operator(-)integer(20)operator(,) integer(1)operator(\)) comment(# Seek back 20 bytes) +comment(#-----------------------------) +ident(written) operator(=) ident(os)operator(.)ident(write)operator(()ident(datafile)operator(.)ident(fileno)operator(()operator(\))operator(,) ident(mystr)operator(\)) +keyword(if) ident(written) operator(!=) predefined(len)operator(()ident(mystr)operator(\))operator(:) + ident(warnings)operator(.)ident(warn)operator(()string operator(%) operator(()ident(written)operator(,) predefined(len)operator(()ident(mystr)operator(\))operator(\))operator(\)) +comment(#-----------------------------) +ident(pos) operator(=) ident(os)operator(.)ident(lseek)operator(()ident(myfile)operator(.)ident(fileno)operator(()operator(\))operator(,) integer(0)operator(,) integer(1)operator(\)) comment(# don't change position) +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_8.1) +keyword(def) ident(ContReader)operator(()ident(infile)operator(\))operator(:) + ident(lines) operator(=) operator([)operator(]) + keyword(for) ident(line) keyword(in) ident(infile)operator(:) + ident(line) operator(=) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + keyword(if) ident(line)operator(.)ident(endswith)operator(()stringoperator(\))operator(:) + ident(lines)operator(.)ident(append)operator(()ident(line)operator([)operator(:)operator(-)integer(1)operator(])operator(\)) + keyword(continue) + ident(lines)operator(.)ident(append)operator(()ident(line)operator(\)) + keyword(yield) stringoperator(.)ident(join)operator(()ident(lines)operator(\)) + ident(lines) operator(=) operator([)operator(]) + keyword(if) ident(lines)operator(:) + keyword(yield) stringoperator(.)ident(join)operator(()ident(lines)operator(\)) + +keyword(for) ident(line) keyword(in) ident(ContReader)operator(()ident(datafile)operator(\))operator(:) + keyword(pass) comment(# process full record in 'line' here) + +comment(# ^^PLEAC^^_8.2) +keyword(import) ident(os) +ident(count) operator(=) predefined(int)operator(()ident(os)operator(.)ident(popen)operator(()string operator(+) ident(filename)operator(\))operator(.)ident(read)operator(()operator(\))operator(\)) +comment(#-----------------------------) +keyword(for) ident(count)operator(,) ident(line) keyword(in) predefined(enumerate)operator(()predefined(open)operator(()ident(filename)operator(\))operator(\))operator(:) + keyword(pass) +ident(count) operator(+=) integer(1) comment(# indexing is zero based) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(\)) +ident(count) operator(=) integer(0) +keyword(for) ident(line) keyword(in) ident(myfile)operator(:) + ident(count) operator(+=) integer(1) +comment(# 'count' now holds the number of lines read) +comment(#-----------------------------) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(\)) +ident(count) operator(=) integer(0) +keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(myfile)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + keyword(break) + ident(count) operator(+=) integer(1) +comment(#-----------------------------) +ident(count) operator(=) integer(0) +keyword(while) pre_constant(True)operator(:) + ident(s) operator(=) ident(myfile)operator(.)ident(read)operator(()integer(2)operator(**)integer(16)operator(\)) + ident(count) operator(+=) ident(s)operator(.)ident(count)operator(()stringoperator(\)) +comment(#-----------------------------) +keyword(for) ident(line)operator(,) ident(count) keyword(in) predefined(zip)operator(()predefined(open)operator(()ident(filename)operator(\))operator(,) predefined(xrange)operator(()integer(1)operator(,) ident(sys)operator(.)ident(maxint)operator(\))operator(\))operator(:) + keyword(pass) +comment(# 'count' now holds the number of lines read) +comment(#-----------------------------) +keyword(import) ident(fileinput) +ident(fi) operator(=) ident(fileinput)operator(.)ident(FileInput)operator(()ident(filename)operator(\)) +keyword(while) ident(fi)operator(.)ident(readline)operator(()operator(\))operator(:) keyword(pass) + +ident(count) operator(=) ident(fi)operator(.)ident(lineno)operator(()operator(\)) +comment(#-----------------------------) +keyword(def) ident(SepReader)operator(()ident(infile)operator(,) ident(sep) operator(=) stringoperator(\))operator(:) + ident(text) operator(=) ident(infile)operator(.)ident(read)operator(()integer(10000)operator(\)) + keyword(if) keyword(not) ident(text)operator(:) + keyword(return) + keyword(while) pre_constant(True)operator(:) + ident(fields) operator(=) ident(text)operator(.)ident(split)operator(()ident(sep)operator(\)) + keyword(for) ident(field) keyword(in) ident(fields)operator([)operator(:)operator(-)integer(1)operator(])operator(:) + keyword(yield) ident(field) + ident(text) operator(=) ident(fields)operator([)operator(-)integer(1)operator(]) + ident(new_text) operator(=) ident(infile)operator(.)ident(read)operator(()integer(10000)operator(\)) + keyword(if) keyword(not) ident(new_text)operator(:) + keyword(yield) ident(text) + keyword(break) + ident(text) operator(+=) ident(new_text) + +ident(para_count) operator(=) integer(0) +keyword(for) ident(para) keyword(in) ident(SepReader)operator(()predefined(open)operator(()ident(filename)operator(\))operator(\))operator(:) + ident(para_count) operator(+=) integer(1) +comment(# FIXME: For my test case (Python-pre2.2 README from CVS\) this) +comment(# returns 175 paragraphs while Perl returns 174.) +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_8.3) +keyword(for) ident(line) keyword(in) ident(sys)operator(.)ident(stdin)operator(:) + keyword(for) ident(word) keyword(in) ident(line)operator(.)ident(split)operator(()operator(\))operator(:) + keyword(pass) comment(# do something with 'chunk') +comment(#-----------------------------) +ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +keyword(for) ident(line) keyword(in) ident(sys)operator(.)ident(stdin)operator(:) + ident(pos) operator(=) integer(0) + keyword(while) pre_constant(True)operator(:) + ident(match) operator(=) ident(pat)operator(.)ident(search)operator(()ident(line)operator(,) ident(pos)operator(\)) + keyword(if) keyword(not) ident(match)operator(:) + keyword(break) + ident(pos) operator(=) ident(match)operator(.)ident(end)operator(()integer(1)operator(\)) + comment(# do something with match.group(1\)) + +comment(# EXPERIMENTAL in the sre implementation but) +comment(# likely to be included in future (post-2.2\) releases.) +ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +keyword(for) ident(line) keyword(in) ident(sys)operator(.)ident(stdin)operator(:) + ident(scanner) operator(=) ident(pat)operator(.)ident(scanner)operator(()ident(line)operator(\)) + keyword(while) pre_constant(True)operator(:) + ident(match) operator(=) ident(scanner)operator(.)ident(search)operator(()operator(\)) + keyword(if) keyword(not) ident(match)operator(:) + keyword(break) + comment(# do something with match.group(1\)) + + +comment(#-----------------------------) +comment(# Make a word frequency count) +keyword(import) ident(fileinput)operator(,) ident(re) +ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +ident(seen) operator(=) operator({)operator(}) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(pos) operator(=) integer(0) + keyword(while) pre_constant(True)operator(:) + ident(match) operator(=) ident(pat)operator(.)ident(search)operator(()ident(line)operator(,) ident(pos)operator(\)) + keyword(if) keyword(not) ident(match)operator(:) + keyword(break) + ident(pos) operator(=) ident(match)operator(.)ident(end)operator(()integer(1)operator(\)) + ident(text) operator(=) ident(match)operator(.)ident(group)operator(()integer(1)operator(\))operator(.)ident(lower)operator(()operator(\)) + ident(seen)operator([)ident(text)operator(]) operator(=) ident(seen)operator(.)ident(get)operator(()ident(text)operator(,) integer(0)operator(\)) operator(+) integer(1) + +comment(# output dict in a descending numeric sort of its values) +keyword(for) ident(text)operator(,) ident(count) keyword(in) predefined(sorted)operator(()ident(seen)operator(.)ident(items)operator(,) ident(key)operator(=)keyword(lambda) ident(item)operator(:) ident(item)operator([)integer(1)operator(])operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(count)operator(,) ident(text)operator(\)) + +comment(#-----------------------------) +comment(# Line frequency count) +keyword(import) ident(fileinput)operator(,) ident(sys) +ident(seen) operator(=) operator({)operator(}) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + ident(text) operator(=) ident(line)operator(.)ident(lower)operator(()operator(\)) + ident(seen)operator([)ident(text)operator(]) operator(=) ident(seen)operator(.)ident(get)operator(()ident(text)operator(,) integer(0)operator(\)) operator(+) integer(1) + +keyword(for) ident(text)operator(,) ident(count) keyword(in) predefined(sorted)operator(()ident(seen)operator(.)ident(items)operator(,) ident(key)operator(=)keyword(lambda) ident(item)operator(:) ident(item)operator([)integer(1)operator(])operator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()string operator(%) operator(()ident(count)operator(,) ident(text)operator(\))operator(\)) + +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_8.4) +ident(lines) operator(=) ident(myfile)operator(.)ident(readlines)operator(()operator(\)) +keyword(while) ident(lines)operator(:) + ident(line) operator(=) ident(lines)operator(.)ident(pop)operator(()operator(\)) + comment(# do something with 'line') + +comment(#-----------------------------) +keyword(for) ident(line) keyword(in) predefined(reversed)operator(()ident(myfile)operator(\))operator(:) + keyword(pass) comment(# do something with line) +comment(#-----------------------------) +keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(lines)operator(\))operator(\))operator(:) + ident(line) operator(=) ident(lines)operator([)operator(-)ident(i)operator(]) +comment(#-----------------------------) +keyword(for) ident(paragraph) keyword(in) predefined(sorted)operator(()ident(SepReader)operator(()ident(infile)operator(\))operator(\))operator(:) + keyword(pass) comment(# do something) +comment(#-----------------------------) + + + +comment(# ^^PLEAC^^_8.5) +keyword(import) ident(time) +keyword(while) pre_constant(True)operator(:) + keyword(for) ident(line) keyword(in) ident(infile)operator(:) + keyword(pass) comment(# do something with the line) + ident(time)operator(.)ident(sleep)operator(()ident(SOMETIME)operator(\)) + ident(infile)operator(.)ident(seek)operator(()integer(0)operator(,) integer(1)operator(\)) +comment(#-----------------------------) +keyword(import) ident(time) +ident(naptime) operator(=) integer(1) + +ident(logfile) operator(=) predefined(open)operator(()stringoperator(\)) +keyword(while) pre_constant(True)operator(:) + keyword(for) ident(line) keyword(in) ident(logfile)operator(:) + keyword(print) ident(line)operator(.)ident(rstrip)operator(()operator(\)) + ident(time)operator(.)ident(sleep)operator(()ident(naptime)operator(\)) + ident(infile)operator(.)ident(seek)operator(()integer(0)operator(,) integer(1)operator(\)) +comment(#-----------------------------) +keyword(while) pre_constant(True)operator(:) + ident(curpos) operator(=) ident(logfile)operator(.)ident(tell)operator(()operator(\)) + keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(logfile)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + keyword(break) + ident(curpos) operator(=) ident(logfile)operator(.)ident(tell)operator(()operator(\)) + ident(sleep)operator(()ident(naptime)operator(\)) + ident(logfile)operator(.)ident(seek)operator(()ident(curpos)operator(,) integer(0)operator(\)) comment(# seek to where we had been) +comment(#-----------------------------) +keyword(import) ident(os) +keyword(if) ident(os)operator(.)ident(stat)operator(()ident(LOGFILENAME)operator(\))operator(.)ident(st_nlink) operator(==) integer(0)operator(:) + keyword(raise) exception(SystemExit) +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_8.6) +keyword(import) ident(random)operator(,) ident(fileinput) +ident(text) operator(=) pre_constant(None) +keyword(for) ident(line) keyword(in) ident(fileinput)operator(.)ident(input)operator(()operator(\))operator(:) + keyword(if) ident(random)operator(.)ident(randrange)operator(()ident(fileinput)operator(.)ident(lineno)operator(()operator(\))operator(\)) operator(==) integer(0)operator(:) + ident(text) operator(=) ident(line) +comment(# 'text' is the random line) +comment(#-----------------------------) +comment(# XXX is the perl code correct? Where is the fortunes file opened?) +keyword(import) ident(sys) +ident(adage) operator(=) pre_constant(None) +keyword(for) ident(i)operator(,) ident(rec) keyword(in) predefined(enumerate)operator(()ident(SepReader)operator(()predefined(open)operator(()stringoperator(\))operator(,) stringoperator(\))operator(\))operator(:) + keyword(if) ident(random)operator(.)ident(randrange)operator(()ident(i)operator(+)integer(1)operator(\)) operator(==) integer(0)operator(:) + ident(adage) operator(=) ident(rec) +keyword(print) ident(adage) +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_8.7) +keyword(import) ident(random) +ident(lines) operator(=) ident(data)operator(.)ident(readlines)operator(()operator(\)) +ident(random)operator(.)ident(shuffle)operator(()ident(lines)operator(\)) +keyword(for) ident(line) keyword(in) ident(lines)operator(:) + keyword(print) ident(line)operator(.)ident(rstrip)operator(()operator(\)) +comment(#-----------------------------) + + + +comment(# ^^PLEAC^^_8.8) +comment(# using efficient caching system) +keyword(import) ident(linecache) +ident(linecache)operator(.)ident(getline)operator(()ident(filename)operator(,) ident(DESIRED_LINE_NUMBER)operator(\)) + +comment(# or doing it more oldskool) +ident(lineno) operator(=) integer(0) +keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(infile)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line) keyword(or) ident(lineno) operator(==) ident(DESIRED_LINE_NUMBER)operator(:) + keyword(break) + ident(lineno) operator(+=) integer(1) +comment(#-----------------------------) +ident(lines) operator(=) ident(infile)operator(.)ident(readlines)operator(()operator(\)) +ident(line) operator(=) ident(lines)operator([)ident(DESIRED_LINE_NUMBER)operator(]) +comment(#-----------------------------) +keyword(for) ident(i) keyword(in) predefined(range)operator(()ident(DESIRED_LINE_NUMBER)operator(\))operator(:) + ident(line) operator(=) ident(infile)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + keyword(break) +comment(#-----------------------------) + +comment(## Not sure what this thing is doing. Allow fast access to a given) +comment(## line number?) + +comment(# usage: build_index(*DATA_HANDLE, *INDEX_HANDLE\)) + +comment(# ^^PLEAC^^_8.9) +comment(# given $RECORD with field separated by PATTERN,) +comment(# extract @FIELDS.) +ident(fields) operator(=) ident(re)operator(.)ident(split)operator(()ident(pattern_string)operator(,) ident(text)operator(\)) +comment(#-----------------------------) +ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()ident(pattern_string)operator(\)) +ident(fields) operator(=) ident(pat)operator(.)ident(split)operator(()ident(text)operator(\)) +comment(#-----------------------------) +ident(re)operator(.)ident(split)operator(()stringoperator(,) stringoperator(\)) +comment(#-----------------------------) +operator([)integer(3)operator(,) stringoperator(,) integer(5)operator(,) stringoperator(,) integer(2)operator(]) +comment(#-----------------------------) +ident(fields) operator(=) ident(record)operator(.)ident(split)operator(()stringoperator(\)) +comment(#-----------------------------) +ident(fields) operator(=) ident(re)operator(.)ident(split)operator(()stringoperator(,) ident(record)operator(\)) +comment(#-----------------------------) +ident(fields) operator(=) ident(re)operator(.)ident(split)operator(()stringoperator(,) ident(record)operator(\)) +comment(#-----------------------------) +ident(fields) operator(=) ident(record)operator(.)ident(split)operator(()stringoperator(\)) +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_8.10) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +ident(prev_pos) operator(=) ident(pos) operator(=) integer(0) +keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(myfile)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + keyword(break) + ident(prev_pos) operator(=) ident(pos) + ident(pos) operator(=) ident(myfile)operator(.)ident(tell)operator(()operator(\)) +ident(myfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +ident(myfile)operator(.)ident(truncate)operator(()ident(prev_pos)operator(\)) +comment(#-----------------------------) + + + +comment(# ^^PLEAC^^_8.11) +predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +comment(#-----------------------------) +ident(gifname) operator(=) string +ident(gif_file) operator(=) predefined(open)operator(()ident(gifname)operator(,) stringoperator(\)) + +comment(# Don't think there's an equivalent for these in Python) +comment(#binmode(GIF\); # now DOS won't mangle binary input from GIF) +comment(#binmode(STDOUT\); # now DOS won't mangle binary output to STDOUT) + +comment(#-----------------------------) +keyword(while) pre_constant(True)operator(:) + ident(buff) operator(=) ident(gif)operator(.)ident(read)operator(()integer(8) operator(*) integer(2)operator(**)integer(10)operator(\)) + keyword(if) keyword(not) ident(buff)operator(:) + keyword(break) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(buff)operator(\)) +comment(#-----------------------------) + + + +comment(# ^^PLEAC^^_8.12) +ident(address) operator(=) ident(recsize) operator(*) ident(recno) +ident(myfile)operator(.)ident(seek)operator(()ident(address)operator(,) integer(0)operator(\)) +predefined(buffer) operator(=) ident(myfile)operator(.)ident(read)operator(()ident(recsize)operator(\)) +comment(#-----------------------------) +ident(address) operator(=) ident(recsize) operator(*) operator(()ident(recno)operator(-)integer(1)operator(\)) +comment(#-----------------------------) + + + +comment(# ^^PLEAC^^_8.13) +keyword(import) ident(posixfile) +ident(address) operator(=) ident(recsize) operator(*) ident(recno) +ident(myfile)operator(.)ident(seek)operator(()ident(address)operator(\)) +predefined(buffer) operator(=) ident(myfile)operator(.)ident(read)operator(()ident(recsize)operator(\)) +comment(# ... work with the buffer, then turn it back into a string and ...) +ident(myfile)operator(.)ident(seek)operator(()operator(-)ident(recsize)operator(,) ident(posixfile)operator(.)ident(SEEK_CUR)operator(\)) +ident(myfile)operator(.)ident(write)operator(()predefined(buffer)operator(\)) +ident(myfile)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) +comment(## Not yet implemented) +comment(# weekearly -- set someone's login date back a week) +comment(# @@INCOMPLETE@@) + + +comment(# ^^PLEAC^^_8.14) +comment(## Note: this isn't optimal -- the 's+=c' may go O(N**2\) so don't) +comment(## use for large strings.) +ident(myfile)operator(.)ident(seek)operator(()ident(addr)operator(\)) +ident(s) operator(=) string +keyword(while) pre_constant(True)operator(:) + ident(c) operator(=) ident(myfile)operator(.)ident(read)operator(()integer(1)operator(\)) + keyword(if) keyword(not) ident(c) keyword(or) ident(c) operator(==) stringoperator(:) + keyword(break) + ident(s) operator(+=) ident(c) +comment(#-----------------------------) +ident(myfile)operator(.)ident(seek)operator(()ident(addr)operator(\)) +ident(offset) operator(=) integer(0) +keyword(while) pre_constant(True)operator(:) + ident(s) operator(=) ident(myfile)operator(.)ident(read)operator(()integer(1000)operator(\)) + ident(x) operator(=) ident(s)operator(.)ident(find)operator(()stringoperator(\)) + keyword(if) ident(x) operator(!=) operator(-)integer(1)operator(:) + ident(offset) operator(+=) ident(x) + keyword(break) + ident(offset) operator(+=) predefined(len)operator(()ident(s)operator(\)) + keyword(if) predefined(len)operator(()ident(s)operator(\)) operator(!=) integer(1000)operator(:) comment(# EOF) + keyword(break) +ident(myfile)operator(.)ident(seek)operator(()ident(addr)operator(\)) +ident(s) operator(=) ident(myfile)operator(.)ident(read)operator(()ident(offset) operator(-) integer(1)operator(\)) +ident(myfile)operator(.)ident(read)operator(()integer(1)operator(\)) + +comment(#-----------------------------) +comment(## Not Implemented) +comment(# bgets - get a string from an address in a binary file) +comment(#-----------------------------) +comment(#!/usr/bin/perl) +comment(# strings - pull strings out of a binary file) +keyword(import) ident(re)operator(,) ident(sys) + +comment(## Assumes SepReader from above) + +ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +keyword(for) ident(block) keyword(in) ident(SepReader)operator(()ident(sys)operator(.)ident(stdin)operator(,) stringoperator(\))operator(:) + ident(pos) operator(=) integer(0) + keyword(while) pre_constant(True)operator(:) + ident(match) operator(=) ident(pat)operator(.)ident(search)operator(()ident(block)operator(,) ident(pos)operator(\)) + keyword(if) keyword(not) ident(match)operator(:) + keyword(break) + keyword(print) ident(match)operator(.)ident(group)operator(()integer(1)operator(\)) + ident(pos) operator(=) ident(match)operator(.)ident(end)operator(()integer(1)operator(\)) +comment(#-----------------------------) + + +comment(# @@PLEAC@@_8.15) + +comment(# RECORDSIZE is the length of a record, in bytes.) +comment(# TEMPLATE is the unpack template for the record) +comment(# FILE is the file to read from) +comment(# FIELDS is a tuple, one element per field) +keyword(import) ident(struct) +ident(RECORDSIZE)operator(=) ident(struct)operator(.)ident(calcsize)operator(()ident(TEMPLATE)operator(\)) +keyword(while) pre_constant(True)operator(:) + ident(record) operator(=) ident(FILE)operator(.)ident(read)operator(()ident(RECORDSIZE)operator(\))operator(:) + keyword(if) predefined(len)operator(()ident(record)operator(\))operator(!=)ident(RECORDSIZE)operator(:) + keyword(raise) string + ident(FIELDS) operator(=) ident(struct)operator(.)ident(unpack)operator(()ident(TEMPLATE)operator(,) ident(record)operator(\)) +comment(# ----) + + +comment(# ^^PLEAC^^_8.16) +comment(# NOTE: to parse INI file, see the stanard ConfigParser module.) +keyword(import) ident(re) +ident(pat) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +keyword(for) ident(line) keyword(in) ident(config_file)operator(:) + keyword(if) string keyword(in) ident(line)operator(:) comment(# no comments) + ident(line) operator(=) ident(line)operator([)operator(:)ident(line)operator(.)ident(index)operator(()stringoperator(\))operator(]) + ident(line) operator(=) ident(line)operator(.)ident(strip)operator(()operator(\)) comment(# no leading or trailing white) + keyword(if) keyword(not) ident(line)operator(:) comment(# anything left?) + keyword(continue) + ident(m) operator(=) ident(pat)operator(.)ident(search)operator(()ident(line)operator(\)) + ident(var) operator(=) ident(line)operator([)operator(:)ident(m)operator(.)ident(start)operator(()operator(\))operator(]) + ident(value) operator(=) ident(line)operator([)ident(m)operator(.)ident(end)operator(()operator(\))operator(:)operator(]) + ident(User_Preferences)operator([)ident(var)operator(]) operator(=) ident(value) + + +comment(# ^^PLEAC^^_8.17) +keyword(import) ident(os) + +ident(mode)operator(,) ident(ino)operator(,) ident(dev)operator(,) ident(nlink)operator(,) ident(uid)operator(,) ident(gid)operator(,) ident(size)operator(,) \ +ident(atime)operator(,) ident(mtime)operator(,) ident(ctime) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) + +ident(mode) operator(&=) oct(07777) comment(# discard file type info) + +comment(#-----------------------------) +ident(info) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) +keyword(if) ident(info)operator(.)ident(st_uid) operator(==) integer(0)operator(:) + keyword(print) stringoperator(,) ident(filename) +keyword(if) ident(info)operator(.)ident(st_atime) operator(>) ident(info)operator(.)ident(st_mtime)operator(:) + keyword(print) ident(filename)operator(,) string +comment(#-----------------------------) +keyword(import) ident(os) +keyword(def) ident(is_safe)operator(()ident(path)operator(\))operator(:) + ident(info) operator(=) ident(os)operator(.)ident(stat)operator(()ident(path)operator(\)) + + comment(# owner neither superuser nor me ) + comment(# the real uid is in stored in the $< variable) + keyword(if) ident(info)operator(.)ident(st_uid) keyword(not) keyword(in) operator(()integer(0)operator(,) ident(os)operator(.)ident(getuid)operator(()operator(\))operator(\))operator(:) + keyword(return) pre_constant(False) + + comment(# check whether group or other can write file.) + comment(# use 066 to detect either reading or writing) + keyword(if) ident(info)operator(.)ident(st_mode) operator(&) oct(022)operator(:) comment(# someone else can write this) + keyword(if) keyword(not) ident(os)operator(.)ident(path)operator(.)ident(isdir)operator(()ident(path)operator(\))operator(:) comment(# non-directories aren't safe) + keyword(return) pre_constant(False) + comment(# but directories with the sticky bit (01000\) are) + keyword(if) keyword(not) operator(()ident(info)operator(.)ident(st_mode) operator(&) oct(01000)operator(\))operator(:) + keyword(return) pre_constant(False) + keyword(return) pre_constant(True) +comment(#-----------------------------) +comment(## XXX What is '_PC_CHOWN_RESTRICTED'?) + +keyword(def) ident(is_verysafe)operator(()ident(path)operator(\))operator(:) + ident(terms) operator(=) operator([)operator(]) + keyword(while) pre_constant(True)operator(:) + ident(path)operator(,) ident(ending) operator(=) ident(os)operator(.)ident(path)operator(.)ident(split)operator(()ident(path)operator(\)) + keyword(if) keyword(not) ident(ending)operator(:) + keyword(break) + ident(terms)operator(.)ident(insert)operator(()integer(0)operator(,) ident(ending)operator(\)) + keyword(for) ident(term) keyword(in) ident(terms)operator(:) + ident(path) operator(=) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(path)operator(,) ident(term)operator(\)) + keyword(if) keyword(not) ident(is_safe)operator(()ident(path)operator(\))operator(:) + keyword(return) pre_constant(False) + keyword(return) pre_constant(True) +comment(#-----------------------------) + +comment(# Program: tctee) +comment(# Not Implemented (requires reimplementing Perl's builtin '>>', '|',) +comment(# etc. semantics\)) + +comment(# @@PLEAC@@_8.18) +comment(#!/usr/bin/python) +comment(# tailwtmp - watch for logins and logouts;) +comment(# uses linux utmp structure, from /usr/include/bits/utmp.h) + +comment(# /* The structure describing an entry in the user accounting database. */) +comment(# struct utmp) +comment(# {) +comment(# short int ut_type; /* Type of login. */) +comment(# pid_t ut_pid; /* Process ID of login process. */) +comment(# char ut_line[UT_LINESIZE]; /* Devicename. */) +comment(# char ut_id[4]; /* Inittab ID. */) +comment(# char ut_user[UT_NAMESIZE]; /* Username. */) +comment(# char ut_host[UT_HOSTSIZE]; /* Hostname for remote login. */) +comment(# struct exit_status ut_exit; /* Exit status of a process marked) +comment(# as DEAD_PROCESS. */) +comment(# long int ut_session; /* Session ID, used for windowing. */) +comment(# struct timeval ut_tv; /* Time entry was made. */) +comment(# int32_t ut_addr_v6[4]; /* Internet address of remote host. */) +comment(# char __unused[20]; /* Reserved for future use. */) +comment(# };) + +comment(# /* Values for the `ut_type' field of a `struct utmp'. */) +comment(# #define EMPTY 0 /* No valid user accounting information. */) +comment(# ) +comment(# #define RUN_LVL 1 /* The system's runlevel. */) +comment(# #define BOOT_TIME 2 /* Time of system boot. */) +comment(# #define NEW_TIME 3 /* Time after system clock changed. */) +comment(# #define OLD_TIME 4 /* Time when system clock changed. */) +comment(# ) +comment(# #define INIT_PROCESS 5 /* Process spawned by the init process. */) +comment(# #define LOGIN_PROCESS 6 /* Session leader of a logged in user. */) +comment(# #define USER_PROCESS 7 /* Normal process. */) +comment(# #define DEAD_PROCESS 8 /* Terminated process. */) +comment(# ) +comment(# #define ACCOUNTING 9) + +keyword(import) ident(time) +keyword(import) ident(struct) +keyword(import) ident(os) + +keyword(class) ident(WTmpRecord)operator(:) + ident(fmt) operator(=) stringoperator(;) + ident(_fieldnames) operator(=) operator([)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,)stringoperator(,) + stringoperator(,) stringoperator(,) stringoperator(,) string operator(]) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(_rec_size) operator(=) ident(struct)operator(.)ident(calcsize)operator(()pre_constant(self)operator(.)ident(fmt)operator(\)) + keyword(def) ident(size)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_rec_size) + keyword(def) ident(unpack)operator(()pre_constant(self)operator(,) ident(bin_data)operator(\))operator(:) + ident(rec) operator(=) ident(struct)operator(.)ident(unpack)operator(()pre_constant(self)operator(.)ident(fmt)operator(,) ident(bin_data)operator(\)) + pre_constant(self)operator(.)ident(_rec) operator(=) operator([)operator(]) + keyword(for) ident(i) keyword(in) predefined(range)operator(()predefined(len)operator(()ident(rec)operator(\))operator(\))operator(:) + keyword(if) ident(i) keyword(in) operator(()integer(2)operator(,)integer(3)operator(,)integer(4)operator(,)integer(5)operator(\))operator(:) + comment(# remove character zeros from strings) + pre_constant(self)operator(.)ident(_rec)operator(.)ident(append)operator(() ident(rec)operator([)ident(i)operator(])operator(.)ident(split)operator(()stringoperator(\))operator([)integer(0)operator(]) operator(\)) + keyword(else)operator(:) + pre_constant(self)operator(.)ident(_rec)operator(.)ident(append)operator(()ident(rec)operator([)ident(i)operator(])operator(\)) + keyword(return) pre_constant(self)operator(.)ident(_rec) + keyword(def) ident(fieldnames)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_fieldnames) + keyword(def) ident(__getattr__)operator(()pre_constant(self)operator(,)ident(name)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_rec)operator([)pre_constant(self)operator(.)ident(_fieldnames)operator(.)ident(index)operator(()ident(name)operator(\))operator(]) + +ident(rec) operator(=) ident(WTmpRecord)operator(()operator(\)) +ident(f) operator(=) predefined(open)operator(()stringoperator(,)stringoperator(\)) +ident(f)operator(.)ident(seek)operator(()integer(0)operator(,)integer(2)operator(\)) +keyword(while) pre_constant(True)operator(:) + keyword(while) pre_constant(True)operator(:) + predefined(bin) operator(=) ident(f)operator(.)ident(read)operator(()ident(rec)operator(.)ident(size)operator(()operator(\))operator(\)) + keyword(if) predefined(len)operator(()predefined(bin)operator(\)) operator(!=) ident(rec)operator(.)ident(size)operator(()operator(\))operator(:) + keyword(break) + ident(rec)operator(.)ident(unpack)operator(()predefined(bin)operator(\)) + keyword(if) ident(rec)operator(.)ident(type) operator(!=) integer(0)operator(:) + keyword(print) string operator(%) \ + operator(()ident(rec)operator(.)ident(type)operator(,) ident(rec)operator(.)ident(User)operator(,) ident(rec)operator(.)ident(Line)operator(,) + ident(time)operator(.)ident(strftime)operator(()stringoperator(,)ident(time)operator(.)ident(localtime)operator(()ident(rec)operator(.)ident(time)operator(\))operator(\))operator(,) + ident(rec)operator(.)ident(Hostname)operator(,) ident(rec)operator(.)ident(PID)operator(,) ident(rec)operator(.)ident(addr)operator(\)) + ident(time)operator(.)ident(sleep)operator(()integer(1)operator(\)) +ident(f)operator(.)ident(close)operator(()operator(\)) + +comment(# @@PLEAC@@_8.19) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_8.20) +comment(#!/usr/bin/python) +comment(# laston - find out when given user last logged on) +keyword(import) ident(sys) +keyword(import) ident(struct) +keyword(import) ident(pwd) +keyword(import) ident(time) +keyword(import) ident(re) + +ident(f) operator(=) predefined(open)operator(()stringoperator(,)stringoperator(\)) + +ident(fmt) operator(=) string +ident(rec_size) operator(=) ident(struct)operator(.)ident(calcsize)operator(()ident(fmt)operator(\)) + +keyword(for) ident(user) keyword(in) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(:) + keyword(if) ident(re)operator(.)ident(match)operator(()stringoperator(,) ident(user)operator(\))operator(:) + ident(user_id) operator(=) predefined(int)operator(()ident(user)operator(\)) + keyword(else)operator(:) + keyword(try)operator(:) + ident(user_id) operator(=) ident(pwd)operator(.)ident(getpwnam)operator(()ident(user)operator(\))operator([)integer(2)operator(]) + keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(user)operator(\)) + keyword(continue) + ident(f)operator(.)ident(seek)operator(()ident(rec_size) operator(*) ident(user_id)operator(\)) + predefined(bin) operator(=) ident(f)operator(.)ident(read)operator(()ident(rec_size)operator(\)) + keyword(if) predefined(len)operator(()predefined(bin)operator(\)) operator(==) ident(rec_size)operator(:) + ident(data) operator(=) ident(struct)operator(.)ident(unpack)operator(()ident(fmt)operator(,) predefined(bin)operator(\)) + keyword(if) ident(data)operator([)integer(0)operator(])operator(:) + ident(logged_in) operator(=) string operator(%) operator(()ident(time)operator(.)ident(strftime)operator(()stringoperator(,) + ident(time)operator(.)ident(localtime)operator(()ident(data)operator([)integer(0)operator(])operator(\))operator(\))operator(\)) + ident(line) operator(=) string operator(%) operator(()ident(data)operator([)integer(1)operator(])operator(\)) + ident(host) operator(=) string operator(%) operator(()ident(data)operator([)integer(2)operator(])operator(\)) + keyword(else)operator(:) + ident(logged_in) operator(=) string + ident(line) operator(=) string + ident(host) operator(=) string + keyword(print) string operator(%) operator(()ident(user)operator(,) ident(user_id)operator(,) ident(logged_in)operator(,) ident(line)operator(,) ident(host)operator(\)) + keyword(else)operator(:) + keyword(print) string +ident(f)operator(.)ident(close)operator(()operator(\)) + + +comment(# ^^PLEAC^^_9.0) +comment(#-----------------------------) +ident(entry) operator(=) ident(os)operator(.)ident(stat)operator(()stringoperator(\)) +comment(#-----------------------------) +ident(entry) operator(=) ident(os)operator(.)ident(stat)operator(()stringoperator(\)) +comment(#-----------------------------) +ident(entry) operator(=) ident(os)operator(.)ident(stat)operator(()ident(INFILE)operator(.)ident(name)operator(\)) +comment(#-----------------------------) +ident(entry) operator(=) ident(os)operator(.)ident(stat)operator(()stringoperator(\)) +ident(ctime) operator(=) ident(entry)operator(.)ident(st_ino) +ident(size) operator(=) ident(entry)operator(.)ident(st_size) +comment(#-----------------------------) +ident(f) operator(=) predefined(open)operator(()ident(filename)operator(\)) + +ident(f)operator(.)ident(seek)operator(()integer(0)operator(,) integer(2)operator(\)) +keyword(if) keyword(not) ident(f)operator(.)ident(tell)operator(()operator(\))operator(:) + keyword(raise) exception(SystemExit)operator(()stringoperator(%)ident(filename)operator(\)) +comment(#-----------------------------) + +keyword(for) ident(filename) keyword(in) ident(os)operator(.)ident(listdir)operator(()stringoperator(\))operator(:) + keyword(print) stringoperator(,) ident(filename) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.1) +comment(#-----------------------------) +ident(fstat) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) +ident(readtime) operator(=) ident(fstat)operator(.)ident(st_atime) +ident(writetime) operator(=) ident(fstat)operator(.)ident(st_mtime) + +ident(os)operator(.)ident(utime)operator(()ident(filename)operator(,) operator(()ident(newreadtime)operator(,) ident(newwritetime)operator(\))operator(\)) + +comment(#DON'T DO THIS:) +ident(readtime)operator(,) ident(writetime) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\))operator([)integer(7)operator(:)integer(9)operator(]) +comment(#-----------------------------) +ident(SECONDS_PER_DAY) operator(=) integer(60) operator(*) integer(60) operator(*) integer(24) +ident(fstat) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) +ident(atime) operator(=) ident(fstat)operator(.)ident(st_atime) operator(-) integer(7) operator(*) ident(SECONDS_PER_DAY) +ident(mtime) operator(=) ident(fstat)operator(.)ident(st_mtime) operator(-) integer(7) operator(*) ident(SECONDS_PER_DAY) + +ident(os)operator(.)ident(utime)operator(()ident(filename)operator(,) operator(()ident(atime)operator(,) ident(mtime)operator(\))operator(\)) +comment(#-----------------------------) +ident(mtime) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\))operator(.)ident(st_mtime) +ident(utime)operator(()ident(filename)operator(,) operator(()ident(time)operator(.)ident(time)operator(()operator(\))operator(,) ident(mtime)operator(\))operator(\)) +comment(#-----------------------------) +comment(#!/usr/bin/perl -w) +comment(# uvi - vi a file without changing its access times) + +keyword(import) ident(sys)operator(,) ident(os) +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\)) operator(!=) integer(2)operator(:) + keyword(raise) exception(SystemExit)operator(()stringoperator(\)) +ident(filename) operator(=) ident(argv)operator([)integer(1)operator(]) +ident(fstat) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) +comment(# WARNING: potential security risk) +ident(os)operator(.)ident(system)operator(() operator(()ident(os)operator(.)ident(environ)operator(.)ident(get)operator(()stringoperator(\)) keyword(or) stringoperator(\)) operator(+) string operator(+) ident(filename)operator(\)) +ident(os)operator(.)ident(utime)operator(()ident(filename)operator(,) operator(()ident(fstat)operator(.)ident(st_atime)operator(,) ident(fstat)operator(.)ident(st_mtime)operator(\))operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.2) +comment(#-----------------------------) +ident(os)operator(.)ident(remove)operator(()ident(filename)operator(\)) + +ident(err_flg) operator(=) integer(0) +keyword(for) ident(filename) keyword(in) ident(filenames)operator(:) + keyword(try)operator(:) + ident(os)operator(.)ident(remove)operator(()ident(filename)operator(\)) + keyword(except) exception(OSError)operator(,) ident(err)operator(:) + ident(err_flg) operator(=) integer(1) +keyword(if) ident(err_flg)operator(:) + keyword(raise) exception(OSError)operator(()string operator(%) operator(()ident(filenames)operator(,) ident(err)operator(\))operator(\)) +comment(#-----------------------------) +ident(os)operator(.)ident(remove)operator(()ident(filename)operator(\)) +comment(#-----------------------------) +ident(success) operator(=) integer(0) +keyword(for) ident(filename) keyword(in) ident(filenames)operator(:) + keyword(try)operator(:) + ident(os)operator(.)ident(remove)operator(()ident(filename)operator(\)) + ident(success) operator(+=) integer(1) + keyword(except) exception(OSError)operator(,) ident(err)operator(:) + keyword(pass) +keyword(if) ident(success) operator(!=) predefined(len)operator(()ident(filenames)operator(\))operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()string operator(%) \ + operator(()ident(success)operator(,) predefined(len)operator(()ident(filenames)operator(\))operator(\))operator(\)) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.3) +comment(#-----------------------------) +keyword(import) ident(shutil) +ident(shutil)operator(.)ident(copy)operator(()ident(oldfile)operator(,) ident(newfile)operator(\)) +comment(#-----------------------------) +comment(## NOTE: this doesn't do the same thing as the Perl code,) +comment(## eg, handling of partial writes.) +ident(infile) operator(=) predefined(open)operator(()ident(oldfile)operator(\)) +ident(outfile) operator(=) predefined(open)operator(()ident(newfile)operator(,) stringoperator(\)) + +ident(blksize) operator(=) integer(16384) comment(# preferred block size?) + +keyword(while) pre_constant(True)operator(:) + ident(buf) operator(=) ident(infile)operator(.)ident(read)operator(()ident(blksize)operator(\)) + keyword(if) keyword(not) ident(buf)operator(:) + keyword(break) + ident(outfile)operator(.)ident(write)operator(()ident(buf)operator(\)) + +ident(infile)operator(.)ident(close)operator(()operator(\)) +ident(outfile)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) +comment(# WARNING: these are insecure - do not use in hostile environments) +ident(os)operator(.)ident(system)operator(()string operator(%) operator(()ident(oldfile)operator(,) ident(newfile)operator(\))operator(\)) comment(# unix) +ident(os)operator(.)ident(system)operator(()string operator(%) operator(()ident(oldfile)operator(,) ident(newfile)operator(\))operator(\)) comment(# dos, vms) +comment(#-----------------------------) +keyword(import) ident(shutil) + +ident(shutil)operator(.)ident(copy)operator(()stringoperator(,) stringoperator(\)) + +ident(shutil)operator(.)ident(copy)operator(()stringoperator(,) stringoperator(\)) +ident(os)operator(.)ident(remove)operator(()stringoperator(\)) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.4) +comment(#-----------------------------) +keyword(import) ident(os) +ident(seen) operator(=) operator({)operator(}) + +keyword(def) ident(do_my_thing)operator(()ident(filename)operator(\))operator(:) + ident(fstat) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) + ident(key) operator(=) operator(()ident(fstat)operator(.)ident(st_ino)operator(,) ident(fstat)operator(.)ident(st_dev)operator(\)) + keyword(if) keyword(not) ident(seen)operator(.)ident(get)operator(()ident(key)operator(\))operator(:) + comment(# do something with filename because we haven't) + comment(# seen it before) + keyword(pass) + ident(seen)operator([)ident(key)operator(]) operator(=) ident(seen)operator(.)ident(get)operator(()ident(key)operator(,) integer(0) operator(\)) operator(+) integer(1) + +comment(#-----------------------------) +keyword(for) ident(filename) keyword(in) ident(files)operator(:) + ident(fstat) operator(=) ident(os)operator(.)ident(stat)operator(()ident(filename)operator(\)) + ident(key) operator(=) operator(()ident(fstat)operator(.)ident(st_ino)operator(,) ident(fstat)operator(.)ident(st_dev)operator(\)) + ident(seen)operator(.)ident(setdefault)operator(()ident(key)operator(,) operator([)operator(])operator(\))operator(.)ident(append)operator(()ident(filename)operator(\)) + +ident(keys) operator(=) ident(seen)operator(.)ident(keys)operator(()operator(\)) +ident(keys)operator(.)ident(sort)operator(()operator(\)) +keyword(for) ident(inodev) keyword(in) ident(keys)operator(:) + ident(ino)operator(,) ident(dev) operator(=) ident(inodev) + ident(filenames) operator(=) ident(seen)operator([)ident(inodev)operator(]) + keyword(if) predefined(len)operator(()ident(filenames)operator(\)) operator(>) integer(1)operator(:) + comment(# 'filenames' is a list of filenames for the same file) + keyword(pass) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.5) +comment(#-----------------------------) +keyword(for) ident(filename) keyword(in) ident(os)operator(.)ident(listdir)operator(()ident(dirname)operator(\))operator(:) + comment(# do something with "$dirname/$file") + keyword(pass) +comment(#-----------------------------) +comment(# XXX No -T equivalent in Python) +comment(#-----------------------------) +comment(# 'readir' always skipes '.' and '..' on OSes where those are) +comment(# standard directory names) +keyword(for) ident(filename) keyword(in) ident(os)operator(.)ident(listdir)operator(()ident(dirname)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +comment(# XX Not Implemented -- need to know what DirHandle does) +comment(# use DirHandle;) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.6) +comment(#-----------------------------) +keyword(import) ident(glob) +ident(filenames) operator(=) ident(glob)operator(.)ident(glob)operator(()stringoperator(\)) +comment(#-----------------------------) +ident(filenames) operator(=) operator([)ident(filename) keyword(for) ident(filename) keyword(in) ident(os)operator(.)ident(listdir)operator(()ident(path)operator(\)) keyword(if) ident(filename)operator(.)ident(endswith)operator(()stringoperator(\))operator(]) +comment(#-----------------------------) +keyword(import) ident(re) +ident(allowed_name) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(,) ident(re)operator(.)ident(I)operator(\))operator(.)ident(search) +ident(filenames) operator(=) operator([)ident(f) keyword(for) ident(f) keyword(in) ident(os)operator(.)ident(listdir)operator(()ident(path)operator(\)) keyword(if) ident(allowed_name)operator(()ident(f)operator(\))operator(]) +comment(#-----------------------------) +keyword(import) ident(re)operator(,) ident(os) +ident(allowed_name) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(,) ident(re)operator(.)ident(I)operator(\))operator(.)ident(search) + +ident(fnames) operator(=) operator([)ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(dirname)operator(,) ident(fname)operator(\)) + keyword(for) ident(fname) keyword(in) ident(os)operator(.)ident(listdir)operator(()ident(dirname)operator(\)) + keyword(if) ident(allowed_name)operator(()ident(fname)operator(\))operator(]) +comment(#-----------------------------) +ident(dirs) operator(=) operator([)ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(path)operator(,) ident(f)operator(\)) + keyword(for) ident(f) keyword(in) ident(os)operator(.)ident(listdir)operator(()ident(path)operator(\)) keyword(if) ident(f)operator(.)ident(isdigit)operator(()operator(\))operator(]) +ident(dirs) operator(=) operator([)ident(d) keyword(for) ident(d) keyword(in) ident(dirs) keyword(if) ident(os)operator(.)ident(path)operator(.)ident(isdir)operator(()ident(d)operator(\))operator(]) +ident(dirs) operator(=) predefined(sorted)operator(()ident(dirs)operator(,) ident(key)operator(=)predefined(int)operator(\)) comment(# Sort by numeric value - "9" before "11") +comment(#-----------------------------) + +comment(# @@PLEAC@@_9.7) +comment(# Processing All Files in a Directory Recursively) + +comment(# os.walk is new in 2.3.) + +comment(# For pre-2.3 code, there is os.path.walk, which is) +comment(# little harder to use.) + +comment(#-----------------------------) +keyword(import) ident(os) +keyword(for) ident(root)operator(,) ident(dirs)operator(,) ident(files) keyword(in) ident(os)operator(.)ident(walk)operator(()ident(top)operator(\))operator(:) + keyword(pass) comment(# do whatever) + +comment(#-----------------------------) +keyword(import) ident(os)operator(,) ident(os)operator(.)ident(path) +keyword(for) ident(root)operator(,) ident(dirs)operator(,) ident(files) keyword(in) ident(os)operator(.)ident(walk)operator(()ident(top)operator(\))operator(:) + keyword(for) ident(name) keyword(in) ident(dirs)operator(:) + keyword(print) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(root)operator(,) ident(name)operator(\)) operator(+) string + keyword(for) ident(name) keyword(in) ident(files)operator(:) + keyword(print) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(root)operator(,) ident(name)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(os)operator(,) ident(os)operator(.)ident(path) +ident(numbytes) operator(=) integer(0) +keyword(for) ident(root)operator(,) ident(dirs)operator(,) ident(files) keyword(in) ident(os)operator(.)ident(walk)operator(()ident(top)operator(\))operator(:) + keyword(for) ident(name) keyword(in) ident(files)operator(:) + ident(path) operator(=) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(root)operator(,) ident(name)operator(\)) + ident(numbytes) operator(+=) ident(os)operator(.)ident(path)operator(.)ident(getsize)operator(()ident(path)operator(\)) +keyword(print) string operator(%) operator(()ident(top)operator(,) ident(numbytes)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(os)operator(,) ident(os)operator(.)ident(path) +ident(saved_size)operator(,) ident(saved_name) operator(=) operator(-)integer(1)operator(,) string +keyword(for) ident(root)operator(,) ident(dirs)operator(,) ident(files) keyword(in) ident(os)operator(.)ident(walk)operator(()ident(top)operator(\))operator(:) + keyword(for) ident(name) keyword(in) ident(files)operator(:) + ident(path) operator(=) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(root)operator(,) ident(name)operator(\)) + ident(size) operator(=) ident(os)operator(.)ident(path)operator(.)ident(getsize)operator(()ident(path)operator(\)) + keyword(if) ident(size) operator(>) ident(saved_size)operator(:) + ident(saved_size) operator(=) ident(size) + ident(saved_name) operator(=) ident(path) +keyword(print) string operator(%) operator(() + ident(saved_name)operator(,) ident(top)operator(,) ident(saved_size)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(os)operator(,) ident(os)operator(.)ident(path)operator(,) ident(time) +ident(saved_age)operator(,) ident(saved_name) operator(=) pre_constant(None)operator(,) string +keyword(for) ident(root)operator(,) ident(dirs)operator(,) ident(files) keyword(in) ident(os)operator(.)ident(walk)operator(()ident(top)operator(\))operator(:) + keyword(for) ident(name) keyword(in) ident(files)operator(:) + ident(path) operator(=) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(root)operator(,) ident(name)operator(\)) + ident(age) operator(=) ident(os)operator(.)ident(path)operator(.)ident(getmtime)operator(()ident(path)operator(\)) + keyword(if) ident(saved_age) keyword(is) pre_constant(None) keyword(or) ident(age) operator(>) ident(saved_age)operator(:) + ident(saved_age) operator(=) ident(age) + ident(saved_name) operator(=) ident(path) +keyword(print) string operator(%) operator(()ident(saved_name)operator(,) ident(time)operator(.)ident(ctime)operator(()ident(saved_age)operator(\))operator(\)) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# fdirs - find all directories) +keyword(import) ident(sys)operator(,) ident(os)operator(,) ident(os)operator(.)ident(path) +ident(argv) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) keyword(or) operator([)stringoperator(]) +keyword(for) ident(top) keyword(in) ident(argv)operator(:) + keyword(for) ident(root)operator(,) ident(dirs)operator(,) ident(files) keyword(in) ident(os)operator(.)ident(walk)operator(()ident(top)operator(\))operator(:) + keyword(for) ident(name) keyword(in) ident(dirs)operator(:) + ident(path) operator(=) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()ident(root)operator(,) ident(name)operator(\)) + keyword(print) ident(path) + + +comment(# ^^PLEAC^^_9.8) +comment(#-----------------------------) +comment(# DeleteDir - remove whole directory trees like rm -r) +keyword(import) ident(shutil) +ident(shutil)operator(.)ident(rmtree)operator(()ident(path)operator(\)) + +comment(# DON'T DO THIS:) +keyword(import) ident(os)operator(,) ident(sys) +keyword(def) ident(DeleteDir)operator(()predefined(dir)operator(\))operator(:) + keyword(for) ident(name) keyword(in) ident(os)operator(.)ident(listdir)operator(()predefined(dir)operator(\))operator(:) + predefined(file) operator(=) ident(os)operator(.)ident(path)operator(.)ident(join)operator(()predefined(dir)operator(,) ident(name)operator(\)) + keyword(if) keyword(not) ident(os)operator(.)ident(path)operator(.)ident(islink)operator(()predefined(file)operator(\)) keyword(and) ident(os)operator(.)ident(path)operator(.)ident(isdir)operator(()predefined(file)operator(\))operator(:) + ident(DeleteDir)operator(()predefined(file)operator(\)) + keyword(else)operator(:) + ident(os)operator(.)ident(remove)operator(()predefined(file)operator(\)) + ident(os)operator(.)ident(rmdir)operator(()predefined(dir)operator(\)) + +comment(# @@PLEAC@@_9.9) +comment(# Renaming Files) + +comment(# code sample one to one from my perlcookbook) +comment(# looks strange to me.) +keyword(import) ident(os) +keyword(for) ident(fname) keyword(in) ident(fnames)operator(:) + ident(newname) operator(=) ident(fname) + comment(# change the file's name) + keyword(try)operator(:) + ident(os)operator(.)ident(rename)operator(()ident(fname)operator(,) ident(newname)operator(\)) + keyword(except) exception(OSError)operator(,) ident(err)operator(:) + keyword(print) string operator(%) \ + operator(()ident(fname)operator(,) ident(newfile)operator(,) ident(err)operator(\)) + +comment(# use os.renames if newname needs directory creation.) + +comment(#A vaguely Pythonic solution is:) +keyword(import) ident(glob) +keyword(def) ident(rename)operator(()ident(files)operator(,) ident(transfunc)operator(\)) + keyword(for) ident(fname) keyword(in) ident(fnames)operator(:) + ident(newname) operator(=) ident(transfunc)operator(()ident(fname)operator(\)) + keyword(try)operator(:) + ident(os)operator(.)ident(rename)operator(()ident(fname)operator(,) ident(newname)operator(\)) + keyword(except) exception(OSError)operator(,) ident(err)operator(:) + keyword(print) string operator(%) \ + operator(()ident(fname)operator(,) ident(newfile)operator(,) ident(err)operator(\)) + +keyword(def) ident(transfunc)operator(()ident(fname)operator(\))operator(:) + keyword(return) ident(fname)operator([)operator(:)operator(-)integer(5)operator(]) +ident(rename)operator(()ident(glob)operator(.)ident(glob)operator(()stringoperator(\))operator(,) ident(transfunc)operator(\)) + +keyword(def) ident(transfunc)operator(()ident(fname)operator(\))operator(:) + keyword(return) ident(fname)operator(.)ident(lower)operator(()operator(\)) +ident(rename)operator(()operator([)ident(f) keyword(for) ident(f) keyword(in) ident(glob)operator(.)ident(glob)operator(()stringoperator(\)) keyword(if) keyword(not) ident(f)operator(.)ident(startswith)operator(()stringerror() + +keyword(def) ident(transfunc)operator(()ident(fname)operator(\))operator(:) + keyword(return) ident(fname) operator(+) string +ident(rename)operator(()ident(glob)operator(.)ident(glob)operator(()stringoperator(\))operator(,) ident(transfunc)operator(\)) + +keyword(def) ident(transfunc)operator(()ident(fname)operator(\))operator(:) + ident(answer) operator(=) predefined(raw_input)operator(()ident(fname) operator(+) stringoperator(\)) + keyword(if) ident(answer)operator(.)ident(upper)operator(()operator(\))operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + keyword(return) ident(fname)operator(.)ident(replace)operator(()stringoperator(,) stringoperator(\)) +ident(rename)operator(()ident(glob)operator(.)ident(glob)operator(()stringoperator(\))operator(,) ident(transfunc)operator(\)) + +keyword(def) ident(transfunc)operator(()ident(fname)operator(\))operator(:) + keyword(return) string operator(+) ident(fname)operator([)operator(:)operator(-)integer(1)operator(]) +ident(rename)operator(()ident(glob)operator(.)ident(glob)operator(()stringoperator(\))operator(,) ident(transfunc)operator(\)) + +comment(# This _could_ be made to eval code taken directly from the command line, ) +comment(# but it would be fragile) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_9.10) +comment(#-----------------------------) +keyword(import) ident(os) + +ident(base) operator(=) ident(os)operator(.)ident(path)operator(.)ident(basename)operator(()ident(path)operator(\)) +ident(dirname) operator(=) ident(os)operator(.)ident(path)operator(.)ident(dirname)operator(()ident(path)operator(\)) +ident(dirname)operator(,) ident(filename) operator(=) ident(os)operator(.)ident(path)operator(.)ident(split)operator(()ident(path)operator(\)) +ident(base)operator(,) ident(ext) operator(=) ident(os)operator(.)ident(path)operator(.)ident(splitext)operator(()ident(filename)operator(\)) + +comment(#-----------------------------) +ident(path) operator(=) string +ident(filename) operator(=) ident(os)operator(.)ident(path)operator(.)ident(basename)operator(()ident(path)operator(\)) +ident(dirname) operator(=) ident(os)operator(.)ident(path)operator(.)ident(dirname)operator(()ident(path)operator(\)) + +keyword(print) string operator(%) operator(()ident(dirname)operator(,) ident(filename)operator(\)) +comment(# dir is /usr/lib, file is libc.a) +comment(#-----------------------------) +ident(path) operator(=) string +ident(dirname)operator(,) ident(filename) operator(=) ident(os)operator(.)ident(path)operator(.)ident(split)operator(()ident(path)operator(\)) +ident(name)operator(,) ident(ext) operator(=) ident(os)operator(.)ident(path)operator(.)ident(splitext)operator(()ident(filename)operator(\)) + +keyword(print) string operator(%) operator(()ident(dirname)operator(,) ident(name)operator(,) ident(ext)operator(\)) +comment(# NOTE: The Python code prints) +comment(# dir is /usr/lib, name is libc, extension is .a) +comment(# while the Perl code prints a '/' after the directory name) +comment(# dir is /usr/lib/, name is libc, extension is .a) +comment(#-----------------------------) +keyword(import) ident(macpath) +ident(path) operator(=) string +ident(dirname)operator(,) ident(base) operator(=) ident(macpath)operator(.)ident(split)operator(()ident(path)operator(\)) +ident(name)operator(,) ident(ext) operator(=) ident(macpath)operator(.)ident(splitext)operator(()ident(base)operator(\)) + +keyword(print) string operator(%) operator(()ident(dirname)operator(,) ident(name)operator(,) ident(ext)operator(\)) +comment(# dir is Hard%20Drive:System%20Folder, name is README, extension is .txt) +comment(#-----------------------------) +comment(# DON'T DO THIS - it's not portable.) +keyword(def) ident(extension)operator(()ident(path)operator(\))operator(:) + ident(pos) operator(=) ident(path)operator(.)ident(find)operator(()stringoperator(\)) + keyword(if) ident(pos) operator(==) operator(-)integer(1)operator(:) + keyword(return) string + ident(ext) operator(=) ident(path)operator([)ident(pos)operator(+)integer(1)operator(:)operator(]) + keyword(if) string keyword(in) ident(ext)operator(:) + comment(# wasn't passed a basename -- this is of the form 'x.y/z') + keyword(return) string + keyword(return) ident(ext) +comment(#-----------------------------) + +comment(# @@PLEAC@@_9.11) + +comment(#!/usr/bin/python) +comment(# sysmirror - build spectral forest of symlinks) +keyword(import) ident(sys)operator(,) ident(os)operator(,) ident(os)operator(.)ident(path) + +ident(pgmname) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(]) +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(!=)integer(3)operator(:) + keyword(print) string operator(%) ident(pgmname) + keyword(raise) exception(SystemExit) + +operator(()ident(srcdir)operator(,) ident(dstdir)operator(\)) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)integer(3)operator(]) +keyword(if) keyword(not) ident(os)operator(.)ident(path)operator(.)ident(isdir)operator(()ident(srcdir)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(pgmname)operator(,)ident(srcdir)operator(\)) + keyword(raise) exception(SystemExit) +keyword(if) keyword(not) ident(os)operator(.)ident(path)operator(.)ident(isdir)operator(()ident(dstdir)operator(\))operator(:) + keyword(try)operator(:) + ident(os)operator(.)ident(mkdir)operator(()ident(dstdir)operator(\)) + keyword(except) exception(OSError)operator(:) + keyword(print) string operator(%) operator(()ident(pgmname)operator(,)ident(dstdir)operator(\)) + keyword(raise) exception(SystemExit) + +comment(# fix relative paths) +ident(srcdir) operator(=) ident(os)operator(.)ident(path)operator(.)ident(abspath)operator(()ident(srcdir)operator(\)) +ident(dstdir) operator(=) ident(os)operator(.)ident(path)operator(.)ident(abspath)operator(()ident(dstdir)operator(\)) + +keyword(def) ident(wanted)operator(()ident(arg)operator(,) ident(dirname)operator(,) ident(names)operator(\))operator(:) + keyword(for) ident(direntry) keyword(in) ident(names)operator(:) + ident(relname) operator(=) string operator(%) operator(()ident(dirname)operator(,) ident(direntry)operator(\)) + keyword(if) ident(os)operator(.)ident(path)operator(.)ident(isdir)operator(()ident(relname)operator(\))operator(:) + ident(mode) operator(=) ident(os)operator(.)ident(stat)operator(()ident(relname)operator(\))operator(.)ident(st_mode) + keyword(try)operator(:) + ident(os)operator(.)ident(mkdir)operator(()string operator(%) operator(()ident(dstdir)operator(,)ident(relname)operator(\))operator(,) ident(mode)operator(\)) + keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(dstdir)operator(,)ident(relname)operator(\)) + keyword(raise) exception(SystemExit) + keyword(else)operator(:) + keyword(if) ident(relname)operator([)operator(:)integer(2)operator(]) operator(==) stringoperator(:) + ident(relname) operator(=) ident(relname)operator([)integer(2)operator(:)operator(]) + ident(os)operator(.)ident(symlink)operator(()string operator(%) operator(()ident(srcdir)operator(,) ident(relname)operator(\))operator(,) string operator(%) operator(()ident(dstdir)operator(,)ident(relname)operator(\))operator(\)) + +ident(os)operator(.)ident(chdir)operator(()ident(srcdir)operator(\)) +ident(os)operator(.)ident(path)operator(.)ident(walk)operator(()stringoperator(,)ident(wanted)operator(,)pre_constant(None)operator(\)) + +comment(# @@PLEAC@@_9.12) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + + +comment(# ^^PLEAC^^_10.0) +comment(#-----------------------------) +comment(# DO NOT DO THIS...) +ident(greeted) operator(=) integer(0) +keyword(def) ident(hello)operator(()operator(\))operator(:) + keyword(global) ident(greeted) + ident(greeted) operator(+=) integer(1) + keyword(print) string + +comment(#... as using a callable object to save state is cleaner) +comment(# class hello) +comment(# def __init__(self\):) +comment(# self.greeted = 0) +comment(# def __call__(self\):) +comment(# self.greeted += 1) +comment(# print "hi there") +comment(# hello = hello(\)) +comment(#-----------------------------) +ident(hello)operator(()operator(\)) comment(# call subroutine hello with no arguments/parameters) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.1) +comment(#-----------------------------) +keyword(import) ident(math) +comment(# Provided for demonstration purposes only. Use math.hypot(\) instead.) +keyword(def) ident(hypotenuse)operator(()ident(side1)operator(,) ident(side2)operator(\))operator(:) + keyword(return) ident(math)operator(.)ident(sqrt)operator(()ident(side1)operator(**)integer(2) operator(+) ident(side2)operator(**)integer(2)operator(\)) + +ident(diag) operator(=) ident(hypotenuse)operator(()integer(3)operator(,) integer(4)operator(\)) comment(# diag is 5.0) +comment(#-----------------------------) +keyword(print) ident(hypotenuse)operator(()integer(3)operator(,) integer(4)operator(\)) comment(# prints 5.0) + +ident(a) operator(=) operator(()integer(3)operator(,) integer(4)operator(\)) +keyword(print) ident(hypotenuse)operator(()operator(*)ident(a)operator(\)) comment(# prints 5.0) +comment(#-----------------------------) +ident(both) operator(=) ident(men) operator(+) ident(women) +comment(#-----------------------------) +ident(nums) operator(=) operator([)float(1)float(.4)operator(,) float(3)float(.5)operator(,) float(6)float(.7)operator(]) +comment(# Provided for demonstration purposes only. Use:) +comment(# ints = [int(num\) for num in nums] ) +keyword(def) ident(int_all)operator(()ident(nums)operator(\))operator(:) + ident(retlist) operator(=) operator([)operator(]) comment(# make new list for return) + keyword(for) ident(n) keyword(in) ident(nums)operator(:) + ident(retlist)operator(.)ident(append)operator(()predefined(int)operator(()ident(n)operator(\))operator(\)) + keyword(return) ident(retlist) +ident(ints) operator(=) ident(int_all)operator(()ident(nums)operator(\)) comment(# nums unchanged) +comment(#-----------------------------) +ident(nums) operator(=) operator([)float(1)float(.4)operator(,) float(3)float(.5)operator(,) float(6)float(.7)operator(]) + +keyword(def) ident(trunc_em)operator(()ident(nums)operator(\))operator(:) + keyword(for) ident(i)operator(,)ident(elem) keyword(in) predefined(enumerate)operator(()ident(nums)operator(\))operator(:) + ident(nums)operator([)ident(i)operator(]) operator(=) predefined(int)operator(()ident(elem)operator(\)) +ident(trunc_em)operator(()ident(nums)operator(\)) comment(# nums now [1,3,6]) + +comment(#-----------------------------) +comment(# By convention, if a method (or function\) modifies an object) +comment(# in-place, it returns None rather than the modified object.) +comment(# None of Python's built-in functions modify in-place; methods) +comment(# such as list.sort(\) are somewhat more common.) +ident(mylist) operator(=) operator([)integer(3)operator(,)integer(2)operator(,)integer(1)operator(]) +ident(mylist) operator(=) ident(mylist)operator(.)ident(sort)operator(()operator(\)) comment(# incorrect - returns None) +ident(mylist) operator(=) predefined(sorted)operator(()ident(mylist)operator(\)) comment(# correct - returns sorted copy) +ident(mylist)operator(.)ident(sort)operator(()operator(\)) comment(# correct - sorts in-place) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.2) +comment(#-----------------------------) +comment(# Using global variables is discouraged - by default variables) +comment(# are visible only at and below the scope at which they are declared.) +comment(# Global variables modified by a function or method must be declared ) +comment(# using the "global" keyword if they are modified) +keyword(def) ident(somefunc)operator(()operator(\))operator(:) + ident(variable) operator(=) ident(something) comment(# variable is invisible outside of somefunc) +comment(#-----------------------------) +keyword(import) ident(sys) +ident(name)operator(,) ident(age) operator(=) ident(sys)operator(.)ident(args)operator([)integer(1)operator(:)operator(]) comment(# assumes two and only two command line parameters) +ident(start) operator(=) ident(fetch_time)operator(()operator(\)) +comment(#-----------------------------) +ident(a)operator(,) ident(b) operator(=) ident(pair) +ident(c) operator(=) ident(fetch_time)operator(()operator(\)) + +keyword(def) ident(check_x)operator(()ident(x)operator(\))operator(:) + ident(y) operator(=) string + ident(run_check)operator(()operator(\)) + keyword(if) ident(condition)operator(:) + keyword(print) stringoperator(,) ident(x) +comment(#-----------------------------) +keyword(def) ident(save_list)operator(()operator(*)ident(args)operator(\))operator(:) + ident(Global_List)operator(.)ident(extend)operator(()ident(args)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.3) +comment(#-----------------------------) +comment(## Python allows static nesting of scopes for reading but not writing,) +comment(## preferring to use objects. The closest equivalent to:) +comment(#{) +comment(# my $counter;) +comment(# sub next_counter { return ++$counter }) +comment(#}) +comment(## is:) +keyword(def) ident(next_counter)operator(()ident(counter)operator(=)operator([)integer(0)operator(])operator(\))operator(:) comment(# default lists are created once only.) + ident(counter)operator([)integer(0)operator(]) operator(+=) integer(1) + keyword(return) ident(counter)operator([)integer(0)operator(]) + +comment(# As that's a little tricksy (and can't make more than one counter\),) +comment(# many Pythonistas would prefer either:) +keyword(def) ident(make_counter)operator(()operator(\))operator(:) + ident(counter) operator(=) integer(0) + keyword(while) pre_constant(True)operator(:) + ident(counter) operator(+=) integer(1) + keyword(yield) ident(counter) +ident(next_counter) operator(=) ident(make_counter)operator(()operator(\))operator(.)ident(next) + +comment(# Or:) +keyword(class) ident(Counter)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(counter) operator(=) integer(0) + keyword(def) ident(__call__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(counter) operator(+=) integer(1) + keyword(return) pre_constant(self)operator(.)ident(counter) +ident(next_counter) operator(=) ident(Counter)operator(()operator(\)) + +comment(#-----------------------------) +comment(## A close equivalent of) +comment(#BEGIN {) +comment(# my $counter = 42;) +comment(# sub next_counter { return ++$counter }) +comment(# sub prev_counter { return --$counter }) +comment(#}) +comment(## is to use a list (to save the counter\) and closured functions:) +keyword(def) ident(make_counter)operator(()ident(start)operator(=)integer(0)operator(\))operator(:) + ident(counter) operator(=) operator([)ident(start)operator(]) + keyword(def) ident(next_counter)operator(()operator(\))operator(:) + ident(counter)operator([)integer(0)operator(]) operator(+=) integer(1) + keyword(return) ident(counter)operator([)integer(0)operator(]) + keyword(def) ident(prev_counter)operator(()operator(\))operator(:) + ident(counter)operator([)integer(0)operator(]) operator(-=) integer(1) + keyword(return) ident(counter)operator([)integer(0)operator(]) + keyword(return) ident(next_counter)operator(,) ident(prev_counter) +ident(next_counter)operator(,) ident(prev_counter) operator(=) ident(make_counter)operator(()operator(\)) + +comment(## A clearer way uses a class:) +keyword(class) ident(Counter)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(start)operator(=)integer(0)operator(\))operator(:) + pre_constant(self)operator(.)ident(value) operator(=) ident(start) + keyword(def) predefined(next)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(value) operator(+=) integer(1) + keyword(return) pre_constant(self)operator(.)ident(value) + keyword(def) ident(prev)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(value) operator(-=) integer(1) + keyword(return) pre_constant(self)operator(.)ident(value) + keyword(def) ident(__int__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(value) + +ident(counter) operator(=) ident(Counter)operator(()integer(42)operator(\)) +ident(next_counter) operator(=) ident(counter)operator(.)ident(next) +ident(prev_counter) operator(=) ident(counter)operator(.)ident(prev) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.4) +comment(## This sort of code inspection is liable to change as) +comment(## Python evolves. There may be cleaner ways to do this.) +comment(## This also may not work for code called from functions) +comment(## written in C.) +comment(#-----------------------------) +keyword(import) ident(sys) +ident(this_function) operator(=) ident(sys)operator(.)ident(_getframe)operator(()integer(0)operator(\))operator(.)ident(f_code)operator(.)ident(co_name) +comment(#-----------------------------) +ident(i) operator(=) integer(0) comment(# how far up the call stack to look) +ident(module) operator(=) ident(sys)operator(.)ident(_getframe)operator(()ident(i)operator(\))operator(.)ident(f_globals)operator([)stringoperator(]) +ident(filename) operator(=) ident(sys)operator(.)ident(_getframe)operator(()ident(i)operator(\))operator(.)ident(f_code)operator(.)ident(co_filename) +ident(line) operator(=) ident(sys)operator(.)ident(_getframe)operator(()ident(i)operator(\))operator(.)ident(f_lineno) +ident(subr) operator(=) ident(sys)operator(.)ident(_getframe)operator(()ident(i)operator(\))operator(.)ident(f_code)operator(.)ident(co_name) +ident(has_args) operator(=) predefined(bool)operator(()ident(sys)operator(.)ident(_getframe)operator(()ident(i)operator(+)integer(1)operator(\))operator(.)ident(f_code)operator(.)ident(co_argcount)operator(\)) + +comment(# 'wantarray' is Perl specific) + +comment(#-----------------------------) +ident(me) operator(=) ident(whoami)operator(()operator(\)) +ident(him) operator(=) ident(whowasi)operator(()operator(\)) + +keyword(def) ident(whoami)operator(()operator(\))operator(:) + ident(sys)operator(.)ident(_getframe)operator(()integer(1)operator(\))operator(.)ident(f_code)operator(.)ident(co_name) +keyword(def) ident(whowasi)operator(()operator(\))operator(:) + ident(sys)operator(.)ident(_getframe)operator(()integer(2)operator(\))operator(.)ident(f_code)operator(.)ident(co_name) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.5) +comment(#-----------------------------) +comment(# Every variable name is a reference to an object, thus nothing special) +comment(# needs to be done to pass a list or a dict as a parameter.) +ident(list_diff)operator(()ident(list1)operator(,) ident(list2)operator(\)) +comment(#-----------------------------) +comment(# Note: if one parameter to zip(\) is longer it will be truncated) +keyword(def) ident(add_vecpair)operator(()ident(x)operator(,) ident(y)operator(\))operator(:) + keyword(return) operator([)ident(x1)operator(+)ident(y1) keyword(for) ident(x1)operator(,) ident(y1) keyword(in) predefined(zip)operator(()ident(x)operator(,) ident(y)operator(\))operator(]) + +ident(a) operator(=) operator([)integer(1)operator(,) integer(2)operator(]) +ident(b) operator(=) operator([)integer(5)operator(,) integer(8)operator(]) +keyword(print) stringoperator(.)ident(join)operator(()operator([)predefined(str)operator(()ident(n)operator(\)) keyword(for) ident(n) keyword(in) ident(add_vecpair)operator(()ident(a)operator(,) ident(b)operator(\))operator(])operator(\)) +comment(#=> 6 10) +comment(#-----------------------------) +comment(# DO NOT DO THIS:) +keyword(assert) predefined(isinstance)operator(()ident(x)operator(,) predefined(type)operator(()operator([)operator(])operator(\))operator(\)) keyword(and) predefined(isinstance)operator(()ident(y)operator(,) predefined(type)operator(()operator([)operator(])operator(\))operator(\))operator(,) \ + string +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.6) +comment(#-----------------------------) +comment(# perl return context is not something standard in python...) +comment(# but still you can achieve something alike if you really need it) +comment(# (but you must really need it badly since you should never use this!!\)) +comment(#) +comment(# see http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/284742 for more) +comment(#) +comment(# NB: it has been tested under Python 2.3.x and no guarantees can be given) +comment(# that it works under any future Python version.) +keyword(import) ident(inspect)operator(,)ident(dis) + +keyword(def) ident(expecting)operator(()operator(\))operator(:) + string + ident(f) operator(=) ident(inspect)operator(.)ident(currentframe)operator(()operator(\))operator(.)ident(f_back)operator(.)ident(f_back) + ident(bytecode) operator(=) ident(f)operator(.)ident(f_code)operator(.)ident(co_code) + ident(i) operator(=) ident(f)operator(.)ident(f_lasti) + ident(instruction) operator(=) predefined(ord)operator(()ident(bytecode)operator([)ident(i)operator(+)integer(3)operator(])operator(\)) + keyword(if) ident(instruction) operator(==) ident(dis)operator(.)ident(opmap)operator([)stringoperator(])operator(:) + ident(howmany) operator(=) predefined(ord)operator(()ident(bytecode)operator([)ident(i)operator(+)integer(4)operator(])operator(\)) + keyword(return) ident(howmany) + keyword(elif) ident(instruction) operator(==) ident(dis)operator(.)ident(opmap)operator([)stringoperator(])operator(:) + keyword(return) integer(0) + keyword(return) integer(1) + +keyword(def) ident(cleverfunc)operator(()operator(\))operator(:) + ident(howmany) operator(=) ident(expecting)operator(()operator(\)) + keyword(if) ident(howmany) operator(==) integer(0)operator(:) + keyword(print) string + keyword(if) ident(howmany) operator(==) integer(2)operator(:) + keyword(return) integer(1)operator(,)integer(2) + keyword(elif) ident(howmany) operator(==) integer(3)operator(:) + keyword(return) integer(1)operator(,)integer(2)operator(,)integer(3) + keyword(return) integer(1) + +ident(cleverfunc)operator(()operator(\)) +ident(x) operator(=) ident(cleverfunc)operator(()operator(\)) +keyword(print) ident(x) +ident(x)operator(,)ident(y) operator(=) ident(cleverfunc)operator(()operator(\)) +keyword(print) ident(x)operator(,)ident(y) +ident(x)operator(,)ident(y)operator(,)ident(z) operator(=) ident(cleverfunc)operator(()operator(\)) +keyword(print) ident(x)operator(,)ident(y)operator(,)ident(z) + +comment(# ^^PLEAC^^_10.7) +comment(#-----------------------------) +ident(thefunc)operator(()ident(increment)operator(=) stringoperator(,) ident(start)operator(=)stringoperator(,) ident(finish)operator(=)stringoperator(\)) +ident(thefunc)operator(()ident(start)operator(=) stringoperator(,)ident(finish)operator(=)stringoperator(\)) +ident(thefunc)operator(()ident(finish)operator(=) stringoperator(\)) +ident(thefunc)operator(()ident(start)operator(=)stringoperator(,) ident(increment)operator(=)stringoperator(\)) +comment(#-----------------------------) +keyword(def) ident(thefunc)operator(()ident(increment)operator(=)stringoperator(,) + ident(finish)operator(=)stringoperator(,) + ident(start)operator(=)stringoperator(\))operator(:) + keyword(if) ident(increment)operator(.)ident(endswith)operator(()stringoperator(\))operator(:) + keyword(pass) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.8) +comment(#-----------------------------) +ident(a)operator(,) ident(_)operator(,) ident(c) operator(=) ident(func)operator(()operator(\)) comment(# Use _ as a placeholder...) +ident(a)operator(,) ident(ignore)operator(,) ident(c) operator(=) ident(func)operator(()operator(\)) comment(# ...or assign to an otherwise unused variable) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.9) +comment(#-----------------------------) +keyword(def) ident(somefunc)operator(()operator(\))operator(:) + ident(mylist) operator(=) operator([)operator(]) + ident(mydict) operator(=) operator({)operator(}) + comment(# ...) + keyword(return) ident(mylist)operator(,) ident(mydict) + +ident(mylist)operator(,) ident(mydict) operator(=) ident(somefunc)operator(()operator(\)) +comment(#-----------------------------) +keyword(def) ident(fn)operator(()operator(\))operator(:) + keyword(return) ident(a)operator(,) ident(b)operator(,) ident(c) + +comment(#-----------------------------) +ident(h0)operator(,) ident(h1)operator(,) ident(h2) operator(=) ident(fn)operator(()operator(\)) +ident(tuple_of_dicts) operator(=) ident(fn)operator(()operator(\)) comment(# eg: tuple_of_dicts[2]["keystring"]) +ident(r0)operator(,) ident(r1)operator(,) ident(r2) operator(=) ident(fn)operator(()operator(\)) comment(# eg: r2["keystring"]) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.10) +comment(#-----------------------------) +comment(# Note: Exceptions are almost always preferred to error values) +keyword(return) +comment(#-----------------------------) +keyword(def) ident(empty_retval)operator(()operator(\))operator(:) + keyword(return) pre_constant(None) + +keyword(def) ident(empty_retval)operator(()operator(\))operator(:) + keyword(return) comment(# identical to return None) + +keyword(def) ident(empty_retval)operator(()operator(\))operator(:) + keyword(pass) comment(# None returned by default (empty func needs pass\)) +comment(#-----------------------------) +ident(a) operator(=) ident(yourfunc)operator(()operator(\)) +keyword(if) ident(a)operator(:) + keyword(pass) +comment(#-----------------------------) +ident(a) operator(=) ident(sfunc)operator(()operator(\)) +keyword(if) keyword(not) ident(a)operator(:) + keyword(raise) exception(AssertionError)operator(()stringoperator(\)) + +keyword(assert) ident(sfunc)operator(()operator(\))operator(,) string +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.11) +comment(# Prototypes are inapplicable to Python as Python disallows calling) +comment(# functions without using brackets, and user functions are able to) +comment(# mimic built-in functions with no special actions required as they) +comment(# only flatten lists (and convert dicts to named arguments\) if) +comment(# explicitly told to do so. Python functions use named parameters) +comment(# rather than shifting arguments:) + +keyword(def) ident(myfunc)operator(()ident(a)operator(,) ident(b)operator(,) ident(c)operator(=)integer(4)operator(\))operator(:) + keyword(print) ident(a)operator(,) ident(b)operator(,) ident(c) + +ident(mylist) operator(=) operator([)integer(1)operator(,)integer(2)operator(]) + +ident(mydict1) operator(=) operator({)stringoperator(:) integer(2)operator(,) stringoperator(:) integer(3)operator(}) +ident(mydict2) operator(=) operator({)stringoperator(:) integer(2)operator(}) + +ident(myfunc)operator(()integer(1)operator(,)integer(2)operator(,)integer(3)operator(\)) +comment(#=> 1 2 3) + +ident(myfunc)operator(()integer(1)operator(,)integer(2)operator(\)) +comment(#=> 1 2 4) + +ident(myfunc)operator(()operator(*)ident(mylist)operator(\)) +comment(#=> 1 2 4) + +ident(myfunc)operator(()integer(5)operator(,) operator(*)ident(mylist)operator(\)) +comment(#=> 5, 1, 2) + +ident(myfunc)operator(()integer(5)operator(,) operator(**)ident(mydict1)operator(\)) +comment(#=> 5, 2, 3) + +ident(myfunc)operator(()integer(5)operator(,) operator(**)ident(mydict2)operator(\)) +comment(#=> 5, 2, 4) + +ident(myfunc)operator(()ident(c)operator(=)integer(3)operator(,) ident(b)operator(=)integer(2)operator(,) ident(a)operator(=)integer(1)operator(\)) +comment(#=> 1, 2, 3) + +ident(myfunc)operator(()ident(b)operator(=)integer(2)operator(,) ident(a)operator(=)integer(1)operator(\)) +comment(#=> 1, 2, 4) + +ident(myfunc)operator(()ident(mylist)operator(,) ident(mydict1)operator(\)) +comment(#=> [1, 2] {'c': 3, 'b': 2} 4) + +comment(# For demonstration purposes only - don't do this) +keyword(def) ident(mypush)operator(()ident(mylist)operator(,) operator(*)ident(vals)operator(\))operator(:) + ident(mylist)operator(.)ident(extend)operator(()ident(vals)operator(\)) + +ident(mylist) operator(=) operator([)operator(]) +ident(mypush)operator(()ident(mylist)operator(,) integer(1)operator(,) integer(2)operator(,) integer(3)operator(,) integer(4)operator(,) integer(5)operator(\)) +keyword(print) ident(mylist) +comment(#=> [1, 2, 3, 4, 5]) + +comment(# ^^PLEAC^^_10.12) +comment(#-----------------------------) +keyword(raise) exception(ValueError)operator(()stringoperator(\)) comment(# specific exception class) +keyword(raise) exception(Exception)operator(()stringoperator(\)) comment(# general exception) +keyword(raise) string comment(# string exception (deprecated\)) +comment(#-----------------------------) +comment(# Note that bare excepts are considered bad style. Normally you should) +comment(# trap specific exceptions. For instance these bare excepts will) +comment(# catch KeyboardInterrupt, SystemExit, and MemoryError as well as) +comment(# more common errors. In addition they force you to import sys to) +comment(# get the error message.) +keyword(import) ident(warnings)operator(,) ident(sys) +keyword(try)operator(:) + ident(func)operator(()operator(\)) +keyword(except)operator(:) + ident(warnings)operator(.)ident(warn)operator(()string operator(+) predefined(str)operator(()ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\))operator(\)) +comment(#-----------------------------) +keyword(try)operator(:) + ident(func)operator(()operator(\)) +keyword(except)operator(:) + ident(warnings)operator(.)ident(warn)operator(()string operator(+) predefined(str)operator(()ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\))operator(\)) +comment(#-----------------------------) +keyword(class) ident(MoonPhaseError)operator(()exception(Exception)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(phase)operator(\))operator(:) + pre_constant(self)operator(.)ident(phase) operator(=) ident(phase) +keyword(class) ident(FullMoonError)operator(()ident(MoonPhaseError)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + ident(MoonPhaseError)operator(.)ident(__init__)operator(()stringoperator(\)) + +keyword(def) ident(func)operator(()operator(\))operator(:) + keyword(raise) ident(FullMoonError)operator(()operator(\)) + +comment(# Ignore only FullMoonError exceptions) +keyword(try)operator(:) + ident(func)operator(()operator(\)) +keyword(except) ident(FullMoonError)operator(:) + keyword(pass) +comment(#-----------------------------) +comment(# Ignore only MoonPhaseError for a full moon) +keyword(try)operator(:) + ident(func)operator(()operator(\)) +keyword(except) ident(MoonPhaseError)operator(,) ident(err)operator(:) + keyword(if) ident(err)operator(.)ident(phase) operator(!=) stringoperator(:) + keyword(raise) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.13) +comment(# There is no direct equivalent to 'local' in Python, and) +comment(# it's impossible to write your own. But then again, even in) +comment(# Perl it's considered poor style.) + +comment(# DON'T DO THIS (You probably shouldn't use global variables anyway\):) +keyword(class) ident(Local)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(globalname)operator(,) ident(val)operator(\))operator(:) + pre_constant(self)operator(.)ident(globalname) operator(=) ident(globalname) + pre_constant(self)operator(.)ident(globalval) operator(=) predefined(globals)operator(()operator(\))operator([)ident(globalname)operator(]) + predefined(globals)operator(()operator(\))operator([)ident(globalname)operator(]) operator(=) ident(val) + + keyword(def) ident(__del__)operator(()pre_constant(self)operator(\))operator(:) + predefined(globals)operator(()operator(\))operator([)pre_constant(self)operator(.)ident(globalname)operator(]) operator(=) pre_constant(self)operator(.)ident(globalval) + +ident(foo) operator(=) integer(4) + +keyword(def) ident(blah)operator(()operator(\))operator(:) + keyword(print) ident(foo) + +keyword(def) ident(blech)operator(()operator(\))operator(:) + ident(temp) operator(=) ident(Local)operator(()stringoperator(,) integer(6)operator(\)) + ident(blah)operator(()operator(\)) + +ident(blah)operator(()operator(\)) +ident(blech)operator(()operator(\)) +ident(blah)operator(()operator(\)) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.14) +comment(#-----------------------------) +ident(grow) operator(=) ident(expand) +ident(grow)operator(()operator(\)) comment(# calls expand(\)) + +comment(#-----------------------------) +ident(one)operator(.)ident(var) operator(=) ident(two)operator(.)ident(table) comment(# make one.var the same as two.table) +ident(one)operator(.)ident(big) operator(=) ident(two)operator(.)ident(small) comment(# make one.big the same as two.small) +comment(#-----------------------------) +ident(fred) operator(=) ident(barney) comment(# alias fred to barney) +comment(#-----------------------------) +ident(s) operator(=) ident(red)operator(()stringoperator(\)) +keyword(print) ident(s) +comment(#> careful here) +comment(#-----------------------------) +comment(# Note: the 'text' should be HTML escaped if it can contain) +comment(# any of the characters '<', '>' or '&') +keyword(def) ident(red)operator(()ident(text)operator(\))operator(:) + keyword(return) string)delimiter(")> operator(+) ident(text) operator(+) string)delimiter(")> +comment(#-----------------------------) +keyword(def) ident(color_font)operator(()ident(color)operator(,) ident(text)operator(\))operator(:) + keyword(return) string%s)delimiter(")> operator(%) operator(()ident(color)operator(,) ident(text)operator(\)) + +keyword(def) ident(red)operator(()ident(text)operator(\))operator(:) keyword(return) ident(color_font)operator(()stringoperator(,) ident(text)operator(\)) +keyword(def) ident(green)operator(()ident(text)operator(\))operator(:) keyword(return) ident(color_font)operator(()stringoperator(,) ident(text)operator(\)) +keyword(def) ident(blue)operator(()ident(text)operator(\))operator(:) keyword(return) ident(color_font)operator(()stringoperator(,) ident(text)operator(\)) +keyword(def) ident(purple)operator(()ident(text)operator(\))operator(:) keyword(return) ident(color_font)operator(()stringoperator(,) ident(text)operator(\)) +comment(# etc) +comment(#-----------------------------) +comment(# This is done in Python by making an object, instead of) +comment(# saving state in a local anonymous context.) +keyword(class) ident(ColorFont)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(color)operator(\))operator(:) + pre_constant(self)operator(.)ident(color) operator(=) ident(color) + keyword(def) ident(__call__)operator(()pre_constant(self)operator(,) ident(text)operator(\))operator(:) + keyword(return) string%s)delimiter(")> operator(%) operator(()pre_constant(self)operator(.)ident(color)operator(,) ident(text)operator(\)) + +ident(colors) operator(=) stringoperator(.)ident(split)operator(()stringoperator(\)) +keyword(for) ident(name) keyword(in) ident(colors)operator(:) + predefined(globals)operator(()operator(\))operator([)ident(name)operator(]) operator(=) ident(ColorFont)operator(()ident(name)operator(\)) +comment(#-----------------------------) +comment(# If you really don't want to make a new class, you can) +comment(# fake it somewhat by passing in default args.) +ident(colors) operator(=) stringoperator(.)ident(split)operator(()stringoperator(\)) +keyword(for) ident(name) keyword(in) ident(colors)operator(:) + keyword(def) ident(temp)operator(()ident(text)operator(,) ident(color) operator(=) ident(name)operator(\))operator(:) + keyword(return) string%s)delimiter(")> operator(%) operator(()ident(color)operator(,) ident(text)operator(\)) + predefined(globals)operator(()operator(\))operator([)ident(name)operator(]) operator(=) ident(temp) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.15) + +comment(# Python has the ability to derive from ModuleType and add) +comment(# new __getattr__ and __setattr__ methods. I don't know the) +comment(# expected way to use them to emulate Perl's AUTOLOAD. Instead,) +comment(# here's how something similar would be done in Python. This) +comment(# uses the ColorFont defined above.) + +comment(#-----------------------------) +keyword(class) ident(AnyColor)operator(:) + keyword(def) ident(__getattr__)operator(()pre_constant(self)operator(,) ident(name)operator(\))operator(:) + keyword(return) ident(ColorFont)operator(()ident(name)operator(\)) + +ident(colors) operator(=) ident(AnyColor)operator(()operator(\)) + +keyword(print) ident(colors)operator(.)ident(chartreuse)operator(()stringoperator(\)) + +comment(#-----------------------------) +comment(## Skipping this translation because 'local' is too Perl) +comment(## specific, and there isn't enough context to figure out) +comment(## what this is supposed to do.) +comment(#{) +comment(# local *yellow = \\&violet;) +comment(# local (*red, *green\) = (\\&green, \\&red\);) +comment(# print_stuff(\);) +comment(#}) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.16) +comment(#-----------------------------) +keyword(def) ident(outer)operator(()ident(arg1)operator(\))operator(:) + ident(x) operator(=) ident(arg1) operator(+) integer(35) + keyword(def) ident(inner)operator(()operator(\))operator(:) + keyword(return) ident(x) operator(*) integer(19) + keyword(return) ident(x) operator(+) ident(inner)operator(()operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_10.17) +comment(#-----------------------------) +keyword(import) ident(mailbox)operator(,) ident(sys) +ident(mbox) operator(=) ident(mailbox)operator(.)ident(PortableUnixMailbox)operator(()ident(sys)operator(.)ident(stdin)operator(\)) + +keyword(def) ident(extract_data)operator(()ident(msg)operator(,) ident(idx)operator(\))operator(:) + ident(subject) operator(=) ident(msg)operator(.)ident(getheader)operator(()stringoperator(,) stringoperator(\))operator(.)ident(strip)operator(()operator(\)) + keyword(if) ident(subject)operator([)operator(:)integer(3)operator(])operator(.)ident(lower)operator(()operator(\)) operator(==) stringoperator(:) + ident(subject) operator(=) ident(subject)operator([)integer(3)operator(:)operator(])operator(.)ident(lstrip)operator(()operator(\)) + ident(text) operator(=) ident(msg)operator(.)ident(fp)operator(.)ident(read)operator(()operator(\)) + keyword(return) ident(subject)operator(,) ident(idx)operator(,) ident(msg)operator(,) ident(text) +ident(messages) operator(=) operator([)ident(extract_data)operator(()ident(idx)operator(,) ident(msg)operator(\)) keyword(for) ident(idx)operator(,) ident(msg) keyword(in) predefined(enumerate)operator(()ident(mbox)operator(\))operator(]) + +comment(#-----------------------------) +comment(# Sorts by subject then by original position in the list) +keyword(for) ident(subject)operator(,) ident(pos)operator(,) ident(msg)operator(,) ident(text) keyword(in) predefined(sorted)operator(()ident(messages)operator(\))operator(:) + keyword(print) stringoperator(%)operator(()ident(msg)operator(,) ident(text)operator(\)) + +comment(#-----------------------------) +comment(# Sorts by subject then date then original position) +keyword(def) ident(subject_date_position)operator(()ident(elem)operator(\))operator(:) + keyword(return) operator(()ident(elem)operator([)integer(0)operator(])operator(,) ident(elem)operator([)integer(2)operator(])operator(.)ident(getdate)operator(()stringoperator(\))operator(,) ident(elem)operator([)integer(1)operator(])operator(\)) +ident(messages)operator(.)ident(sort)operator(()ident(key)operator(=)ident(subject_date_position)operator(\)) + +comment(# Pre 2.4:) +ident(messages) operator(=) predefined(sorted)operator(()ident(messages)operator(,) ident(key)operator(=)ident(subject_date_position)operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.0) +comment(#Introduction.) +comment(# In Python, all names are references.) +comment(# All objects are inherently anonymous, they don't know what names refer to them.) +keyword(print) ident(ref) comment(# prints the value that the name ref refers to. ) +ident(ref) operator(=) integer(3) comment(# assigns the name ref to the value 3.) +comment(#-----------------------------) +ident(aref) operator(=) ident(mylist) +comment(#-----------------------------) +ident(aref) operator(=) operator([)integer(3)operator(,) integer(4)operator(,) integer(5)operator(]) comment(# aref is a name for this list) +ident(href) operator(=) operator({)stringoperator(:) stringoperator(,) stringoperator(:) stringoperator(}) comment(# href is a name for this dictionary) +comment(#-----------------------------) +comment(# Python doesn't have autovivification as (for simple types\) there is no difference between a name and a reference.) +comment(# If we try the equivalent of the Perl code we get the list, not a reference to the list.) +comment(#-----------------------------) +comment(# To handle multidimensional arrays, you should use an extension to Python,) +comment(# such as numarray (http://www.stsci.edu/resources/software_hardware/numarray\)) +comment(#-----------------------------) +comment(# In Python, assignment doesn't return anything. ) +comment(#-----------------------------) +ident(Nat) operator(=) operator({) stringoperator(:) stringoperator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) hex(0x5bb5580) +operator(}) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.1) +ident(aref) operator(=) ident(mylist) +ident(anon_list) operator(=) operator([)integer(1)operator(,) integer(3)operator(,) integer(5)operator(,) integer(7)operator(,) integer(9)operator(]) +ident(anon_copy) operator(=) ident(anon_list) +ident(implicit_creation) operator(=) operator([)integer(2)operator(,) integer(4)operator(,) integer(6)operator(,) integer(8)operator(,) integer(10)operator(]) +comment(#-----------------------------) +ident(anon_list)operator(.)ident(append)operator(()integer(11)operator(\)) +comment(#-----------------------------) +ident(two) operator(=) ident(implicit_creation)operator([)integer(0)operator(]) +comment(#-----------------------------) +comment(# To get the last index of a list, you can use len(\) ) +comment(# [or list.__len__(\) - but don't] directly) +ident(last_idx) operator(=) predefined(len)operator(()ident(aref)operator(\)) operator(-) integer(1) + +comment(# Normally, though, you'd use an index of -1 for the last) +comment(# element, -2 for the second last, etc.) +keyword(print) ident(implicit_creation)operator([)operator(-)integer(1)operator(]) +comment(#=> 10) + +ident(num_items) operator(=) predefined(len)operator(()ident(aref)operator(\)) +comment(#-----------------------------) +ident(last_idx) operator(=) ident(aref)operator(.)ident(__len__)operator(()operator(\)) operator(-) integer(1) +ident(num_items) operator(=) ident(aref)operator(.)ident(__len__)operator(()operator(\)) +comment(#-----------------------------) +keyword(if) keyword(not) predefined(isinstance)operator(()ident(someVar)operator(,) predefined(type)operator(()operator([)operator(])operator(\))operator(\))operator(:) + keyword(print) string +comment(#-----------------------------) +keyword(print) ident(list_ref) +comment(#-----------------------------) +comment(# sort is in place.) +ident(list_ref)operator(.)ident(sort)operator(()operator(\)) +comment(#-----------------------------) +ident(list_ref)operator(.)ident(append)operator(()ident(item)operator(\)) +comment(#-----------------------------) +keyword(def) ident(list_ref)operator(()operator(\))operator(:) + keyword(return) operator([)operator(]) + +ident(aref1) operator(=) ident(list_ref)operator(()operator(\)) +ident(aref2) operator(=) ident(list_ref)operator(()operator(\)) +comment(# aref1 and aref2 point to different lists.) +comment(#-----------------------------) +ident(list_ref)operator([)ident(N)operator(]) comment(# refers to the Nth item in the list_ref list.) +comment(#-----------------------------) +comment(# The following two statements are equivalent and return up to 3 elements) +comment(# at indices 3, 4, and 5 (if they exist\).) +ident(pie)operator([)integer(3)operator(:)integer(6)operator(]) +ident(pie)operator([)integer(3)operator(:)integer(6)operator(:)integer(1)operator(]) +comment(#-----------------------------) +comment(# This will insert 3 elements, overwriting elements at indices 3,4, or 5 - if they exist.) +ident(pie)operator([)integer(3)operator(:)integer(6)operator(]) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +comment(#-----------------------------) +keyword(for) ident(item) keyword(in) ident(pie)operator(:) + keyword(print) ident(item) + +comment(# DON'T DO THIS (this type of indexing should be done with enumerate\)) +comment(# xrange does not create a list 0..len(pie\) - 1, it creates an object ) +comment(# that returns one index at a time.) +keyword(for) ident(idx) keyword(in) predefined(xrange)operator(()predefined(len)operator(()ident(pie)operator(\))operator(\))operator(:) + keyword(print) ident(pie)operator([)ident(idx)operator(]) + +comment(# @@PLEAC@@_11.2) +comment(# Making Hashes of Arrays) + +predefined(hash)operator([)stringoperator(])operator(.)ident(append)operator(()stringoperator(\)) + +keyword(for) ident(mystr) keyword(in) predefined(hash)operator(.)ident(keys)operator(()operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(mystr)operator(,) predefined(hash)operator([)ident(mystr)operator(])operator(\)) + +predefined(hash)operator([)stringoperator(]) operator(=) operator([)integer(3)operator(,) integer(4)operator(,) integer(5)operator(]) + +ident(values) operator(=) predefined(hash)operator([)stringoperator(]) + +predefined(hash)operator([)stringoperator(])operator(.)ident(append)operator(()ident(value)operator(\)) + +comment(# autovivification also does not work in python.) +ident(residents) operator(=) ident(phone2name)operator([)ident(number)operator(]) +comment(# do this instead) +ident(residents) operator(=) ident(phone2name)operator(.)ident(get)operator(()ident(number)operator(,) operator([)operator(])operator(\)) + + +comment(# @@PLEAC@@_11.3) +comment(# Taking References to Hashes) + +ident(href) operator(=) predefined(hash) +ident(anon_hash) operator(=) operator({) stringoperator(:)stringoperator(,) string operator(:) string operator(}) +ident(anon_hash_copy) operator(=) ident(anon_hash)operator(.)ident(copy)operator(()operator(\)) + +predefined(hash) operator(=) ident(href) +ident(value) operator(=) ident(href)operator([)ident(key)operator(]) +predefined(slice) operator(=) operator([)ident(href)operator([)ident(k)operator(]) keyword(for) ident(k) keyword(in) operator(()ident(key1)operator(,) ident(key2)operator(,) ident(key3)operator(\))operator(]) +ident(keys) operator(=) predefined(hash)operator(.)ident(keys)operator(()operator(\)) + +keyword(import) ident(types) +keyword(if) predefined(type)operator(()ident(someref)operator(\)) operator(!=) ident(types)operator(.)ident(DictType)operator(:) + keyword(raise) string operator(%) predefined(type)operator(()ident(someref)operator(\)) +keyword(if) predefined(isinstance)operator(()ident(someref)operator(,)predefined(dict)operator(\))operator(:) + keyword(raise) string operator(%) predefined(type)operator(()ident(someref)operator(\)) + +keyword(for) ident(href) keyword(in) operator(() ident(ENV)operator(,) ident(INC) operator(\))operator(:) + keyword(for) ident(key) keyword(in) ident(href)operator(.)ident(keys)operator(()operator(\))operator(:) + keyword(print) string %s)delimiter(")> operator(%) operator(()ident(key)operator(,) ident(href)operator([)ident(key)operator(])operator(\)) + +ident(values) operator(=) operator([)ident(hash_ref)operator([)ident(k)operator(]) keyword(for) ident(k) keyword(in) operator(()ident(key1)operator(,) ident(key2)operator(,) ident(key3)operator(\))operator(]) + +keyword(for) ident(key) keyword(in) operator(()stringoperator(,) stringoperator(,) stringoperator(\))operator(:) + ident(hash_ref)operator([)ident(k)operator(]) operator(+=) integer(7) comment(# not like in perl but the same result.) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.4) +comment(#-----------------------------) +ident(cref) operator(=) ident(func) +ident(cref) operator(=) keyword(lambda) ident(a)operator(,) ident(b)operator(:) operator(...) +comment(#-----------------------------) +ident(returned) operator(=) ident(cref)operator(()ident(arguments)operator(\)) +comment(#-----------------------------) +ident(funcname) operator(=) string +predefined(locals)operator(()operator(\))operator([)ident(funcname)operator(])operator(()operator(\))operator(;) +comment(#-----------------------------) +ident(commands) operator(=) operator({) + stringoperator(:) ident(joy)operator(,) + stringoperator(:) ident(sullen)operator(,) + stringoperator(:) operator(()keyword(lambda) operator(:) ident(sys)operator(.)ident(exit)operator(()operator(\))operator(\))operator(,) comment(# In this case "done: sys.exit" would suffice) + stringoperator(:) ident(angry)operator(,) + operator(}) + +keyword(print) stringoperator(,) +ident(cmd) operator(=) predefined(raw_input)operator(()operator(\)) +keyword(if) ident(cmd) keyword(in) ident(commands)operator(:) + ident(commands)operator([)ident(cmd)operator(])operator(()operator(\)) +keyword(else)operator(:) + keyword(print) string operator(%) ident(cmd) +comment(#-----------------------------) +keyword(def) ident(counter_maker)operator(()operator(\))operator(:) + ident(start) operator(=) operator([)integer(0)operator(]) + keyword(def) ident(counter_function)operator(()operator(\))operator(:) + comment(# start refers to the variable defined in counter_maker, but) + comment(# we can't reassign or increment variables in parent scopes.) + comment(# By using a one-element list we can modify the list without) + comment(# reassigning the variable. This way of using a list is very) + comment(# like a reference.) + ident(start)operator([)integer(0)operator(]) operator(+=) integer(1) + keyword(return) ident(start)operator([)integer(0)operator(])operator(-)integer(1) + keyword(return) ident(counter_function) + +ident(counter) operator(=) ident(counter_maker)operator(()operator(\)) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(5)operator(\))operator(:) + keyword(print) ident(counter)operator(()operator(\)) +comment(#-----------------------------) +ident(counter1) operator(=) ident(counter_maker)operator(()operator(\)) +ident(counter2) operator(=) ident(counter_maker)operator(()operator(\)) + +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(5)operator(\))operator(:) + keyword(print) ident(counter1)operator(()operator(\)) +keyword(print) ident(counter1)operator(()operator(\))operator(,) ident(counter2)operator(()operator(\)) +comment(#=> 0) +comment(#=> 1) +comment(#=> 2) +comment(#=> 3) +comment(#=> 4) +comment(#=> 5 0) +comment(#-----------------------------) +keyword(import) ident(time) +keyword(def) ident(timestamp)operator(()operator(\))operator(:) + ident(start_time) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) + keyword(def) ident(elapsed)operator(()operator(\))operator(:) + keyword(return) ident(time)operator(.)ident(time)operator(()operator(\)) operator(-) ident(start_time) + keyword(return) ident(elapsed) +ident(early) operator(=) ident(timestamp)operator(()operator(\)) +ident(time)operator(.)ident(sleep)operator(()integer(20)operator(\)) +ident(later) operator(=) ident(timestamp)operator(()operator(\)) +ident(time)operator(.)ident(sleep)operator(()integer(10)operator(\)) +keyword(print) string operator(%) ident(early)operator(()operator(\)) +keyword(print) string operator(%) ident(later)operator(()operator(\)) +comment(#=> It's been 30 seconds since early.) +comment(#=> It's been 10 seconds since later.) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.5) +comment(# A name is a reference to an object and an object can be referred to) +comment(# by any number of names. There is no way to manipulate pointers or) +comment(# an object's id. This section is thus inapplicable.) +ident(x) operator(=) integer(1) +ident(y) operator(=) ident(x) +keyword(print) ident(x)operator(,) predefined(id)operator(()ident(x)operator(\))operator(,) ident(y)operator(,) predefined(id)operator(()ident(y)operator(\)) +ident(x) operator(+=) integer(1) comment(# "x" now refers to a different object than y) +keyword(print) ident(x)operator(,) predefined(id)operator(()ident(x)operator(\))operator(,) ident(y)operator(,) predefined(id)operator(()ident(y)operator(\)) +ident(y) operator(=) integer(4) comment(# "y" now refers to a different object than it did before) +keyword(print) ident(x)operator(,) predefined(id)operator(()ident(x)operator(\))operator(,) ident(y)operator(,) predefined(id)operator(()ident(y)operator(\)) + +comment(# Some objects (including ints and strings\) are immutable, however, which) +comment(# can give the illusion of a by-value/by-reference distinction:) +ident(a) operator(=) ident(x) operator(=) operator([)integer(1)operator(]) +ident(b) operator(=) ident(y) operator(=) integer(1) +ident(c) operator(=) ident(z) operator(=) string +keyword(print) ident(a)operator(,) ident(b)operator(,) ident(c) +comment(#=> [1] 1 s) + +ident(x) operator(+=) ident(x) comment(# calls list.__iadd__ which is inplace.) +ident(y) operator(+=) ident(y) comment(# can't find int.__iadd__ so calls int.__add__ which isn't inplace) +ident(z) operator(+=) ident(z) comment(# can't find str.__iadd__ so calls str.__add__ which isn't inplace ) +keyword(print) ident(a)operator(,) ident(b)operator(,) ident(c) +comment(#=> [1, 1] 1 s) + +comment(# @@PLEAC@@_11.6) +comment(# As indicated by the previous section, everything is referenced, so) +comment(# just create a list as normal, and beware that augmented assignment) +comment(# works differently with immutable objects to mutable ones:) +ident(mylist) operator(=) operator([)integer(1)operator(,) stringoperator(,) operator([)integer(1)operator(])operator(]) +keyword(print) ident(mylist) +comment(#=> [1, s, [1]]) + +keyword(for) ident(elem) keyword(in) ident(mylist)operator(:) + ident(elem) operator(*=) integer(2) +keyword(print) ident(mylist) +comment(#=> [1, s, [1, 1]]) + +ident(mylist)operator([)integer(0)operator(]) operator(*=) integer(2) +ident(mylist)operator([)operator(-)integer(1)operator(]) operator(*=) integer(2) +keyword(print) ident(mylist) +comment(#=> [1, s, [1, 1, 1, 1]]) + +comment(# If you need to modify every value in a list, you should use a list comprehension) +comment(# which does NOT modify inplace:) +keyword(import) ident(math) +ident(mylist) operator(=) operator([)operator(()ident(val)operator(**)integer(3) operator(*) integer(4)operator(/)integer(3)operator(*)ident(math)operator(.)ident(pi)operator(\)) keyword(for) ident(val) keyword(in) ident(mylist)operator(]) + +comment(# @@PLEAC@@_11.7) +comment(#-----------------------------) +ident(c1) operator(=) ident(mkcounter)operator(()integer(20)operator(\)) +ident(c2) operator(=) ident(mkcounter)operator(()integer(77)operator(\)) + +keyword(print) string operator(%) ident(c1)operator([)stringoperator(])operator(()operator(\)) comment(# 21) +keyword(print) string operator(%) ident(c2)operator([)stringoperator(])operator(()operator(\)) comment(# 78) +keyword(print) string operator(%) ident(c1)operator([)stringoperator(])operator(()operator(\)) comment(# 22) +keyword(print) string operator(%) ident(c1)operator([)stringoperator(])operator(()operator(\)) comment(# 21) +keyword(print) string operator(%) ident(c2)operator([)stringoperator(])operator(()operator(\)) comment(# 77) +comment(#-----------------------------) +comment(# DON'T DO THIS. Use an object instead ) +keyword(def) ident(mkcounter)operator(()ident(start)operator(\))operator(:) + ident(count) operator(=) operator([)ident(start)operator(]) + keyword(def) predefined(next)operator(()operator(\))operator(:) + ident(count)operator([)integer(0)operator(]) operator(+=) integer(1) + keyword(return) ident(count)operator([)integer(0)operator(]) + keyword(def) ident(prev)operator(()operator(\))operator(:) + ident(count)operator([)integer(0)operator(]) operator(-=) integer(1) + keyword(return) ident(count)operator([)integer(0)operator(]) + keyword(def) ident(get)operator(()operator(\))operator(:) + keyword(return) ident(count)operator([)integer(0)operator(]) + keyword(def) predefined(set)operator(()ident(value)operator(\))operator(:) + ident(count)operator([)integer(0)operator(]) operator(=) ident(value) + keyword(return) ident(count)operator([)integer(0)operator(]) + keyword(def) ident(bump)operator(()ident(incr)operator(\))operator(:) + ident(count)operator([)integer(0)operator(]) operator(+=) ident(incr) + keyword(return) ident(count)operator([)integer(0)operator(]) + keyword(def) ident(reset)operator(()operator(\))operator(:) + ident(count)operator([)integer(0)operator(]) operator(=) ident(start) + keyword(return) ident(count)operator([)integer(0)operator(]) + keyword(return) operator({) + stringoperator(:) predefined(next)operator(,) stringoperator(:) ident(prev)operator(,) stringoperator(:) ident(get)operator(,) stringoperator(:) predefined(set)operator(,) + stringoperator(:) ident(bump)operator(,) stringoperator(:) ident(reset)operator(,) stringoperator(:) ident(prev)operator(}) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.8) +comment(#-----------------------------) +ident(mref) operator(=) ident(obj)operator(.)ident(meth) +comment(# later...) +ident(mref)operator(()stringoperator(,) stringoperator(,) stringoperator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.9) +comment(#-----------------------------) +ident(record) operator(=) operator({) + stringoperator(:) stringoperator(,) + stringoperator(:) integer(132)operator(,) + stringoperator(:) stringoperator(,) + stringoperator(:) integer(23)operator(,) + stringoperator(:) integer(37000)operator(,) + stringoperator(:) operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(,) +operator(}) +keyword(print) string operator(%) operator(()ident(record)operator([)stringoperator(])operator(,) + stringoperator(.)ident(join)operator(()ident(record)operator([)stringoperator(])operator(\))operator(\)) +comment(#-----------------------------) +ident(byname) operator(=) operator({)operator(}) +ident(byname)operator([)ident(record)operator([)stringoperator(])operator(]) operator(=) ident(record) + +ident(rp) operator(=) ident(byname)operator(.)ident(get)operator(()stringoperator(\)) +keyword(if) ident(rp)operator(:) + keyword(print) stringoperator(%) ident(rp)operator([)stringoperator(]) + +ident(byname)operator([)stringoperator(])operator([)stringoperator(])operator(.)ident(append)operator(()stringoperator(\)) +keyword(print) string operator(%) predefined(len)operator(()ident(byname)operator([)stringoperator(])operator([)stringoperator(])operator(\)) + +keyword(for) ident(name)operator(,) ident(record) keyword(in) ident(byname)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(name)operator(,) ident(record)operator([)stringoperator(])operator(\)) + +ident(employees) operator(=) operator({)operator(}) +ident(employees)operator([)ident(record)operator([)stringoperator(])operator(]) operator(=) ident(record)operator(;) + +comment(# lookup by id) +ident(rp) operator(=) ident(employees)operator(.)ident(get)operator(()integer(132)operator(\)) +keyword(if) operator(()ident(rp)operator(\))operator(:) + keyword(print) string operator(%) ident(rp)operator([)stringoperator(]) + +ident(byname)operator([)stringoperator(])operator([)stringoperator(]) operator(*=) float(1)float(.035) + +ident(peons) operator(=) operator([)ident(r) keyword(for) ident(r) keyword(in) ident(employees)operator(.)ident(values)operator(()operator(\)) keyword(if) ident(r)operator([)stringoperator(]) operator(==) stringoperator(]) +ident(tsevens) operator(=) operator([)ident(r) keyword(for) ident(r) keyword(in) ident(employees)operator(.)ident(values)operator(()operator(\)) keyword(if) ident(r)operator([)stringoperator(]) operator(==) integer(27)operator(]) + +comment(# Go through all records) +keyword(print) ident(employees)operator(.)ident(values)operator(()operator(\)) + +keyword(for) ident(rp) keyword(in) predefined(sorted)operator(()ident(employees)operator(.)ident(values)operator(()operator(\))operator(,) ident(key)operator(=)keyword(lambda) ident(x)operator(:)ident(x)operator([)stringoperator(])operator(\))operator(:) + keyword(print) stringoperator(%)operator(()ident(rp)operator([)stringoperator(])operator(,) ident(rp)operator([)stringoperator(])operator(\)) + +comment(# use @byage, an array of arrays of records) +ident(byage) operator(=) operator({)operator(}) +ident(byage)operator([)ident(record)operator([)stringoperator(])operator(]) operator(=) ident(byage)operator(.)ident(get)operator(()ident(record)operator([)stringoperator(])operator(,) operator([)operator(])operator(\)) operator(+) operator([)ident(record)operator(]) + +keyword(for) ident(age)operator(,) ident(records) keyword(in) ident(byage)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) ident(records) + keyword(print) stringoperator(%)ident(age)operator(,) + keyword(for) ident(rp) keyword(in) ident(records)operator(:) + keyword(print) ident(rp)operator([)stringoperator(])operator(,) + keyword(print) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.10) +comment(#-----------------------------) +ident(FieldName)operator(:) ident(Value) +comment(#-----------------------------) +keyword(for) ident(record) keyword(in) ident(list_of_records)operator(:) + comment(# Note: sorted added in Python 2.4) + keyword(for) ident(key) keyword(in) predefined(sorted)operator(()ident(record)operator(.)ident(keys)operator(()operator(\))operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(key)operator(,) ident(record)operator([)ident(key)operator(])operator(\)) + keyword(print) +comment(#-----------------------------) +keyword(import) ident(re) +ident(list_of_records) operator(=) operator([)operator({)operator(})operator(]) +keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + comment(# EOF) + keyword(break) + comment(# Remove trailing \\n:) + ident(line) operator(=) ident(line)operator([)operator(:)integer(1)operator(]) + keyword(if) keyword(not) ident(line)operator(.)ident(strip)operator(()operator(\))operator(:) + comment(# New record) + ident(list_of_records)operator(.)ident(append)operator(()operator({)operator(})operator(\)) + keyword(continue) + ident(key)operator(,) ident(value) operator(=) ident(re)operator(.)ident(split)operator(()stringoperator(,) ident(line)operator(,) integer(1)operator(\)) + comment(# Assign the key/value to the last item in the list_of_records:) + ident(list_of_records)operator([)operator(-)integer(1)operator(])operator([)ident(key)operator(]) operator(=) ident(value) +comment(#-----------------------------) +comment(# @@PLEAC@@_11.11) +keyword(import) ident(pprint) + +ident(mylist) operator(=) operator([)operator([)integer(1)operator(,)integer(2)operator(,)integer(3)operator(])operator(,) operator([)integer(4)operator(,) operator([)integer(5)operator(,)integer(6)operator(,)integer(7)operator(])operator(,) integer(8)operator(,)integer(9)operator(,) operator([)integer(0)operator(,)integer(3)operator(,)integer(5)operator(])operator(])operator(,) integer(7)operator(,) integer(8)operator(]) +ident(mydict) operator(=) operator({)stringoperator(:) stringoperator(,) stringoperator(:)operator([)integer(1)operator(,)integer(2)operator(,)integer(3)operator(])operator(}) +ident(pprint)operator(.)ident(pprint)operator(()ident(mylist)operator(,) ident(width)operator(=)integer(1)operator(\)) + +ident(fmtdict) operator(=) ident(pprint)operator(.)ident(pformat)operator(()ident(mydict)operator(,) ident(width)operator(=)integer(1)operator(\)) +keyword(print) ident(fmtdict) +comment(# "import pprint; help(pprint\)" for more details) + +comment(# @@INCOMPLETE@@) +comment(# Note that pprint does not currently handle user objects) + +comment(#-----------------------------) +comment(# @@PLEAC@@_11.12) +ident(newlist) operator(=) predefined(list)operator(()ident(mylist)operator(\)) comment(# shallow copy) +ident(newdict) operator(=) predefined(dict)operator(()ident(mydict)operator(\)) comment(# shallow copy) + +comment(# Pre 2.3:) +keyword(import) ident(copy) +ident(newlist) operator(=) ident(copy)operator(.)ident(copy)operator(()ident(mylist)operator(\)) comment(# shallow copy) +ident(newdict) operator(=) ident(copy)operator(.)ident(copy)operator(()ident(mydict)operator(\)) comment(# shallow copy) + +comment(# shallow copies copy a data structure, but don't copy the items in those) +comment(# data structures so if there are nested data structures, both copy and) +comment(# original will refer to the same object) +ident(mylist) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +ident(newlist) operator(=) predefined(list)operator(()ident(mylist)operator(\)) +ident(mylist)operator([)integer(0)operator(]) operator(=) string +keyword(print) ident(mylist)operator(,) ident(newlist) +comment(#=> ['0', '2', '3'] ['1', '2', '3']) + +ident(mylist) operator(=) operator([)operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(,) integer(4)operator(]) +ident(newlist) operator(=) predefined(list)operator(()ident(mylist)operator(\)) +ident(mylist)operator([)integer(0)operator(])operator([)integer(0)operator(]) operator(=) string +keyword(print) ident(mylist)operator(,) ident(newlist) +comment(#=> [['0', '2', '3'], 4] [['0', '2', '3'], 4]) +comment(#-----------------------------) +keyword(import) ident(copy) +ident(newlist) operator(=) ident(copy)operator(.)ident(deepcopy)operator(()ident(mylist)operator(\)) comment(# deep copy) +ident(newdict) operator(=) ident(copy)operator(.)ident(deepcopy)operator(()ident(mydict)operator(\)) comment(# deep copy) + +comment(# deep copies copy a data structure recursively:) +keyword(import) ident(copy) + +ident(mylist) operator(=) operator([)operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(,) integer(4)operator(]) +ident(newlist) operator(=) ident(copy)operator(.)ident(deepcopy)operator(()ident(mylist)operator(\)) +ident(mylist)operator([)integer(0)operator(])operator([)integer(0)operator(]) operator(=) string +keyword(print) ident(mylist)operator(,) ident(newlist) +comment(#=> [['0', '2', '3'], 4] [['1', '2', '3'], 4]) +comment(#-----------------------------) +comment(# @@PLEAC@@_11.13) +keyword(import) ident(pickle) +keyword(class) ident(Foo)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(val) operator(=) integer(1) + +ident(x) operator(=) ident(Foo)operator(()operator(\)) +ident(x)operator(.)ident(val) operator(=) integer(3) +ident(p_x) operator(=) ident(pickle)operator(.)ident(dumps)operator(()ident(x)operator(\)) comment(# Also pickle.dump(x, myfile\) which writes to myfile) +keyword(del) ident(x) +ident(x) operator(=) ident(pickle)operator(.)ident(loads)operator(()ident(p_x)operator(\)) comment(# Also x = pickle.load(myfile\) which loads from myfile) +keyword(print) ident(x)operator(.)ident(val) +comment(#=> 3) +comment(#-----------------------------) +comment(# @@PLEAC@@_11.14) +keyword(import) ident(os)operator(,) ident(shelve) +ident(fname) operator(=) string +keyword(if) keyword(not) ident(os)operator(.)ident(path)operator(.)ident(exists)operator(()ident(fname)operator(\))operator(:) + ident(d) operator(=) ident(shelve)operator(.)ident(open)operator(()stringoperator(\)) + keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(100000)operator(\))operator(:) + ident(d)operator([)predefined(str)operator(()ident(i)operator(\))operator(]) operator(=) ident(i) + ident(d)operator(.)ident(close)operator(()operator(\)) + +ident(d) operator(=) ident(shelve)operator(.)ident(open)operator(()stringoperator(\)) +keyword(print) ident(d)operator([)stringoperator(]) +keyword(print) ident(d)operator([)stringoperator(]) comment(# KeyError) +comment(#-----------------------------) + +comment(# @@PLEAC@@_11.15) +comment(# bintree - binary tree demo program) +comment(# Use the heapq module instead?) +keyword(import) ident(random) +keyword(import) ident(warnings) + +keyword(class) ident(BTree)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(value) operator(=) pre_constant(None) + + comment(### insert given value into proper point of) + comment(### the tree, extending this node if necessary.) + keyword(def) ident(insert)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + keyword(if) pre_constant(self)operator(.)ident(value) keyword(is) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(left) operator(=) ident(BTree)operator(()operator(\)) + pre_constant(self)operator(.)ident(right) operator(=) ident(BTree)operator(()operator(\)) + pre_constant(self)operator(.)ident(value) operator(=) ident(value) + keyword(elif) pre_constant(self)operator(.)ident(value) operator(>) ident(value)operator(:) + pre_constant(self)operator(.)ident(left)operator(.)ident(insert)operator(()ident(value)operator(\)) + keyword(elif) pre_constant(self)operator(.)ident(value) operator(<) ident(value)operator(:) + pre_constant(self)operator(.)ident(right)operator(.)ident(insert)operator(()ident(value)operator(\)) + keyword(else)operator(:) + ident(warnings)operator(.)ident(warn)operator(()stringoperator(%)ident(value)operator(\)) + + comment(# recurse on left child, ) + comment(# then show current value, ) + comment(# then recurse on right child.) + keyword(def) ident(in_order)operator(()pre_constant(self)operator(\))operator(:) + keyword(if) pre_constant(self)operator(.)ident(value) keyword(is) keyword(not) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(left)operator(.)ident(in_order)operator(()operator(\)) + keyword(print) pre_constant(self)operator(.)ident(value)operator(,) + pre_constant(self)operator(.)ident(right)operator(.)ident(in_order)operator(()operator(\)) + + comment(# show current value, ) + comment(# then recurse on left child, ) + comment(# then recurse on right child.) + keyword(def) ident(pre_order)operator(()pre_constant(self)operator(\))operator(:) + keyword(if) pre_constant(self)operator(.)ident(value) keyword(is) keyword(not) pre_constant(None)operator(:) + keyword(print) pre_constant(self)operator(.)ident(value)operator(,) + pre_constant(self)operator(.)ident(left)operator(.)ident(pre_order)operator(()operator(\)) + pre_constant(self)operator(.)ident(right)operator(.)ident(pre_order)operator(()operator(\)) + + comment(# recurse on left child, ) + comment(# then recurse on right child,) + comment(# then show current value. ) + keyword(def) ident(post_order)operator(()pre_constant(self)operator(\))operator(:) + keyword(if) pre_constant(self)operator(.)ident(value) keyword(is) keyword(not) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(left)operator(.)ident(post_order)operator(()operator(\)) + pre_constant(self)operator(.)ident(right)operator(.)ident(post_order)operator(()operator(\)) + keyword(print) pre_constant(self)operator(.)ident(value)operator(,) + + comment(# find out whether provided value is in the tree.) + comment(# if so, return the node at which the value was found.) + comment(# cut down search time by only looking in the correct) + comment(# branch, based on current value.) + keyword(def) ident(search)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + keyword(if) pre_constant(self)operator(.)ident(value) keyword(is) keyword(not) pre_constant(None)operator(:) + keyword(if) pre_constant(self)operator(.)ident(value) operator(==) ident(value)operator(:) + keyword(return) pre_constant(self) + keyword(if) ident(value) operator(<) pre_constant(self)operator(.)ident(value)operator(:) + keyword(return) pre_constant(self)operator(.)ident(left)operator(.)ident(search)operator(()ident(value)operator(\)) + keyword(else)operator(:) + keyword(return) pre_constant(self)operator(.)ident(right)operator(.)ident(search)operator(()ident(value)operator(\)) + +keyword(def) ident(test)operator(()operator(\))operator(:) + ident(root) operator(=) ident(BTree)operator(()operator(\)) + + keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(20)operator(\))operator(:) + ident(root)operator(.)ident(insert)operator(()ident(random)operator(.)ident(randint)operator(()integer(1)operator(,) integer(1000)operator(\))operator(\)) + + comment(# now dump out the tree all three ways) + keyword(print) stringoperator(,) ident(root)operator(.)ident(pre_order)operator(()operator(\)) + keyword(print) stringoperator(,) ident(root)operator(.)ident(in_order)operator(()operator(\)) + keyword(print) stringoperator(,) ident(root)operator(.)ident(post_order)operator(()operator(\)) + + comment(### prompt until empty line) + keyword(while) pre_constant(True)operator(:) + ident(val) operator(=) predefined(raw_input)operator(()stringoperator(\))operator(.)ident(strip)operator(()operator(\)) + keyword(if) keyword(not) ident(val)operator(:) + keyword(break) + ident(val) operator(=) predefined(int)operator(()ident(val)operator(\)) + ident(found) operator(=) ident(root)operator(.)ident(search)operator(()ident(val)operator(\)) + keyword(if) ident(found)operator(:) + keyword(print) stringoperator(%)operator(()ident(val)operator(,) ident(found)operator(,) ident(found)operator(.)ident(value)operator(\)) + keyword(else)operator(:) + keyword(print) string operator(%) ident(val) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(test)operator(()operator(\)) + + +comment(# ^^PLEAC^^_12.0) +comment(#-----------------------------) +comment(## Python's "module" is the closest equivalent to Perl's "package") + + +comment(#=== In the file "Alpha.py") +ident(name) operator(=) string + +comment(#=== End of file) + +comment(#=== In the file "Omega.py") + +ident(name) operator(=) string +comment(#=== End of file) + +keyword(import) ident(Alpha)operator(,) ident(Omega) +keyword(print) string operator(%) operator(()ident(Alpha)operator(.)ident(name)operator(,) ident(Omega)operator(.)ident(name)operator(\)) +comment(#> Alpha is first, Omega is last.) +comment(#-----------------------------) +comment(# Python does not have an equivalent to "compile-time load") +keyword(import) ident(sys) + +comment(# Depending on the implementation, this could use a builtin) +comment(# module or load a file with the extension .py, .pyc, pyo, .pyd,) +comment(# .so, .dll, or (with imputils\) load from other files.) +keyword(import) ident(Cards)operator(.)ident(Poker) + +comment(#-----------------------------) +comment(#=== In the file Cards/Poker.py) +ident(__all__) operator(=) operator([)stringoperator(,) stringoperator(]) comment(# not usually needed) +ident(card_deck) operator(=) operator([)operator(]) +keyword(def) ident(shuffle)operator(()operator(\))operator(:) + keyword(pass) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.1) +comment(#-----------------------------) +comment(#== In the file "YourModule.py") + +ident(__version__) operator(=) operator(()integer(1)operator(,) integer(0)operator(\)) comment(# Or higher) +ident(__all__) operator(=) operator([)stringoperator(,) stringoperator(]) comment(# Override names included in "... import *") + comment(# Note: 'import *' is considered poor style) + comment(# and it is rare to use this variable.) +comment(########################) +comment(# your code goes here) +comment(########################) + +comment(#-----------------------------) +keyword(import) ident(YourModule) comment(# Import the module into my package) + comment(# (does not import any of its symbols\)) + +keyword(import) ident(YourModule) keyword(as) ident(Module) comment(# Use a different name for the module) + +keyword(from) ident(YourModule) keyword(import) operator(*) comment(# Import all module symbols not starting) + comment(# with an underscore (default\); if __all__) + comment(# is defined, only imports those symbols.) + comment(# Using this is discouraged unless the ) + comment(# module is specifically designed for it.) + +keyword(from) ident(YourModule) keyword(import) ident(name1)operator(,) ident(name2)operator(,) ident(xxx) + comment(# Import the named symbols from the module) + +keyword(from) ident(YourModule) keyword(import) ident(name1) keyword(as) ident(name2) + comment(# Import the named object, but use a) + comment(# different name to access it locally.) + +comment(#-----------------------------) +ident(__all__) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) +comment(#-----------------------------) +ident(__all__) operator(=) operator([)stringoperator(,) stringoperator(]) +comment(#-----------------------------) +keyword(from) ident(YourModule) keyword(import) ident(Op_Func)operator(,) ident(Table)operator(,) ident(F1) +comment(#-----------------------------) +keyword(from) ident(YourModule) keyword(import) ident(Functions)operator(,) ident(Table) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.2) +comment(#-----------------------------) +comment(# no import) +ident(mod) operator(=) string +keyword(try)operator(:) + predefined(__import__)operator(()ident(mod)operator(\)) +keyword(except) exception(ImportError)operator(,) ident(err)operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) operator(()ident(mod)operator(,) ident(err)operator(\))operator(\)) + +comment(# imports into current package) +keyword(try)operator(:) + keyword(import) ident(module) +keyword(except) exception(ImportError)operator(,) ident(err)operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) operator(()ident(err)operator(,) operator(\))operator(\)) + +comment(# imports into current package, if the name is known) +keyword(try)operator(:) + keyword(import) ident(module) +keyword(except) exception(ImportError)operator(,) ident(err)operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) operator(()ident(err)operator(,) operator(\))operator(\)) + +comment(# Use a fixed local name for a named module) +ident(mod) operator(=) string +keyword(try)operator(:) + ident(local_name) operator(=) predefined(__import__)operator(()ident(mod)operator(\)) +keyword(except) exception(ImportError)operator(,) ident(err)operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) operator(()ident(mod)operator(,) ident(err)operator(\))operator(\)) + +comment(# Use the given name for the named module.) +comment(# (You probably don't need to do this.\)) +ident(mod) operator(=) string +keyword(try)operator(:) + predefined(globals)operator(()operator(\))operator([)ident(mod)operator(]) operator(=) predefined(__import__)operator(()ident(mod)operator(\)) +keyword(except) exception(ImportError)operator(,) ident(err)operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) operator(()ident(mod)operator(,) ident(err)operator(\))operator(\)) + +comment(#-----------------------------) +ident(DBs) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +keyword(for) ident(mod) keyword(in) ident(DBs)operator(.)ident(split)operator(()operator(\))operator(:) + keyword(try)operator(:) + ident(loaded_module) operator(=) predefined(__import__)operator(()ident(mod)operator(\)) + keyword(except) exception(ImportError)operator(:) + keyword(continue) + comment(# __import__ returns a reference to the top-most module) + comment(# Need to get the actual submodule requested.) + keyword(for) ident(term) keyword(in) ident(mod)operator(.)ident(split)operator(()stringoperator(\))operator([)operator(:)operator(-)integer(1)operator(])operator(:) + ident(loaded_module) operator(=) predefined(getattr)operator(()ident(loaded_module)operator(,) ident(term)operator(\)) + keyword(break) +keyword(else)operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) ident(DBs)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.3) +comment(#-----------------------------) +keyword(import) ident(sys) +keyword(if) ident(__name__) operator(==) stringoperator(:) + keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\)) operator(!=) integer(3) keyword(or) keyword(not) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(.)ident(isdigit)operator(()operator(\)) \ + keyword(or) keyword(not) ident(sys)operator(.)ident(argv)operator([)integer(2)operator(])operator(.)ident(isdigit)operator(()operator(\))operator(:) + keyword(raise) exception(SystemExit)operator(()string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) + +keyword(import) ident(Some)operator(.)ident(Module) +keyword(import) ident(More)operator(.)ident(Modules) +comment(#-----------------------------) +keyword(if) ident(opt_b)operator(:) + keyword(import) ident(math) +comment(#-----------------------------) +keyword(from) ident(os) keyword(import) ident(O_EXCL)operator(,) ident(O_CREAT)operator(,) ident(O_RDWR) + +comment(#-----------------------------) +keyword(import) ident(os) +ident(O_EXCL) operator(=) ident(os)operator(.)ident(O_EXCL) +ident(O_CREAT) operator(=) ident(os)operator(.)ident(O_CREAT) +ident(O_RDWR) operator(=) ident(os)operator(.)ident(O_RDWR) +comment(#-----------------------------) +keyword(import) ident(os) +ident(O_EXCL)operator(,) ident(O_CREAT)operator(,) ident(O_RDWR) operator(=) ident(os)operator(.)ident(O_EXCL)operator(,) ident(os)operator(.)ident(O_CREAT)operator(,) ident(os)operator(.)ident(O_RDWR) +comment(#-----------------------------) +ident(load_module)operator(()stringoperator(,) stringoperator(.)ident(split)operator(()operator(\))operator(\)) + +keyword(def) ident(load_module)operator(()ident(module_name)operator(,) ident(symbols)operator(\))operator(:) + ident(module) operator(=) predefined(__import__)operator(()ident(module_name)operator(\)) + keyword(for) ident(symbol) keyword(in) ident(symbols)operator(:) + predefined(globals)operator(()operator(\))operator([)ident(symbol)operator(]) operator(=) predefined(getattr)operator(()ident(module)operator(,) ident(symbol)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.4) +comment(#-----------------------------) +comment(# Python doesn't have Perl-style packages) + +comment(# Flipper.py) +ident(__version__) operator(=) operator(()integer(1)operator(,) integer(0)operator(\)) + +ident(__all__) operator(=) operator([)stringoperator(,) stringoperator(]) + +ident(Separatrix) operator(=) string comment(# default to blank) + +keyword(def) ident(flip_boundary)operator(()ident(sep) operator(=) pre_constant(None)operator(\))operator(:) + ident(prev_sep) operator(=) ident(Separatrix) + keyword(if) ident(sep) keyword(is) keyword(not) pre_constant(None)operator(:) + keyword(global) ident(Separatrix) + ident(Separatrix) operator(=) ident(sep) + keyword(return) ident(prev_sep) + +keyword(def) ident(flip_words)operator(()ident(line)operator(\))operator(:) + ident(words) operator(=) ident(line)operator(.)ident(split)operator(()ident(Separatrix)operator(\)) + ident(words)operator(.)ident(reverse)operator(()operator(\)) + keyword(return) ident(Separatrix)operator(.)ident(join)operator(()ident(words)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.5) +comment(#-----------------------------) +ident(this_pack) operator(=) ident(__name__) +comment(#-----------------------------) +ident(that_pack) operator(=) ident(sys)operator(.)ident(_getframe)operator(()integer(1)operator(\))operator(.)ident(f_globals)operator(.)ident(get)operator(()stringoperator(,) string)delimiter(")>operator(\)) +comment(#-----------------------------) +keyword(print) stringoperator(,) ident(__name__) +comment(#-----------------------------) +keyword(def) ident(nreadline)operator(()ident(count)operator(,) ident(myfile)operator(\))operator(:) + keyword(if) ident(count) operator(<=) integer(0)operator(:) + keyword(raise) exception(ValueError)operator(()string 0)delimiter(")>operator(\)) + keyword(return) operator([)ident(myfile)operator(.)ident(readline)operator(()operator(\)) keyword(for) ident(i) keyword(in) predefined(range)operator(()ident(count)operator(\))operator(]) + +keyword(def) ident(main)operator(()operator(\))operator(:) + ident(myfile) operator(=) predefined(open)operator(()stringoperator(\)) + ident(a)operator(,) ident(b)operator(,) ident(c) operator(=) ident(nreadline)operator(()integer(3)operator(,) ident(myfile)operator(\)) + ident(myfile)operator(.)ident(close)operator(()operator(\)) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(main)operator(()operator(\)) + +comment(# DON'T DO THIS:) +keyword(import) ident(sys) + +keyword(def) ident(nreadline)operator(()ident(count)operator(,) ident(handle_name)operator(\))operator(:) + keyword(assert) ident(count) operator(>) integer(0)operator(,) string 0)delimiter(")> + predefined(locals) operator(=) ident(sys)operator(.)ident(_getframe)operator(()integer(1)operator(\))operator(.)ident(f_locals) + keyword(if) keyword(not) predefined(locals)operator(.)ident(has_key)operator(()ident(handle_name)operator(\))operator(:) + keyword(raise) exception(AssertionError)operator(()stringoperator(\)) + ident(infile) operator(=) predefined(locals)operator([)ident(handle_name)operator(]) + ident(retlist) operator(=) operator([)operator(]) + keyword(for) ident(line) keyword(in) ident(infile)operator(:) + ident(retlist)operator(.)ident(append)operator(()ident(line)operator(\)) + ident(count) operator(-=) integer(1) + keyword(if) ident(count) operator(==) integer(0)operator(:) + keyword(break) + keyword(return) ident(retlist) + +keyword(def) ident(main)operator(()operator(\))operator(:) + ident(FH) operator(=) predefined(open)operator(()stringoperator(\)) + ident(a)operator(,) ident(b)operator(,) ident(c) operator(=) ident(nreadline)operator(()integer(3)operator(,) stringoperator(\)) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(main)operator(()operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.6) +comment(#-----------------------------) +comment(## There is no direct equivalent in Python to an END block) +keyword(import) ident(time)operator(,) ident(os)operator(,) ident(sys) + +comment(# Tricks to ensure the needed functions exist during module cleanup) +keyword(def) ident(_getgmtime)operator(()ident(asctime)operator(=)ident(time)operator(.)ident(asctime)operator(,) ident(gmtime)operator(=)ident(time)operator(.)ident(gmtime)operator(,) + ident(t)operator(=)ident(time)operator(.)ident(time)operator(\))operator(:) + keyword(return) ident(asctime)operator(()ident(gmtime)operator(()ident(t)operator(()operator(\))operator(\))operator(\)) + +keyword(class) ident(Logfile)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) predefined(file)operator(\))operator(:) + pre_constant(self)operator(.)ident(file) operator(=) predefined(file) + + keyword(def) ident(_logmsg)operator(()pre_constant(self)operator(,) ident(msg)operator(,) ident(argv0)operator(=)ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(,) ident(pid)operator(=)ident(os)operator(.)ident(getpid)operator(()operator(\))operator(,) + ident(_getgmtime)operator(=)ident(_getgmtime)operator(\))operator(:) + comment(# more tricks to keep all needed references) + ident(now) operator(=) ident(_getgmtime)operator(()operator(\)) + keyword(print)operator(>>)pre_constant(self)operator(.)ident(file)operator(,) ident(argv0)operator(,) ident(pid)operator(,) ident(now) operator(+) stringoperator(,) ident(msg) + + keyword(def) ident(logmsg)operator(()pre_constant(self)operator(,) ident(msg)operator(\))operator(:) + pre_constant(self)operator(.)ident(_logmsg)operator(()pre_constant(self)operator(.)ident(file)operator(,) ident(msg)operator(\)) + + keyword(def) ident(__del__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(_logmsg)operator(()stringoperator(\)) + pre_constant(self)operator(.)ident(file)operator(.)ident(close)operator(()operator(\)) + + keyword(def) ident(__getattr__)operator(()pre_constant(self)operator(,) ident(attr)operator(\))operator(:) + comment(# forward everything else to the file handle) + keyword(return) predefined(getattr)operator(()pre_constant(self)operator(.)ident(file)operator(,) ident(attr)operator(\)) + +comment(# 0 means unbuffered) +ident(LF) operator(=) ident(Logfile)operator(()predefined(open)operator(()stringoperator(,) stringoperator(,) integer(0)operator(\))operator(\)) +ident(logmsg) operator(=) ident(LF)operator(.)ident(logmsg) + +comment(#-----------------------------) +comment(## It is more appropriate to use try/finally around the) +comment(## main code, so the order of initialization and finalization) +comment(## can be specified.) +keyword(if) ident(__name__) operator(==) stringoperator(:) + keyword(import) ident(logger) + ident(logger)operator(.)ident(init)operator(()stringoperator(\)) + keyword(try)operator(:) + ident(main)operator(()operator(\)) + keyword(finally)operator(:) + ident(logger)operator(.)ident(close)operator(()operator(\)) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.7) +comment(#-----------------------------) +comment(#% python -c 'import sys\\) +keyword(for) ident(i)operator(,) ident(name) keyword(in) predefined(zip)operator(()predefined(xrange)operator(()ident(sys)operator(.)ident(maxint)operator(\))operator(,) ident(sys)operator(.)ident(path)operator(\))operator(:)\ + keyword(print) ident(i)operator(,) predefined(repr)operator(()ident(name)operator(\)) +comment(#> 0 '') +comment(#> 1 '/usr/lib/python2.2') +comment(#> 2 '/usr/lib/python2.2/plat-linux2') +comment(#> 3 '/usr/lib/python2.2/lib-tk') +comment(#-----------------------------) +comment(# syntax for sh, bash, ksh, or zsh) +comment(#$ export PYTHONPATH=$HOME/pythonlib) + +comment(# syntax for csh or tcsh) +comment(#% setenv PYTHONPATH ~/pythonlib) +comment(#-----------------------------) +keyword(import) ident(sys) +ident(sys)operator(.)ident(path)operator(.)ident(insert)operator(()integer(0)operator(,) stringoperator(\)) +comment(#-----------------------------) +keyword(import) ident(FindBin) +ident(sys)operator(.)ident(path)operator(.)ident(insert)operator(()integer(0)operator(,) ident(FindBin)operator(.)ident(Bin)operator(\)) +comment(#-----------------------------) +keyword(import) ident(FindBin) +ident(Bin) operator(=) string +predefined(bin) operator(=) predefined(getattr)operator(()ident(FindBin)operator(,) ident(Bin)operator(\)) +ident(sys)operator(.)ident(path)operator(.)ident(insert)operator(()integer(0)operator(,) predefined(bin) operator(+) stringoperator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.8) +comment(#-----------------------------) +comment(#% h2xs -XA -n Planets) +comment(#% h2xs -XA -n Astronomy::Orbits) +comment(#-----------------------------) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) +comment(# Need a distutils example) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.9) +comment(#-----------------------------) +comment(# Python compiles a file to bytecode the first time it is imported and ) +comment(# stores this compiled form in a .pyc file. There is thus less need for) +comment(# incremental compilation as once there is a .pyc file, the sourcecode) +comment(# is only recompiled if it is modified. ) + +comment(# ^^PLEAC^^_12.10) +comment(#-----------------------------) +comment(# See previous section) + +comment(# ^^PLEAC^^_12.11) +comment(#-----------------------------) +comment(## Any definition in a Python module overrides the builtin) +comment(## for that module) + +comment(#=== In MyModule) +keyword(def) predefined(open)operator(()operator(\))operator(:) + keyword(pass) comment(# TBA) +comment(#-----------------------------) +keyword(from) ident(MyModule) keyword(import) predefined(open) +predefined(file) operator(=) predefined(open)operator(()operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.12) +comment(#-----------------------------) +keyword(def) ident(even_only)operator(()ident(n)operator(\))operator(:) + keyword(if) ident(n) operator(&) integer(1)operator(:) comment(# one way to test) + keyword(raise) exception(AssertionError)operator(()string operator(%) operator(()ident(n)operator(,)operator(\))operator(\)) + comment(#....) + +comment(#-----------------------------) +keyword(def) ident(even_only)operator(()ident(n)operator(\))operator(:) + keyword(if) ident(n) operator(%) integer(2)operator(:) comment(# here's another) + comment(# choice of exception depends on the problem) + keyword(raise) exception(TypeError)operator(()string operator(%) operator(()ident(n)operator(,)operator(\))operator(\)) + comment(#....) + +comment(#-----------------------------) +keyword(import) ident(warnings) +keyword(def) ident(even_only)operator(()ident(n)operator(\))operator(:) + keyword(if) ident(n) operator(&) integer(1)operator(:) comment(# test whether odd number) + ident(warnings)operator(.)ident(warn)operator(()string operator(%) operator(()ident(n)operator(\))operator(\)) + ident(n) operator(+=) integer(1) + comment(#....) +comment(#-----------------------------) +ident(warnings)operator(.)ident(filterwarnings)operator(()stringoperator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.13) +comment(#-----------------------------) +ident(val) operator(=) predefined(getattr)operator(()predefined(__import__)operator(()ident(packname)operator(\))operator(,) ident(varname)operator(\)) +ident(vals) operator(=) predefined(getattr)operator(()predefined(__import__)operator(()ident(packname)operator(\))operator(,) ident(aryname)operator(\)) +predefined(getattr)operator(()predefined(__import__)operator(()ident(packname)operator(\))operator(,) ident(funcname)operator(\))operator(()stringoperator(\)) + +comment(#-----------------------------) +comment(# DON'T DO THIS [Use math.log(val, base\) instead]) +keyword(import) ident(math) +keyword(def) ident(make_log)operator(()ident(n)operator(\))operator(:) + keyword(def) ident(logn)operator(()ident(val)operator(\))operator(:) + keyword(return) ident(math)operator(.)ident(log)operator(()ident(val)operator(,) ident(n)operator(\)) + keyword(return) ident(logn) + +comment(# Modifying the global dictionary - this could also be done) +comment(# using locals(\), or someobject.__dict__) +ident(globaldict) operator(=) predefined(globals)operator(()operator(\)) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(2)operator(,) integer(1000)operator(\))operator(:) + ident(globaldict)operator([)stringoperator(%)ident(i)operator(]) operator(=) ident(make_log)operator(()ident(i)operator(\)) + +comment(# DON'T DO THIS) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(2)operator(,)integer(1000)operator(\))operator(:) + keyword(exec) stringoperator(%)ident(i) keyword(in) predefined(globals)operator(()operator(\)) + +keyword(print) ident(log20)operator(()integer(400)operator(\)) +comment(#=>2.0) +comment(#-----------------------------) +ident(blue) operator(=) ident(colours)operator(.)ident(blue) +ident(someobject)operator(.)ident(blue) operator(=) ident(colours)operator(.)ident(azure) comment(# someobject could be a module...) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.14) +comment(#-----------------------------) +comment(# Python extension modules can be imported and used just like) +comment(# a pure python module.) +comment(#) +comment(# See http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ for) +comment(# information on how to create extension modules in Pyrex [a) +comment(# language that's basically Python with type definitions which) +comment(# converts to compiled C code]) +comment(#) +comment(# See http://www.boost.org/libs/python/doc/ for information on how) +comment(# to create extension modules in C++.) +comment(#) +comment(# See http://www.swig.org/Doc1.3/Python.html for information on how) +comment(# to create extension modules in C/C++) +comment(#) +comment(# See http://docs.python.org/ext/ext.html for information on how to) +comment(# create extension modules in C/C++ (manual reference count management\).) +comment(#) +comment(# See http://cens.ioc.ee/projects/f2py2e/ for information on how to) +comment(# create extension modules in Fortran) +comment(#) +comment(# See http://www.scipy.org/Weave for information on how to ) +comment(# include inline C code in Python code.) +comment(#) +comment(# @@INCOMPLETE@@ Need examples of FineTime extensions using the different methods...) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.15) +comment(#-----------------------------) +comment(# See previous section) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.16) +comment(#-----------------------------) +comment(# To document code, use docstrings. A docstring is a bare string that) +comment(# is placed at the beginning of a module or immediately after the ) +comment(# definition line of a class, method, or function. Normally, the) +comment(# first line is a brief description of the object; if a longer) +comment(# description is needed, it commences on the third line (the second) +comment(# line being left blank\). Multiline comments should use triple) +comment(# quoted strings.) +comment(# ) +comment(# Docstrings are automagically assigned to an object's __doc__ property.) +comment(#) +comment(# In other words these three classes are identical:) +keyword(class) ident(Foo)operator(()predefined(object)operator(\))operator(:) + string + +keyword(class) ident(Foo)operator(()predefined(object)operator(\))operator(:) + ident(__doc__) operator(=) string + +keyword(class) ident(Foo)operator(()predefined(object)operator(\))operator(:) + keyword(pass) +ident(Foo)operator(.)ident(__doc__) operator(=) string + +comment(# as are these two functions:) +keyword(def) ident(foo)operator(()operator(\))operator(:) + string + +keyword(def) ident(foo)operator(()operator(\))operator(:) + keyword(pass) +ident(foo)operator(.)ident(__doc__) operator(=) string + +comment(# the pydoc module is used to display a range of information about ) +comment(# an object including its docstrings:) +keyword(import) ident(pydoc) +keyword(print) ident(pydoc)operator(.)ident(getdoc)operator(()predefined(int)operator(\)) +ident(pydoc)operator(.)ident(help)operator(()predefined(int)operator(\)) + +comment(# In the interactive interpreter, objects' documentation can be ) +comment(# using the help function:) +ident(help)operator(()predefined(int)operator(\)) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.17) +comment(#-----------------------------) +comment(# Recent Python distributions are built and installed with disutils.) +comment(# ) +comment(# To build and install under unix) +comment(# ) +comment(# % python setup.py install) +comment(# ) +comment(# If you want to build under one login and install under another) +comment(# ) +comment(# % python setup.py build) +comment(# $ python setup.py install) +comment(# ) +comment(# A package may also be available prebuilt, eg, as an RPM or Windows) +comment(# installer. Details will be specific to the operating system.) + +comment(#-----------------------------) +comment(# % python setup.py --prefix ~/python-lib) +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_12.18) +comment(#-----------------------------) +comment(#== File Some/Module.py) + +comment(# There are so many differences between Python and Perl that) +comment(# it isn't worthwhile trying to come up with an equivalent to) +comment(# this Perl code. The Python code is much smaller, and there's) +comment(# no need to have a template.) + +comment(#-----------------------------) + +comment(# ^^PLEAC^^_12.19) +comment(#-----------------------------) +comment(#% pmdesc) +comment(#-----------------------------) +keyword(import) ident(sys)operator(,) ident(pydoc) + +keyword(def) ident(print_module_info)operator(()ident(path)operator(,) ident(modname)operator(,) ident(desc)operator(\))operator(:) + comment(# Skip files starting with "test_") + keyword(if) ident(modname)operator(.)ident(split)operator(()stringoperator(\))operator([)operator(-)integer(1)operator(])operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + keyword(return) + keyword(try)operator(:) + comment(# This assumes the modules are safe for importing,) + comment(# in that they don't have side effects. Could also) + comment(# grep the file for the __version__ line.) + ident(mod) operator(=) ident(pydoc)operator(.)ident(safeimport)operator(()ident(modname)operator(\)) + keyword(except) ident(pydoc)operator(.)ident(ErrorDuringImport)operator(:) + keyword(return) + ident(version) operator(=) predefined(getattr)operator(()ident(mod)operator(,) stringoperator(,) stringoperator(\)) + keyword(if) predefined(isinstance)operator(()ident(version)operator(,) predefined(type)operator(()stringoperator(\))operator(\))operator(:) + comment(# Use the string if it's given) + keyword(pass) + keyword(else)operator(:) + comment(# Assume it's a list of version numbers, from major to minor) + stringoperator(.)ident(join)operator(()predefined(map)operator(()predefined(str)operator(,) ident(version)operator(\))operator(\)) + ident(synopsis)operator(,) ident(text) operator(=) ident(pydoc)operator(.)ident(splitdoc)operator(()ident(desc)operator(\)) + keyword(print) string operator(%) operator(()ident(modname)operator(,) ident(version)operator(,) ident(synopsis)operator(\)) + +ident(scanner) operator(=) ident(pydoc)operator(.)ident(ModuleScanner)operator(()operator(\)) +ident(scanner)operator(.)ident(run)operator(()ident(print_module_info)operator(\)) + +comment(#-----------------------------) + + +comment(# ^^PLEAC^^_13.0) +comment(#-----------------------------) +comment(# Inside a module named 'Data' / file named 'Data.py') +keyword(class) ident(Encoder)operator(()predefined(object)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +ident(obj) operator(=) operator([)integer(3)operator(,) integer(5)operator(]) +keyword(print) predefined(type)operator(()ident(obj)operator(\))operator(,) predefined(id)operator(()ident(obj)operator(\))operator(,) ident(ob)operator([)integer(1)operator(]) + +comment(## Changing the class of builtin types is not supported) +comment(## in Python.) + +comment(#-----------------------------) +ident(obj)operator(.)ident(Stomach) operator(=) string comment(# directly accessing an object's contents) +ident(obj)operator(.)ident(NAME) operator(=) string comment(# uppercase field name to make it stand out) +operator(()ident(optional)operator(\)) +comment(#-----------------------------) +ident(encoded) operator(=) predefined(object)operator(.)ident(encode)operator(()stringoperator(\)) +comment(#-----------------------------) +ident(encoded) operator(=) ident(Data)operator(.)ident(Encoder)operator(.)ident(encode)operator(()stringoperator(\)) +comment(#-----------------------------) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +predefined(object) operator(=) ident(Class)operator(()operator(\)) +comment(#-----------------------------) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(class_only_method)operator(()operator(\))operator(:) + keyword(pass) comment(# more code here) + ident(class_only_method) operator(=) predefined(staticmethod)operator(()ident(class_only_method)operator(\)) + +comment(#-----------------------------) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(instance_only_method)operator(()pre_constant(self)operator(\))operator(:) + keyword(pass) comment(# more code here) +comment(#-----------------------------) +ident(lector) operator(=) ident(Human)operator(.)ident(Cannibal)operator(()operator(\)) +ident(lector)operator(.)ident(feed)operator(()stringoperator(\)) +ident(lector)operator(.)ident(move)operator(()stringoperator(\)) +comment(#-----------------------------) +comment(# NOTE: it is rare to use these forms except inside of) +comment(# methods to call specific methods from a parent class) +ident(lector) operator(=) ident(Human)operator(.)ident(Cannibal)operator(()operator(\)) +ident(Human)operator(.)ident(Cannibal)operator(.)ident(feed)operator(()ident(lector)operator(,) stringoperator(\)) +ident(Human)operator(.)ident(Cannibal)operator(.)ident(move)operator(()ident(lector)operator(,) stringoperator(\)) +comment(#-----------------------------) +keyword(print)operator(>>)ident(sys)operator(.)ident(stderr)operator(,) string + +comment(# ^^PLEAC^^_13.1) +comment(#-----------------------------) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +keyword(import) ident(time) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(start) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) comment(# init data fields) + pre_constant(self)operator(.)ident(age) operator(=) integer(0) +comment(#-----------------------------) +keyword(import) ident(time) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + comment(# Sets self.start to the current time, and self.age to 0. If called) + comment(# with arguments, interpret them as key+value pairs to) + comment(# initialize the object with) + pre_constant(self)operator(.)ident(age) operator(=) integer(0) + pre_constant(self)operator(.)ident(__dict__)operator(.)ident(update)operator(()ident(kwargs)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.2) +comment(#-----------------------------) +keyword(import) ident(time) +keyword(class) ident(Class)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__del__)operator(()pre_constant(self)operator(\))operator(:) + keyword(print) pre_constant(self)operator(,) stringoperator(,) ident(time)operator(.)ident(ctime)operator(()operator(\)) +comment(#-----------------------------) +comment(## Why is the perl code introducing a cycle? I guess it's an) +comment(## example of how to keep from calling the finalizer) +pre_constant(self)operator(.)ident(WHATEVER) operator(=) pre_constant(self) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.3) +comment(#-----------------------------) +comment(# It is standard practice to access attributes directly:) +keyword(class) ident(MyClass)operator(()predefined(object)operator(\)) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) string + pre_constant(self)operator(.)ident(age) operator(=) integer(0) +ident(obj) operator(=) ident(MyClass)operator(()operator(\)) +ident(obj)operator(.)ident(name) operator(=) string +keyword(print) ident(obj)operator(.)ident(name) +ident(obj)operator(.)ident(age) operator(+=) integer(1) + +comment(# If you later find that you need to compute an attribute, you can always ) +comment(# retrofit a property(\), leaving user code untouched:) +keyword(class) ident(MyClass)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(_name) operator(=) string + pre_constant(self)operator(.)ident(_age) operator(=) integer(0) + + keyword(def) ident(get_name)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_name) + keyword(def) ident(set_name)operator(()pre_constant(self)operator(,) ident(name)operator(\))operator(:) + pre_constant(self)operator(.)ident(_name) operator(=) ident(name)operator(.)ident(title)operator(()operator(\)) + ident(name) operator(=) predefined(property)operator(()ident(get_name)operator(,) ident(set_name)operator(\)) + + keyword(def) ident(get_age)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_age) + keyword(def) ident(set_age)operator(()pre_constant(self)operator(,) ident(val)operator(\))operator(:) + keyword(if) ident(val) operator(<) integer(0)operator(:) + keyword(raise) exception(ValueError)operator(()string operator(%) ident(val)operator(\)) + pre_constant(self)operator(.)ident(_age) operator(=) ident(val) + ident(age) operator(=) predefined(property)operator(()ident(get_age)operator(,) ident(set_age)operator(\)) +ident(obj) operator(=) ident(MyClass)operator(()operator(\)) +ident(obj)operator(.)ident(name) operator(=) string +keyword(print) ident(obj)operator(.)ident(name) +ident(obj)operator(.)ident(age) operator(+=) integer(1) + +comment(# DON'T DO THIS - explicit getters and setters should not be used:) +keyword(class) ident(MyClass)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) string + keyword(def) ident(get_name)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(name) + keyword(def) ident(set_name)operator(()pre_constant(self)operator(,) ident(name)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) ident(name)operator(.)ident(title)operator(()operator(\)) +ident(obj) operator(=) ident(MyClass)operator(()operator(\)) +ident(obj)operator(.)ident(set_name)operator(()stringoperator(\)) +keyword(print) ident(obj)operator(.)ident(get_name)operator(()operator(\)) +comment(#-----------------------------) +comment(## DON'T DO THIS (It's complex, ugly, and unnecessary\):) +keyword(class) ident(MyClass)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(age) operator(=) integer(0) + keyword(def) ident(name)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(\))operator(:) + keyword(if) predefined(len)operator(()ident(args)operator(\)) operator(==) integer(0)operator(:) + keyword(return) pre_constant(self)operator(.)ident(name) + keyword(elif) predefined(len)operator(()ident(args)operator(\)) operator(==) integer(1)operator(:) + pre_constant(self)operator(.)ident(name) operator(=) ident(args)operator([)integer(0)operator(]) + keyword(else)operator(:) + keyword(raise) exception(TypeError)operator(()stringoperator(\)) + keyword(def) ident(age)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(\))operator(:) + ident(prev) operator(=) pre_constant(self)operator(.)ident(age) + keyword(if) ident(args)operator(:) + pre_constant(self)operator(.)ident(age) operator(=) ident(args)operator([)integer(0)operator(]) + keyword(return) ident(prev) + +comment(# sample call of get and set: happy birthday!) +ident(obj)operator(.)ident(age)operator(()integer(1) operator(+) ident(obj)operator(.)ident(age)operator(()operator(\))operator(\)) + +comment(#-----------------------------) +ident(him) operator(=) ident(Person)operator(()operator(\)) +ident(him)operator(.)ident(NAME) operator(=) string +ident(him)operator(.)ident(AGE) operator(=) integer(23) +comment(#-----------------------------) +comment(# Here's another way to implement the 'obj.method(\)' is a getter) +comment(# and 'obj.method(value\)' is a settor. Again, this is not a) +comment(# common Python idiom and should not be used. See below for a) +comment(# more common way to do parameter checking of attribute assignment.) + +keyword(import) ident(re)operator(,) ident(sys) + +keyword(def) ident(carp)operator(()ident(s)operator(\))operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()string operator(+) ident(s) operator(+) stringoperator(\)) + +keyword(class) ident(Class)operator(:) + ident(no_name) operator(=) operator([)operator(]) + + keyword(def) ident(name)operator(()pre_constant(self)operator(,) ident(value) operator(=) ident(no_name)operator(\))operator(:) + keyword(if) ident(value) keyword(is) ident(Class)operator(.)ident(no_name)operator(:) + keyword(return) pre_constant(self)operator(.)ident(NAME) + ident(value) operator(=) pre_constant(self)operator(.)ident(_enforce_name_value)operator(()ident(value)operator(\)) + pre_constant(self)operator(.)ident(NAME) operator(=) ident(value) + + keyword(def) ident(_enforce_name_value)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(if) keyword(not) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(if) keyword(not) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(return) ident(value)operator(.)ident(upper)operator(()operator(\)) comment(# enforce capitalization) +comment(#-----------------------------) +comment(# A more typical way to enforce restrictions on a value) +comment(# to set) +keyword(class) ident(Class)operator(:) + keyword(def) ident(__setattr__)operator(()pre_constant(self)operator(,) ident(name)operator(,) ident(value)operator(\))operator(:) + keyword(if) ident(name) operator(==) stringoperator(:) + ident(value) operator(=) pre_constant(self)operator(.)ident(_enforce_name_value)operator(()ident(value)operator(\)) comment(# Do any conversions) + pre_constant(self)operator(.)ident(__dict__)operator([)ident(name)operator(]) operator(=) ident(value) comment(# Do the default __setattr__ action) + + keyword(def) ident(_enforce_name_value)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(if) keyword(not) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(if) keyword(not) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(value)operator(\))operator(:) + ident(carp)operator(()stringoperator(\)) + keyword(return) ident(value)operator(.)ident(upper)operator(()operator(\)) comment(# enforce capitalization) + +comment(#-----------------------------) +keyword(class) ident(Person)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(name) operator(=) pre_constant(None)operator(,) ident(age) operator(=) pre_constant(None)operator(,) ident(peers) operator(=) pre_constant(None)operator(\))operator(:) + keyword(if) ident(peers) keyword(is) pre_constant(None)operator(:) ident(peers) operator(=) operator([)operator(]) comment(# See Python FAQ 6.25) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) + pre_constant(self)operator(.)ident(age) operator(=) ident(age) + pre_constant(self)operator(.)ident(peers) operator(=) ident(peers) + + keyword(def) ident(exclaim)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) string operator(%) \ + operator(()pre_constant(self)operator(.)ident(name)operator(,) pre_constant(self)operator(.)ident(age)operator(,) stringoperator(.)ident(join)operator(()pre_constant(self)operator(.)ident(peers)operator(\))operator(\)) + + keyword(def) ident(happy_birthday)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(age) operator(+=) integer(1) + keyword(return) pre_constant(self)operator(.)ident(age) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.4) +comment(#-----------------------------) +comment(## In the module named 'Person' ...) +keyword(def) ident(population)operator(()operator(\))operator(:) + keyword(return) ident(Person)operator(.)ident(body_count)operator([)integer(0)operator(]) + +keyword(class) ident(Person)operator(()predefined(object)operator(\))operator(:) + ident(body_count) operator(=) operator([)integer(0)operator(]) comment(# class variable - shared across all instances) + + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(body_count)operator([)integer(0)operator(]) operator(+=) integer(1) + + keyword(def) ident(__del__)operator(()pre_constant(self)operator(\))operator(:) comment(# Beware - may be non-deterministic (Jython\)!) + pre_constant(self)operator(.)ident(body_count)operator([)integer(0)operator(]) operator(-=) integer(1) + +comment(# later, the user can say this:) +keyword(import) ident(Person) +ident(people) operator(=) operator([)operator(]) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(10)operator(\))operator(:) + ident(people)operator(.)ident(append)operator(()ident(Person)operator(.)ident(Person)operator(()operator(\))operator(\)) +keyword(print) stringoperator(,) ident(Person)operator(.)ident(population)operator(()operator(\))operator(,) string + +comment(#=> There are 10 people alive.) +comment(#-----------------------------) +ident(him) operator(=) ident(Person)operator(()operator(\)) +ident(him)operator(.)ident(gender) operator(=) string + +ident(her) operator(=) ident(Person)operator(()operator(\)) +ident(her)operator(.)ident(gender) operator(=) string + +comment(#-----------------------------) +ident(FixedArray)operator(.)ident(max_bounds) operator(=) integer(100) comment(# set for whole class) +ident(alpha) operator(=) ident(FixedArray)operator(.)ident(FixedArray)operator(()operator(\)) +keyword(print) stringoperator(,) ident(alpha)operator(.)ident(max_bounds) +comment(#=>100) + +ident(beta) operator(=) ident(FixedArray)operator(.)ident(FixedArray)operator(()operator(\)) +ident(beta)operator(.)ident(max_bounds) operator(=) integer(50) comment(# still sets for whole class) +keyword(print) stringoperator(,) ident(alpha)operator(.)ident(max_bounds) +comment(#=>50) +comment(#-----------------------------) +comment(# In the module named 'FixedArray') + +keyword(class) ident(FixedArray)operator(()predefined(object)operator(\))operator(:) + ident(_max_bounds) operator(=) operator([)integer(7)operator(]) comment(# Shared across whole class) + + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(bounds)operator(=)pre_constant(None)operator(\))operator(:) + keyword(if) ident(bounds) keyword(is) keyword(not) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(max_bounds) operator(=) ident(bounds) + + keyword(def) ident(get_max_bounds)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_max_bounds)operator([)integer(0)operator(]) + keyword(def) ident(set_max_bounds)operator(()pre_constant(self)operator(,) ident(val)operator(\))operator(:) + pre_constant(self)operator(.)ident(_max_bounds)operator([)integer(0)operator(]) operator(=) ident(val) + ident(max_bounds) operator(=) predefined(property)operator(()ident(get_max_bounds)operator(,) ident(set_max_bounds)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.5) +comment(#-----------------------------) +comment(# There isn't the severe separation between scalar, arrays and hashs) +comment(# in Python, so there isn't a direct equivalent to the Perl code.) +keyword(class) ident(Person)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(name)operator(=)pre_constant(None)operator(,) ident(age)operator(=)pre_constant(None)operator(,) ident(peers)operator(=)pre_constant(None)operator(\))operator(:) + keyword(if) ident(peers) keyword(is) pre_constant(None)operator(:) + ident(peers) operator(=) operator([)operator(]) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) + pre_constant(self)operator(.)ident(age) operator(=) ident(age) + pre_constant(self)operator(.)ident(peers) operator(=) ident(peers) + +ident(p) operator(=) ident(Person)operator(()stringoperator(,) integer(13)operator(,) operator([)stringoperator(,) stringoperator(,) stringoperator(])operator(\)) + +comment(# or this way. (This is not the prefered style as objects should) +comment(# be constructed with all the appropriate data, if possible.\)) + +ident(p) operator(=) ident(Person)operator(()operator(\)) comment(# allocate an empty Person) +ident(p)operator(.)ident(name) operator(=) string comment(# set its name field) +ident(p)operator(.)ident(age) operator(=) integer(13) comment(# set its age field) +ident(p)operator(.)ident(peers)operator(.)ident(extend)operator(() operator([)stringoperator(,) stringoperator(,) string operator(]) operator(\)) comment(# set its peers field) + +ident(p)operator(.)ident(peers) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) + +ident(p)operator(.)ident(peers)operator([)operator(:)operator(])operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(]) + +comment(# fetch various values, including the zeroth friend) +keyword(print) string operator(%) \ + operator(()ident(p)operator(.)ident(age)operator(,) ident(p)operator(.)ident(name)operator(,) ident(p)operator(.)ident(peers)operator([)integer(0)operator(])operator(\)) +comment(#-----------------------------) +comment(# This isn't very Pythonic - should create objects with the) +comment(# needed data, and not depend on defaults and modifing the object.) +keyword(import) ident(sys) +keyword(def) ident(carp)operator(()ident(s)operator(\))operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()string operator(+) ident(s) operator(+) stringoperator(\)) + +keyword(class) ident(Person)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(name) operator(=) stringoperator(,) ident(age) operator(=) integer(0)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) + pre_constant(self)operator(.)ident(age) operator(=) ident(age) + keyword(def) ident(__setattr__)operator(()pre_constant(self)operator(,) ident(name)operator(,) ident(value)operator(\))operator(:) + keyword(if) ident(name) operator(==) stringoperator(:) + comment(# This is very unpythonic) + keyword(if) keyword(not) predefined(isinstance)operator(()ident(value)operator(,) predefined(type)operator(()integer(0)operator(\))operator(\))operator(:) + ident(carp)operator(()string operator(%) operator(()ident(value)operator(,)operator(\))operator(\)) + keyword(if) ident(value) operator(>) integer(150)operator(:) ident(carp)operator(()string operator(%) operator(()ident(value)operator(,)operator(\))operator(\)) + pre_constant(self)operator(.)ident(__dict__)operator([)ident(name)operator(]) operator(=) ident(value) + +keyword(class) ident(Family)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(head) operator(=) pre_constant(None)operator(,) ident(address) operator(=) stringoperator(,) ident(members) operator(=) pre_constant(None)operator(\))operator(:) + keyword(if) ident(members) keyword(is) pre_constant(None)operator(:) ident(members) operator(=) operator([)operator(]) + pre_constant(self)operator(.)ident(head) operator(=) ident(head) keyword(or) ident(Person)operator(()operator(\)) + pre_constant(self)operator(.)ident(address) operator(=) ident(address) + pre_constant(self)operator(.)ident(members) operator(=) ident(members) + +ident(folks) operator(=) ident(Family)operator(()operator(\)) + +ident(dad) operator(=) ident(folks)operator(.)ident(head) +ident(dad)operator(.)ident(name) operator(=) string +ident(dad)operator(.)ident(age) operator(=) integer(34) + +keyword(print) string operator(%) operator(()ident(folks)operator(.)ident(head)operator(.)ident(name)operator(,) ident(folks)operator(.)ident(head)operator(.)ident(age)operator(\)) +comment(#-----------------------------) +keyword(class) ident(Card)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(name)operator(=)pre_constant(None)operator(,) ident(color)operator(=)pre_constant(None)operator(,) ident(cost)operator(=)pre_constant(None)operator(,) + predefined(type)operator(=)pre_constant(None)operator(,) ident(release)operator(=)pre_constant(None)operator(,) ident(text)operator(=)pre_constant(None)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) + pre_constant(self)operator(.)ident(color) operator(=) ident(color) + pre_constant(self)operator(.)ident(cost) operator(=) ident(cost) + pre_constant(self)operator(.)ident(type) operator(=) predefined(type) + pre_constant(self)operator(.)ident(release) operator(=) ident(release) + pre_constant(self)operator(.)ident(type) operator(=) predefined(type) +comment(#-----------------------------) +comment(# For positional args) +keyword(class) ident(Card)operator(:) + ident(_names) operator(=) operator(()stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(\)) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(\))operator(:) + keyword(assert) predefined(len)operator(()ident(args)operator(\)) operator(<=) predefined(len)operator(()pre_constant(self)operator(.)ident(_names)operator(\)) + keyword(for) ident(k)operator(,) ident(v) keyword(in) predefined(zip)operator(()pre_constant(self)operator(.)ident(_names)operator(,) ident(args)operator(\))operator(:) + predefined(setattr)operator(()pre_constant(self)operator(,) ident(k)operator(,) pre_constant(None)operator(\)) +comment(#-----------------------------) +comment(# For keyword args) +keyword(class) ident(Card)operator(:) + ident(_names) operator(=) operator(()stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(\)) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + keyword(for) ident(k) keyword(in) pre_constant(self)operator(.)ident(_names)operator(:) comment(# Set the defaults) + predefined(setattr)operator(()pre_constant(self)operator(,) ident(k)operator(,) pre_constant(None)operator(\)) + keyword(for) ident(k)operator(,) ident(v) keyword(in) ident(kwargs)operator(.)ident(items)operator(()operator(\))operator(:) comment(# add in the kwargs) + keyword(assert) ident(k) keyword(in) pre_constant(self)operator(.)ident(_names)operator(,) string operator(+) ident(k) + predefined(setattr)operator(()pre_constant(self)operator(,) ident(k)operator(,) ident(v)operator(\)) +comment(#-----------------------------) +keyword(class) ident(hostent)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(addr_list) operator(=) pre_constant(None)operator(,) ident(length) operator(=) pre_constant(None)operator(,) + ident(addrtype) operator(=) pre_constant(None)operator(,) ident(aliases) operator(=) pre_constant(None)operator(,) ident(name) operator(=) pre_constant(None)operator(\))operator(:) + pre_constant(self)operator(.)ident(addr_list) operator(=) ident(addr_list) keyword(or) operator([)operator(]) + pre_constant(self)operator(.)ident(length) operator(=) ident(length) keyword(or) integer(0) + pre_constant(self)operator(.)ident(addrtype) operator(=) ident(addrtype) keyword(or) string + pre_constant(self)operator(.)ident(aliases) operator(=) ident(aliases) keyword(or) operator([)operator(]) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) keyword(or) string +comment(#-----------------------------) +comment(## XXX What do I do with these?) +comment(#define h_type h_addrtype) +comment(#define h_addr h_addr_list[0]) +comment(#-----------------------------) +comment(# make (hostent object\)->type(\) same as (hostent object\)->addrtype(\)) +comment(#) +comment(# *hostent::type = \\&hostent::addrtype;) +comment(#) +comment(# # make (hostenv object\)->) +comment(# addr(\)) +comment(# same as (hostenv object\)->addr_list(0\)) +comment(#sub hostent::addr { shift->addr_list(0,@_\) }) +comment(#-----------------------------) +comment(# No equivalent to Net::hostent (Python uses an unnamed tuple\)) +comment(#package Extra::hostent;) +comment(#use Net::hostent;) +comment(#@ISA = qw(hostent\);) +comment(#sub addr { shift->addr_list(0,@_\) }) +comment(#1;) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.6) +comment(#-----------------------------) +keyword(class) ident(Class)operator(()ident(Parent)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +comment(## Note: this is unusual in Python code) +ident(ob1) operator(=) ident(SomeClass)operator(()operator(\)) +comment(# later on) +ident(ob2) operator(=) ident(ob1)operator(.)ident(__class__)operator(()operator(\)) +comment(#-----------------------------) +comment(## Note: this is unusual in Python code) +ident(ob1) operator(=) ident(Widget)operator(()operator(\)) +ident(ob2) operator(=) ident(ob1)operator(.)ident(__class__)operator(()operator(\)) +comment(#-----------------------------) +comment(# XXX I do not know the intent of the original Perl code) +comment(# Do not use this style of programming in Python.) +keyword(import) ident(time) +keyword(class) ident(Person)operator(()ident(possible)operator(,)ident(base)operator(,)ident(classes)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + comment(# Call the parents' constructors, if there are any) + keyword(for) ident(baseclass) keyword(in) pre_constant(self)operator(.)ident(__class__)operator(.)ident(__bases__)operator(:) + ident(init) operator(=) predefined(getattr)operator(()ident(baseclass)operator(,) stringoperator(\)) + keyword(if) ident(init) keyword(is) keyword(not) pre_constant(None)operator(:) + ident(init)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\)) + pre_constant(self)operator(.)ident(PARENT) operator(=) ident(parent) comment(# init data fields) + pre_constant(self)operator(.)ident(START) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) + pre_constant(self)operator(.)ident(AGE) operator(=) integer(0) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.7) +comment(#-----------------------------) +ident(methname) operator(=) string +predefined(getattr)operator(()ident(obj)operator(,) ident(methname)operator(\))operator(()integer(10)operator(\)) comment(# calls obj->flicker(10\);) + +comment(# call three methods on the object, by name) +keyword(for) ident(m) keyword(in) operator(()stringoperator(,) stringoperator(,) stringoperator(\))operator(:) + predefined(getattr)operator(()ident(obj)operator(,) ident(m)operator(\))operator(()operator(\)) +comment(#-----------------------------) +ident(methods) operator(=) operator(()stringoperator(,) stringoperator(,) stringoperator(\)) +ident(his_info) operator(=) operator({)operator(}) +keyword(for) ident(m) keyword(in) ident(methods)operator(:) + ident(his_info)operator([)ident(m)operator(]) operator(=) predefined(getattr)operator(()ident(ob)operator(,) ident(m)operator(\))operator(()operator(\)) + +comment(# same as this:) + +ident(his_info) operator(=) operator({) + stringoperator(:) ident(ob)operator(.)ident(name)operator(()operator(\))operator(,) + stringoperator(:) ident(ob)operator(.)ident(rank)operator(()operator(\))operator(,) + stringoperator(:) ident(ob)operator(.)ident(serno)operator(()operator(\))operator(,) +operator(}) +comment(#-----------------------------) +ident(fnref) operator(=) ident(ob)operator(.)ident(method) +comment(#-----------------------------) +ident(fnref)operator(()integer(10)operator(,) stringoperator(\)) +comment(#-----------------------------) +ident(obj)operator(.)ident(method)operator(()integer(10)operator(,) stringoperator(\)) +comment(#-----------------------------) +comment(# XXX Not sure if this is the correct translation.) +comment(# XXX Is 'can' special?) +keyword(if) predefined(isinstance)operator(()ident(obj_target)operator(,) ident(obj)operator(.)ident(__class__)operator(\))operator(:) + ident(obj)operator(.)ident(can)operator(()stringoperator(\))operator(()ident(obj_target)operator(,) operator(*)ident(arguments)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.8) +comment(#-----------------------------) +predefined(isinstance)operator(()ident(obj)operator(,) ident(mimetools)operator(.)ident(Message)operator(\)) +predefined(issubclass)operator(()ident(obj)operator(.)ident(__class__)operator(,) ident(mimetools)operator(.)ident(Message)operator(\)) + +keyword(if) predefined(hasattr)operator(()ident(obj)operator(,) stringoperator(\))operator(:) comment(# check method validity) + keyword(pass) +comment(#-----------------------------) +comment(## Explicit type checking is needed fewer times than you think.) +ident(his_print_method) operator(=) predefined(getattr)operator(()ident(obj)operator(,) stringoperator(,) pre_constant(None)operator(\)) +comment(#-----------------------------) +ident(__version__) operator(=) operator(()integer(3)operator(,) integer(0)operator(\)) +ident(Some_Module)operator(.)ident(__version__) + +comment(# Almost never used, and doesn't work for builtin types, which don't) +comment(# have a __module__.) + +ident(his_vers) operator(=) ident(obj)operator(.)ident(__module__)operator(.)ident(__version__) +comment(#-----------------------------) +keyword(if) ident(Some_Module)operator(.)ident(__version__) operator(<) operator(()integer(3)operator(,) integer(0)operator(\))operator(:) + keyword(raise) exception(ImportError)operator(()string operator(%) + operator(()ident(Some_Module)operator(.)ident(__version__)operator(,)operator(\))operator(\)) +comment(# or more simply) +keyword(assert) ident(Some_Module)operator(.)ident(__version__) operator(>=) operator(()integer(3)operator(,) integer(0)operator(\))operator(,) string + +comment(#-----------------------------) +ident(__VERSION__) operator(=) string +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.9) +comment(#-----------------------------) +comment(# Note: This uses the standard Python idiom of accessing the) +comment(# attributes directly rather than going through a method call.) +comment(# See earlier in this chapter for examples of how this does) +comment(# not break encapsulation.) +keyword(class) ident(Person)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(name) operator(=) stringoperator(,) ident(age) operator(=) integer(0)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) + pre_constant(self)operator(.)ident(age) operator(=) ident(age) +comment(#-----------------------------) +comment(# Prefered: dude = Person("Jason", 23\)) +ident(dude) operator(=) ident(Person)operator(()operator(\)) +ident(dude)operator(.)ident(name) operator(=) string +ident(dude)operator(.)ident(age) operator(=) integer(23) +keyword(print) string operator(%) operator(()ident(dude)operator(.)ident(name)operator(,) ident(dude)operator(.)ident(age)operator(\)) +comment(#-----------------------------) +keyword(class) ident(Employee)operator(()ident(Person)operator(\))operator(:) + keyword(pass) +comment(#-----------------------------) +comment(# Prefered: empl = Employee("Jason", 23\)) +ident(emp) operator(=) ident(Employee)operator(()operator(\)) +ident(empl)operator(.)ident(name) operator(=) string +ident(empl)operator(.)ident(age) operator(=) integer(23) +keyword(print) string operator(%) operator(()ident(empl)operator(.)ident(name)operator(,) ident(empl)operator(.)ident(age)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.10) +comment(#-----------------------------) +comment(# This doesn't need to be done since if 'method' doesn't) +comment(# exist in the Class it will be looked for in its BaseClass(es\)) +keyword(class) ident(Class)operator(()ident(BaseClass)operator(\))operator(:) + keyword(def) ident(method)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + ident(BaseClass)operator(.)ident(method)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\)) + +comment(# This lets you pick the specific method in one of the base classes) +keyword(class) ident(Class)operator(()ident(BaseClass1)operator(,) ident(BaseClass2)operator(\))operator(:) + keyword(def) ident(method)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + ident(BaseClass2)operator(.)ident(method)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\)) + +comment(# This looks for the first method in the base class(es\) without) +comment(# specifically knowing which base class. This reimplements) +comment(# the default action so isn't really needed.) +keyword(class) ident(Class)operator(()ident(BaseClass1)operator(,) ident(BaseClass2)operator(,) ident(BaseClass3)operator(\))operator(:) + keyword(def) ident(method)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + keyword(for) ident(baseclass) keyword(in) pre_constant(self)operator(.)ident(__class__)operator(.)ident(__bases__)operator(:) + ident(f) operator(=) predefined(getattr)operator(()ident(baseclass)operator(,) stringoperator(\)) + keyword(if) ident(f) keyword(is) keyword(not) pre_constant(None)operator(:) + keyword(return) ident(f)operator(()operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\)) + keyword(raise) exception(NotImplementedError)operator(()stringoperator(\)) + +comment(#-----------------------------) +pre_constant(self)operator(.)ident(meth)operator(()operator(\)) comment(# Call wherever first meth is found) + +ident(Where)operator(.)ident(meth)operator(()pre_constant(self)operator(\)) comment(# Call in the base class "Where") + +comment(# XXX Does Perl only have single inheritence? Or does) +comment(# it check all base classes? No directly equivalent way) +comment(# to do this in Python, but see above.) +comment(#-----------------------------) +keyword(import) ident(time) + +comment(# The Perl code calls a private '_init' function, but in) +comment(# Python there's need for the complexity of 'new' mechanism) +comment(# so it's best just to put the '_init' code in '__init__'.) +keyword(class) ident(Class)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(\))operator(:) + comment(# init data fields) + pre_constant(self)operator(.)ident(START) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) + pre_constant(self)operator(.)ident(AGE) operator(=) integer(0) + pre_constant(self)operator(.)ident(EXTRA) operator(=) ident(args) comment(# anything extra) +comment(#-----------------------------) +ident(obj) operator(=) ident(Widget)operator(()ident(haircolor) operator(=) stringoperator(,) ident(freckles) operator(=) integer(121)operator(\)) +comment(#-----------------------------) +keyword(class) ident(Class)operator(()ident(Base1)operator(,) ident(Base2)operator(,) ident(Base3)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\))operator(:) + keyword(for) ident(base) keyword(in) pre_constant(self)operator(.)ident(__class__)operator(.)ident(__bases__)operator(:) + ident(f) operator(=) predefined(getattr)operator(()ident(base)operator(,) stringoperator(\)) + keyword(if) ident(f) keyword(is) keyword(not) pre_constant(None)operator(:) + ident(f)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwargs)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.11) +comment(#-----------------------------) +comment(# NOTE: Python prefers direct attribute lookup rather than) +comment(# method calls. Python 2.2 will introduce a 'get_set' which) +comment(# *may* be equivalent, but I don't know enough about it. So) +comment(# instead I'll describe a class that lets you restrict access) +comment(# to only specific attributes.) + +keyword(class) ident(Private)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(names)operator(\))operator(:) + pre_constant(self)operator(.)ident(__names) operator(=) ident(names) + pre_constant(self)operator(.)ident(__data) operator(=) operator({)operator(}) + keyword(def) ident(__getattr__)operator(()pre_constant(self)operator(,) ident(name)operator(\))operator(:) + keyword(if) ident(name) keyword(in) pre_constant(self)operator(.)ident(__names)operator(:) + keyword(return) pre_constant(self)operator(.)ident(__data)operator([)ident(name)operator(]) + keyword(raise) exception(AttributeError)operator(()ident(name)operator(\)) + keyword(def) ident(__setattr__)operator(()pre_constant(self)operator(,) ident(name)operator(,) ident(value)operator(\))operator(:) + keyword(if) ident(name)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + pre_constant(self)operator(.)ident(__dict__)operator([)ident(name)operator(]) operator(=) ident(value) + keyword(return) + keyword(if) ident(name) keyword(in) pre_constant(self)operator(.)ident(__names)operator(:) + pre_constant(self)operator(.)ident(__data)operator([)ident(name)operator(]) operator(=) ident(value) + keyword(return) + keyword(raise) exception(TypeError)operator(()string operator(%) operator(()ident(name)operator(,)operator(\))operator(\)) + +keyword(class) ident(Person)operator(()ident(Private)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(parent) operator(=) pre_constant(None)operator(\))operator(:) + ident(Private)operator(.)ident(__init__)operator(()pre_constant(self)operator(,) operator([)stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(])operator(\)) + pre_constant(self)operator(.)ident(parent) operator(=) ident(parent) + keyword(def) ident(new_child)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) ident(Person)operator(()pre_constant(self)operator(\)) +comment(#-----------------------------) +ident(dad) operator(=) ident(Person)operator(()operator(\)) +ident(dad)operator(.)ident(name) operator(=) string +ident(dad)operator(.)ident(age) operator(=) integer(23) +ident(kid) operator(=) ident(dad)operator(.)ident(new_child)operator(()operator(\)) +ident(kid)operator(.)ident(name) operator(=) string +ident(kid)operator(.)ident(age) operator(=) integer(2) +keyword(print) stringoperator(,) ident(kid)operator(.)ident(parent)operator(.)ident(name) +comment(#=>Kid's parent is Jason) + +comment(# ^^PLEAC^^_13.12) +comment(#-----------------------------) +comment(## XXX No clue on what this does. For that matter, what's) +comment(## "The Data Inheritance Problem"?) + +comment(# ^^PLEAC^^_13.13) +comment(#-----------------------------) +ident(node)operator(.)ident(NEXT) operator(=) ident(node) +comment(#-----------------------------) +comment(# This is not a faithful copy of the Perl code, but it does) +comment(# show how to have the container's __del__ remove cycles in) +comment(# its contents. Note that Python 2.0 includes a garbage) +comment(# collector that is able to remove these sorts of cycles, but) +comment(# it's still best to prevent cycles in your code.) +keyword(class) ident(Node)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(value) operator(=) pre_constant(None)operator(\))operator(:) + pre_constant(self)operator(.)ident(next) operator(=) pre_constant(self) + pre_constant(self)operator(.)ident(prev) operator(=) pre_constant(self) + pre_constant(self)operator(.)ident(value) operator(=) ident(value) + +keyword(class) ident(Ring)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(ring) operator(=) pre_constant(None) + pre_constant(self)operator(.)ident(count) operator(=) integer(0) + + keyword(def) ident(__str__)operator(()pre_constant(self)operator(\))operator(:) + comment(# Helpful when debugging, to print the contents of the ring) + ident(s) operator(=) string operator(%) pre_constant(self)operator(.)ident(count) + ident(x) operator(=) pre_constant(self)operator(.)ident(ring) + keyword(if) ident(x) keyword(is) pre_constant(None)operator(:) + keyword(return) ident(s) + ident(values) operator(=) operator([)operator(]) + keyword(while) pre_constant(True)operator(:) + ident(values)operator(.)ident(append)operator(()ident(x)operator(.)ident(value)operator(\)) + ident(x) operator(=) ident(x)operator(.)ident(next) + keyword(if) ident(x) keyword(is) pre_constant(self)operator(.)ident(ring)operator(:) + keyword(break) + keyword(return) ident(s) operator(+) string )delimiter(")>operator(.)ident(join)operator(()predefined(map)operator(()predefined(str)operator(,) ident(values)operator(\))operator(\)) operator(+) string)delimiter(")> + + keyword(def) ident(search)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + ident(node) operator(=) pre_constant(self)operator(.)ident(ring) + keyword(while) pre_constant(True)operator(:) + keyword(if) ident(node)operator(.)ident(value) operator(==) ident(value)operator(:) + keyword(return) ident(node) + ident(node) operator(=) ident(node)operator(.)ident(next) + keyword(if) ident(node) keyword(is) pre_constant(self)operator(.)ident(ring)operator(:) + keyword(break) + + keyword(def) ident(insert_value)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + ident(node) operator(=) ident(Node)operator(()ident(value)operator(\)) + keyword(if) pre_constant(self)operator(.)ident(ring) keyword(is) keyword(not) pre_constant(None)operator(:) + ident(node)operator(.)ident(prev)operator(,) ident(node)operator(.)ident(next) operator(=) pre_constant(self)operator(.)ident(ring)operator(.)ident(prev)operator(,) pre_constant(self)operator(.)ident(ring) + pre_constant(self)operator(.)ident(ring)operator(.)ident(prev)operator(.)ident(next) operator(=) pre_constant(self)operator(.)ident(ring)operator(.)ident(prev) operator(=) ident(node) + pre_constant(self)operator(.)ident(ring) operator(=) ident(node) + pre_constant(self)operator(.)ident(count) operator(+=) integer(1) + + keyword(def) ident(delete_value)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + ident(node) operator(=) pre_constant(self)operator(.)ident(search)operator(()ident(value)operator(\)) + keyword(if) ident(node) keyword(is) keyword(not) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(delete_node)operator(()ident(node)operator(\)) + + keyword(def) ident(delete_node)operator(()pre_constant(self)operator(,) ident(node)operator(\))operator(:) + keyword(if) ident(node) keyword(is) ident(node)operator(.)ident(next)operator(:) + ident(node)operator(.)ident(next) operator(=) ident(node)operator(.)ident(prev) operator(=) pre_constant(None) + pre_constant(self)operator(.)ident(ring) operator(=) pre_constant(None) + keyword(else)operator(:) + ident(node)operator(.)ident(prev)operator(.)ident(next)operator(,) ident(node)operator(.)ident(next)operator(.)ident(prev) operator(=) ident(node)operator(.)ident(next)operator(,) ident(node)operator(.)ident(prev) + keyword(if) ident(node) keyword(is) pre_constant(self)operator(.)ident(ring)operator(:) + pre_constant(self)operator(.)ident(ring) operator(=) ident(node)operator(.)ident(next) + pre_constant(self)operator(.)ident(count) operator(-=) integer(1) + + keyword(def) ident(__del__)operator(()pre_constant(self)operator(\))operator(:) + keyword(while) pre_constant(self)operator(.)ident(ring) keyword(is) keyword(not) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(delete_node)operator(()pre_constant(self)operator(.)ident(ring)operator(\)) + +ident(COUNT) operator(=) integer(1000) +keyword(for) ident(rep) keyword(in) predefined(range)operator(()integer(20)operator(\))operator(:) + ident(r) operator(=) ident(Ring)operator(()operator(\)) + keyword(for) ident(i) keyword(in) predefined(range)operator(()ident(COUNT)operator(\))operator(:) + ident(r)operator(.)ident(insert_value)operator(()ident(i)operator(\)) +comment(#-----------------------------) + +comment(# ^^PLEAC^^_13.14) +comment(#-----------------------------) +keyword(import) ident(UserString) +keyword(class) ident(MyString)operator(()ident(UserString)operator(.)ident(UserString)operator(\))operator(:) + keyword(def) ident(__cmp__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + keyword(return) predefined(cmp)operator(()pre_constant(self)operator(.)ident(data)operator(.)ident(upper)operator(()operator(\))operator(,) ident(other)operator(.)ident(upper)operator(()operator(\))operator(\)) + +keyword(class) ident(Person)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(name)operator(,) ident(idnum)operator(\))operator(:) + pre_constant(self)operator(.)ident(name) operator(=) ident(name) + pre_constant(self)operator(.)ident(idnum) operator(=) ident(idnum) + keyword(def) ident(__str__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) string operator(%) operator(()pre_constant(self)operator(.)ident(name)operator(.)ident(lower)operator(()operator(\))operator(.)ident(capitalize)operator(()operator(\))operator(,) pre_constant(self)operator(.)ident(idnum)operator(\)) + +comment(#-----------------------------) +keyword(class) ident(TimeNumber)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(hours)operator(,) ident(minutes)operator(,) ident(seconds)operator(\))operator(:) + keyword(assert) ident(minutes) operator(<) integer(60) keyword(and) ident(seconds) operator(<) integer(60) + pre_constant(self)operator(.)ident(hours) operator(=) ident(hours) + pre_constant(self)operator(.)ident(minutes) operator(=) ident(minutes) + pre_constant(self)operator(.)ident(seconds) operator(=) ident(seconds) + keyword(def) ident(__str__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) string operator(%) operator(()pre_constant(self)operator(.)ident(hours)operator(,) pre_constant(self)operator(.)ident(minutes)operator(,) pre_constant(self)operator(.)ident(seconds)operator(\)) + keyword(def) ident(__add__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + ident(seconds) operator(=) pre_constant(self)operator(.)ident(seconds) operator(+) ident(other)operator(.)ident(seconds) + ident(minutes) operator(=) pre_constant(self)operator(.)ident(minutes) operator(+) ident(other)operator(.)ident(minutes) + ident(hours) operator(=) pre_constant(self)operator(.)ident(hours) operator(+) ident(other)operator(.)ident(hours) + keyword(if) ident(seconds) operator(>=) integer(60)operator(:) + ident(seconds) operator(%=) integer(60) + ident(minutes) operator(+=) integer(1) + keyword(if) ident(minutes) operator(>=) integer(60)operator(:) + ident(minutes) operator(%=) integer(60) + ident(hours) operator(+=) integer(1) + keyword(return) ident(TimeNumber)operator(()ident(hours)operator(,) ident(minutes)operator(,) ident(seconds)operator(\)) + + keyword(def) ident(__sub__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + keyword(raise) exception(NotImplementedError) + + keyword(def) ident(__mul__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + keyword(raise) exception(NotImplementedError) + + keyword(def) ident(__div__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + keyword(raise) exception(NotImplementedError) + +ident(t1) operator(=) ident(TimeNumber)operator(()integer(0)operator(,) integer(58)operator(,) integer(59)operator(\)) +ident(sec) operator(=) ident(TimeNumber)operator(()integer(0)operator(,) integer(0)operator(,) integer(1)operator(\)) +predefined(min) operator(=) ident(TimeNumber)operator(()integer(0)operator(,) integer(1)operator(,) integer(0)operator(\)) +keyword(print) ident(t1) operator(+) ident(sec) operator(+) predefined(min) operator(+) predefined(min) +comment(# 1:01:00) + +comment(#-----------------------------) +comment(# For demo purposes only - the StrNum class is superfluous in this) +comment(# case as plain strings would give the same result.) +keyword(class) ident(StrNum)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(value)operator(\))operator(:) + pre_constant(self)operator(.)ident(value) operator(=) ident(value) + + keyword(def) ident(__cmp__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# both <=> and cmp) + comment(# providing <=> gives us <, ==, etc. for free.) + comment(# __lt__, __eq__, and __gt__ can also be individually specified) + keyword(return) predefined(cmp)operator(()pre_constant(self)operator(.)ident(value)operator(,) ident(other)operator(.)ident(value)operator(\)) + + keyword(def) ident(__str__)operator(()pre_constant(self)operator(\))operator(:) comment(# "") + keyword(return) pre_constant(self)operator(.)ident(value) + + keyword(def) ident(__nonzero__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# bool) + keyword(return) predefined(bool)operator(()pre_constant(self)operator(.)ident(value)operator(\)) + + keyword(def) ident(__int__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# 0+) + keyword(return) predefined(int)operator(()pre_constant(self)operator(.)ident(value)operator(\)) + + keyword(def) ident(__add__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# +) + keyword(return) ident(StrNum)operator(()pre_constant(self)operator(.)ident(value) operator(+) ident(other)operator(.)ident(value)operator(\)) + + keyword(def) ident(__radd__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# +, inverted) + keyword(return) ident(StrNum)operator(()ident(other)operator(.)ident(value) operator(+) pre_constant(self)operator(.)ident(value)operator(\)) + + keyword(def) ident(__mul__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# *) + keyword(return) ident(StrNum)operator(()pre_constant(self)operator(.)ident(value) operator(*) ident(other)operator(\)) + + keyword(def) ident(__rmul__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) comment(# *, inverted) + keyword(return) ident(StrNum)operator(()pre_constant(self)operator(.)ident(value) operator(*) ident(other)operator(\)) + + +keyword(def) ident(demo)operator(()operator(\))operator(:) + comment(# show_strnum - demo operator overloading) + ident(x) operator(=) ident(StrNum)operator(()stringoperator(\)) + ident(y) operator(=) ident(StrNum)operator(()stringoperator(\)) + ident(z) operator(=) ident(x) operator(+) ident(y) + ident(r) operator(=) ident(z) operator(*) integer(3) + keyword(print) string operator(%) operator(()ident(x)operator(,) ident(y)operator(,) ident(z)operator(,) ident(r)operator(\)) + keyword(if) ident(x) operator(<) ident(y)operator(:) + ident(s) operator(=) string + keyword(else)operator(:) + ident(s) operator(=) string + keyword(print) ident(x)operator(,) stringoperator(,) ident(s)operator(,) ident(y) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(demo)operator(()operator(\)) +comment(# values are Red, Black, RedBlack, and RedBlackRedBlackRedBlack) +comment(# Red is GE Black) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# demo_fixnum - show operator overloading) + +comment(# sum of STRFixNum: 40 and STRFixNum: 12 is STRFixNum: 52) +comment(# product of STRFixNum: 40 and STRFixNum: 12 is STRFixNum: 480) +comment(# STRFixNum: 3 has 0 places) +comment(# div of STRFixNum: 40 by STRFixNum: 12 is STRFixNum: 3.33) +comment(# square of that is STRFixNum: 11.11) + +comment(# This isn't excatly the same as the original Perl code since) +comment(# I couldn't figure out why the PLACES variable was used.) +comment(#-----------------------------) +keyword(import) ident(re) +ident(_places_re) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + +ident(default_places) operator(=) integer(0) + +keyword(class) ident(FixNum)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(value)operator(,) ident(places) operator(=) pre_constant(None)operator(\))operator(:) + pre_constant(self)operator(.)ident(value) operator(=) ident(value) + keyword(if) ident(places) keyword(is) pre_constant(None)operator(:) + comment(# get from the value) + ident(m) operator(=) ident(_places_re)operator(.)ident(search)operator(()predefined(str)operator(()ident(value)operator(\))operator(\)) + keyword(if) ident(m)operator(:) + ident(places) operator(=) predefined(int)operator(()ident(m)operator(.)ident(group)operator(()integer(1)operator(\))operator(\)) + keyword(else)operator(:) + ident(places) operator(=) ident(default_places) + pre_constant(self)operator(.)ident(places) operator(=) ident(places) + + keyword(def) ident(__add__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + keyword(return) ident(FixNum)operator(()pre_constant(self)operator(.)ident(value) operator(+) ident(other)operator(.)ident(value)operator(,) + predefined(max)operator(()pre_constant(self)operator(.)ident(places)operator(,) ident(other)operator(.)ident(places)operator(\))operator(\)) + + keyword(def) ident(__mul__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + keyword(return) ident(FixNum)operator(()pre_constant(self)operator(.)ident(value) operator(*) ident(other)operator(.)ident(value)operator(,) + predefined(max)operator(()pre_constant(self)operator(.)ident(places)operator(,) ident(other)operator(.)ident(places)operator(\))operator(\)) + + keyword(def) ident(__div__)operator(()pre_constant(self)operator(,) ident(other)operator(\))operator(:) + comment(# Force to use floating point, since 2/3 in Python is 0) + comment(# Don't use float(\) since that will convert strings) + keyword(return) ident(FixNum)operator(()operator(()pre_constant(self)operator(.)ident(value)operator(+)float(0)float(.0)operator(\)) operator(/) ident(other)operator(.)ident(value)operator(,) + predefined(max)operator(()pre_constant(self)operator(.)ident(places)operator(,) ident(other)operator(.)ident(places)operator(\))operator(\)) + + keyword(def) ident(__str__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) string operator(%) operator(()pre_constant(self)operator(.)ident(__class__)operator(.)ident(__name__)operator(,) + pre_constant(self)operator(.)ident(places)operator(,) pre_constant(self)operator(.)ident(value)operator(\)) + keyword(def) ident(__int__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) predefined(int)operator(()pre_constant(self)operator(.)ident(value)operator(\)) + + keyword(def) ident(__float__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(value) + +keyword(def) ident(demo)operator(()operator(\))operator(:) + ident(x) operator(=) ident(FixNum)operator(()integer(40)operator(\)) + ident(y) operator(=) ident(FixNum)operator(()integer(12)operator(,) integer(0)operator(\)) + + keyword(print) stringoperator(,) ident(x)operator(,) stringoperator(,) ident(y)operator(,) stringoperator(,) ident(x)operator(+)ident(y) + keyword(print) stringoperator(,) ident(x)operator(,) stringoperator(,) ident(y)operator(,) stringoperator(,) ident(x)operator(*)ident(y) + + ident(z) operator(=) ident(x)operator(/)ident(y) + keyword(print) string operator(%) operator(()ident(z)operator(,) ident(z)operator(.)ident(places)operator(\)) + keyword(if) keyword(not) ident(z)operator(.)ident(places)operator(:) + ident(z)operator(.)ident(places) operator(=) integer(2) + + keyword(print) stringoperator(,) ident(x)operator(,) stringoperator(,) ident(y)operator(,) stringoperator(,) ident(z) + keyword(print) stringoperator(,) ident(z)operator(*)ident(z) + +keyword(if) ident(__name__) operator(==) stringoperator(:) + ident(demo)operator(()operator(\)) + + +comment(# ^^PLEAC^^_13.15) +comment(# You can't tie a variable, but you can use properties. ) +keyword(import) ident(itertools) +keyword(class) ident(ValueRing)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(colours)operator(\))operator(:) + pre_constant(self)operator(.)ident(colourcycle) operator(=) ident(itertools)operator(.)ident(cycle)operator(()ident(colours)operator(\)) + + keyword(def) ident(next_colour)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(colourcycle)operator(.)ident(next)operator(()operator(\)) + ident(colour) operator(=) predefined(property)operator(()ident(next_colour)operator(\)) +ident(vr) operator(=) ident(ValueRing)operator(()operator([)stringoperator(,) stringoperator(])operator(\)) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(6)operator(\))operator(:) + keyword(print) ident(vr)operator(.)ident(colour)operator(,) +keyword(print) + +comment(# Note that you MUST refer directly to the property) +ident(x) operator(=) ident(vr)operator(.)ident(colour) +keyword(print) ident(x)operator(,) ident(x)operator(,) ident(x) +comment(#-------------------------------------) +comment(# Ties are generally unnecessary in Python because of its strong OO support -) +comment(# The resulting code is MUCH shorter:) +keyword(class) ident(AppendDict)operator(()predefined(dict)operator(\))operator(:) + keyword(def) ident(__setitem__)operator(()pre_constant(self)operator(,) ident(key)operator(,) ident(val)operator(\))operator(:) + keyword(if) ident(key) keyword(in) pre_constant(self)operator(:) + pre_constant(self)operator([)ident(key)operator(])operator(.)ident(append)operator(()ident(val)operator(\)) + keyword(else)operator(:) + predefined(super)operator(()ident(AppendDict)operator(,) pre_constant(self)operator(\))operator(.)ident(__setitem__)operator(()ident(key)operator(,) operator([)ident(val)operator(])operator(\)) +ident(tab) operator(=) ident(AppendDict)operator(()operator(\)) +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) string + +keyword(for) ident(key)operator(,) ident(val) keyword(in) ident(tab)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) ident(key)operator(,) string)delimiter(")>operator(,) ident(val) +comment(#-------------------------------------) +keyword(class) ident(CaselessDict)operator(()predefined(dict)operator(\))operator(:) + keyword(def) ident(__setitem__)operator(()pre_constant(self)operator(,) ident(key)operator(,) ident(val)operator(\))operator(:) + predefined(super)operator(()ident(CaselessDict)operator(,) pre_constant(self)operator(\))operator(.)ident(__setitem__)operator(()ident(key)operator(.)ident(lower)operator(()operator(\))operator(,) ident(val)operator(\)) + keyword(def) ident(__getitem__)operator(()pre_constant(self)operator(,) ident(key)operator(\))operator(:) + keyword(return) predefined(super)operator(()ident(CaselessDict)operator(,) pre_constant(self)operator(\))operator(.)ident(__getitem__)operator(()ident(key)operator(.)ident(lower)operator(()operator(\))operator(\)) + +ident(tab) operator(=) ident(CaselessDict)operator(()operator(\)) +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) string + +keyword(for) ident(key)operator(,) ident(val) keyword(in) ident(tab)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) ident(key)operator(,) stringoperator(,) ident(val) +comment(#=>villain is bad wolf) +comment(#=>heroine is red riding hood) +comment(#-------------------------------------) +keyword(class) ident(RevDict)operator(()predefined(dict)operator(\))operator(:) + keyword(def) ident(__setitem__)operator(()pre_constant(self)operator(,) ident(key)operator(,) ident(val)operator(\))operator(:) + predefined(super)operator(()ident(RevDict)operator(,) pre_constant(self)operator(\))operator(.)ident(__setitem__)operator(()ident(key)operator(,) ident(val)operator(\)) + predefined(super)operator(()ident(RevDict)operator(,) pre_constant(self)operator(\))operator(.)ident(__setitem__)operator(()ident(val)operator(,) ident(key)operator(\)) + +ident(tab) operator(=) ident(RevDict)operator(()operator(\)) +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) string +ident(tab)operator([)stringoperator(]) operator(=) operator(()stringoperator(,) stringoperator(\)) + +keyword(for) ident(key)operator(,) ident(val) keyword(in) ident(tab)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(print) ident(key)operator(,) stringoperator(,) ident(val) +comment(#=>blue is azul) +comment(#=>('No Way!', 'Way!'\) is evil) +comment(#=>rojo is red) +comment(#=>evil is ('No Way!', 'Way!'\)) +comment(#=>azul is blue) +comment(#=>verde is green) +comment(#=>green is verde) +comment(#=>red is rojo) +comment(#-------------------------------------) +keyword(import) ident(itertools) +keyword(for) ident(elem) keyword(in) ident(itertools)operator(.)ident(count)operator(()operator(\))operator(:) + keyword(print) stringoperator(,) ident(elem) +comment(#-------------------------------------) +comment(# You could use FileDispatcher from section 7.18) +ident(tee) operator(=) ident(FileDispatcher)operator(()ident(sys)operator(.)ident(stderr)operator(,) ident(sys)operator(.)ident(stdout)operator(\)) +comment(#-------------------------------------) +comment(# @@PLEAC@@_14.0) + +comment(# See http://www.python.org/doc/topics/database/ for Database Interfaces details.) +comment(# currently listed on http://www.python.org/doc/topics/database/modules/) +comment(#) +comment(# DB/2, Informix, Interbase, Ingres, JDBC, MySQL, pyodbc, mxODBC, ODBC Interface,) +comment(# DCOracle, DCOracle2, PyGresQL, psycopg, PySQLite, sapdbapi, Sybase, ThinkSQL.) +comment(#) + +comment(# @@PLEAC@@_14.1) +comment(#-------------------------------------) +keyword(import) ident(anydbm) +ident(filename) operator(=) string +keyword(try)operator(:) + ident(db) operator(=) ident(anydbm)operator(.)ident(open)operator(()ident(filename)operator(\)) +keyword(except) ident(anydbm)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(filename)operator(,) ident(err)operator(\)) + +ident(db)operator([)stringoperator(]) operator(=) string comment(# put value into database) +keyword(if) string keyword(in) ident(db)operator(:) comment(# check whether in database) + ident(val) operator(=) ident(db)operator(.)ident(pop)operator(()stringoperator(\)) comment(# retrieve and remove from database) +ident(db)operator(.)ident(close)operator(()operator(\)) comment(# close the database) +comment(#-------------------------------------) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# userstats - generates statistics on who logged in.) +comment(# call with an argument to display totals) + +keyword(import) ident(sys)operator(,) ident(os)operator(,) ident(anydbm)operator(,) ident(re) + +ident(db_file) operator(=) string comment(# where data is kept between runs) + +keyword(try)operator(:) + ident(db) operator(=) ident(anydbm)operator(.)ident(open)operator(()ident(db_file)operator(,)stringoperator(\)) comment(# open, create if it does not exist) +keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(db_file)operator(,) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\)) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\)) operator(>) integer(1)operator(:) + keyword(if) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) operator(==) stringoperator(:) + ident(userlist) operator(=) ident(db)operator(.)ident(keys)operator(()operator(\)) + keyword(else)operator(:) + ident(userlist) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) + ident(userlist)operator(.)ident(sort)operator(()operator(\)) + keyword(for) ident(user) keyword(in) ident(userlist)operator(:) + keyword(if) ident(db)operator(.)ident(has_key)operator(()ident(user)operator(\))operator(:) + keyword(print) string operator(%) operator(()ident(user)operator(,) ident(db)operator([)ident(user)operator(])operator(\)) + keyword(else)operator(:) + keyword(print) string operator(%) operator(()ident(user)operator(,) integer(0)operator(\)) +keyword(else)operator(:) + ident(who) operator(=) ident(os)operator(.)ident(popen)operator(()stringoperator(\))operator(.)ident(readlines)operator(()operator(\)) comment(# run who(1\)) + keyword(if) predefined(len)operator(()ident(who)operator(\))operator(<)integer(1)operator(:) + keyword(print) string comment(# exit) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + comment(# extract username (first thin on the line\) and update) + ident(user_re) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + keyword(for) ident(line) keyword(in) ident(who)operator(:) + ident(fnd) operator(=) ident(user_re)operator(.)ident(search)operator(()ident(line)operator(\)) + keyword(if) keyword(not) ident(fnd)operator(:) + keyword(print) string operator(%) ident(line) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + ident(user) operator(=) ident(fnd)operator(.)ident(groups)operator(()operator(\))operator([)integer(0)operator(]) + keyword(if) keyword(not) ident(db)operator(.)ident(has_key)operator(()ident(user)operator(\))operator(:) + ident(db)operator([)ident(user)operator(]) operator(=) string + ident(db)operator([)ident(user)operator(]) operator(=) predefined(str)operator(()predefined(int)operator(()ident(db)operator([)ident(user)operator(])operator(\))operator(+)integer(1)operator(\)) comment(# only strings are allowed) +ident(db)operator(.)ident(close)operator(()operator(\)) + + + + +comment(# @@PLEAC@@_14.2) +comment(# Emptying a DBM File) + +keyword(import) ident(anydbm) + +keyword(try)operator(:) + ident(db) operator(=) ident(anydbm)operator(.)ident(open)operator(()ident(FILENAME)operator(,)stringoperator(\)) comment(# open, for writing) +keyword(except) ident(anydbm)operator(.)ident(error)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(filename)operator(,) ident(err)operator(\)) + keyword(raise) exception(SystemExit)operator(()integer(1)operator(\)) + +ident(db)operator(.)ident(clear)operator(()operator(\)) +ident(db)operator(.)ident(close)operator(()operator(\)) +comment(# -------------------------------) +keyword(try)operator(:) + ident(db) operator(=) ident(anydbm)operator(.)ident(open)operator(()ident(filename)operator(,)stringoperator(\)) comment(# open, always create a new empty db) +keyword(except) ident(anydbm)operator(.)ident(error)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(filename)operator(,) ident(err)operator(\)) + keyword(raise) exception(SystemExit)operator(()integer(1)operator(\)) + +ident(db)operator(.)ident(close)operator(()operator(\)) +comment(# -------------------------------) +keyword(import) ident(os) +keyword(try)operator(:) + ident(os)operator(.)ident(remove)operator(()ident(FILENAME)operator(\)) +keyword(except) exception(OSError)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(FILENAME)operator(,) + ident(err)operator(\)) + keyword(raise) exception(SystemExit) + +keyword(try)operator(:) + ident(db) operator(=) ident(anydbm)operator(.)ident(open)operator(()ident(FILENAME)operator(,)stringoperator(\)) comment(# open, flways create a new empty db) +keyword(except) ident(anydbm)operator(.)ident(error)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(FILENAME)operator(,) ident(err)operator(\)) + keyword(raise) exception(SystemExit) + +comment(# @@PLEAC@@_14.3) +comment(# Converting Between DBM Files) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# db2gdbm: converts DB to GDBM) + +keyword(import) ident(sys) +keyword(import) ident(dbm)operator(,) ident(gdbm) + +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(<)integer(3)operator(:) + keyword(print) string + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +operator(()ident(infile)operator(,) ident(outfile)operator(\)) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) + +comment(# open the files) +keyword(try)operator(:) + ident(db_in) operator(=) ident(dbm)operator(.)ident(open)operator(()ident(infile)operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(infile)operator(,) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\)) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +keyword(try)operator(:) + ident(db_out) operator(=) ident(dbm)operator(.)ident(open)operator(()ident(outfile)operator(,)stringoperator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(outfile)operator(,) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\)) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +comment(# copy (don't use db_out = db_in because it's slow on big databases\)) +comment(# is this also so for python ?) +keyword(for) ident(k) keyword(in) ident(db_in)operator(.)ident(keys)operator(()operator(\))operator(:) + ident(db_out)operator([)ident(k)operator(]) operator(=) ident(db_in)operator([)ident(k)operator(]) + +comment(# these close happen automatically at program exit) +ident(db_out)operator(.)ident(close)operator(()operator(\)) +ident(db_in)operator(.)ident(close)operator(()operator(\)) + + + +comment(# @@PLEAC@@_14.4) + +ident(OUTPUT)operator(.)ident(update)operator(()ident(INPUT1)operator(\)) +ident(OUTPUT)operator(.)ident(update)operator(()ident(INPUT2)operator(\)) + +ident(OUTPUT) operator(=) ident(anydbm)operator(.)ident(open)operator(()stringoperator(,)stringoperator(\)) +keyword(for) ident(INPUT) keyword(in) operator(()ident(INPUT1)operator(,) ident(INPUT2)operator(,) ident(INPUT1)operator(\))operator(:) + keyword(for) ident(key)operator(,) ident(value) keyword(in) ident(INPUT)operator(.)ident(iteritems)operator(()operator(\))operator(:) + keyword(if) ident(OUTPUT)operator(.)ident(has_key)operator(()ident(key)operator(\))operator(:) + comment(# decide which value to use and set OUTPUT[key] if necessary) + keyword(print) string operator(%) operator(() + ident(key)operator(,) ident(OUTPUT)operator([)ident(key)operator(])operator(,) ident(value) operator(\)) + keyword(else)operator(:) + ident(OUTPUT)operator([)ident(key)operator(]) operator(=) ident(value) + +comment(# @@PLEAC@@_14.5) +comment(# On systems where the Berkeley DB supports it, dbhash takes an) +comment(# "l" flag:) +keyword(import) ident(dbhash) +ident(dbhash)operator(.)ident(open)operator(()stringoperator(,) stringoperator(\)) comment(# 'c': create if doesn't exist) + +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_14.6) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_14.7) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_14.8) +comment(# shelve uses anydbm to access and chooses between DBMs.) +comment(# anydbm detect file formats automatically.) +keyword(import) ident(shelve) +ident(db) operator(=) ident(shelve)operator(.)ident(open)operator(()stringoperator(\)) + +ident(name1) operator(=) string +ident(name2) operator(=) string + +comment(# shelve uses pickle to convert objects into strings and back.) +comment(# This is automatic.) +ident(db)operator([)ident(name1)operator(]) operator(=) operator([)stringoperator(,) stringoperator(]) +ident(db)operator([)ident(name2)operator(]) operator(=) operator([)stringoperator(,) stringoperator(]) + +ident(greg1) operator(=) ident(db)operator([)ident(name1)operator(]) +ident(greg2) operator(=) ident(db)operator([)ident(name2)operator(]) + +keyword(print) string operator(%) operator(()predefined(id)operator(()ident(greg1)operator(\))operator(,) predefined(id)operator(()ident(greg2)operator(\))operator(\)) + +keyword(if) ident(greg1) operator(==) ident(greg2)operator(:) + keyword(print) string +keyword(else)operator(:) + keyword(print) string + +comment(# Changes to mutable entries are not written back by default.) +comment(# You can get the copy, change it, and put it back.) +ident(entry) operator(=) ident(db)operator([)ident(name1)operator(]) +ident(entry)operator([)integer(0)operator(]) operator(=) string +ident(db)operator([)ident(name1)operator(]) operator(=) ident(entry) + +comment(# Or you can open shelve with writeback option. Then you can) +comment(# change mutable entries directly. (New in 2.3\)) +ident(db) operator(=) ident(shelve)operator(.)ident(open)operator(()stringoperator(,) ident(writeback)operator(=)pre_constant(True)operator(\)) +ident(db)operator([)ident(name2)operator(])operator([)integer(0)operator(]) operator(=) string + +comment(# However, writeback option can consume vast amounts of memory) +comment(# to do its magic. You can clear cache with sync(\).) +ident(db)operator(.)ident(sync)operator(()operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_14.9) +comment(# DON'T DO THIS.) +keyword(import) ident(os) keyword(as) ident(_os)operator(,) ident(shelve) keyword(as) ident(_shelve) + +ident(_fname) operator(=) string +keyword(if) keyword(not) ident(_os)operator(.)ident(path)operator(.)ident(exists)operator(()ident(_fname)operator(\))operator(:) + ident(var1) operator(=) string + ident(var2) operator(=) string +ident(_d) operator(=) ident(_shelve)operator(.)ident(open)operator(()stringoperator(\)) +predefined(globals)operator(()operator(\))operator(.)ident(update)operator(()ident(_d)operator(\)) + +keyword(print) stringoperator(%)operator(()ident(var1)operator(,) ident(var2)operator(\)) +ident(var1) operator(=) predefined(raw_input)operator(()stringoperator(\)) +ident(var2) operator(=) predefined(raw_input)operator(()stringoperator(\)) + +keyword(for) ident(key)operator(,) ident(val) keyword(in) predefined(globals)operator(()operator(\))operator(.)ident(items)operator(()operator(\))operator(:) + keyword(if) keyword(not) ident(key)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + ident(_d)operator([)ident(key)operator(]) operator(=) ident(val) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_14.10) +comment(#-----------------------------) +keyword(import) ident(dbmodule) + +ident(dbconn) operator(=) ident(dbmodule)operator(.)ident(connect)operator(()ident(arguments)operator(...)operator(\)) + +ident(cursor) operator(=) ident(dbconn)operator(.)ident(cursor)operator(()operator(\)) +ident(cursor)operator(.)ident(execute)operator(()ident(sql)operator(\)) + +keyword(while) pre_constant(True)operator(:) + ident(row) operator(=) ident(cursor)operator(.)ident(fetchone)operator(()operator(\)) + keyword(if) ident(row) keyword(is) pre_constant(None)operator(:) + keyword(break) + operator(...) + +ident(cursor)operator(.)ident(close)operator(()operator(\)) +ident(dbconn)operator(.)ident(close)operator(()operator(\)) + +comment(#-----------------------------) +keyword(import) ident(MySQLdb) +keyword(import) ident(pwd) + +ident(dbconn) operator(=) ident(MySQLdb)operator(.)ident(connect)operator(()ident(db)operator(=)stringoperator(,) ident(host)operator(=)stringoperator(,) + ident(port)operator(=)integer(3306)operator(,) ident(user)operator(=)stringoperator(,) ident(passwd)operator(=)stringoperator(\)) + +ident(cursor) operator(=) ident(dbconn)operator(.)ident(cursor)operator(()operator(\)) +ident(cursor)operator(.)ident(execute)operator(()stringoperator(\)) + +comment(# Note: some databases use %s for parameters, some use ? or other) +comment(# formats) +ident(sql_fmt) operator(=) string + +keyword(for) ident(userent) keyword(in) ident(pwd)operator(.)ident(getpwall)operator(()operator(\))operator(:) + comment(# the second argument contains a list of parameters which will) + comment(# be quoted before being put in the query) + ident(cursor)operator(.)ident(execute)operator(()ident(sql_fmt)operator(,) operator(()ident(userent)operator(.)ident(pw_uid)operator(,) ident(userent)operator(.)ident(pw_name)operator(\))operator(\)) + +ident(cursor)operator(.)ident(execute)operator(()stringoperator(\)) + +keyword(for) ident(row) keyword(in) ident(cursor)operator(.)ident(fetchall)operator(()operator(\))operator(:) + comment(# NULL will be displayed as None) + keyword(print) stringoperator(.)ident(join)operator(()predefined(map)operator(()predefined(str)operator(,) ident(row)operator(\))operator(\)) + +ident(cursor)operator(.)ident(execute)operator(()stringoperator(\)) +ident(cursor)operator(.)ident(close)operator(()operator(\)) +ident(dbconn)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) + +comment(# @@PLEAC@@_14.11) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.1) +comment(#-----------------------------) +comment(# Parsing program arguments) +comment(# -- getopt way (All Python versions\)) + +comment(#-----------------------------) +comment(# Preamble) + +keyword(import) ident(sys) +keyword(import) ident(getopt) + +comment(# getopt(\) explicitly receives arguments for it to process.) +comment(# No magic. Explicit is better than implicit.) + +comment(# PERL: @ARGV) +ident(argv) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(]) + +comment(# Note that sys.argv[0] is the script name, and need to be) +comment(# stripped.) + +comment(#-----------------------------) +comment(# Short options) + +comment(# PERL: getopt("vDo"\);) +comment(# Polluting the caller's namespace is evil. Don't do that.) + +comment(# PERL: getopt("vDo:", \\%opts\);) +ident(opts)operator(,) ident(rest) operator(=) ident(getopt)operator(.)ident(getopt)operator(()ident(argv)operator(,) stringoperator(\)) + +comment(# If you want switches to take arguments, you must say so.) +comment(# Unlike PERL, which silently performs its magic, switches) +comment(# specified without trailing colons are considered boolean) +comment(# flags by default.) + +comment(# PERL: getopt("vDo", \\%opts\);) +ident(opts)operator(,) ident(rest) operator(=) ident(getopt)operator(.)ident(getopt)operator(()ident(argv)operator(,) stringoperator(\)) + +comment(# PERL: getopts("vDo:", \\%opts\);) +comment(# getopt/getopts distinction is not present in Python 'getopt') +comment(# module.) + +comment(#-----------------------------) +comment(# getopt(\) return values, compared to PERL) + +comment(# getopt(\) returns two values. The first is a list of) +comment(# (option, value\) pair. (Not a dictionary, i.e. Python hash.\)) +comment(# The second is the list of arguments left unprocessed.) + +comment(# Example) +comment(# >>> argv = "-v ARG1 -D ARG2 -o ARG3".split(\)) +comment(# >>> opts, rest = getopt.getopt(argv, "v:D:o:"\)) +comment(# >>> print opts) +comment(# [('-v', 'ARG1'\), ('-D', 'ARG2'\), ('-o', 'ARG3'\)]) + +comment(#-----------------------------) +comment(# Long options) + +comment(# getopt(\) handles long options too. Pass a list of option) +comment(# names as the third argument. If an option takes an argument,) +comment(# append an equal sign.) + +ident(opts)operator(,) ident(rest) operator(=) ident(getopt)operator(.)ident(getopt)operator(()ident(argv)operator(,) stringoperator(,) operator([) + stringoperator(,) stringoperator(,) stringoperator(])operator(\)) + +comment(#-----------------------------) +comment(# Switch clustering) + +comment(# getopt(\) does switch clustering just fine.) + +comment(# Example) +comment(# >>> argv1 = '-r -f /tmp/testdir'.split(\)) +comment(# >>> argv2 = '-rf /tmp/testdir'.split(\)) +comment(# >>> print getopt.getopt(argv1, 'rf'\)) +comment(# ([('-r', ''\), ('-f', ''\)], ['/tmp/testdir']\)) +comment(# >>> print getopt.getopt(argv2, 'rf'\)) +comment(# ([('-r', ''\), ('-f', ''\)], ['/tmp/testdir']\)) + +comment(#-----------------------------) +comment(# @@INCOMPLETE@@) + +comment(# TODO: Complete this section using 'getopt'. Show how to) +comment(# use the parsed result.) + +comment(# http://www.python.org/doc/current/lib/module-getopt.html) +comment(# Python library reference has a "typical usage" demo.) + +comment(# TODO: Introduce 'optparse', a very powerful command line) +comment(# option parsing module. New in 2.3.) + + +comment(# @@PLEAC@@_15.2) +comment(##------------------) +keyword(import) ident(sys) + +keyword(def) ident(is_interactive_python)operator(()operator(\))operator(:) + keyword(try)operator(:) + ident(ps) operator(=) ident(sys)operator(.)ident(ps1) + keyword(except)operator(:) + keyword(return) pre_constant(False) + keyword(return) pre_constant(True) +comment(##------------------) +keyword(import) ident(sys) +keyword(def) ident(is_interactive)operator(()operator(\))operator(:) + comment(# only False if stdin is redirected like "-t" in perl.) + keyword(return) ident(sys)operator(.)ident(stdin)operator(.)ident(isatty)operator(()operator(\)) + +comment(# Or take advantage of Python's Higher Order Functions:) +ident(is_interactive) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(isatty) +comment(##------------------) +keyword(import) ident(posix) +keyword(def) ident(is_interactive_posix)operator(()operator(\))operator(:) + ident(tty) operator(=) predefined(open)operator(()stringoperator(\)) + ident(tpgrp) operator(=) ident(posix)operator(.)ident(tcgetpgrp)operator(()ident(tty)operator(.)ident(fileno)operator(()operator(\))operator(\)) + ident(pgrp) operator(=) ident(posix)operator(.)ident(getpgrp)operator(()operator(\)) + ident(tty)operator(.)ident(close)operator(()operator(\)) + keyword(return) operator(()ident(tpgrp) operator(==) ident(pgrp)operator(\)) + +comment(# test with:) +comment(# python 15.2.py) +comment(# echo "dummy" | python 15.2.py | cat) +keyword(print) stringoperator(,) ident(is_interactive_python)operator(()operator(\)) +keyword(print) stringoperator(,) ident(is_interactive)operator(()operator(\)) +keyword(print) stringoperator(,) ident(is_interactive_posix)operator(()operator(\)) + +keyword(if) ident(is_interactive)operator(()operator(\))operator(:) + keyword(while) pre_constant(True)operator(:) + keyword(try)operator(:) + ident(ln) operator(=) predefined(raw_input)operator(()stringoperator(\)) + keyword(except)operator(:) + keyword(break) + keyword(print) stringoperator(,) ident(ln) + + +comment(# @@PLEAC@@_15.3) + +comment(# Python has no Term::Cap module.) +comment(# One could use the curses, but this was not ported to windows,) +comment(# use console.) + +comment(# just run clear) +keyword(import) ident(os) +ident(os)operator(.)ident(system)operator(()stringoperator(\)) +comment(# cache output) +ident(clear) operator(=) ident(os)operator(.)ident(popen)operator(()stringoperator(\))operator(.)ident(read)operator(()operator(\)) +keyword(print) ident(clear) +comment(# or to avoid print's newline) +ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(clear)operator(\)) + +comment(# @@PLEAC@@_15.4) +comment(# Determining Terminal or Window Size) + +comment(# eiter use ioctl) +keyword(import) ident(struct)operator(,) ident(fcntl)operator(,) ident(termios)operator(,) ident(sys) + +ident(s) operator(=) ident(struct)operator(.)ident(pack)operator(()stringoperator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(\)) +ident(hchar)operator(,) ident(wchar) operator(=) ident(struct)operator(.)ident(unpack)operator(()stringoperator(,) ident(fcntl)operator(.)ident(ioctl)operator(()ident(sys)operator(.)ident(stdout)operator(.)ident(fileno)operator(()operator(\))operator(,) + ident(termios)operator(.)ident(TIOCGWINSZ)operator(,) ident(s)operator(\))operator(\))operator([)operator(:)integer(2)operator(]) +comment(# or curses) +keyword(import) ident(curses) +operator(()ident(hchar)operator(,)ident(wchar)operator(\)) operator(=) ident(curses)operator(.)ident(getmaxyx)operator(()operator(\)) + +comment(# graph contents of values) +keyword(import) ident(struct)operator(,) ident(fcntl)operator(,) ident(termios)operator(,) ident(sys) +ident(width) operator(=) ident(struct)operator(.)ident(unpack)operator(()stringoperator(,) ident(fcntl)operator(.)ident(ioctl)operator(()ident(sys)operator(.)ident(stdout)operator(.)ident(fileno)operator(()operator(\))operator(,) + ident(termios)operator(.)ident(TIOCGWINSZ)operator(,) + ident(struct)operator(.)ident(pack)operator(()stringoperator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(\))operator(\))operator(\))operator([)integer(1)operator(]) +keyword(if) ident(width)operator(<)integer(10)operator(:) + keyword(print) string + keyword(raise) exception(SystemExit) + +ident(max_value) operator(=) integer(0) +keyword(for) ident(v) keyword(in) ident(values)operator(:) + ident(max_value) operator(=) predefined(max)operator(()ident(max_value)operator(,)ident(v)operator(\)) + +ident(ratio) operator(=) operator(()ident(width)operator(-)integer(10)operator(\))operator(/)ident(max_value) comment(# chars per unit) +keyword(for) ident(v) keyword(in) ident(values)operator(:) + keyword(print) string operator(%) operator(()ident(v)operator(,) stringoperator(*)operator(()ident(v)operator(*)ident(ratio)operator(\))operator(\)) + +comment(# @@PLEAC@@_15.5) + +comment(# there seems to be no standard ansi module) +comment(# and BLINK does not blink here.) +ident(RED) operator(=) string +ident(RESET) operator(=) string +ident(BLINK) operator(=) string +ident(NOBLINK) operator(=) string + +keyword(print) ident(RED)operator(+)stringoperator(+)ident(RESET) +keyword(print) string +keyword(print) stringoperator(+)ident(BLINK)operator(+)stringoperator(+)ident(NOBLINK)operator(+)string + +comment(# @@PLEAC@@_15.6) + +comment(# Show ASCII values for keypresses) + +comment(# _Getch is from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892) +keyword(class) ident(_Getch)operator(:) + string + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + keyword(try)operator(:) + pre_constant(self)operator(.)ident(impl) operator(=) ident(_GetchWindows)operator(()operator(\)) + keyword(except) exception(ImportError)operator(:) + pre_constant(self)operator(.)ident(impl) operator(=) ident(_GetchUnix)operator(()operator(\)) + + keyword(def) ident(__call__)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(impl)operator(()operator(\)) + + +keyword(class) ident(_GetchUnix)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + keyword(import) ident(tty)operator(,) ident(sys) + + keyword(def) ident(__call__)operator(()pre_constant(self)operator(\))operator(:) + keyword(import) ident(sys)operator(,) ident(tty)operator(,) ident(termios) + ident(fd) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(fileno)operator(()operator(\)) + ident(old_settings) operator(=) ident(termios)operator(.)ident(tcgetattr)operator(()ident(fd)operator(\)) + keyword(try)operator(:) + ident(tty)operator(.)ident(setraw)operator(()ident(sys)operator(.)ident(stdin)operator(.)ident(fileno)operator(()operator(\))operator(\)) + ident(ch) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(read)operator(()integer(1)operator(\)) + keyword(finally)operator(:) + ident(termios)operator(.)ident(tcsetattr)operator(()ident(fd)operator(,) ident(termios)operator(.)ident(TCSADRAIN)operator(,) ident(old_settings)operator(\)) + keyword(return) ident(ch) + + +keyword(class) ident(_GetchWindows)operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + keyword(import) ident(msvcrt) + + keyword(def) ident(__call__)operator(()pre_constant(self)operator(\))operator(:) + keyword(import) ident(msvcrt) + keyword(return) ident(msvcrt)operator(.)ident(getch)operator(()operator(\)) + + +ident(getch) operator(=) ident(_Getch)operator(()operator(\)) + +keyword(print) string +keyword(try)operator(:) + keyword(while) pre_constant(True)operator(:) + ident(char) operator(=) predefined(ord)operator(()ident(getch)operator(()operator(\))operator(\)) + keyword(if) ident(char) operator(==) integer(3)operator(:) + keyword(break) + keyword(print) string operator(%) operator(()ident(char)operator(,) ident(char)operator(,) ident(char)operator(\)) +keyword(except) ident(KeyboardError)operator(:) + keyword(pass) +comment(#----------------------------------------) + +comment(# @@PLEAC@@_15.7) +keyword(print) stringoperator(;) +comment(#----------------------------------------) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.8) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.9) +comment(# On Windows) +keyword(import) ident(msvcrt) +keyword(if) ident(msvcrt)operator(.)ident(kbhit)operator(()operator(\))operator(:) + ident(c) operator(=) ident(msvcrt)operator(.)ident(getch) + +comment(# See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892) +comment(# @@INCOMPLETE@@) + + +comment(# @@PLEAC@@_15.10) +comment(#----------------------------------------) +keyword(import) ident(getpass) +keyword(import) ident(pwd) +keyword(import) ident(crypt) +ident(password) operator(=) ident(getpass)operator(.)ident(getpass)operator(()stringoperator(\)) +ident(username) operator(=) ident(getpass)operator(.)ident(getuser)operator(()operator(\)) +ident(encrypted) operator(=) ident(pwd)operator(.)ident(getpwnam)operator(()ident(username)operator(\))operator(.)ident(pw_passwd) +keyword(if) keyword(not) ident(encrypted) keyword(or) ident(encrypted) operator(==) stringoperator(:) + comment(# If using shadow passwords, this will be empty or 'x') + keyword(print) string +keyword(elif) ident(crypt)operator(.)ident(crypt)operator(()ident(password)operator(,) ident(encrypted)operator(\)) operator(!=) ident(encrypted)operator(:) + keyword(print) stringoperator(,) ident(username) +keyword(else)operator(:) + keyword(print) stringoperator(,) ident(username) +comment(#----------------------------------------) + +comment(# @@PLEAC@@_15.11) + +comment(# simply importing readline gives line edit capabilities to raw_) +keyword(import) ident(readline) +ident(readline)operator(.)ident(add_history)operator(()stringoperator(\)) +ident(line) operator(=) predefined(raw_input)operator(()operator(\)) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# vbsh - very bad shell) + +keyword(import) ident(os) +keyword(import) ident(readline) + +keyword(while) pre_constant(True)operator(:) + keyword(try)operator(:) + ident(cmd) operator(=) predefined(raw_input)operator(()stringoperator(\)) + keyword(except) exception(EOFError)operator(:) + keyword(break) + ident(status) operator(=) ident(os)operator(.)ident(system)operator(()ident(cmd)operator(\)) + ident(exit_value) operator(=) ident(status) operator(>>) integer(8) + ident(signal_num) operator(=) ident(status) operator(&) integer(127) + ident(dumped_core) operator(=) ident(status) operator(&) integer(128) keyword(and) string keyword(or) string + keyword(print) string operator(%) operator(() + ident(exit_value)operator(,) ident(signal_num)operator(,) ident(dumped_core)operator(\)) + + + +ident(readline)operator(.)ident(add_history)operator(()stringoperator(\)) +ident(readline)operator(.)ident(remove_history_item)operator(()ident(position)operator(\)) +ident(line) operator(=) ident(readline)operator(.)ident(get_history_item)operator(()ident(index)operator(\)) + +comment(# an interactive python shell would be) +keyword(import) ident(code)operator(,) ident(readline) +ident(code)operator(.)ident(InteractiveConsole)operator(()operator(\))operator(.)ident(interact)operator(()stringoperator(\)) + +comment(# @@PLEAC@@_15.12) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.13) +comment(#----------------------------------------) +comment(# This entry uses pexpect, a pure Python Expect-like module.) +comment(# http://pexpect.sourceforge.net/) + +comment(# for more information, check pexpect's documentation and example.) + +keyword(import) ident(pexpect) + +comment(#----------------------------------------) +comment(# spawn program) +keyword(try)operator(:) + ident(command) operator(=) ident(pexpect)operator(.)ident(spawn)operator(()stringoperator(\)) +keyword(except) ident(pexpect)operator(.)ident(ExceptionPexpect)operator(:) + comment(# couldn't spawn program) + keyword(pass) + +comment(#----------------------------------------) +comment(# you can pass any filelike object to setlog) +comment(# passing None will stop logging) + +comment(# stop logging) +ident(command)operator(.)ident(setlog)operator(()pre_constant(None)operator(\)) + +comment(# log to stdout) +keyword(import) ident(sys) +ident(command)operator(.)ident(setlog)operator(()ident(sys)operator(.)ident(stdout)operator(\)) + +comment(# log to specific file) +ident(fp) operator(=) predefined(file)operator(()stringoperator(,) stringoperator(\)) +ident(command)operator(.)ident(setlog)operator(()ident(fp)operator(\)) + +comment(#----------------------------------------) +comment(# expecting simple string) +ident(command)operator(.)ident(expect)operator(()string)delimiter(")>operator(\)) + +comment(# expecting regular expression) +comment(# actually, string is always treated as regular expression) + +comment(# so it's the same thing) +ident(command)operator(.)ident(expect)operator(()stringoperator(\)) + +comment(# you can do it this way, too) +keyword(import) ident(re) +ident(regex) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +ident(command)operator(.)ident(expect)operator(()ident(regex)operator(\)) + +comment(#----------------------------------------) +comment(# expecting with timeout) +keyword(try)operator(:) + ident(command)operator(.)ident(expect)operator(()stringoperator(,) integer(10)operator(\)) +keyword(except) ident(pexpect)operator(.)ident(TIMEOUT)operator(:) + comment(# timed out) + keyword(pass) + +comment(# setting default timeout) +ident(command)operator(.)ident(timeout) operator(=) integer(10) + +comment(# since we set default timeout, following does same as above) +keyword(try)operator(:) + ident(command)operator(.)ident(expect)operator(()stringoperator(\)) +keyword(except) ident(pexpect)operator(.)ident(TIMEOUT)operator(:) + comment(# timed out) + keyword(pass) + +comment(#----------------------------------------) +comment(# what? do you *really* want to wait forever?) + +comment(#----------------------------------------) +comment(# sending line: normal way) +ident(command)operator(.)ident(sendline)operator(()stringoperator(\)) + +comment(# you can also treat it as file) +keyword(print)operator(>>)ident(command)operator(,) string + +comment(#----------------------------------------) +comment(# finalization) + +comment(# close connection with child process) +comment(# (that is, freeing file descriptor\)) +ident(command)operator(.)ident(close)operator(()operator(\)) + +comment(# kill child process) +keyword(import) ident(signal) +ident(command)operator(.)ident(kill)operator(()ident(signal)operator(.)ident(SIGKILL)operator(\)) + +comment(#----------------------------------------) +comment(# expecting multiple choices) +ident(which) operator(=) ident(command)operator(.)ident(expect)operator(()operator([)stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(])operator(\)) + +comment(# return value is index of matched choice) +comment(# 0: invalid) +comment(# 1: success) +comment(# 2: error) +comment(# 3: boom) + +comment(#----------------------------------------) +comment(# avoiding exception handling) +ident(choices) operator(=) operator([)stringoperator(,) stringoperator(,) stringoperator(,) stringoperator(]) +ident(choices)operator(.)ident(append)operator(()ident(pexpect)operator(.)ident(TIMEOUT)operator(\)) +ident(choices)operator(.)ident(append)operator(()ident(pexpect)operator(.)ident(EOF)operator(\)) + +ident(which) operator(=) ident(command)operator(.)ident(expect)operator(()ident(choices)operator(\)) + +comment(# if TIMEOUT or EOF occurs, appropriate index is returned) +comment(# (instead of raising exception\)) +comment(# 4: TIMEOUT) +comment(# 5: EOF) + +comment(# @@PLEAC@@_15.14) +keyword(from) ident(Tkinter) keyword(import) operator(*) + +keyword(def) ident(print_callback)operator(()operator(\))operator(:) + keyword(print) string + +ident(main) operator(=) ident(Tk)operator(()operator(\)) + +ident(menubar) operator(=) ident(Menu)operator(()ident(main)operator(\)) +ident(main)operator(.)ident(config)operator(()ident(menu)operator(=)ident(menubar)operator(\)) + +ident(file_menu) operator(=) ident(Menu)operator(()ident(menubar)operator(\)) +ident(menubar)operator(.)ident(add_cascade)operator(()ident(label)operator(=)stringoperator(,) ident(underline)operator(=)integer(1)operator(,) ident(menu)operator(=)ident(file_menu)operator(\)) +ident(file_menu)operator(.)ident(add_command)operator(()ident(label)operator(=)stringoperator(,) ident(command)operator(=)ident(print_callback)operator(\)) + +ident(main)operator(.)ident(mainloop)operator(()operator(\)) + +comment(# using a class) +keyword(from) ident(Tkinter) keyword(import) operator(*) + +keyword(class) ident(Application)operator(()ident(Tk)operator(\))operator(:) + keyword(def) ident(print_callback)operator(()pre_constant(self)operator(\))operator(:) + keyword(print) string + keyword(def) ident(debug_callback)operator(()pre_constant(self)operator(\))operator(:) + keyword(print) stringoperator(,) pre_constant(self)operator(.)ident(debug)operator(.)ident(get)operator(()operator(\)) + keyword(print) stringoperator(,) pre_constant(self)operator(.)ident(debug_level)operator(.)ident(get)operator(()operator(\)) + + keyword(def) ident(createWidgets)operator(()pre_constant(self)operator(\))operator(:) + ident(menubar) operator(=) ident(Menu)operator(()pre_constant(self)operator(\)) + pre_constant(self)operator(.)ident(config)operator(()ident(menu)operator(=)ident(menubar)operator(\)) + ident(file_menu) operator(=) ident(Menu)operator(()ident(menubar)operator(\)) + ident(menubar)operator(.)ident(add_cascade)operator(()ident(label)operator(=)stringoperator(,) + ident(underline)operator(=)integer(1)operator(,) ident(menu)operator(=)ident(file_menu)operator(\)) + ident(file_menu)operator(.)ident(add_command)operator(()ident(label)operator(=)stringoperator(,) + ident(command)operator(=)pre_constant(self)operator(.)ident(print_callback)operator(\)) + ident(file_menu)operator(.)ident(add_command)operator(()ident(label)operator(=)stringoperator(,) + ident(command)operator(=)ident(sys)operator(.)ident(exit)operator(\)) + comment(# ) + ident(options_menu) operator(=) ident(Menu)operator(()ident(menubar)operator(\)) + ident(menubar)operator(.)ident(add_cascade)operator(()ident(label)operator(=)stringoperator(,) + ident(underline)operator(=)integer(0)operator(,) ident(menu)operator(=)ident(options_menu)operator(\)) + ident(options_menu)operator(.)ident(add_checkbutton)operator(() + ident(label)operator(=)stringoperator(,) + ident(variable)operator(=)pre_constant(self)operator(.)ident(debug)operator(,) + ident(command)operator(=)pre_constant(self)operator(.)ident(debug_callback)operator(,) + ident(onvalue)operator(=)integer(1)operator(,) ident(offvalue)operator(=)integer(0)operator(\)) + ident(options_menu)operator(.)ident(add_separator)operator(()operator(\)) + ident(options_menu)operator(.)ident(add_radiobutton)operator(() + ident(label) operator(=) stringoperator(,) + ident(variable) operator(=) pre_constant(self)operator(.)ident(debug_level)operator(,) + ident(value) operator(=) integer(1) + operator(\)) + ident(options_menu)operator(.)ident(add_radiobutton)operator(() + ident(label) operator(=) stringoperator(,) + ident(variable) operator(=) pre_constant(self)operator(.)ident(debug_level)operator(,) + ident(value) operator(=) integer(2) + operator(\)) + ident(options_menu)operator(.)ident(add_radiobutton)operator(() + ident(label) operator(=) stringoperator(,) + ident(variable) operator(=) pre_constant(self)operator(.)ident(debug_level)operator(,) + ident(value) operator(=) integer(3) + operator(\)) + + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(master)operator(=)pre_constant(None)operator(\))operator(:) + ident(Tk)operator(.)ident(__init__)operator(()pre_constant(self)operator(,) ident(master)operator(\)) + comment(# bound variables must be IntVar, StrVar, ...) + pre_constant(self)operator(.)ident(debug) operator(=) ident(IntVar)operator(()operator(\)) + pre_constant(self)operator(.)ident(debug)operator(.)ident(set)operator(()integer(0)operator(\)) + pre_constant(self)operator(.)ident(debug_level) operator(=) ident(IntVar)operator(()operator(\)) + pre_constant(self)operator(.)ident(debug_level)operator(.)ident(set)operator(()integer(1)operator(\)) + pre_constant(self)operator(.)ident(createWidgets)operator(()operator(\)) + +ident(app) operator(=) ident(Application)operator(()operator(\)) +ident(app)operator(.)ident(mainloop)operator(()operator(\)) + +comment(# @@PLEAC@@_15.15) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.16) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.17) +comment(# Start Python scripts without the annoying DOS window on win32) +comment(# Use extension ".pyw" on files - eg: "foo.pyw" instead of "foo.py") +comment(# Or run programs using "pythonw.exe" rather than "python.exe" ) + +comment(# @@PLEAC@@_15.18) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_15.19) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + + +comment(# @@PLEAC@@_16.1) +keyword(import) ident(popen2) + +comment(# other popen methods than popen4 can lead to deadlocks) +comment(# if there is much data on stdout and stderr) + +operator(()ident(err_out)operator(,) ident(stdin)operator(\)) operator(=) ident(popen2)operator(.)ident(popen4)operator(()stringoperator(\)) +ident(lines) operator(=) ident(err_out)operator(.)ident(read)operator(()operator(\)) comment(# collect output into one multiline string) + +operator(()ident(err_out)operator(,) ident(stdin)operator(\)) operator(=) ident(popen2)operator(.)ident(popen4)operator(()stringoperator(\)) +ident(lines) operator(=) ident(err_out)operator(.)ident(readlines)operator(()operator(\)) comment(# collect output into a list, one line per element) + +comment(#-----------------------------) + +operator(()ident(err_out)operator(,) ident(stdin)operator(\)) operator(=) ident(popen2)operator(.)ident(popen4)operator(()stringoperator(\)) +ident(output) operator(=) operator([)operator(]) +keyword(while) pre_constant(True)operator(:) + ident(line) operator(=) ident(err_out)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(line)operator(:) + keyword(break) + ident(output)operator(.)ident(appen)operator(()ident(line)operator(\)) +ident(output) operator(=) stringoperator(.)ident(join)operator(()ident(output)operator(\)) + +comment(# @@PLEAC@@_16.2) +keyword(import) ident(os) +ident(myfile) operator(=) string +ident(status) operator(=) ident(os)operator(.)ident(system)operator(()string operator(%) ident(myfile)operator(\)) + +comment(#-----------------------------) +keyword(import) ident(os) +ident(os)operator(.)ident(system)operator(()stringoutfile)delimiter(")>operator(\)) +ident(os)operator(.)ident(system)operator(()stringoutfile 2>errfile)delimiter(")>operator(\)) + +ident(status) operator(=) ident(os)operator(.)ident(system)operator(()string operator(%) operator(()ident(program)operator(,) ident(arg1)operator(,) ident(arg2)operator(\))operator(\)) +keyword(if) ident(status) operator(!=) integer(0)operator(:) + keyword(print) string operator(%) operator(()ident(program)operator(,) ident(status)operator(\)) + keyword(raise) exception(SystemExit) + + +comment(# @@PLEAC@@_16.3) +comment(# -----------------------------) +keyword(import) ident(os) +keyword(import) ident(sys) +keyword(import) ident(glob) + +ident(args) operator(=) ident(glob)operator(.)ident(glob)operator(()stringoperator(\)) +keyword(try)operator(:) + ident(os)operator(.)ident(execvp)operator(()stringoperator(,) ident(args)operator(\)) +keyword(except) exception(OSError)operator(,) ident(e)operator(:) + keyword(print) string operator(%) ident(err) + keyword(raise) exception(SystemExit) + +comment(# The error message does not contain the line number like the "die" in) +comment(# perl. But if you want to show more information for debugging, you can) +comment(# delete the try...except and you get a nice traceback which shows all) +comment(# line numbers and filenames.) + +comment(# -----------------------------) +ident(os)operator(.)ident(execvp)operator(()stringoperator(,) operator([)stringoperator(])operator(\)) + +comment(# @@PLEAC@@_16.4) +comment(# -------------------------) +comment(# Read from a child process) + +keyword(import) ident(sys) +keyword(import) ident(popen2) +ident(pipe) operator(=) ident(popen2)operator(.)ident(Popen4)operator(()stringoperator(\)) +ident(pid) operator(=) ident(pipe)operator(.)ident(pid) +keyword(for) ident(line) keyword(in) ident(pipe)operator(.)ident(fromchild)operator(.)ident(readlines)operator(()operator(\))operator(:) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(line)operator(\)) + +comment(# Popen4 provides stdout and stderr.) +comment(# This avoids deadlocks if you get data) +comment(# from both streams.) +comment(#) +comment(# If you don't need the pid, you) +comment(# can use popen2.popen4(...\)) + +comment(# -----------------------------) +comment(# Write to a child process) + +keyword(import) ident(popen2) + +ident(pipe) operator(=) ident(popen2)operator(.)ident(Popen4)operator(()string foo.gz)delimiter(")>operator(\)) +ident(pid) operator(=) ident(pipe)operator(.)ident(pid) +ident(pipe)operator(.)ident(tochild)operator(.)ident(write)operator(()stringoperator(\)) +ident(pipe)operator(.)ident(tochild)operator(.)ident(close)operator(()operator(\)) comment(# programm will get EOF on STDIN) + +comment(# @@PLEAC@@_16.5) +keyword(class) ident(OutputFilter)operator(()predefined(object)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(target)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwds)operator(\))operator(:) + pre_constant(self)operator(.)ident(target) operator(=) ident(target) + pre_constant(self)operator(.)ident(setup)operator(()operator(*)ident(args)operator(,) operator(**)ident(kwds)operator(\)) + pre_constant(self)operator(.)ident(textbuffer) operator(=) string + + keyword(def) ident(setup)operator(()pre_constant(self)operator(,) operator(*)ident(args)operator(,) operator(**)ident(kwds)operator(\))operator(:) + keyword(pass) + + keyword(def) ident(write)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(if) ident(data)operator(.)ident(endswith)operator(()stringoperator(\))operator(:) + ident(data) operator(=) pre_constant(self)operator(.)ident(process)operator(()pre_constant(self)operator(.)ident(textbuffer) operator(+) ident(data)operator(\)) + pre_constant(self)operator(.)ident(textbuffer) operator(=) string + keyword(if) ident(data) keyword(is) keyword(not) pre_constant(None)operator(:) + pre_constant(self)operator(.)ident(target)operator(.)ident(write)operator(()ident(data)operator(\)) + keyword(else)operator(:) + pre_constant(self)operator(.)ident(textbuffer) operator(+=) ident(data) + + keyword(def) ident(process)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(return) ident(data) + +keyword(class) ident(HeadFilter)operator(()ident(OutputFilter)operator(\))operator(:) + keyword(def) ident(setup)operator(()pre_constant(self)operator(,) ident(maxcount)operator(\))operator(:) + pre_constant(self)operator(.)ident(count) operator(=) integer(0) + pre_constant(self)operator(.)ident(maxcount) operator(=) ident(maxcount) + + keyword(def) ident(process)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(if) pre_constant(self)operator(.)ident(count) operator(<) pre_constant(self)operator(.)ident(maxcount)operator(:) + pre_constant(self)operator(.)ident(count) operator(+=) integer(1) + keyword(return) ident(data) + +keyword(class) ident(NumberFilter)operator(()ident(OutputFilter)operator(\))operator(:) + keyword(def) ident(setup)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(count)operator(=)integer(0) + + keyword(def) ident(process)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + pre_constant(self)operator(.)ident(count) operator(+=) integer(1) + keyword(return) stringoperator(%)operator(()pre_constant(self)operator(.)ident(count)operator(,) ident(data)operator(\)) + +keyword(class) ident(QuoteFilter)operator(()ident(OutputFilter)operator(\))operator(:) + keyword(def) ident(process)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(return) string )delimiter(")> operator(+) ident(data) + +keyword(import) ident(sys) +ident(f) operator(=) ident(HeadFilter)operator(()ident(sys)operator(.)ident(stdout)operator(,) integer(100)operator(\)) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(130)operator(\))operator(:) + keyword(print)operator(>>)ident(f)operator(,) ident(i) + +keyword(print) + +ident(txt) operator(=) string +ident(f1) operator(=) ident(NumberFilter)operator(()ident(sys)operator(.)ident(stdout)operator(\)) +ident(f2) operator(=) ident(QuoteFilter)operator(()ident(f1)operator(\)) +keyword(for) ident(line) keyword(in) ident(txt)operator(.)ident(split)operator(()stringoperator(\))operator(:) + keyword(print)operator(>>)ident(f2)operator(,) ident(line) +keyword(print) +ident(f1) operator(=) ident(QuoteFilter)operator(()ident(sys)operator(.)ident(stdout)operator(\)) +ident(f2) operator(=) ident(NumberFilter)operator(()ident(f1)operator(\)) +keyword(for) ident(line) keyword(in) ident(txt)operator(.)ident(split)operator(()stringoperator(\))operator(:) + keyword(print)operator(>>)ident(f2)operator(,) ident(line) + + +comment(# @@PLEAC@@_16.6) +comment(# This script accepts several filenames) +comment(# as argument. If the file is zipped, unzip) +comment(# it first. Then read each line if the file) +keyword(import) ident(os) +keyword(import) ident(sys) +keyword(import) ident(popen2) + +keyword(for) predefined(file) keyword(in) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(:) + keyword(if) predefined(file)operator(.)ident(endswith)operator(()stringoperator(\)) keyword(or) predefined(file)operator(.)ident(endswith)operator(()stringoperator(\))operator(:) + operator(()ident(stdout)operator(,) ident(stdin)operator(\)) operator(=) ident(popen2)operator(.)ident(popen2)operator(()string operator(%) predefined(file)operator(\)) + ident(fd) operator(=) ident(stdout) + keyword(else)operator(:) + ident(fd) operator(=) predefined(open)operator(()predefined(file)operator(\)) + keyword(for) ident(line) keyword(in) ident(fd)operator(:) + comment(# ....) + ident(sys)operator(.)ident(stdout)operator(.)ident(write)operator(()ident(line)operator(\)) + ident(fd)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) + +comment(#-----------------------------) +comment(# Ask for filename and open it) +keyword(import) ident(sys) +keyword(print) string +ident(line) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(readline)operator(()operator(\)) +predefined(file) operator(=) ident(line)operator(.)ident(strip)operator(()operator(\)) comment(# chomp) +predefined(open)operator(()predefined(file)operator(\)) + +comment(# @@PLEAC@@_16.7) +comment(# Execute foo_command and read the output) + +keyword(import) ident(popen2) +operator(()ident(stdout_err)operator(,) ident(stdin)operator(\)) operator(=) ident(popen2)operator(.)ident(popen4)operator(()stringoperator(\)) +keyword(for) ident(line) keyword(in) ident(stdout_err)operator(.)ident(readlines)operator(()operator(\))operator(:) + comment(# ....) + +comment(# @@PLEAC@@_16.8) +comment(# Open command in a pipe) +comment(# which reads from stdin and writes to stdout) + +keyword(import) ident(popen2) +ident(pipe) operator(=) ident(popen2)operator(.)ident(Popen4)operator(()stringoperator(\)) comment(# Unix command) +ident(pipe)operator(.)ident(tochild)operator(.)ident(write)operator(()stringoperator(\)) +ident(pipe)operator(.)ident(tochild)operator(.)ident(close)operator(()operator(\)) +ident(output) operator(=) ident(pipe)operator(.)ident(fromchild)operator(.)ident(read)operator(()operator(\)) + +comment(# @@PLEAC@@_16.9) + +comment(# popen3: get stdout and stderr of new process) +comment(# Attetion: This can lead to deadlock,) +comment(# since the buffer of stderr or stdout might get filled.) +comment(# You need to use select if you want to avoid this.) + +keyword(import) ident(popen2) +operator(()ident(child_stdout)operator(,) ident(child_stdin)operator(,) ident(child_stderr)operator(\)) operator(=) ident(popen2)operator(.)ident(popen3)operator(()operator(...)operator(\)) + +comment(# @@PLEAC@@_16.10) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_16.11) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_16.12) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_16.13) +comment(#) +comment(# Print available signals and their value) +comment(# See "man signal" "man kill" on unix.) + +keyword(import) ident(signal) +keyword(for) ident(name) keyword(in) predefined(dir)operator(()ident(signal)operator(\))operator(:) + keyword(if) ident(name)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + ident(value) operator(=) predefined(getattr)operator(()ident(signal)operator(,) ident(name)operator(\)) + keyword(print) string operator(%) operator(()ident(name)operator(,) ident(value)operator(\)) + +comment(# @@PLEAC@@_16.14) +comment(# You can send signals to processes) +comment(# with os.kill(pid, signal\)) + + +comment(# @@PLEAC@@_16.15) +keyword(import) ident(signal) + +keyword(def) ident(get_sig_quit)operator(()ident(signum)operator(,) ident(frame)operator(\))operator(:) + operator(...)operator(.) + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGQUIT)operator(,) ident(get_sig_quit)operator(\)) comment(# Install handler) + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGINT)operator(,) ident(signal)operator(.)ident(SIG_IGN)operator(\)) comment(# Ignore this signal) +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGSTOP)operator(,) ident(signal)operator(.)ident(SIG_DFL)operator(\)) comment(# Restore to default handling) + +comment(# @@PLEAC@@_16.16) +comment(# Example of handler: User must Enter Name ctrl-c does not help) + +keyword(import) ident(sys) +keyword(import) ident(signal) + +keyword(def) ident(ding)operator(()ident(signum)operator(,) ident(frame)operator(\))operator(:) + keyword(print) string + keyword(return) + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGINT)operator(,) ident(ding)operator(\)) +keyword(print) string + +ident(name) operator(=) string +keyword(while) keyword(not) ident(name)operator(:) + keyword(try)operator(:) + ident(name) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(readline)operator(()operator(\))operator(.)ident(strip)operator(()operator(\)) + keyword(except)operator(:) + keyword(pass) + +keyword(print) string operator(%) ident(name) + +comment(# @@PLEAC@@_16.17) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_16.18) +keyword(import) ident(signal) + +comment(# ignore signal INT) +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGINT)operator(,) ident(signal)operator(.)ident(SIG_IGN)operator(\)) + +comment(# Install signal handler) +keyword(def) ident(tsktsk)operator(()ident(signum)operator(,) ident(frame)operator(\))operator(:) + keyword(print) string + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGINT)operator(,) ident(tsktsk)operator(\)) + +comment(# @@PLEAC@@_16.19) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_16.20) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_16.21) +keyword(import) ident(signal) + +keyword(def) ident(handler)operator(()ident(signum)operator(,) ident(frame)operator(\))operator(:) + keyword(raise) string + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGALRM)operator(,) ident(handler)operator(\)) + +keyword(try)operator(:) + ident(signal)operator(.)ident(alarm)operator(()integer(5)operator(\)) comment(# signal.alarm(3600\)) + + comment(# long-time operation) + keyword(while) pre_constant(True)operator(:) + keyword(print) string + + ident(signal)operator(.)ident(alarm)operator(()integer(0)operator(\)) +keyword(except)operator(:) + ident(signal)operator(.)ident(alarm)operator(()integer(0)operator(\)) + keyword(print) string +keyword(else)operator(:) + keyword(print) string + +comment(# @@PLEAC@@_16.22) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.0) +comment(# Socket Programming (tcp/ip and udp/ip\)) + +keyword(import) ident(socket) + +comment(# Convert human readable form to 32 bit value) +ident(packed_ip) operator(=) ident(socket)operator(.)ident(inet_aton)operator(()stringoperator(\)) +ident(packed_ip) operator(=) ident(socket)operator(.)ident(inet_aton)operator(()stringoperator(\)) + +comment(# Convert 32 bit value to ip adress) +ident(ip_adress) operator(=) ident(socket)operator(.)ident(inet_ntoa)operator(()ident(packed_ip)operator(\)) + +comment(# Create socket object) +ident(socketobj) operator(=) ident(socket)operator(()ident(family)operator(,) predefined(type)operator(\)) comment(# Example socket.AF_INT, socket.SOCK_STREAM) + +comment(# Get socketname) +ident(socketobj)operator(.)ident(getsockname)operator(()operator(\)) comment(# Example, get port adress of client) + +comment(# @@PLEAC@@_17.1) + +comment(# Example: Connect to a server (tcp\)) +comment(# Connect to a smtp server at localhost and send an email.) +comment(# For real applications you should use smtplib.) + +keyword(import) ident(socket) +ident(s) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_INET)operator(,) ident(socket)operator(.)ident(SOCK_STREAM)operator(\)) +ident(s)operator(.)ident(connect)operator(()operator(()stringoperator(,) integer(25)operator(\))operator(\)) comment(# SMTP) +keyword(print) ident(s)operator(.)ident(recv)operator(()integer(1024)operator(\)) +ident(s)operator(.)ident(send)operator(()string)char(\\n)delimiter(")>operator(\)) +keyword(print) ident(s)operator(.)ident(recv)operator(()integer(1024)operator(\)) +ident(s)operator(.)ident(send)operator(()string)char(\\n)delimiter(")>operator(\)) +keyword(print) ident(s)operator(.)ident(recv)operator(()integer(1024)operator(\)) +ident(s)operator(.)ident(send)operator(()stringoperator(\)) +keyword(print) ident(s)operator(.)ident(recv)operator(()integer(1024)operator(\)) +ident(s)operator(.)ident(send)operator(()stringoperator(\)) +keyword(print) ident(s)operator(.)ident(recv)operator(()integer(1024)operator(\)) +ident(s)operator(.)ident(close)operator(()operator(\)) + +comment(# @@PLEAC@@_17.2) + +comment(# Create a Server, calling handler for every client) +comment(# You can test it with "telnet localhost 1029") + +keyword(from) ident(SocketServer) keyword(import) ident(TCPServer) +keyword(from) ident(SocketServer) keyword(import) ident(BaseRequestHandler) + +keyword(class) ident(MyHandler)operator(()ident(BaseRequestHandler)operator(\))operator(:) + keyword(def) ident(handle)operator(()pre_constant(self)operator(\))operator(:) + keyword(print) string + +ident(server) operator(=) ident(TCPServer)operator(()operator(()stringoperator(,) integer(1029)operator(\))operator(,) ident(MyHandler)operator(\)) +ident(server)operator(.)ident(serve_forever)operator(()operator(\)) + +comment(# @@PLEAC@@_17.3) +comment(# This is the continuation of 17.2) + +keyword(import) ident(time) +keyword(from) ident(SocketServer) keyword(import) ident(TCPServer) +keyword(from) ident(SocketServer) keyword(import) ident(BaseRequestHandler) + +keyword(class) ident(MyHandler)operator(()ident(BaseRequestHandler)operator(\))operator(:) + keyword(def) ident(handle)operator(()pre_constant(self)operator(\))operator(:) + comment(# self.request is the socket object) + keyword(print) string operator(%) operator(() + ident(time)operator(.)ident(strftime)operator(()stringoperator(\))operator(,) + pre_constant(self)operator(.)ident(client_address)operator([)integer(0)operator(])operator(,) + pre_constant(self)operator(.)ident(client_address)operator([)integer(1)operator(]) + operator(\)) + pre_constant(self)operator(.)ident(request)operator(.)ident(send)operator(()stringoperator(\)) + ident(bufsize)operator(=)integer(1024) + ident(response)operator(=)pre_constant(self)operator(.)ident(request)operator(.)ident(recv)operator(()ident(bufsize)operator(\))operator(.)ident(strip)operator(()operator(\)) comment(# or recv(bufsize, flags\)) + ident(data_to_send)operator(=)string operator(%) ident(response) + pre_constant(self)operator(.)ident(request)operator(.)ident(send)operator(()ident(data_to_send)operator(\)) comment(# or send(data, flags\)) + keyword(print) string operator(%) pre_constant(self)operator(.)ident(client_address)operator([)integer(0)operator(]) + +ident(server) operator(=) ident(TCPServer)operator(()operator(()stringoperator(,) integer(1028)operator(\))operator(,) ident(MyHandler)operator(\)) +ident(server)operator(.)ident(serve_forever)operator(()operator(\)) + +comment(# -----------------) +comment(# Using select) + +keyword(import) ident(select) +keyword(import) ident(socket) + +ident(in_list) operator(=) operator([)operator(]) +ident(in_list)operator(.)ident(append)operator(()ident(mysocket)operator(\)) +ident(in_list)operator(.)ident(append)operator(()ident(myfile)operator(\)) +comment(# ...) + +ident(out_list) operator(=) operator([)operator(]) +ident(out_list)operator(.)ident(append)operator(()operator(...)operator(\)) + +ident(except_list) operator(=) operator([)operator(]) +ident(except_list)operator(.)ident(append)operator(()operator(...)operator(\)) + +operator(()ident(in_)operator(,) ident(out_)operator(,) ident(exc_)operator(\)) operator(=) ident(select)operator(.)ident(select)operator(()ident(in_list)operator(,) ident(out_list)operator(,) ident(except_list)operator(,) ident(timeout)operator(\)) + +keyword(for) ident(fd) keyword(in) ident(in_)operator(:) + keyword(print) stringoperator(,) ident(fd) +keyword(for) ident(fd) keyword(in) ident(out_)operator(:) + keyword(print) stringoperator(,) ident(fd) +keyword(for) ident(fd) keyword(in) ident(exc_)operator(:) + keyword(print) stringoperator(,) ident(fd) + +comment(# Missing: setting TCP_NODELAY) + +comment(# @@PLEAC@@_17.4) + +keyword(import) ident(socket) +comment(# Set up a UDP socket) +ident(s) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_INET)operator(,) ident(socket)operator(.)ident(SOCK_DGRAM)operator(\)) +comment(# send ) +ident(MSG) operator(=) string +ident(HOSTNAME) operator(=) string +ident(PORTNO) operator(=) integer(10000) +ident(s)operator(.)ident(connect)operator(()operator(()ident(HOSTNAME)operator(,) ident(PORTNO)operator(\))operator(\)) +keyword(if) predefined(len)operator(()ident(MSG)operator(\)) operator(!=) ident(s)operator(.)ident(send)operator(()ident(MSG)operator(\))operator(:) + comment(# where to get error message "$!".) + keyword(print) string operator(%) operator(()ident(HOSTNAME)operator(,)ident(PORTNO)operator(\)) + keyword(raise) exception(SystemExit)operator(()integer(1)operator(\)) +ident(MAXLEN) operator(=) integer(1024) +operator(()ident(data)operator(,)ident(addr)operator(\)) operator(=) ident(s)operator(.)ident(recvfrom)operator(()ident(MAXLEN)operator(\)) +ident(s)operator(.)ident(close)operator(()operator(\)) +keyword(print) string operator(%) operator(()ident(addr)operator([)integer(0)operator(])operator(,)ident(addr)operator([)integer(1)operator(])operator(,) ident(data)operator(\)) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# clockdrift - compare another system's clock with this one) + +keyword(import) ident(socket) +keyword(import) ident(struct) +keyword(import) ident(sys) +keyword(import) ident(time) + +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(>)integer(1)operator(:) + ident(him) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +keyword(else)operator(:) + ident(him) operator(=) string + +ident(SECS_of_70_YEARS) operator(=) integer(2208988800) + +ident(s) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_INET)operator(,) ident(socket)operator(.)ident(SOCK_DGRAM)operator(\)) +ident(s)operator(.)ident(connect)operator(()operator(()ident(him)operator(,)ident(socket)operator(.)ident(getservbyname)operator(()stringoperator(,)stringoperator(\))operator(\))operator(\)) +ident(s)operator(.)ident(send)operator(()stringoperator(\)) +operator(()ident(ptime)operator(,) ident(src)operator(\)) operator(=) ident(s)operator(.)ident(recvfrom)operator(()integer(4)operator(\)) +ident(host) operator(=) ident(socket)operator(.)ident(gethostbyaddr)operator(()ident(src)operator([)integer(0)operator(])operator(\)) +ident(delta) operator(=) ident(struct)operator(.)ident(unpack)operator(()stringoperator(,) ident(ptime)operator(\))operator([)integer(0)operator(]) operator(-) ident(SECS_of_70_YEARS) operator(-) ident(time)operator(.)ident(time)operator(()operator(\)) +keyword(print) string operator(%) operator(()ident(host)operator([)integer(0)operator(])operator(,) ident(delta)operator(\)) + + + +comment(# @@PLEAC@@_17.5) + +keyword(import) ident(socket) +keyword(import) ident(sys) + +ident(s) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_INET)operator(,) ident(socket)operator(.)ident(SOCK_DGRAM)operator(\)) +keyword(try)operator(:) + ident(s)operator(.)ident(bind)operator(()operator(()stringoperator(,) ident(server_port)operator(\))operator(\)) +keyword(except) ident(socket)operator(.)ident(error)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(() + ident(server_port)operator(,) ident(err)operator(\)) + keyword(raise) exception(SystemExit) + +keyword(while) pre_constant(True)operator(:) + ident(datagram) operator(=) ident(s)operator(.)ident(recv)operator(()ident(MAX_TO_READ)operator(\)) + keyword(if) keyword(not) ident(datagram)operator(:) + keyword(break) + comment(# do something) +ident(s)operator(.)ident(close)operator(()operator(\)) + +comment(# or ) +keyword(import) ident(SocketServer) + +keyword(class) ident(handler)operator(()ident(SocketServer)operator(.)ident(DatagramRequestHandler)operator(\))operator(:) + keyword(def) ident(handle)operator(()pre_constant(self)operator(\))operator(:) + comment(# do something (with self.request[0]\)) + +ident(s) operator(=) ident(SocketServer)operator(.)ident(UDPServer)operator(()operator(()stringoperator(,)integer(10000)operator(\))operator(,) ident(handler)operator(\)) +ident(s)operator(.)ident(serve_forever)operator(()operator(\)) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# udpqotd - UDP message server) + +keyword(import) ident(SocketServer) + +ident(PORTNO) operator(=) integer(5151) + +keyword(class) ident(handler)operator(()ident(SocketServer)operator(.)ident(DatagramRequestHandler)operator(\))operator(:) + keyword(def) ident(handle)operator(()pre_constant(self)operator(\))operator(:) + ident(newmsg) operator(=) pre_constant(self)operator(.)ident(rfile)operator(.)ident(readline)operator(()operator(\))operator(.)ident(rstrip)operator(()operator(\)) + keyword(print) string operator(%) operator(()pre_constant(self)operator(.)ident(client_address)operator([)integer(0)operator(])operator(,) ident(newmsg)operator(\)) + pre_constant(self)operator(.)ident(wfile)operator(.)ident(write)operator(()pre_constant(self)operator(.)ident(server)operator(.)ident(oldmsg)operator(\)) + pre_constant(self)operator(.)ident(server)operator(.)ident(oldmsg) operator(=) ident(newmsg) + +ident(s) operator(=) ident(SocketServer)operator(.)ident(UDPServer)operator(()operator(()stringoperator(,)ident(PORTNO)operator(\))operator(,) ident(handler)operator(\)) +keyword(print) string operator(%) ident(PORTNO) +ident(s)operator(.)ident(oldmsg) operator(=) string +ident(s)operator(.)ident(serve_forever)operator(()operator(\)) + + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# udpmsg - send a message to the udpquotd server) + +keyword(import) ident(socket) +keyword(import) ident(sys) + +ident(MAXLEN) operator(=) integer(1024) +ident(PORTNO) operator(=) integer(5151) +ident(TIMEOUT) operator(=) integer(5) + +ident(server_host) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +ident(msg) operator(=) stringoperator(.)ident(join)operator(()ident(sys)operator(.)ident(argv)operator([)integer(2)operator(:)operator(])operator(\)) + +ident(sock) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_INET)operator(,) ident(socket)operator(.)ident(SOCK_DGRAM)operator(\)) +ident(sock)operator(.)ident(settimeout)operator(()ident(TIMEOUT)operator(\)) +ident(sock)operator(.)ident(connect)operator(()operator(()ident(server_host)operator(,) ident(PORTNO)operator(\))operator(\)) +ident(sock)operator(.)ident(send)operator(()ident(msg)operator(\)) +keyword(try)operator(:) + ident(msg) operator(=) ident(sock)operator(.)ident(recv)operator(()ident(MAXLEN)operator(\)) + ident(ipaddr)operator(,) ident(port) operator(=) ident(sock)operator(.)ident(getpeername)operator(()operator(\)) + ident(hishost) operator(=) ident(socket)operator(.)ident(gethostbyaddr)operator(()ident(ipaddr)operator(\)) + keyword(print) string operator(%) operator(() ident(hishost)operator([)integer(0)operator(])operator(,) ident(msg)operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) operator(() + ident(server_host) operator(\)) +ident(sock)operator(.)ident(close)operator(()operator(\)) + + +comment(# @@PLEAC@@_17.6) + +keyword(import) ident(socket) +keyword(import) ident(os)operator(,) ident(os)operator(.)ident(path) + +keyword(if) ident(os)operator(.)ident(path)operator(.)ident(exists)operator(()stringoperator(\))operator(:) + ident(os)operator(.)ident(remove)operator(()stringoperator(\)) + +ident(server) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_UNIX)operator(,) ident(socket)operator(.)ident(SOCK_DGRAM)operator(\)) +ident(server)operator(.)ident(bind)operator(()stringoperator(\)) + +ident(client) operator(=) ident(socket)operator(.)ident(socket)operator(()ident(socket)operator(.)ident(AF_UNIX)operator(,) ident(socket)operator(.)ident(SOCK_DGRAM)operator(\)) +ident(client)operator(.)ident(connect)operator(()stringoperator(\)) + +comment(# @@PLEAC@@_17.7) + +ident(ipaddr)operator(,) ident(port) operator(=) ident(s)operator(.)ident(getpeername)operator(()operator(\)) +ident(hostname)operator(,) ident(aliaslist)operator(,) ident(ipaddrlist) operator(=) ident(socket)operator(.)ident(gethostbyaddr)operator(()ident(ipaddr)operator(\)) +ident(ipaddr) operator(=) ident(socket)operator(.)ident(gethostbyname)operator(()stringoperator(\)) +comment(# '194.109.137.226') +ident(hostname)operator(,) ident(aliaslist)operator(,) ident(ipaddrlist) operator(=) ident(socket)operator(.)ident(gethostbyname_ex)operator(()stringoperator(\)) +comment(# ('fang.python.org', ['www.python.org'], ['194.109.137.226']\)) +ident(socket)operator(.)ident(gethostbyname_ex)operator(()stringoperator(\)) +comment(# ('www.l.google.com', ['www.google.org', 'www.google.com'], ) +comment(# ['64.233.161.147','64.233.161.104', '64.233.161.99']\)) + +comment(# @@PLEAC@@_17.8) + +keyword(import) ident(os) + +ident(kernel)operator(,) ident(hostname)operator(,) ident(release)operator(,) ident(version)operator(,) ident(hardware) operator(=) ident(os)operator(.)ident(uname)operator(()operator(\)) + +keyword(import) ident(socket) + +ident(address) operator(=) ident(socket)operator(.)ident(gethostbyname)operator(()ident(hostname)operator(\)) +ident(hostname) operator(=) ident(socket)operator(.)ident(gethostbyaddr)operator(()ident(address)operator(\)) +ident(hostname)operator(,) ident(aliaslist)operator(,) ident(ipaddrlist) operator(=) ident(socket)operator(.)ident(gethostbyname_ex)operator(()ident(hostname)operator(\)) +comment(# e.g. ('lx3.local', ['lx3', 'b70'], ['192.168.0.13', '192.168.0.70']\)) + +comment(# @@PLEAC@@_17.9) + +ident(socket)operator(.)ident(shutdown)operator(()integer(0)operator(\)) comment(# Further receives are disallowed) +ident(socket)operator(.)ident(shutdown)operator(()integer(1)operator(\)) comment(# Further sends are disallowed.) +ident(socket)operator(.)ident(shutdown)operator(()integer(2)operator(\)) comment(# Further sends and receives are disallowed.) + +comment(#) + +ident(server)operator(.)ident(send)operator(()stringoperator(\)) comment(# send some data) +ident(server)operator(.)ident(shutdown)operator(()integer(1)operator(\)) comment(# send eof; no more writing) +ident(answer) operator(=) ident(server)operator(.)ident(recv)operator(()integer(1000)operator(\)) comment(# but you can still read) + +comment(# @@PLEAC@@_17.10) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.11) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.12) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.13) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.14) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.15) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_17.16) +comment(#------------------------------) +comment(# Restart programm on signal SIGHUP) +comment(# Script must be executable: chmod a+x foo.py) + +comment(#!/usr/bin/env python) +keyword(import) ident(os) +keyword(import) ident(sys) +keyword(import) ident(time) +keyword(import) ident(signal) + +keyword(def) ident(phoenix)operator(()ident(signum)operator(,) ident(frame)operator(\))operator(:) + keyword(print) string operator(%) operator(()pre_constant(self)operator(,) ident(args)operator(\)) + ident(os)operator(.)ident(execv)operator(()pre_constant(self)operator(,) ident(args)operator(\)) + +pre_constant(self) operator(=) ident(os)operator(.)ident(path)operator(.)ident(abspath)operator(()ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) +ident(args) operator(=) ident(sys)operator(.)ident(argv)operator([)operator(:)operator(]) +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGHUP)operator(,) ident(phoenix)operator(\)) + +keyword(while) pre_constant(True)operator(:) + keyword(print) string + ident(time)operator(.)ident(sleep)operator(()integer(1)operator(\)) + +comment(#--------------------) +comment(# Read config file on SIGHUP) +keyword(import) ident(signal) + +ident(config_file) operator(=) string + +keyword(def) ident(read_config)operator(()operator(\))operator(:) + predefined(execfile)operator(()ident(config_file)operator(\)) + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGHUP)operator(,) ident(read_config)operator(\)) + +comment(# @@PLEAC@@_17.17) + +comment(# chroot) + +keyword(import) ident(os) + +keyword(try)operator(:) + ident(os)operator(.)ident(chroot)operator(()stringoperator(\)) +keyword(except) exception(Exception)operator(:) + keyword(print) string + keyword(raise) exception(SystemExit)operator(()integer(1)operator(\)) + +comment(#-----------------------------) +comment(# fork (Unix\): Create a new process) +comment(# if pid == 0 --> parent process) +comment(# else child process) + +keyword(import) ident(os) + +ident(pid) operator(=) ident(os)operator(.)ident(fork)operator(()operator(\)) +keyword(if) ident(pid)operator(:) + keyword(print) string operator(%) ident(pid) + keyword(raise) exception(SystemExit) +keyword(else)operator(:) + keyword(print) string + + +comment(# ----------------------------) +comment(# setsid (Unix\): Create a new session) +keyword(import) ident(os) +predefined(id)operator(=)ident(os)operator(.)ident(setsid)operator(()operator(\)) + +comment(# ----------------------------) +comment(# Work until INT TERM or HUP signal is received) +keyword(import) ident(time) +keyword(import) ident(signal) + +ident(time_to_die) operator(=) integer(0) + +keyword(def) ident(sighandler)operator(()ident(signum)operator(,) ident(frame)operator(\))operator(:) + keyword(print) string + keyword(global) ident(time_to_die) + ident(time_to_die) operator(=) integer(1) + +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGINT)operator(,) ident(sighandler)operator(\)) +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGTERM)operator(,) ident(sighandler)operator(\)) +ident(signal)operator(.)ident(signal)operator(()ident(signal)operator(.)ident(SIGHUP)operator(,) ident(sighandler)operator(\)) + +keyword(while) keyword(not) ident(time_to_die)operator(:) + keyword(print) string + ident(time)operator(.)ident(sleep)operator(()integer(1)operator(\)) + +comment(# @@PLEAC@@_17.18) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_18.1) + +keyword(import) ident(socket) +keyword(try)operator(:) + ident(host_info) operator(=) ident(socket)operator(.)ident(gethostbyname_ex)operator(()ident(name)operator(\)) + comment(# (hostname, aliaslist, ipaddrlist\)) +keyword(except) ident(socket)operator(.)ident(gaierror)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(name)operator(,) ident(err)operator([)integer(1)operator(])operator(\)) + +comment(# if you only need the first one) +keyword(import) ident(socket) +keyword(try)operator(:) + ident(address) operator(=) ident(socket)operator(.)ident(gethostbyname)operator(()ident(name)operator(\)) +keyword(except) ident(socket)operator(.)ident(gaierror)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(name)operator(,) ident(err)operator([)integer(1)operator(])operator(\)) + +comment(# if you have an ip address) +keyword(try)operator(:) + ident(host_info) operator(=) ident(socket)operator(.)ident(gethostbyaddr)operator(()ident(address)operator(\)) + comment(# (hostname, aliaslist, ipaddrlist\)) +keyword(except) ident(socket)operator(.)ident(gaierror)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(address)operator(,) ident(err)operator([)integer(1)operator(])operator(\)) + +comment(# checking back) +keyword(import) ident(socket) +keyword(try)operator(:) + ident(host_info) operator(=) ident(socket)operator(.)ident(gethostbyaddr)operator(()ident(address)operator(\)) +keyword(except) ident(socket)operator(.)ident(gaierror)operator(,) ident(err)operator(:) + keyword(print) string operator(%) operator(()ident(address)operator(,) ident(err)operator([)integer(1)operator(])operator(\)) + keyword(raise) exception(SystemExit)operator(()integer(1)operator(\)) +keyword(try)operator(:) + ident(host_info) operator(=) ident(socket)operator(.)ident(gethostbyname_ex)operator(()ident(name)operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) operator(()ident(name)operator(,) ident(err)operator([)integer(1)operator(])operator(\)) + keyword(raise) exception(SystemExit)operator(()integer(1)operator(\)) + +ident(found) operator(=) ident(address) keyword(in) ident(host_info)operator([)integer(2)operator(]) + +comment(# use dnspython for more complex jobs.) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# mxhost - find mx exchangers for a host) + +keyword(import) ident(sys) + +keyword(import) ident(dns) +keyword(import) ident(dns)operator(.)ident(resolver) + +ident(answers) operator(=) ident(dns)operator(.)ident(resolver)operator(.)ident(query)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(,) stringoperator(\)) +keyword(for) ident(rdata) keyword(in) ident(answers)operator(:) + keyword(print) ident(rdata)operator(.)ident(preference)operator(,) ident(rdata)operator(.)ident(exchange) + + + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# hostaddrs - canonize name and show addresses) + +keyword(import) ident(sys) +keyword(import) ident(socket) +ident(name) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +ident(hent) operator(=) ident(socket)operator(.)ident(gethostbyname_ex)operator(()ident(name)operator(\)) +keyword(print) string %s)delimiter(")> operator(%) operator(() + ident(hent)operator([)integer(0)operator(])operator(,) + predefined(len)operator(()ident(hent)operator([)integer(1)operator(])operator(\))operator(==)integer(0) keyword(and) string keyword(or) stringoperator(.)ident(join)operator(()ident(hent)operator([)integer(1)operator(])operator(\))operator(,) + stringoperator(.)ident(join)operator(()ident(hent)operator([)integer(2)operator(])operator(\)) operator(\)) + + +comment(# @@PLEAC@@_18.2) +keyword(import) ident(ftplib) +ident(ftp) operator(=) ident(ftplib)operator(.)ident(FTP)operator(()stringoperator(\)) +ident(ftp)operator(.)ident(login)operator(()ident(username)operator(,) ident(password)operator(\)) +ident(ftp)operator(.)ident(cwd)operator(()ident(directory)operator(\)) + +comment(# get file) +ident(outfile) operator(=) predefined(open)operator(()ident(filename)operator(,) stringoperator(\)) +ident(ftp)operator(.)ident(retrbinary)operator(()string operator(%) ident(filename)operator(,) ident(outfile)operator(.)ident(write)operator(\)) +ident(outfile)operator(.)ident(close)operator(()operator(\)) + +comment(# upload file) +ident(upfile) operator(=) predefined(open)operator(()ident(upfilename)operator(,) stringoperator(\)) +ident(ftp)operator(.)ident(storbinary)operator(()string operator(%) ident(upfilename)operator(,) ident(upfile)operator(\)) +ident(upfile)operator(.)ident(close)operator(()operator(\)) + +ident(ftp)operator(.)ident(quit)operator(()operator(\)) + + +comment(# @@PLEAC@@_18.3) +keyword(import) ident(smtplib) +keyword(from) ident(email)operator(.)ident(MIMEText) keyword(import) ident(MIMEText) + +ident(msg) operator(=) ident(MIMEText)operator(()ident(body)operator(\)) +ident(msg)operator([)stringoperator(]) operator(=) ident(from_address) +ident(msg)operator([)stringoperator(]) operator(=) ident(to_address) +ident(msg)operator([)stringoperator(]) operator(=) ident(subject) + +ident(mailer) operator(=) ident(smtplib)operator(.)ident(SMTP)operator(()operator(\)) +ident(mailer)operator(.)ident(connect)operator(()operator(\)) +ident(mailer)operator(.)ident(sendmail)operator(()ident(from_address)operator(,) operator([)ident(to_address)operator(])operator(,) ident(msg)operator(.)ident(as_string)operator(()operator(\))operator(\)) + +comment(# @@PLEAC@@_18.4) +keyword(import) ident(nntplib) + +comment(# You can except nntplib.NNTPError to process errors) +comment(# instead of displaying traceback.) + +ident(server) operator(=) ident(nntplib)operator(.)ident(NNTP)operator(()stringoperator(\)) +ident(response)operator(,) ident(count)operator(,) ident(first)operator(,) ident(last)operator(,) ident(name) operator(=) ident(server)operator(.)ident(group)operator(()stringoperator(\)) +ident(headers) operator(=) ident(server)operator(.)ident(head)operator(()ident(first)operator(\)) +ident(bodytext) operator(=) ident(server)operator(.)ident(body)operator(()ident(first)operator(\)) +ident(article) operator(=) ident(server)operator(.)ident(article)operator(()ident(first)operator(\)) + +ident(f) operator(=) predefined(file)operator(()stringoperator(\)) +ident(server)operator(.)ident(post)operator(()ident(f)operator(\)) + +ident(response)operator(,) ident(grouplist) operator(=) ident(server)operator(.)ident(list)operator(()operator(\)) +keyword(for) ident(group) keyword(in) ident(grouplist)operator(:) + ident(name)operator(,) ident(last)operator(,) ident(first)operator(,) ident(flag) operator(=) ident(group) + keyword(if) ident(flag) operator(==) stringoperator(:) + keyword(pass) comment(# I can post to group) + +comment(# @@PLEAC@@_18.5) +keyword(import) ident(poplib) + +ident(pop) operator(=) ident(poplib)operator(.)ident(POP3)operator(()stringoperator(\)) +ident(pop)operator(.)ident(user)operator(()ident(username)operator(\)) +ident(pop)operator(.)ident(pass_)operator(()ident(password)operator(\)) +ident(count)operator(,) ident(size) operator(=) ident(pop)operator(.)ident(stat)operator(()operator(\)) +keyword(for) ident(i) keyword(in) predefined(range)operator(()integer(1)operator(,) ident(count)operator(+)integer(1)operator(\))operator(:) + ident(reponse)operator(,) ident(message)operator(,) ident(octets) operator(=) ident(pop)operator(.)ident(retr)operator(()ident(i)operator(\)) + comment(# message is a list of lines) + ident(pop)operator(.)ident(dele)operator(()ident(i)operator(\)) + +comment(# You must quit, otherwise mailbox remains locked.) +ident(pop)operator(.)ident(quit)operator(()operator(\)) + +comment(# @@PLEAC@@_18.6) + +keyword(import) ident(telnetlib) + +ident(tn) operator(=) ident(telnetlib)operator(.)ident(Telnet)operator(()ident(hostname)operator(\)) + +ident(tn)operator(.)ident(read_until)operator(()stringoperator(\)) +ident(tn)operator(.)ident(write)operator(()ident(user) operator(+) stringoperator(\)) +ident(tn)operator(.)ident(read_until)operator(()stringoperator(\)) +ident(tn)operator(.)ident(write)operator(()ident(password) operator(+) stringoperator(\)) +comment(# read the logon message up to the prompt) +ident(d) operator(=) ident(tn)operator(.)ident(expect)operator(()operator([)ident(prompt)operator(,)operator(])operator(,) integer(10)operator(\)) +ident(tn)operator(.)ident(write)operator(()stringoperator(\)) +ident(files) operator(=) ident(d)operator([)integer(2)operator(])operator(.)ident(split)operator(()operator(\)) +keyword(print) predefined(len)operator(()ident(files)operator(\))operator(,) string +ident(tn)operator(.)ident(write)operator(()stringoperator(\)) +keyword(print) ident(tn)operator(.)ident(read_all)operator(()operator(\)) comment(# blocks till eof) + +comment(# @@PLEAC@@_18.7) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_18.8) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_18.9) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_19.0) +comment(# Introduction) +comment(#) +comment(# There is no standard cgi/web framework in python,) +comment(# this is reason for ranting now and then.) +comment(#) +comment(# See `PyWebOff `__) +comment(# which compares CherryPy, Quixote, Twisted, WebWare and Zope) +comment(# Karrigell and print stantements. ) +comment(#) +comment(# Then there is Nevow and Standalone ZPT.) + +comment(# @@PLEAC@@_19.1) +comment(# Partial implementation of PLEAC Python section 19.1) +comment(# Written by Seo Sanghyeon) + +comment(# Standard CGI module is where PERL shines. Python) +comment(# module, cgi, is nothing but a form parser. So it is) +comment(# not really fair to compare these two. But I hesitate) +comment(# to introduce any non-standard module. After all,) +comment(# which one should I choose?) + +comment(# I would stick to simple print statements. I believe) +comment(# the following is close to how these tasks are usually) +comment(# done in Python.) + +comment(#-----------------------------) +comment(#!/usr/bin/env python) +comment(# hiweb - using FieldStorage class to get at form data) + +keyword(import) ident(cgi) +ident(form) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) + +comment(# get a value from the form) +ident(value) operator(=) ident(form)operator(.)ident(getvalue)operator(()stringoperator(\)) + +comment(# print a standard header) +keyword(print) string +keyword(print) + +comment(# print a document) +keyword(print) stringYou typed: %s

)delimiter(")> operator(%) operator(() + ident(cgi)operator(.)ident(escape)operator(()ident(value)operator(\))operator(,) + operator(\)) + +comment(#-----------------------------) +keyword(import) ident(cgi) +ident(form) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) + +ident(who) operator(=) ident(form)operator(.)ident(getvalue)operator(()stringoperator(\)) +ident(phone) operator(=) ident(form)operator(.)ident(getvalue)operator(()stringoperator(\)) +ident(picks) operator(=) ident(form)operator(.)ident(getvalue)operator(()stringoperator(\)) + +comment(# if you want to assure `picks' to be a list) +ident(picks) operator(=) ident(form)operator(.)ident(getlist)operator(()stringoperator(\)) + +comment(#-----------------------------) +comment(# Not Implemented) + +comment(# To implement -EXPIRES => '+3d', I need to study about) +keyword(import) ident(cgi) +keyword(import) ident(datetime) + +ident(time_format) operator(=) string +keyword(print) string operator(%) operator(() + operator(()ident(datetime)operator(.)ident(datetime)operator(.)ident(now)operator(()operator(\)) + operator(+) ident(datetime)operator(.)ident(timedelta)operator(()operator(+)integer(3)operator(\))operator(\))operator(.)ident(strftime)operator(()ident(time_format)operator(\)) + operator(\)) +keyword(print) string operator(%) operator(()ident(datetime)operator(.)ident(datetime)operator(.)ident(now)operator(()operator(\))operator(.)ident(strftime)operator(()ident(time_format)operator(\))operator(\)) +keyword(print) string + +comment(#-----------------------------) +comment(# NOTES) + +comment(# CGI::param(\) is a multi-purpose function. Here I want to) +comment(# note which Python functions correspond to it.) + +comment(# PERL version 5.6.1, CGI.pm version 2.80.) +comment(# Python version 2.2.3. cgi.py CVS revision 1.68.) + +comment(# Assume that `form' is the FieldStorage instance.) + +comment(# param(\) with zero argument returns parameter names as) +comment(# a list. It is `form.keys(\)' in Python, following Python's) +comment(# usual mapping interface.) + +comment(# param(\) with one argument returns the value of the named) +comment(# parameter. It is `form.getvalue(\)', but there are some) +comment(# twists:) + +comment(# 1\) A single value is passed.) +comment(# No problem.) + +comment(# 2\) Multiple values are passed.) +comment(# PERL: in LIST context, you get a list. in SCALAR context,) +comment(# you get the first value from the list.) +comment(# Python: `form.getvalue(\)' returns a list if multiple) +comment(# values are passed, a raw value if a single value) +comment(# is passed. With `form.getlist(\)', you always) +comment(# get a list. (When a single value is passed, you) +comment(# get a list with one element.\) With `form.getfirst(\)',) +comment(# you always get a value. (When multiple values are) +comment(# passed, you get the first one.\)) + +comment(# 3\) Parameter name is given, but no value is passed.) +comment(# PERL: returns an empty string, not undef. POD says this) +comment(# feature is new in 2.63, and was introduced to avoid) +comment(# "undefined value" warnings when running with the) +comment(# -w switch.) +comment(# Python: tricky. If you want black values to be retained,) +comment(# you should pass a nonzero `keep_blank_values' keyword) +comment(# argument. Default is not to retain blanks. In case) +comment(# values are not retained, see below.) + +comment(# 4\) Even parameter name is never mentioned.) +comment(# PERL: returns undef.) +comment(# Python: returns None, or whatever you passed as the second) +comment(# argument, or `default` keyword argument. This is) +comment(# consistent with `get(\)' method of the Python mapping) +comment(# interface.) + +comment(# param(\) with more than one argument modifies the already) +comment(# set form data. This functionality is not available in Python) +comment(# cgi module.) + + +comment(# @@PLEAC@@_19.2) +comment(# enable(\) from 'cgitb' module, by default, redirects traceback) +comment(# to the browser. It is defined as 'enable(display=True, logdir=None,) +comment(# context=5\)'.) + +comment(# equivalent to importing CGI::Carp::fatalsToBrowser.) +keyword(import) ident(cgitb) +ident(cgitb)operator(.)ident(enable)operator(()operator(\)) + +comment(# to suppress browser output, you should explicitly say so.) +keyword(import) ident(cgitb) +ident(cgitb)operator(.)ident(enable)operator(()ident(display)operator(=)pre_constant(False)operator(\)) + +comment(# equivalent to call CGI::Carp::carpout with temporary files.) +keyword(import) ident(cgitb) +ident(cgitb)operator(.)ident(enable)operator(()ident(logdir)operator(=)stringoperator(\)) + +comment(# Python exception, traceback facilities are much richer than PERL's) +comment(# die and its friends. You can use your custom exception formatter) +comment(# by replacing sys.excepthook. (equivalent to CGI::Carp::set_message.\)) +comment(# Default formatter is available as traceback.print_exc(\) in pure) +comment(# Python. In fact, what cgitb.enable(\) does is replacing excepthook) +comment(# to cgitb.handler(\), which knows how to format exceptions to HTML.) + +comment(# If this is not enough, (usually this is enough!\) Python 2.3 comes) +comment(# with a new standard module called 'logging', which is complex, but) +comment(# very flexible and entirely customizable.) + +comment(# @@PLEAC@@_19.3) +comment(#) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# webwhoami - show web users id) +keyword(import) ident(getpass) +keyword(print) string +keyword(print) string operator(%) ident(getpass)operator(.)ident(getuser)operator(()operator(\)) + + + +comment(# STDOUT/ERR flushing) +comment(#) +comment(# In contrast to what the perl cookbook says, modpython.org tells) +comment(# STDERR is buffered too.) + +comment(# @@PLEAC@@_19.4) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_19.5) + +comment(# use mod_python in the Apache web server.) + +comment(# Load the module in httpd.conf or apache.conf) + +ident(LoadModule) ident(python_module) ident(libexec)operator(/)ident(mod_python)operator(.)ident(so) + +operator(<)ident(Directory) operator(/)ident(some)operator(/)ident(directory)operator(/)ident(htdocs)operator(/)ident(test)operator(>) + ident(AddHandler) ident(mod_python) operator(.)ident(py) + ident(PythonHandler) ident(mptest) + ident(PythonDebug) ident(On) +operator(<)operator(/)ident(Directory)operator(>) + +comment(# test.py file in /some/directory/htdocs/test) +keyword(from) ident(mod_python) keyword(import) ident(apache) + +keyword(def) ident(handler)operator(()ident(req)operator(\))operator(:) + ident(req)operator(.)ident(write)operator(()stringoperator(\)) + keyword(return) ident(apache)operator(.)ident(OK) + +comment(# @@PLEAC@@_19.6) + +keyword(import) ident(os) +ident(os)operator(.)ident(system)operator(()string operator(%) operator(()predefined(input)operator(,) stringoperator(.)ident(join)operator(()ident(files)operator(\))operator(\))operator(\)) comment(# UNSAFE) + +comment(# python doc lib cgi-security it says) +comment(#) +comment(# To be on the safe side, if you must pass a string gotten from a form to a shell) +comment(# command, you should make sure the string contains only alphanumeric characters, dashes,) +comment(# underscores, and periods.) +keyword(import) ident(re) +ident(cmd) operator(=) string operator(%) operator(()predefined(input)operator(,) stringoperator(.)ident(join)operator(()ident(files)operator(\))operator(\)) +keyword(if) ident(re)operator(.)ident(search)operator(()stringoperator(,) ident(cmd)operator(\))operator(:) + keyword(print) string + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +ident(os)operator(.)ident(system)operator(()ident(cmd)operator(\)) +ident(trans) operator(=) ident(string)operator(.)ident(maketrans)operator(()ident(string)operator(.)ident(ascii_letters)operator(+)ident(string)operator(.)ident(digits)operator(+)stringoperator(,) + +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_19.7) +comment(#-----------------------------) +comment(# This uses nevow's (http://nevow.com\) stan; there's no standard) +comment(# way to generate HTML, though there are many implementations of) +comment(# this basic idea.) +keyword(from) ident(nevow) keyword(import) ident(tags) keyword(as) ident(T) +keyword(print) ident(T)operator(.)ident(ol)operator([)ident(T)operator(.)ident(li)operator([)stringoperator(])operator(,) ident(T)operator(.)ident(li)operator([)stringoperator(])operator(,) ident(T)operator(.)ident(li)operator([)stringoperator(])operator(]) +comment(#
  1. red
  2. blue
  3. green
) + +ident(names) operator(=) stringoperator(.)ident(split)operator(()operator(\)) +keyword(print) ident(T)operator(.)ident(ul)operator([) operator([)ident(T)operator(.)ident(li)operator(()predefined(type)operator(=)stringoperator(\))operator([)ident(name)operator(]) keyword(for) ident(name) keyword(in) ident(names)operator(]) operator(]) +comment(#
  • Larry
  • Moe
  • ) +comment(#
  • Curly
) +comment(#-----------------------------) +keyword(print) ident(T)operator(.)ident(li)operator([)stringoperator(]) +comment(#
  • alpha
  • ) + +keyword(print) ident(T)operator(.)ident(li)operator([)stringoperator(])operator(,) ident(T)operator(.)ident(li)operator([)stringoperator(]) +comment(#
  • alpha
  • omega
  • ) +comment(#-----------------------------) +ident(states) operator(=) operator({) + stringoperator(:) operator([) stringoperator(,) stringoperator(,) string operator(])operator(,) + stringoperator(:) operator([) stringoperator(,) stringoperator(,) string operator(])operator(,) + stringoperator(:) operator([) stringoperator(,) stringoperator(,) string operator(])operator(,) + stringoperator(:) operator([) stringoperator(,) stringoperator(,) string operator(])operator(,) +operator(}) + +keyword(print) string Cities I Have Known)delimiter(")>operator(;) +keyword(print) ident(T)operator(.)ident(tr)operator([)ident(T)operator(.)ident(th)operator(()stringoperator(\))operator(,) ident(T)operator(.)ident(th)operator(()stringoperator(\))operator(]) +keyword(for) ident(k) keyword(in) predefined(sorted)operator(()ident(states)operator(.)ident(keys)operator(()operator(\))operator(\))operator(:) + keyword(print) ident(T)operator(.)ident(tr)operator([) operator([)ident(T)operator(.)ident(th)operator(()ident(k)operator(\))operator(]) operator(+) operator([)ident(T)operator(.)ident(td)operator(()ident(city)operator(\)) keyword(for) ident(city) keyword(in) predefined(sorted)operator(()ident(states)operator([)ident(k)operator(])operator(\))operator(]) operator(]) +keyword(print) string)delimiter(")>operator(;) +comment(#-----------------------------) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(# ) +comment(#) +comment(#
    Cities I Have Known
    State Cities
    California Berkeley Santa RosaSebastopol
    Colorado Boulder DenverFort Collins
    Texas Austin Fort StocktonPlano
    Wisconsin Lake Geneva MadisonSuperior
    ) +comment(#-----------------------------) +keyword(print) ident(T)operator(.)ident(table)operator([) + operator([)ident(T)operator(.)ident(caption)operator([)stringoperator(])operator(,) + ident(T)operator(.)ident(tr)operator([)ident(T)operator(.)ident(th)operator([)stringoperator(])operator(,) ident(T)operator(.)ident(th)operator([)stringoperator(])operator(]) operator(]) operator(+) + operator([)ident(T)operator(.)ident(tr)operator([) operator([)ident(T)operator(.)ident(th)operator(()ident(k)operator(\))operator(]) operator(+) operator([)ident(T)operator(.)ident(td)operator(()ident(city)operator(\)) keyword(for) ident(city) keyword(in) predefined(sorted)operator(()ident(states)operator([)ident(k)operator(])operator(\))operator(])operator(]) + keyword(for) ident(k) keyword(in) predefined(sorted)operator(()ident(states)operator(.)ident(keys)operator(()operator(\))operator(\))operator(])operator(]) +comment(#-----------------------------) +comment(# salcheck - check for salaries) +keyword(import) ident(MySQLdb) +keyword(import) ident(cgi) + +ident(form) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) + +keyword(if) string keyword(in) ident(form)operator(:) + ident(limit) operator(=) predefined(int)operator(()ident(form)operator([)stringoperator(])operator(.)ident(value)operator(\)) +keyword(else)operator(:) + ident(limit) operator(=) string + +comment(# There's not a good way to start an HTML/XML construct with stan) +comment(# without completing it.) +keyword(print) stringSalary Query)delimiter(')> +keyword(print) ident(T)operator(.)ident(h1)operator([)stringoperator(]) +keyword(print) string)delimiter(')> +keyword(print) ident(T)operator(.)ident(p)operator([)stringoperator(,) + ident(T)operator(.)ident(input)operator(()predefined(type)operator(=)stringoperator(,) ident(name)operator(=)stringoperator(,) ident(value)operator(=)ident(limit)operator(\))operator(]) +keyword(print) ident(T)operator(.)ident(input)operator(()predefined(type)operator(=)stringoperator(\)) +keyword(print) string)delimiter(')> + +keyword(if) ident(limit)operator(:) + ident(dbconn) operator(=) ident(MySQLdb)operator(.)ident(connect)operator(()ident(db)operator(=)stringoperator(,) ident(host)operator(=)stringoperator(,) + ident(port)operator(=)integer(3306)operator(,) ident(user)operator(=)stringoperator(,) + ident(passwd)operator(=)stringoperator(\)) + ident(cursor) operator(=) ident(dbconn)operator(.)ident(cursor)operator(()operator(\)) + ident(cursor)operator(.)ident(execute)operator(()string %s)delimiter(""")>operator(,) operator(()ident(limit)operator(,)operator(\))operator(\)) + + keyword(print) ident(T)operator(.)ident(h1)operator([)stringoperator(]) + keyword(print) string)delimiter(")> + + keyword(for) ident(row) keyword(in) ident(cursor)operator(.)ident(fetchall)operator(()operator(\))operator(:) + keyword(print) ident(T)operator(.)ident(tr)operator([) operator([)ident(T)operator(.)ident(td)operator(()ident(cell)operator(\)) keyword(for) ident(cell) keyword(in) ident(row)operator(]) operator(]) + + keyword(print) string)char(\\n)delimiter(")>operator(;) + ident(cursor)operator(.)ident(close)operator(()operator(\)) + ident(dbconn)operator(.)ident(close)operator(()operator(\)) + +keyword(print) string)delimiter(')> +comment(#-----------------------------) + +comment(# @@PLEAC@@_19.8) +comment(#-----------------------------) +ident(url) operator(=) string +keyword(print) string operator(%) ident(url) +keyword(raise) exception(SystemExit) +comment(#-----------------------------) +comment(# oreobounce - set a cookie and redirect the browser) +keyword(import) ident(Cookie) +keyword(import) ident(time) + +ident(c) operator(=) ident(Cookie)operator(.)ident(SimpleCookie)operator(()operator(\)) +ident(c)operator([)stringoperator(]) operator(=) string +ident(now) operator(=) ident(time)operator(.)ident(time)operator(()operator(\)) +ident(future) operator(=) ident(now) operator(+) integer(3)operator(*)operator(()integer(60)operator(*)integer(60)operator(*)integer(24)operator(*)integer(30)operator(\)) comment(# 3 months) +ident(expire_date) operator(=) ident(time)operator(.)ident(strftime)operator(()stringoperator(,) ident(future)operator(\)) +ident(c)operator([)stringoperator(])operator([)stringoperator(]) operator(=) ident(expire_date) +ident(c)operator([)stringoperator(])operator([)stringoperator(]) operator(=) string + +ident(whither) operator(=) string + +comment(# Prints the cookie header) +keyword(print) string +keyword(print) ident(c) +keyword(print) stringoperator(,) ident(whither) +keyword(print) + +comment(#-----------------------------) +comment(#Status: 302 Moved Temporarily) +comment(#Set-Cookie: filling=vanilla%20cr%E4me; domain=.perl.com;) +comment(# expires=Tue, 21-Jul-1998 11:58:55 GMT) +comment(#Location: http://somewhere.perl.com/nonesuch.html) +comment(#-----------------------------) +comment(# os_snipe - redirect to a Jargon File entry about current OS) +keyword(import) ident(os)operator(,) ident(re) +predefined(dir) operator(=) string +ident(matches) operator(=) operator([) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()stringoperator(,) stringoperator(\))operator(,) + operator(()pre_constant(None)operator(,) stringoperator(\))operator(,) + operator(]) + +keyword(for) ident(regex)operator(,) ident(page) keyword(in) ident(matches)operator(:) + keyword(if) keyword(not) ident(regex)operator(:) comment(# default) + keyword(break) + keyword(if) ident(re)operator(.)ident(search)operator(()ident(regex)operator(,) ident(os)operator(.)ident(environ)operator([)stringoperator(])operator(\))operator(:) + keyword(break) +keyword(print) string operator(%) operator(()predefined(dir)operator(,) ident(page)operator(\)) +comment(#-----------------------------) +comment(# There's no special way to print headers) +keyword(print) string +keyword(print) +comment(#-----------------------------) +comment(#Status: 204 No response) +comment(#-----------------------------) + +comment(# @@PLEAC@@_19.9) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# dummyhttpd - start a HTTP daemon and print what the client sends) + +keyword(import) ident(SocketServer) +comment(# or use BaseHTTPServer, SimpleHTTPServer, CGIHTTPServer) + +keyword(def) ident(adr_str)operator(()ident(adr)operator(\))operator(:) + keyword(return) string operator(%) ident(adr) + +keyword(class) ident(RequestHandler)operator(()ident(SocketServer)operator(.)ident(BaseRequestHandler)operator(\))operator(:) + keyword(def) ident(handle)operator(()pre_constant(self)operator(\))operator(:) + keyword(print) string operator(%) ident(adr_str)operator(()pre_constant(self)operator(.)ident(client_address)operator(\)) + keyword(print) pre_constant(self)operator(.)ident(request)operator(.)ident(recv)operator(()integer(10000)operator(\)) + pre_constant(self)operator(.)ident(request)operator(.)ident(send)operator(()string + string + stringoperator(\)) + pre_constant(self)operator(.)ident(request)operator(.)ident(close)operator(()operator(\)) + + +ident(adr) operator(=) operator(()stringoperator(,) integer(8001)operator(\)) +keyword(print) string)delimiter(")> operator(%) ident(adr_str)operator(()ident(adr)operator(\)) +ident(server) operator(=) ident(SocketServer)operator(.)ident(TCPServer)operator(()ident(adr)operator(,) ident(RequestHandler)operator(\)) +ident(server)operator(.)ident(serve_forever)operator(()operator(\)) +ident(server)operator(.)ident(server_close)operator(()operator(\)) + + +comment(# @@PLEAC@@_19.10) + +keyword(import) ident(Cookie) +ident(cookies) operator(=) ident(Cookie)operator(.)ident(SimpleCookie)operator(()operator(\)) +comment(# SimpleCookie is more secure, but does not support all characters.) +ident(cookies)operator([)stringoperator(]) operator(=) string +keyword(print) ident(cookies) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# ic_cookies - sample CGI script that uses a cookie) + +keyword(import) ident(cgi) +keyword(import) ident(os) +keyword(import) ident(Cookie) +keyword(import) ident(datetime) + +ident(cookname) operator(=) string comment(# SimpleCookie does not support blanks) +ident(fieldname) operator(=) string + +ident(cookies) operator(=) ident(Cookie)operator(.)ident(SimpleCookie)operator(()ident(os)operator(.)ident(environ)operator(.)ident(get)operator(()stringoperator(,)stringoperator(\))operator(\)) +keyword(if) ident(cookies)operator(.)ident(has_key)operator(()ident(cookname)operator(\))operator(:) + ident(favorite) operator(=) ident(cookies)operator([)ident(cookname)operator(])operator(.)ident(value) +keyword(else)operator(:) + ident(favorite) operator(=) string + +ident(form) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) +keyword(if) keyword(not) ident(form)operator(.)ident(has_key)operator(()ident(fieldname)operator(\))operator(:) + keyword(print) string + keyword(print) string + keyword(print) string)delimiter(")> + keyword(print) stringHello Ice Cream)delimiter(")> + keyword(print) string)delimiter(")> + keyword(print) string)delimiter(')> operator(%) operator(() + ident(fieldname)operator(,) ident(favorite) operator(\)) + keyword(print) string)delimiter(")> + keyword(print) string)delimiter(")> + keyword(print) string)delimiter(")> +keyword(else)operator(:) + ident(favorite) operator(=) ident(form)operator([)ident(fieldname)operator(])operator(.)ident(value) + ident(cookies)operator([)ident(cookname)operator(]) operator(=) ident(favorite) + ident(expire) operator(=) ident(datetime)operator(.)ident(datetime)operator(.)ident(now)operator(()operator(\)) operator(+) ident(datetime)operator(.)ident(timedelta)operator(()integer(730)operator(\)) + ident(cookies)operator([)ident(cookname)operator(])operator([)stringoperator(]) operator(=) ident(expire)operator(.)ident(strftime)operator(()stringoperator(\)) + ident(cookies)operator([)ident(cookname)operator(])operator([)stringoperator(]) operator(=) string + keyword(print) string + keyword(print) ident(cookies) + keyword(print) string + keyword(print) string)delimiter(")> + keyword(print) stringHello Ice Cream)delimiter(")> + keyword(print) stringYou chose as your favorite flavor )char(\\")content(%s)char(\\")content(

    )delimiter(")> operator(%) ident(favorite) + keyword(print) string)delimiter(")> + + +comment(# @@PLEAC@@_19.11) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_19.12) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_19.13) +comment(#-----------------------------) +comment(# first open and exclusively lock the file) +keyword(import) ident(os)operator(,) ident(cgi)operator(,) ident(fcntl)operator(,) ident(cPickle) +ident(fh) operator(=) predefined(open)operator(()stringoperator(,) stringoperator(\)) +ident(fcntl)operator(.)ident(flock)operator(()ident(fh)operator(.)ident(fileno)operator(()operator(\))operator(,) ident(fcntl)operator(.)ident(LOCK_EX)operator(\)) + +ident(form) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) +comment(# This doesn't produce a readable file; we copy the environment so) +comment(# that we save a plain dictionary (os.environ is a dictionary-like) +comment(# object\).) +ident(cPickle)operator(.)ident(dump)operator(()operator(()ident(form)operator(,) ident(os)operator(.)ident(environ)operator(.)ident(copy)operator(()operator(\))operator(\)) ident(fh)operator(\)) +ident(fh)operator(.)ident(close)operator(()operator(\)) +comment(#-----------------------------) +keyword(import) ident(cgi)operator(,) ident(smtplib)operator(,) ident(sys) + +ident(form) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) +ident(email) operator(=) string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(]) + +keyword(for) ident(key) keyword(in) ident(form)operator(:) + ident(values) operator(=) ident(form)operator([)ident(key)operator(]) + keyword(if) keyword(not) predefined(isinstance)operator(()ident(values)operator(,) predefined(list)operator(\))operator(:) + ident(value) operator(=) operator([)ident(values)operator(.)ident(value)operator(]) + keyword(else)operator(:) + ident(value) operator(=) operator([)ident(v)operator(.)ident(value) keyword(for) ident(v) keyword(in) ident(values)operator(]) + keyword(for) ident(item) keyword(in) ident(values)operator(:) + ident(email) operator(+=) string operator(%) operator(()ident(key)operator(,) ident(value)operator(\)) + +ident(server) operator(=) ident(smtplib)operator(.)ident(SMTP)operator(()stringoperator(\)) +ident(server)operator(.)ident(sendmail)operator(()ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(,) operator([)stringoperator(])operator(,) ident(email)operator(\)) +ident(server)operator(.)ident(quit)operator(()operator(\)) +comment(#-----------------------------) +comment(# @@INCOMPLETE@@ I don't get the point of these:) +comment(# param("_timestamp", scalar localtime\);) +comment(# param("_environs", %ENV\);) +comment(#-----------------------------) +keyword(import) ident(fcntl)operator(,) ident(cPickle) +ident(fh) operator(=) predefined(open)operator(()stringoperator(,) stringoperator(\)) +ident(fcntl)operator(.)ident(flock)operator(()ident(fh)operator(.)ident(fileno)operator(()operator(\))operator(,) ident(fcntl)operator(.)ident(LOCK_SH)operator(\)) + +ident(count) operator(=) integer(0) +keyword(while) pre_constant(True)operator(:) + keyword(try)operator(:) + ident(form)operator(,) ident(environ) operator(=) ident(cPickle)operator(.)ident(load)operator(()ident(fh)operator(\)) + keyword(except) exception(EOFError)operator(:) + keyword(break) + keyword(if) ident(environ)operator(.)ident(get)operator(()stringoperator(\))operator(.)ident(endswith)operator(()stringoperator(\))operator(:) + keyword(continue) + keyword(if) string keyword(in) ident(form)operator(:) + ident(count) operator(+=) predefined(int)operator(()ident(form)operator([)stringoperator(])operator(.)ident(value)operator(\)) +keyword(print) stringoperator(,) ident(count) +comment(#-----------------------------) + +comment(# @@PLEAC@@_19.14) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_20.1) +comment(#-----------------------------) +keyword(import) ident(urllib) +ident(content) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()ident(url)operator(\))operator(.)ident(read)operator(()operator(\)) + +keyword(try)operator(:) + keyword(import) ident(urllib) + ident(content) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()ident(url)operator(\))operator(.)ident(read)operator(()operator(\)) +keyword(except) exception(IOError)operator(:) + keyword(print) string operator(%) ident(url) + +comment(#-----------------------------) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# titlebytes - find the title and size of documents) +comment(#) +comment(# differences to perl) +comment(# ) +comment(# * no URI::Heuristics) +comment(# * perl LWP supports fetching files from local system) +comment(# * fetching a title from ftp or file doesnt work in perl either.) + +keyword(import) ident(sys)operator(,) ident(urllib2)operator(,) ident(HTMLParser) +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(<=)integer(1)operator(:) + keyword(print) string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +ident(raw_url) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) + +comment(# python has no equivalent to pearls URI::Heuristics, which) +comment(# would do some guessing like :) +comment(#) +comment(# perl -> http://www.perl.com) +comment(# www.oreilly.com -> http://www.oreilly.com) +comment(# ftp.funet.fi -> ftp://ftp.funet.fi) +comment(# /etc/passwd -> file:/etc/passwd) + +comment(# simple but pedantic html parser: tpj.com breaks it.) +keyword(class) ident(html)operator(()ident(HTMLParser)operator(.)ident(HTMLParser)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + ident(HTMLParser)operator(.)ident(HTMLParser)operator(.)ident(__init__)operator(()pre_constant(self)operator(\)) + pre_constant(self)operator(.)ident(_data) operator(=) operator({)operator(}) + pre_constant(self)operator(.)ident(_open_tags) operator(=) operator([)operator(]) + keyword(def) ident(handle_starttag)operator(()pre_constant(self)operator(,) ident(tag)operator(,) ident(attrs)operator(\))operator(:) + pre_constant(self)operator(.)ident(_open_tags)operator(.)ident(append)operator(()ident(tag)operator(\)) + keyword(def) ident(handle_endtag)operator(()pre_constant(self)operator(,) ident(tag)operator(\))operator(:) + keyword(if) predefined(len)operator(()pre_constant(self)operator(.)ident(_open_tags)operator(\))operator(>)integer(0)operator(:) + pre_constant(self)operator(.)ident(_open_tags)operator(.)ident(pop)operator(()operator(\)) + keyword(def) ident(handle_data)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(if) predefined(len)operator(()pre_constant(self)operator(.)ident(_open_tags)operator(\))operator(>)integer(0)operator(:) + pre_constant(self)operator(.)ident(_data)operator([)pre_constant(self)operator(.)ident(_open_tags)operator([)operator(-)integer(1)operator(])operator(]) operator(=) ident(data) + keyword(def) ident(__getattr__)operator(()pre_constant(self)operator(,)ident(attr)operator(\))operator(:) + keyword(if) keyword(not) pre_constant(self)operator(.)ident(_data)operator(.)ident(has_key)operator(()ident(attr)operator(\))operator(:) + keyword(return) string + keyword(return) pre_constant(self)operator(.)ident(_data)operator([)ident(attr)operator(]) + +ident(url) operator(=) ident(raw_url) +keyword(print) string)char(\\n)char(\\t)delimiter(")> operator(%) ident(url)operator(,) +comment(# TODO fake user agent "Schmozilla/v9.17 Platinum") +comment(# TODO referer "http://wizard.yellowbrick.oz") +comment(# as we only do http httplib would do also) +keyword(try)operator(:) + ident(response) operator(=) ident(urllib2)operator(.)ident(urlopen)operator(()ident(url)operator(\)) +keyword(except)operator(:) + keyword(print) string operator(%) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(.)ident(reason)operator([)integer(1)operator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) +comment(# title is not in response) +ident(data) operator(=) ident(response)operator(.)ident(read)operator(()operator(\)) +ident(parser) operator(=) ident(html)operator(()operator(\)) +ident(parser)operator(.)ident(feed)operator(()ident(data)operator(\)) +ident(parser)operator(.)ident(close)operator(()operator(\)) comment(# force processing all data) +ident(count) operator(=) predefined(len)operator(()ident(data)operator(.)ident(split)operator(()stringoperator(\))operator(\)) +predefined(bytes) operator(=) predefined(len)operator(()ident(data)operator(\)) +keyword(print) string operator(%) operator(()ident(parser)operator(.)ident(title)operator(,) + ident(count)operator(,) + predefined(bytes)operator(\)) + +comment(# omly bytes is in response.info(\)) + + +comment(# @@PLEAC@@_20.2) + +comment(# GET method) +keyword(import) ident(httplib) +ident(conn) operator(=) ident(httplib)operator(.)ident(HTTPConnection)operator(()stringoperator(\)) +ident(conn)operator(.)ident(request)operator(()stringoperator(,)stringoperator(\)) +ident(r1) operator(=) ident(conn)operator(.)ident(getresponse)operator(()operator(\)) +ident(content) operator(=) ident(r1)operator(.)ident(read)operator(()operator(\)) + +comment(# POST method) +keyword(import) ident(urllib) +ident(params) operator(=) ident(urllib)operator(.)ident(urlencode)operator(()operator({)stringoperator(:) stringoperator(,) stringoperator(:) integer(1)operator(})operator(\)) +ident(content) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()stringoperator(,) ident(params)operator(\))operator(.)ident(read)operator(()operator(\)) + +comment(# fields must be properly escaped) +comment(# script.cgi?field1?arg=%22this%20isn%27t%20%3CEASY%3E%22) + +comment(# proxies can be taken from environment, or specified) +comment(# as the optional thrid parameter to urlopen.) + +comment(# @@PLEAC@@_20.3) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# xurl - extract unique, sorted list of links from URL) + +keyword(from) ident(HTMLParser) keyword(import) ident(HTMLParser) +keyword(import) ident(urllib) +keyword(from) ident(sets) keyword(import) ident(Set) keyword(as) predefined(set) comment(# not needed in 2.4) +keyword(class) ident(myParser)operator(()ident(HTMLParser)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(url)operator(\))operator(:) + pre_constant(self)operator(.)ident(baseUrl) operator(=) ident(url)operator([)operator(:)ident(url)operator(.)ident(rfind)operator(()stringoperator(\))operator(]) + ident(HTMLParser)operator(.)ident(__init__)operator(()pre_constant(self)operator(\)) + keyword(def) ident(reset)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(urls) operator(=) predefined(set)operator(()operator(\)) + ident(HTMLParser)operator(.)ident(reset)operator(()pre_constant(self)operator(\)) + keyword(def) ident(handle_starttag)operator(()pre_constant(self)operator(,) ident(tag)operator(,) ident(attrs)operator(\))operator(:) + keyword(if) ident(tag) operator(==) stringoperator(:) + keyword(if) ident(attrs)operator([)integer(0)operator(])operator([)integer(0)operator(]) operator(==) stringoperator(:) + keyword(if) ident(attrs)operator([)integer(0)operator(])operator([)integer(1)operator(])operator(.)ident(find)operator(()stringoperator(\)) operator(==) operator(-)integer(1)operator(:) + comment(# we need to add the base URL.) + pre_constant(self)operator(.)ident(urls)operator(.)ident(add)operator(()pre_constant(self)operator(.)ident(baseUrl) operator(+) string operator(+) ident(attrs)operator([)integer(0)operator(])operator([)integer(1)operator(])operator(\)) + keyword(else)operator(:) + pre_constant(self)operator(.)ident(urls)operator(.)ident(add)operator(()ident(attrs)operator([)integer(0)operator(])operator([)integer(1)operator(])operator(\)) +ident(url) operator(=) string +ident(p) operator(=) ident(myParser)operator(()ident(url)operator(\)) +ident(s) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()ident(url)operator(\)) +ident(data) operator(=) ident(s)operator(.)ident(read)operator(()operator(\)) +ident(p)operator(.)ident(feed)operator(()ident(data)operator(\)) +ident(urllist) operator(=) ident(p)operator(.)ident(urls)operator(.)ident(_data)operator(.)ident(keys)operator(()operator(\)) +ident(urllist)operator(.)ident(sort)operator(()operator(\)) +keyword(print) stringoperator(.)ident(join)operator(()ident(urllist)operator(\)) + + + +comment(# @@PLEAC@@_20.4) +comment(# Converting ASCII to HTML) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# text2html - trivial html encoding of normal text) + +keyword(import) ident(sys) +keyword(import) ident(re) + +comment(# precompile regular expressions) +ident(re_quoted) operator(=) ident(re)operator(.)ident(compile)operator(()string.*?\)$)delimiter(")>operator(\)) +ident(re_url) operator(=) ident(re)operator(.)ident(compile)operator(()string)delimiter(")>operator(\)) +ident(re_http) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +ident(re_strong) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) +ident(re_em) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + +comment(# split paragraphs) +keyword(for) ident(para) keyword(in) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\))operator(.)ident(split)operator(()stringoperator(\))operator(:) + comment(# TODO encode entities: dont encode "<>" but do "&") + keyword(if) ident(para)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + keyword(print) string)char(\\n)content(%s)char(\\n)content()delimiter(")> operator(%) ident(para) + keyword(else)operator(:) + ident(para) operator(=) ident(re_quoted)operator(.)ident(sub)operator(()string)delimiter(")>operator(,) ident(para)operator(\)) comment(# quoted text) + ident(para) operator(=) ident(re_url)operator(.)ident(sub)operator(()string)content(\\1)content()delimiter(')>operator(,) ident(para)operator(\)) comment(# embedded URL) + ident(para) operator(=) ident(re_http)operator(.)ident(sub)operator(()string)content(\\1)content()delimiter(')>operator(,) ident(para)operator(\)) comment(# guessed URL) + ident(para) operator(=) ident(re_strong)operator(.)ident(sub)operator(()string)content(\\1)content()delimiter(")>operator(,)ident(para)operator(\)) comment(# this is *bold* here) + ident(para) operator(=) ident(re_em)operator(.)ident(sub)operator(()string)content(\\1)content()delimiter(")>operator(,)ident(para)operator(\)) comment(# this is _italic_ here) + keyword(print) string)char(\\n)content(%s)char(\\n)content(

    )delimiter(")> operator(%) ident(para) comment(# add paragraph tags) + + + +comment(#-----------------------------) +keyword(import) ident(sys)operator(,) ident(re) +keyword(import) ident(htmlentitydefs) + +keyword(def) ident(encode_entities)operator(()ident(s)operator(\))operator(:) + keyword(for) ident(k)operator(,)ident(v) keyword(in) ident(htmlentitydefs)operator(.)ident(codepoint2name)operator(.)ident(items)operator(()operator(\))operator(:) + keyword(if) ident(k)operator(<)integer(256)operator(:) comment(# no unicodes) + ident(s) operator(=) ident(s)operator(.)ident(replace)operator(()predefined(chr)operator(()ident(k)operator(\))operator(,)stringoperator(%)ident(v)operator(\)) + keyword(return) ident(s) + +keyword(print) string)delimiter(")> +ident(text) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(read)operator(()operator(\)) +ident(text) operator(=) ident(encode_entities)operator(()ident(text)operator(\)) +ident(text) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,)stringoperator(,)ident(text)operator(\)) comment(# continuation lines) +ident(text) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) + string)content(\\1)content()content(\\2)content()delimiter(')>operator(,) + ident(text)operator(\))operator(;) +keyword(print) ident(text) +keyword(print) string)delimiter(")> + +comment(# @@PLEAC@@_20.5) +comment(# Converting HTML to ASCII) + +comment(#-----------------------------) +keyword(import) ident(os) +ident(ascii) operator(=) ident(os)operator(.)ident(popen)operator(()string operator(+) ident(filename)operator(\))operator(.)ident(read)operator(()operator(\)) + +comment(#-----------------------------) +keyword(import) ident(formatter) +keyword(import) ident(htmllib) + +ident(w) operator(=) ident(formatter)operator(.)ident(DumbWriter)operator(()operator(\)) +ident(f) operator(=) ident(formatter)operator(.)ident(AbstractFormatter)operator(()ident(w)operator(\)) +ident(p) operator(=) ident(htmllib)operator(.)ident(HTMLParser)operator(()ident(f)operator(\)) +ident(p)operator(.)ident(feed)operator(()ident(html)operator(\)) +ident(p)operator(.)ident(close)operator(()operator(\)) + +comment(# Above is a bare minimum to use writer/formatter/parser) +comment(# framework of Python.) + +comment(# Search Python Cookbook for more details, like writing) +comment(# your own writers or formatters.) + +comment(# Recipe #52297 has TtyFormatter, formatting underline) +comment(# and bold in Terminal. Recipe #135005 has a writer) +comment(# accumulating text instead of printing.) + +comment(# @@PLEAC@@_20.6) + +keyword(import) ident(re) + +ident(plain_text) operator(=) ident(re)operator(.)ident(sub)operator(()string]*>)delimiter(")>operator(,)stringoperator(,)ident(html_text)operator(\)) comment(#WRONG) + +comment(# using HTMLParser) +keyword(import) ident(sys)operator(,) ident(HTMLParser) + +keyword(class) ident(html)operator(()ident(HTMLParser)operator(.)ident(HTMLParser)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + ident(HTMLParser)operator(.)ident(HTMLParser)operator(.)ident(__init__)operator(()pre_constant(self)operator(\)) + pre_constant(self)operator(.)ident(_plaintext) operator(=) string + pre_constant(self)operator(.)ident(_ignore) operator(=) pre_constant(False) + keyword(def) ident(handle_starttag)operator(()pre_constant(self)operator(,) ident(tag)operator(,) ident(attrs)operator(\))operator(:) + keyword(if) ident(tag) operator(==) stringoperator(:) + pre_constant(self)operator(.)ident(_ignore) operator(=) pre_constant(True) + keyword(def) ident(handle_endtag)operator(()pre_constant(self)operator(,) ident(tag)operator(\))operator(:) + keyword(if) ident(tag) operator(==) stringoperator(:) + pre_constant(self)operator(.)ident(_ignore) operator(=) pre_constant(False) + keyword(def) ident(handle_data)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(if) predefined(len)operator(()ident(data)operator(\))operator(>)integer(0) keyword(and) keyword(not) pre_constant(self)operator(.)ident(_ignore)operator(:) + pre_constant(self)operator(.)ident(_plaintext) operator(+=) ident(data) + keyword(def) ident(get_plaintext)operator(()pre_constant(self)operator(\))operator(:) + keyword(return) pre_constant(self)operator(.)ident(_plaintext) + keyword(def) ident(error)operator(()pre_constant(self)operator(,)ident(msg)operator(\))operator(:) + comment(# ignore all errors) + keyword(pass) + +ident(html_text) operator(=) predefined(open)operator(()ident(sys)operator(.)ident(argv)operator([)integer(1)operator(])operator(\))operator(.)ident(read)operator(()operator(\)) + +ident(parser) operator(=) ident(html)operator(()operator(\)) +ident(parser)operator(.)ident(feed)operator(()ident(html_text)operator(\)) +ident(parser)operator(.)ident(close)operator(()operator(\)) comment(# force processing all data) +keyword(print) ident(parser)operator(.)ident(get_plaintext)operator(()operator(\)) + +ident(title_s) operator(=) ident(re)operator(.)ident(search)operator(()string)content(\\s)content(*(.*?\))content(\\s)content(*)delimiter(")>operator(,) ident(text)operator(\)) +ident(title) operator(=) ident(title_s) keyword(and) ident(title_s)operator(.)ident(groups)operator(()operator(\))operator([)integer(0)operator(]) keyword(or) string + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# htitlebytes - get html title from URL) +comment(#) + +keyword(import) ident(sys)operator(,) ident(urllib2)operator(,) ident(HTMLParser) +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(<=)integer(1)operator(:) + keyword(print) string operator(%) ident(sys)operator(.)ident(argv)operator([)integer(0)operator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +comment(# simple but pedantic html parser: tpj.com breaks it.) +keyword(class) ident(html)operator(()ident(HTMLParser)operator(.)ident(HTMLParser)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(\))operator(:) + ident(HTMLParser)operator(.)ident(HTMLParser)operator(.)ident(__init__)operator(()pre_constant(self)operator(\)) + pre_constant(self)operator(.)ident(_data) operator(=) operator({)operator(}) + pre_constant(self)operator(.)ident(_open_tags) operator(=) operator([)operator(]) + keyword(def) ident(handle_starttag)operator(()pre_constant(self)operator(,) ident(tag)operator(,) ident(attrs)operator(\))operator(:) + pre_constant(self)operator(.)ident(_open_tags)operator(.)ident(append)operator(()ident(tag)operator(\)) + keyword(def) ident(handle_endtag)operator(()pre_constant(self)operator(,) ident(tag)operator(\))operator(:) + keyword(if) predefined(len)operator(()pre_constant(self)operator(.)ident(_open_tags)operator(\))operator(>)integer(0)operator(:) + pre_constant(self)operator(.)ident(_open_tags)operator(.)ident(pop)operator(()operator(\)) + keyword(def) ident(handle_data)operator(()pre_constant(self)operator(,) ident(data)operator(\))operator(:) + keyword(if) predefined(len)operator(()pre_constant(self)operator(.)ident(_open_tags)operator(\))operator(>)integer(0)operator(:) + pre_constant(self)operator(.)ident(_data)operator([)pre_constant(self)operator(.)ident(_open_tags)operator([)operator(-)integer(1)operator(])operator(]) operator(=) ident(data) + keyword(def) ident(__getattr__)operator(()pre_constant(self)operator(,)ident(attr)operator(\))operator(:) + keyword(if) keyword(not) pre_constant(self)operator(.)ident(_data)operator(.)ident(has_key)operator(()ident(attr)operator(\))operator(:) + keyword(return) string + keyword(return) pre_constant(self)operator(.)ident(_data)operator([)ident(attr)operator(]) + keyword(def) ident(error)operator(()pre_constant(self)operator(,)ident(msg)operator(\))operator(:) + comment(# ignore all errors) + keyword(pass) + +keyword(for) ident(url) keyword(in) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(:)operator(])operator(:) + keyword(print) string operator(%) ident(url)operator(,) + comment(# TODO fake user agent "Schmozilla/v9.17 Platinum") + comment(# TODO referer "http://wizard.yellowbrick.oz") + comment(# as we only do http httplib would do also) + keyword(try)operator(:) + ident(response) operator(=) ident(urllib2)operator(.)ident(urlopen)operator(()ident(url)operator(\)) + keyword(except)operator(:) + keyword(print) string operator(%) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(]) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + comment(# title is not in response) + ident(parser) operator(=) ident(html)operator(()operator(\)) + ident(parser)operator(.)ident(feed)operator(()ident(response)operator(.)ident(read)operator(()operator(\))operator(\)) + ident(parser)operator(.)ident(close)operator(()operator(\)) comment(# force processing all data) + keyword(print) ident(parser)operator(.)ident(title) + + + +comment(# @@PLEAC@@_20.7) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# churl - check urls) + +keyword(import) ident(sys) + +comment(# head request) +keyword(import) ident(urllib) +keyword(def) ident(valid)operator(()ident(url)operator(\))operator(:) + keyword(try)operator(:) + ident(conn) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()ident(url)operator(\)) + keyword(return) integer(1) + keyword(except)operator(:) + keyword(return) integer(0) + +comment(# parser class as in xurl) +keyword(from) ident(HTMLParser) keyword(import) ident(HTMLParser) +keyword(from) ident(sets) keyword(import) ident(Set) keyword(as) predefined(set) comment(# not needed in 2.4) +keyword(class) ident(myParser)operator(()ident(HTMLParser)operator(\))operator(:) + keyword(def) ident(__init__)operator(()pre_constant(self)operator(,) ident(url)operator(\))operator(:) + pre_constant(self)operator(.)ident(baseUrl) operator(=) ident(url)operator([)operator(:)ident(url)operator(.)ident(rfind)operator(()stringoperator(\))operator(]) + ident(HTMLParser)operator(.)ident(__init__)operator(()pre_constant(self)operator(\)) + keyword(def) ident(reset)operator(()pre_constant(self)operator(\))operator(:) + pre_constant(self)operator(.)ident(urls) operator(=) predefined(set)operator(()operator(\)) + ident(HTMLParser)operator(.)ident(reset)operator(()pre_constant(self)operator(\)) + keyword(def) ident(handle_starttag)operator(()pre_constant(self)operator(,) ident(tag)operator(,) ident(attrs)operator(\))operator(:) + keyword(if) ident(tag) operator(==) stringoperator(:) + keyword(if) ident(attrs)operator([)integer(0)operator(])operator([)integer(0)operator(]) operator(==) stringoperator(:) + keyword(if) ident(attrs)operator([)integer(0)operator(])operator([)integer(1)operator(])operator(.)ident(find)operator(()stringoperator(\)) operator(==) operator(-)integer(1)operator(:) + comment(# we need to add the base URL.) + pre_constant(self)operator(.)ident(urls)operator(.)ident(add)operator(()pre_constant(self)operator(.)ident(baseUrl) operator(+) string operator(+) ident(attrs)operator([)integer(0)operator(])operator([)integer(1)operator(])operator(\)) + keyword(else)operator(:) + pre_constant(self)operator(.)ident(urls)operator(.)ident(add)operator(()ident(attrs)operator([)integer(0)operator(])operator([)integer(1)operator(])operator(\)) + +keyword(if) predefined(len)operator(()ident(sys)operator(.)ident(argv)operator(\))operator(<=)integer(1)operator(:) + keyword(print) string)delimiter(")> operator(%) operator(()ident(sys)operator(.)ident(argv)operator([)integer(0)operator(])operator(\)) + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +ident(base_url) operator(=) ident(sys)operator(.)ident(argv)operator([)integer(1)operator(]) +keyword(print) ident(base_url)operator(+)string +ident(p) operator(=) ident(myParser)operator(()ident(base_url)operator(\)) +ident(s) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()ident(base_url)operator(\)) +ident(data) operator(=) ident(s)operator(.)ident(read)operator(()operator(\)) +ident(p)operator(.)ident(feed)operator(()ident(data)operator(\)) +keyword(for) ident(link) keyword(in) ident(p)operator(.)ident(urls)operator(.)ident(_data)operator(.)ident(keys)operator(()operator(\))operator(:) + ident(state) operator(=) string + keyword(if) ident(link)operator(.)ident(startswith)operator(()stringoperator(\))operator(:) + ident(state) operator(=) string + keyword(if) ident(valid)operator(()ident(link)operator(\))operator(:) + ident(state) operator(=) string + keyword(print) string operator(%) operator(()ident(link)operator(,) ident(state)operator(\)) + + + +comment(# @@PLEAC@@_20.8) +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# surl - sort URLs by their last modification date) + +keyword(import) ident(urllib) +keyword(import) ident(time) +keyword(import) ident(sys) + +ident(Date) operator(=) operator({)operator(}) +keyword(while) integer(1)operator(:) + comment(# we only read from stdin not from argv.) + ident(ln) operator(=) ident(sys)operator(.)ident(stdin)operator(.)ident(readline)operator(()operator(\)) + keyword(if) keyword(not) ident(ln)operator(:) + keyword(break) + ident(ln) operator(=) ident(ln)operator(.)ident(strip)operator(()operator(\)) + keyword(try)operator(:) + ident(u) operator(=) ident(urllib)operator(.)ident(urlopen)operator(()ident(ln)operator(\)) + ident(date) operator(=) ident(time)operator(.)ident(mktime)operator(()ident(u)operator(.)ident(info)operator(()operator(\))operator(.)ident(getdate)operator(()stringoperator(\))operator(\)) + keyword(if) keyword(not) ident(Date)operator(.)ident(has_key)operator(()ident(date)operator(\))operator(:) + ident(Date)operator([)ident(date)operator(]) operator(=) operator([)operator(]) + ident(Date)operator([)ident(date)operator(])operator(.)ident(append)operator(()ident(ln)operator(\)) + keyword(except)operator(:) + ident(sys)operator(.)ident(stderr)operator(.)ident(write)operator(()string operator(%) operator(()ident(ln)operator(,) ident(sys)operator(.)ident(exc_info)operator(()operator(\))operator([)integer(1)operator(])operator(\))operator(\)) + +ident(dates) operator(=) ident(Date)operator(.)ident(keys)operator(()operator(\)) +ident(dates)operator(.)ident(sort)operator(()operator(\)) comment(# python 2.4 would have sorted) +keyword(for) ident(d) keyword(in) ident(dates)operator(:) + keyword(print) string operator(%) operator(()ident(time)operator(.)ident(strftime)operator(()stringoperator(,) ident(time)operator(.)ident(localtime)operator(()ident(d)operator(\))operator(\))operator(,) + stringoperator(.)ident(join)operator(()ident(Date)operator([)ident(d)operator(])operator(\))operator(\)) + + +comment(# @@PLEAC@@_20.9) +keyword(import) ident(re) + +keyword(def) ident(template)operator(()ident(filename)operator(,) ident(fillings)operator(\))operator(:) + ident(text) operator(=) predefined(open)operator(()ident(filename)operator(\))operator(.)ident(read)operator(()operator(\)) + keyword(def) ident(repl)operator(()ident(matchobj)operator(\))operator(:) + keyword(if) ident(fillings)operator(.)ident(has_key)operator(()ident(matchobj)operator(.)ident(group)operator(()integer(1)operator(\))operator(\))operator(:) + keyword(return) predefined(str)operator(()ident(fillings)operator([)ident(matchobj)operator(.)ident(group)operator(()integer(1)operator(\))operator(])operator(\)) + keyword(return) string + comment(# replace quoted words with value from fillings dictionary) + ident(text) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) ident(repl)operator(,) ident(text)operator(\)) + keyword(return) ident(text) + +ident(fields) operator(=) operator({) stringoperator(:)stringoperator(,) stringoperator(:)stringoperator(,) stringoperator(:) stringoperator(}) +keyword(print) ident(template)operator(()stringoperator(,) ident(fields)operator(\)) + +comment(# download the following standalone program) +comment(#!/usr/bin/python) +comment(# userrep1 - report duration of user logins using SQL database) + +keyword(import) ident(MySQLdb) +keyword(import) ident(cgi) +keyword(import) ident(re) +keyword(import) ident(sys) + +keyword(def) ident(template)operator(()ident(filename)operator(,) ident(fillings)operator(\))operator(:) + ident(text) operator(=) predefined(open)operator(()ident(filename)operator(\))operator(.)ident(read)operator(()operator(\)) + keyword(def) ident(repl)operator(()ident(matchobj)operator(\))operator(:) + keyword(if) ident(fillings)operator(.)ident(has_key)operator(()ident(matchobj)operator(.)ident(group)operator(()integer(1)operator(\))operator(\))operator(:) + keyword(return) predefined(str)operator(()ident(fillings)operator([)ident(matchobj)operator(.)ident(group)operator(()integer(1)operator(\))operator(])operator(\)) + keyword(return) string + comment(# replace quoted words with value from fillings dictionary) + ident(text) operator(=) ident(re)operator(.)ident(sub)operator(()stringoperator(,) ident(repl)operator(,) ident(text)operator(\)) + keyword(return) ident(text) + +ident(fields) operator(=) ident(cgi)operator(.)ident(FieldStorage)operator(()operator(\)) +keyword(if) keyword(not) ident(fields)operator(.)ident(has_key)operator(()stringoperator(\))operator(:) + keyword(print) string + keyword(print) string + ident(sys)operator(.)ident(exit)operator(()integer(1)operator(\)) + +keyword(def) ident(get_userdata)operator(()ident(username)operator(\))operator(:) + ident(db) operator(=) ident(MySQLdb)operator(.)ident(connect)operator(()ident(passwd)operator(=)stringoperator(,)ident(db)operator(=)stringoperator(,) ident(user)operator(=)stringoperator(\)) + ident(db)operator(.)ident(query)operator(()string + operator(+)string + operator(+)string operator(%) ident(username)operator(\)) + ident(res) operator(=) ident(db)operator(.)ident(store_result)operator(()operator(\))operator(.)ident(fetch_row)operator(()ident(maxrows)operator(=)integer(1)operator(,)ident(how)operator(=)integer(1)operator(\)) + ident(res)operator([)integer(0)operator(])operator([)stringoperator(]) operator(=) ident(username) + ident(db)operator(.)ident(close)operator(()operator(\)) + keyword(return) ident(res)operator([)integer(0)operator(]) + +keyword(print) string + +keyword(print) ident(template)operator(()stringoperator(,) ident(get_userdata)operator(()ident(fields)operator([)stringoperator(])operator(.)ident(value)operator(\))operator(\)) + +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_20.10) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_20.11) +comment(# @@INCOMPLETE@@) +comment(# @@INCOMPLETE@@) + +comment(# @@PLEAC@@_20.12) + +comment(# sample data, use ``LOGFILE = open(sys.argv[1]\)`` in real life) +ident(LOGFILE) operator(=) operator([) + stringoperator(,) + stringoperator(,) + stringoperator(,) + stringoperator(,) + operator(]) + +keyword(import) ident(re) + +comment(# similar too perl version.) +ident(web_server_log_re) operator(=) ident(re)operator(.)ident(compile)operator(()stringoperator(\)) + +comment(# with group naming.) +ident(split_re) operator(=) ident(re)operator(.)ident(compile)operator(()string)content(\\S)content(+\))content(\\s)content( +)content( (?P)content(\\S)content(+\))content(\\s)content( +)content( (?P)content(\\S)content(+\))content(\\s)content( +)content( )content(\\[)content( +)content( (?P[^:]+\):)content( +)content( (?P