Skip to content

Commit fa65541

Browse files
committed
added a bunch of stuff for session 09
1 parent f6786ff commit fa65541

File tree

14 files changed

+893
-81
lines changed

14 files changed

+893
-81
lines changed

Examples/Session08/yield_example.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ def counter():
55
i = i + 1
66
print 'counter: yield', i
77
yield i
8+
return None
89

910

11+
# if __name__ == '__main__':
12+
# print "the generator function:"
13+
# print repr(counter)
14+
# print "call generator function"
1015

11-
if __name__ == '__main__':
12-
print "the generator function:"
13-
print repr(counter)
14-
print "call generator function"
16+
# c = counter()
17+
# print "the generator:"
18+
# print repr(c)
1519

16-
c = counter()
17-
print "the generator:"
18-
print repr(c)
19-
20-
print 'iterate'
21-
for item in c:
22-
print 'received:', item
20+
# print 'iterate'
21+
# for item in c:
22+
# print 'received:', item

Examples/Session09/capitalize/capitalize/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
A really simple module, just to demonstrate disutils
5+
"""
6+
7+
def capitalize(infilename, outfilename):
8+
"""
9+
reads the contents of infilename, and writes it to outfilename, but with
10+
every word capitalized
11+
12+
note: very primitive -- it will mess some files up!
13+
14+
this is called by the capitalize script
15+
"""
16+
infile = open(infilename, 'U')
17+
outfile = open(outfilename, 'w')
18+
19+
for line in infile:
20+
outfile.write( " ".join( [word.capitalize() for word in line.split() ] ) )
21+
outfile.write("\n")
22+
23+
return None

Examples/Session09/capitalize/capitalize/test/__init__.py

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
This is a really simple Text file.
2+
It is here so that I can test the capitalize script.
3+
4+
And that's only there to try out distutils.
5+
6+
So there.
7+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
A really simple script just to demonstrate disutils
5+
"""
6+
7+
import sys, os
8+
from capitalize import capital_mod
9+
10+
11+
if __name__ == "__main__":
12+
try:
13+
infilename = sys.argv[1]
14+
except IndexError:
15+
print "you need to pass in a file to process"
16+
17+
root, ext = os.path.splitext(infilename)
18+
outfilename = root + "_cap" + ext
19+
20+
# do the real work:
21+
print "Capitalizing: %s and storing it in %s"%(infilename, outfilename)
22+
capital_mod.capitalize(infilename, outfilename)
23+
24+
print "I'm done"
25+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
This is about as simple a setup.py as you can have
5+
6+
It installs the capitalize module and script
7+
8+
"""
9+
10+
# classic distutils
11+
#from distutils.core import setup
12+
13+
## uncomment to support "develop" mode
14+
from setuptools import setup
15+
16+
setup(
17+
name='Capitalize',
18+
version='0.1.0',
19+
author='Chris Barker',
20+
py_modules=['capitalize/capital_mod',],
21+
scripts=['scripts/cap_script.py',],
22+
description='Not very useful capitalizing module and script',
23+
)
24+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding: utf-8 -*-
2+
import sys
3+
from StringIO import StringIO
4+
from contextlib import contextmanager
5+
6+
7+
class Context(object):
8+
"""from Doug Hellmann, PyMOTW
9+
http://pymotw.com/2/contextlib/#module-contextlib
10+
"""
11+
def __init__(self, handle_error):
12+
print '__init__(%s)' % handle_error
13+
self.handle_error = handle_error
14+
15+
def __enter__(self):
16+
print '__enter__()'
17+
return self
18+
19+
def __exit__(self, exc_type, exc_val, exc_tb):
20+
print '__exit__(%s, %s, %s)' % (exc_type, exc_val, exc_tb)
21+
return self.handle_error
22+
23+
24+
@contextmanager
25+
def context(boolean):
26+
print "__init__ code here"
27+
try:
28+
print "__enter__ code goes here"
29+
yield object()
30+
except Exception as e:
31+
print "errors handled here"
32+
if not boolean:
33+
raise
34+
finally:
35+
print "__exit__ cleanup goes here"
36+
37+
38+
@contextmanager
39+
def print_encoded(encoding):
40+
old_stdout = sys.stdout
41+
sys.stdout = buff = StringIO()
42+
try:
43+
yield None
44+
finally:
45+
sys.stdout = old_stdout
46+
buff.seek(0)
47+
raw = buff.read()
48+
encoded = raw.encode(encoding)
49+
print encoded

Examples/Session09/decorators.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -*- coding: utf-8 -*-
2+
3+
4+
def substitute(a_function):
5+
"""return a different function than the one supplied"""
6+
def new_function(*args, **kwargs):
7+
return "I'm not that other function"
8+
return new_function
9+
10+
11+
def add(a, b):
12+
print "Function 'add' called with args: %r" % locals()
13+
result = a + b
14+
print "\tResult --> %r" % result
15+
return result
16+
17+
18+
def logged_func(func):
19+
def logged(*args, **kwargs):
20+
print "Function %r called" % func.__name__
21+
if args:
22+
print "\twith args: %r" % (args, )
23+
if kwargs:
24+
print "\twith kwargs: %r" % kwargs
25+
result = func(*args, **kwargs)
26+
print "\t Result --> %r" % result
27+
return result
28+
return logged
29+
30+
31+
def simple_add(a, b):
32+
return a + b
33+
34+
35+
class Memoize:
36+
"""
37+
memoize decorator from avinash.vora
38+
http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
39+
"""
40+
def __init__(self, function): # runs when memoize class is called
41+
self.function = function
42+
self.memoized = {}
43+
44+
def __call__(self, *args): # runs when memoize instance is called
45+
try:
46+
return self.memoized[args]
47+
except KeyError:
48+
self.memoized[args] = self.function(*args)
49+
return self.memoized[args]
50+
51+
52+
@Memoize
53+
def sum2x(n):
54+
return sum(2 * i for i in xrange(n))
55+
56+
import time
57+
58+
59+
def timed_func(func):
60+
def timed(*args, **kwargs):
61+
start = time.time()
62+
result = func(*args, **kwargs)
63+
elapsed = time.time() - start
64+
print "time expired: %s" % elapsed
65+
return result
66+
return timed
67+
68+
@timed_func
69+
@Memoize
70+
def sum2x(n):
71+
return sum(2 * i for i in xrange(n))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python
2+
3+
class C(object):
4+
"""
5+
Property defined in about the most ugly way possible
6+
"""
7+
def __init__(self):
8+
self._x = None
9+
def x(self):
10+
return self._x
11+
x = property(x)
12+
def _set_x(self, value):
13+
self._x = value
14+
x = x.setter(_set_x)
15+
def _del_x(self):
16+
del self._x
17+
x = x.deleter(_del_x)
18+
19+

0 commit comments

Comments
 (0)