-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorate.py
More file actions
67 lines (54 loc) · 1.3 KB
/
Copy pathdecorate.py
File metadata and controls
67 lines (54 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def uppercase_decorator(function):
def wrapper():
func = function()
make_uppercase = func.upper()
return make_uppercase
return wrapper
def say_hi():
return 'hello there'
say_hi = uppercase_decorator(say_hi)
print(say_hi())
# Following is an alternate way of writing it which does the same thing
# by just putting the @ symbol with the name of the decorator
# (metafunction) on line before the function
@uppercase_decorator
def say_hi():
return 'hello there'
print(say_hi())
#%%
from datetime import datetime
def log_datetime(func):
'''Log the date and time of a function'''
def wrapper():
print(f'Function: {func.__name__}\nRun on: {datetime.today().strftime("%Y-%m-%d %H:%M:%S")}')
print(f'{"-"*30}')
func()
return wrapper
@log_datetime
def daily_backup():
print('Daily backup job has finished.')
daily_backup()
#%%
def count(aClass):
aClass.numInstances = 0
print(aClass.numInstances)
return aClass
@count
class Spam:
def __init__(self):
print('New spam object created...')
Spam.numInstances = Spam.numInstances+1
print(Spam.numInstances)
@count
class Other:
pass
print('==========')
a = Spam()
b = Spam()
c = Spam()
d = Spam()
count(Spam)
aa = Other()
bb = Other()
count(aa)
count(Other)