001: import sys
002: sys.dont_write_bytecode=True 
003: import math
004: from go import *
005: 
006: def say(x):
007:   sys.stdout.write("["+x+"]")
008: 
009: 
010: class yourFirstClass:
011:   def __init__(self,x) :
012:     self.mine = x
013:   def x(self):
014:     return self.mine
015:   def setX(self,new):
016:     self.mine=new
017: 
018: @go
019: def _class1():
020:   "example of set, get, init of a class"
021:   y = yourFirstClass("morgantown")
022:   y.setX("fairmont")
023:   y.mine="clarksburg"
024:   print y.x()
025: 
026: def f1(n) :
027:    return n if n < 2 else \
028:             f1(n-2) + f1(n-1)
029: 
030: #@go
031: def _f1() : print 32, f1(32)
032: 
033: def f(n):
034:     a, b = 0, 1
035:     for i in range(0, n):
036:         a, b = b, a + b
037:         print a,b
038:     return a
039: 
040: 
041: 
042: @go
043: def _i1():
044:   "counting"
045:   for i in xrange(5,16,3):
046:     print i
047: 
048: @go
049: def _i2():
050:   "iterating"
051:   for x in ["m","f","c"]:
052:     print x
053: 
054: y ={"a":10,"b":20,"c":30}
055: 
056: @go
057: def _dict1():
058:   "accessing a dictionary"
059:   print y["c"]
060: 
061: @go
062: def _sorteddic():
063:   "example of walking a dictinary in key sort order"
064:   for key in sorted(y.keys()):
065:     print ":key",key,":val:",y[key]
066: 
067: @go
068: def _dict2():
069:   "accessing keys and values"
070:   for k,v in y.iteritems():
071:     print ":key new",k,":val:",v
072: 
073: 
074: def odd(x) : return x % 2 == 1
075: 
076: @go
077: def _odd(): print odd(2), odd(21)
078: 
079: def largeOdd(x) : return odd(x) and x > 10
080: 
081: @go
082: def _largeOdd(): return largeOdd(11)
083: 
084: @go
085: def lcomp():
086:   "example of list comprehension"
087:   print [x/3.0 for x in xrange(60,110,3) if largeOdd(x)]
088: 
089: def myOdds() :
090:   for x in xrange(60,110,3):
091:     if odd(x):
092:       yield(x)
093: 
094: @go
095: def _iterate():
096:   "example of defining your own iterator"
097:   for y in myOdds():
098:     print y
099:     if y> 90:
100:       break
101:     
102: root = math.sqrt
103: 
104: x=2
105: 
106: @go
107: def _x():
108:   "example of global"
109:   print x
110: 
111: def fred(y,x=100,
112:          z=2,pretty = lambda x: int(x)):
113:   "e.g.1) var scope; 2) code as data"
114:   print y+z
115:   print "x",x
116:   print "pretty",pretty(y+z)
117:   print root(100)
118: 
119: @go
120: def _fred(): fred(20)
121: 
122: @go
123: def _fred1():
124:   "example of passing code as data"
125:   fred(22.99,pretty = lambda x: x)
126: 
127:     
128: @go
129: def _string1(fruit="banana"):
130:   index = 0 
131:   while index < len(fruit): 
132:     letter = fruit[index] 
133:     say(letter) 
134:     index = index + 1 
135: 
136: 
137: import string
138: #@go
139: def _string2():
140:   "finds 'na' at '2' (i.e. third arg)"
141:   print string.find("banana", "na")
142: 
143: #@go
144: def _string3():
145:   "as above, but starts at 3"
146:   print string.find("banana", "na", 3)
147: 
148: #@go
149: def _string4():
150:   "fails: specificed range has no 'na'"
151:   print string.find("banana", "na", 2,3)
152: 
153: #@go
154: def _whites():
155:   print "[" + string.whitespace+"]"
156:   for ch in string.whitespace:
157:     print ord(ch),"["+ch+"]"
158: 
159: #@go
160: def _lowers():
161:   print string.lowercase
162: 
163: #@go
164: def isLower1(ch="a"):
165:   print string.find(string.lowercase, ch) != -1
166: 
167: #@go 
168: def isLower2(ch="b"):
169:   print ch in string.lowercase
170: 
171: #@go
172: def isLower3(ch="c",banner="hekko there"):
173:   print banner,'a' <= ch <= 'z'
174: 
175: #@go
176: def _test1():
177:   isLower3(banner="work more on python",ch="D")
178: 
179: #@go
180: def _slice():
181:   a=[1,2,3,4]
182:   print 0,a[0]
183:   print 1,a[1:]
184:   print 2,a[:2]
185:   print 1,3,a[1:3]
186:   print -1,a[-1]
187:     
188: #@go
189: def _alias():
190:   a = [1, 2, 3]
191:   b = a
192:   a[0]=100
193:   print a,b
194:   
195: #@go
196: def _clones():
197:   a = [1, 2, 3]
198:   b = a[:]
199:   a[0]=100
200:   print a,b
201:     
202: @go
203: def _dict():
204:   inventory = {'apples': 430,
205:                'bananas': 312, 'oranges': 525,
206:                'pears': 217}
207:   print "\n:all",inventory
208:   del inventory['pears']
209:   print "\n:less",inventory
210:   print "\n:keys",inventory.keys()
211:   print "\n:values",inventory.values()
212:   print "\n:parts",inventory.items()
213:   print "\n:iterate"
214:   for key,value in inventory.items():
215:     print ":key",key,":value",value
216:     
217: #@go
218: def _counts():
219:   seen = {}
220:   for letter in "Mississippi":
221:     seen[letter] = seen.get(letter, 0) + 1
222:     print seen
223: 
224: import copy
225: 
226: class Holds(dict):
227:   """Dictionary with a default value for unknown keys."""
228:   def __init__(self, default):
229:     self.default = default
230:         
231:   def __getitem__(self, key):
232:     if key in self: return self.get(key)
233:     return self.setdefault(key, copy.deepcopy(self.default))    
234: 
235: def args(*args):
236:    print args
237: 
238: #@go
239: def _args() : print args(1,2,3,4,5)
240: 
241: def memo(f):
242:   memo = {}
243:   def wrapper(*args):
244:     if args in memo:
245:       return memo[args]
246:     else:
247:       tmp = f(*args)
248:       memo[args] = tmp
249:     return tmp
250:   return wrapper
251: 
252: @memo
253: def f2(n) :
254:     return n if n < 2 else f2(n-2) + f2(n-1)
255:   
256: @go
257: def _f2() : print 320,f2(320)
258: 
259: 
260: @go
261: def _holds1():
262:   d = Holds(0)
263:   d['hello'] += 1
264:   print d
265: 
266: #@go
267: def _holds2():
268:   d2 = Holds([])
269:   d2[1].append('hello')
270:   d2[2].append('world')
271:   d2[1].append('there')
272:   print d2
273: 
274: @go
275: def _bigrams(words='i am what i am'.split()):
276:   "Counts of word pairs, in a dict of dicts."
277:   d = Holds(Holds(0))
278:   pairs=zip([None] + words, words + [None])
279:   for (w1, w2) in pairs:
280:     d[w1][w2] += 1
281:     print pairs
282:     print d
283: 
284: # d[w1][w2] += 1 looks better than
285: # d.setdefault(w1,{}).setdefault(w2, 0); d[w1][w2] += 1
286: