Skip to content

Commit 7f2291b

Browse files
re-arranged propertied example code
1 parent e019d3d commit 7f2291b

10 files changed

Lines changed: 774 additions & 2 deletions

week-03/code/decorators/p_wrapper.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ def func():
1111
1212
func()
1313
14+
Advanced:
15+
16+
Try using a class to make a decorator that will wrap a
17+
specified tag around a function that returns a string -- i.e:
18+
19+
@tag_wrapper('h1')
20+
def func2(x, y=4, z=2):
21+
return "the sum of %s and %s and %s is %s"%(x, y, z, x+y+z)
22+
23+
>>> print func2(3,4)
24+
<h1>the sum of 3 and 4 and 2 is 9</h1>
25+
26+
1427
"""
1528

1629
# the simple decorator
@@ -21,6 +34,8 @@ def p_wrapper(f):
2134
"""
2235
pass
2336

37+
38+
2439
# give it a try:
2540
if __name__ == "__main__":
2641

@@ -62,3 +77,18 @@ def func2(x, y=4, z=2):
6277
print func2(3, 5)
6378
print func2(3, 5, 7)
6479

80+
81+
# ## and try the class version:
82+
83+
# @tag_wrapper('h1')
84+
# def func2(x, y=4, z=2):
85+
# return "the sum of %s and %s and %s is %s"%(x, y, z, x+y+z)
86+
87+
# print func2(3,4)
88+
89+
# @tag_wrapper('div')
90+
# def func2(x, y=4, z=2):
91+
# return "the sum of %s and %s and %s is %s"%(x, y, z, x+y+z)
92+
93+
# print func2(5,6,7)
94+

week-03/code/decorators/p_wrapper_solution.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,35 @@ def func():
1515

1616
# the simple decorator
1717

18-
def p_wrapper(f):
18+
def p_wrapper(func):
1919
def function(*args, **kwargs):
20-
result = f(*args, **kwargs)
20+
result = func(*args, **kwargs)
2121
return "<p>" + result + "</p>"
2222
return function
2323

24+
25+
# fancy one using a class:
26+
# this lets you make a decorator with some custom input
27+
# the argument to the __init__ sets what tag you want
28+
# this creates a custom decorator
29+
# the __call__ method is the decorator itself.
30+
class tag_wrapper(object):
31+
def __init__(self, tag='p' ):
32+
"""
33+
inititilze the decorator class with the tag you want
34+
"""
35+
self.open_tag = "<%s>"%tag
36+
self.close_tag = "</%s>"%tag
37+
38+
def __call__(self, func, *args, **kwargs):
39+
"""
40+
The actual decorator function.
41+
42+
using lambda - 'cause why not?
43+
"""
44+
return lambda *args, **kwargs: self.open_tag + func(*args, **kwargs) + self.close_tag
45+
46+
2447
# give it a try:
2548
if __name__ == "__main__":
2649

@@ -62,3 +85,18 @@ def func2(x, y=4, z=2):
6285
print func2(3, 5)
6386
print func2(3, 5, 7)
6487

88+
## and try the class version:
89+
90+
@tag_wrapper('h1')
91+
def func2(x, y=4, z=2):
92+
return "the sum of %s and %s and %s is %s"%(x, y, z, x+y+z)
93+
94+
print func2(3,4)
95+
96+
@tag_wrapper('div')
97+
def func2(x, y=4, z=2):
98+
return "the sum of %s and %s and %s is %s"%(x, y, z, x+y+z)
99+
100+
print func2(5,6,7)
101+
102+
File renamed without changes.

week-03/code/decorators/circle_properties_solution.py renamed to week-03/code/properties-etc/circle_properties_solution.py

File renamed without changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
example of a class method
5+
"""
6+
7+
class C(object):
8+
def __init__(self, x, y):
9+
self.x = x
10+
self.y = y
11+
12+
def a_class_method(klass, y):
13+
print "in a_class_method", klass
14+
return klass( y, y**2 )
15+
a_class_method = classmethod(a_class_method)
16+
17+
class C2(C):
18+
pass
19+
20+
21+
if __name__ == "__main__":
22+
23+
c = C(3, 4)
24+
print type(c), c.x, c.y
25+
26+
c2 = C.a_class_method(3)
27+
print type(c2), c2.x, c2.y
28+
29+
c3 = C2.a_class_method(2)
30+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
example code for properties
5+
"""
6+
7+
class C(object):
8+
_x = None
9+
def getx(self):
10+
return self._x
11+
def setx(self, value):
12+
self._x = value
13+
def delx(self):
14+
del self._x
15+
x = property(getx, setx, delx, "docstring")
16+
17+
if __name__ == "__main__":
18+
c = C
19+
c.x = 5
20+
print c.x
21+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
examples of static and class methods
5+
"""
6+
7+
class C(object):
8+
9+
def a_static_method(a, b):
10+
print "in a_static_method"
11+
return a+b
12+
a_static_method = staticmethod(a_static_method)
13+
14+
if __name__ == "__main__":
15+
16+
print C.a_static_method(3,4)
17+
18+
c = C()
19+
20+
print c.a_static_method(4,5)
21+
22+
File renamed without changes.

week-03/presentation-week03.pdf

27.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)