-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcontext_managers.py
72 lines (49 loc) · 1.85 KB
/
context_managers.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
69
70
71
'''
Challenge: context managers
Let's take our decorators challenge from earlier, and say
we want to implement a lock. Say we're running lots
of my_programs in parallel, and you want to restrict
some functions to only being called once at a time.
Let's say we use a tempfile to hold the lock:
'''
def hold_lock():
filename = tempfile.NamedTemporaryFile(delete=False).name
with open(filename, 'w') as f:
f.write('locked')
return filename
def release_lock(lock_name):
os.remove(lock_name)
'''
Incidentally, you've seen in the Python docs that you're meant
to use "with open" -- that's actually already a context manager.
Building our own might take away some of the air of mystery from it...
So we want to run "hold_lock" before each function starts, and
"release_lock" at the end.
So you could use a decorator, but what if the function
raises an exception? now you have to have a try/except
and it gets yucky...
You might want to try this to get a feel for the ugliness.
But a context manager would be much nicer!
Find out how to write a context manager that calls hold_lock
before a function runs, and calls release_lock at the end,
even if the function raises an exception.
For bonus points: make it so that the user can also write
to the lock file, for debugging purposes, when they're
"inside" the context manager...
'''
from datetime import datetime
def my_program():
main_screen_turn_on()
if somebody_set_us_up_the_bomb():
take_off_every_zig()
def main_screen_turn_on():
print('\n'.join(['*' * 80] * 25))
def somebody_set_us_up_the_bomb():
if datetime.now().microsecond % 7 == 0:
return True
return False
def take_off_every_zig():
for i in range(1, 10001):
if datetime.now().microsecond % 42 == 0:
raise Exception('all your base!')
print('Go {}! '.format(i), end='')