Skip to content

Commit b36a2e2

Browse files
committed
Finished Session 09 Homework.
1 parent f70019d commit b36a2e2

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
from datetime import datetime
3+
import sys
4+
5+
6+
class Timer(object):
7+
8+
def __init__(self, file_object=sys.stdout):
9+
self.file_object = file_object
10+
11+
def __enter__(self):
12+
self.start = datetime.now()
13+
return self
14+
15+
def __exit__(self, e_type, e_value, e_tb):
16+
elasped = datetime.now() - self.start
17+
output = "This took {:f} seconds.".format(elasped.total_seconds())
18+
try:
19+
self.file_object.write(output)
20+
except AttributeError as e:
21+
raise e
22+
23+
return False

Students/salim/session09/decorator_lab.py

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,13 @@
22

33

44
def p_wrapper(func):
5-
def wrap(string, tag):
6-
return '<{}>{}</{}>'.format(tag, string.strip(), tag)
5+
def wrap(*args, **kwargs):
6+
tag = kwargs.get('tag', 'p') # default tag value is <p>
7+
return '<{}>{}</{}>'.format(tag, args[0].strip(), tag)
78
return wrap
89

910
@p_wrapper
10-
def return_a_string():
11-
return
11+
def return_a_string(string):
12+
return string
1213

13-
return_a_string('this is my string', 'p')
14-
15-
16-
# class example
17-
def logged_func(func):
18-
def logged(*args, **kwargs):
19-
print "Function %r called" % func.__name__
20-
if args:
21-
print "\twith args: %r" % args
22-
if kwargs:
23-
print "\twith kwargs: %r" % kwargs
24-
result = func(*args, **kwargs)
25-
print "\t Result --> %r" % result
26-
return result
27-
return logged
28-
29-
@logged_func
30-
def add(a, b):
31-
return a + b
14+
return_a_string('this is my string')

0 commit comments

Comments
 (0)