Skip to content

Commit 07c3022

Browse files
authored
Update II Context Managers.py
1 parent cc5a605 commit 07c3022

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

Writing Functions in Python/II Context Managers.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,86 @@
22
If you've ever seen the "with" keyword in Python and wondered what its deal was, then this is the chapter for you! Context managers
33
are a convenient way to provide connections in Python and guarantee that those connections get cleaned up when you are done using them.
44
This chapter will show you how to use context managers, as well as how to write your own.
5+
6+
Context manager
7+
==============
8+
- with open: when you were done reading the text, the context manager closed the file for you.
9+
10+
>>>>>>>>>> with <context-manager>(<args>) as <variable-name>:
11+
.... # Run code here, is inside context
12+
13+
--- sets up context ---runs code ---removes context
14+
15+
real world eg
16+
=============
17+
18+
++
19+
with open('my_text_file.txt') as my_file:
20+
text = my_file.read()
21+
lenght = len(text)
22+
print('the file is {} chars long'.format(lenght))
23+
24+
# print statement is outside so when task is done, it gets cleaned
25+
********************************************************************************************************************************************"""
26+
## The number of cats
27+
28+
# Open "alice.txt" and assign the file to "file"
29+
""" Count the times word cat is used in the file"""
30+
with open('alice.txt') as file:
31+
text = file.read()
32+
33+
n = 0
34+
for word in text.split():
35+
if word.lower() in ['cat', 'cats']:
36+
n += 1
37+
38+
print('Lewis Carroll uses the word "cat" {} times'.format(n))
39+
#``````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
40+
## The speed of cats
41+
42+
image = get_image_from_instagram()
43+
44+
# Time how long process_with_numpy(image) takes to run
45+
with timer():
46+
print('Numpy version')
47+
process_with_numpy(image)
48+
49+
# Time how long process_with_pytorch(image) takes to run
50+
with timer():
51+
print('Pytorch version')
52+
process_with_pytorch(image)
53+
"""Numpy version
54+
Processing..........done!
55+
Elapsed: 1.53 seconds
56+
Pytorch version
57+
Processing..........done!
58+
Elapsed: 0.33 seconds"""
59+
60+
"""!!!
61+
pytorch version is faster,timer() is a context manager that does not return a value, so the as <variable name> at the end of the with statement isn't necessary"""
62+
63+
"""********************************************************************************************************************************************
64+
Writing context managers
65+
=========================
66+
67+
@ Create context manager
68+
----------------------
69+
---define funtion ---set up code ---yield ---optional:add teardown code ---add decorator:@contextlib.contextmanager
70+
71+
yield: is like return
72+
73+
++
74+
@contextlib.contextmanager
75+
def my_context():
76+
print('hello')
77+
yield 42
78+
print('goodbye')
79+
80+
with my_context() as foo:
81+
print('foo is {}'.format(foo))
82+
83+
# hello
84+
# foos is 42
85+
# goodbye
86+
++
587
********************************************************************************************************************************************"""

0 commit comments

Comments
 (0)