Skip to content

Commit 6d8eb8a

Browse files
author
Mike Driscoll
committed
added code examples for chapters 13-17
1 parent a5d1cc6 commit 6d8eb8a

29 files changed

+426
-0
lines changed

chapter12_unicode/README.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The examples in chapter 12 don't translate into good examples and can be easily
2+
copied and pasted from the book into your interpreter.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# memo_prof.py
2+
@profile
3+
def mem_func():
4+
lots_of_numbers = list(range(1500))
5+
x = ['letters'] * (5 ** 10)
6+
del lots_of_numbers
7+
return None
8+
9+
if __name__ == '__main__':
10+
mem_func()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# profhooks.py
2+
from profilehooks import profile
3+
4+
5+
@profile
6+
def mem_func():
7+
lots_of_numbers = list(range(1500))
8+
x = ['letters'] * (5 ** 10)
9+
del lots_of_numbers
10+
return None
11+
12+
if __name__ == '__main__':
13+
mem_func()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# profhooks2.py
2+
from profilehooks import timecall
3+
4+
@timecall
5+
def mem_func():
6+
lots_of_numbers = list(range(1500))
7+
x = ['letters'] * (5 ** 10)
8+
del lots_of_numbers
9+
return None
10+
11+
if __name__ == '__main__':
12+
mem_func()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# silly_functions.py
2+
import time
3+
4+
@profile
5+
def fast_function():
6+
print("I'm a fast function!")
7+
8+
@profile
9+
def slow_function():
10+
time.sleep(2)
11+
print("I'm a slow function")
12+
13+
if __name__ == '__main__':
14+
fast_function()
15+
slow_function()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# simple_func.py
2+
def my_function():
3+
try:
4+
1 / 0
5+
except ZeroDivisionError:
6+
pass
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def my_function():
2+
try:
3+
1 / 0
4+
except ZeroDivisionError:
5+
pass
6+
7+
if __name__ == "__main__":
8+
import timeit
9+
setup = "from __main__ import my_function"
10+
print(timeit.timeit("my_function()", setup=setup))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random
2+
import time
3+
4+
class MyTimer():
5+
6+
def __init__(self):
7+
self.start = time.time()
8+
9+
def __enter__(self):
10+
return self
11+
12+
def __exit__(self, exc_type, exc_val, exc_tb):
13+
end = time.time()
14+
runtime = end - self.start
15+
msg = 'The function took {time} seconds to complete'
16+
print(msg.format(time=runtime))
17+
18+
19+
def long_runner():
20+
for x in range(5):
21+
sleep_time = random.choice(range(1,5))
22+
time.sleep(sleep_time)
23+
24+
25+
if __name__ == '__main__':
26+
with MyTimer():
27+
long_runner()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import random
2+
import time
3+
4+
def timerfunc(func):
5+
"""
6+
A timer decorator
7+
"""
8+
def function_timer(*args, **kwargs):
9+
"""
10+
A nested function for timing other functions
11+
"""
12+
start = time.time()
13+
value = func(*args, **kwargs)
14+
end = time.time()
15+
runtime = end - start
16+
msg = "The runtime for {func} took {time} seconds to complete"
17+
print(msg.format(func=func.__name__,
18+
time=runtime))
19+
return value
20+
return function_timer
21+
22+
23+
@timerfunc
24+
def long_runner():
25+
for x in range(5):
26+
sleep_time = random.choice(range(1,5))
27+
time.sleep(sleep_time)
28+
29+
if __name__ == '__main__':
30+
long_runner()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from cryptography.fernet import Fernet
2+
3+
cipher_key = Fernet.generate_key()
4+
print(cipher_key)
5+
6+
cipher = Fernet(cipher_key)
7+
text = b'My super secret message'
8+
encrypted_text = cipher.encrypt(text)
9+
print(encrypted_text)
10+
11+
decrypted_text = cipher.decrypt(encrypted_text)
12+
print(decrypted_text)

0 commit comments

Comments
 (0)