-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcatification.py
68 lines (52 loc) · 1.73 KB
/
catification.py
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
68
# Anjana Vakil
# "Using and abusing Python's double-underscore methods and attributes"
# EuroPython 2016
#
# License: Don't use this code for anything, ever! :)
# (But if you do, give credit where credit is due.)
import time
## A boring normal function with a docstring
def add(spam, eggs):
'''A boring function that sums two integers.'''
return spam + eggs
## Hacking with function object dunders!
def catify(fn):
'''Hacks a function to be more feline'''
def moar_tuna(*args):
print("give meow moar tuna plz!")
return "pu" + "r" * sum(args)
fn.__doc__ = "meow!"
fn.__code__ = moar_tuna.__code__
## Context manager to be used with `with`
class CatsInChargeOf:
def __init__(self, fn):
'''Just hanging on to some boring data
in case we need it later...
'''
self.boring_human_fn = fn
self.boring_code = fn.__code__
self.boring_doc = fn.__doc__
def __enter__(self):
'''Called when we enter `with` block'''
self.reign_begins = time.time()
catify(self.boring_human_fn)
return self
def __exit__(self, *exc):
'''Called when we exit the `with` block,
even if there was an exception `exc`.
'''
self.reign_ends = time.time()
self.boring_human_fn.__doc__ = self.boring_doc
self.boring_human_fn.__code__ = self.boring_code
print("\nFeline rule ended after {:.2f} glorious seconds"
.format(self.reign_ends - self.reign_begins))
if __name__ == "__main__":
# add works normally...
add(2,3)
# then along come the cats...
with CatsInChargeOf(add):
add(2,3)
add(4,5)
add(6,7)
# but once they've left, all is normal!
add(2,3)