forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Generators.py
More file actions
117 lines (85 loc) · 2.62 KB
/
06_Generators.py
File metadata and controls
117 lines (85 loc) · 2.62 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'''
6. Generators
6.1 Iteration Protocol
6.2 Customizing Iteration with Generators
6.3 Producer/Consumer Problems and Workflows
6.4 Generator Expressions
'''
## 6.1 Iteration Protocol
a = 'hello'
for c in a: # Loop over characters in a
print(c)
b = { 'name': 'Dave', 'password':'foo'}
for k in b: # Loop over keys in dictionary
print(k)
for k,v in b.items(): # Loop over keys and values in dictionary
print(k,v)
## using hash function
#https://stackoverflow.com/questions/4567089/hash-function-that-produces-short-hashes
import hashlib
import base64
plaintext1 ="long_namexyz_1234567892"
hash_result1 = hashlib.sha256(plaintext1.encode()).digest()[:16]
hash_result1
hash_result2 = hashlib.shake_256(plaintext1.encode()).hexdigest(4)
hash_result2
#encoded_result = base64.b64decode(hash_result1).decode('UTF-8') #base64.b64encode(hash_result1).decode("utf-8")
#encoded_result
'''
6.2 Customizing Iteration ---- GENERATORS ----- using yield
## Generators -A generator is a function that defines iteration
## A generator is any function that uses the yield statement. Calling a generator function creates a generator object.
## It does not immediately execute the function.
'''
def countdown(n):
while n > 0:
yield n
n -= 1
for x in countdown(10):
print(x, end=' ')
y = countdown(5)
for i in y:
print(i)
# Exercise 6.4: A Simple Generator filematch check
def quickLines(filename):
print(filename)
for line in open(filename,'r'):
print(line, end=' ')
def filematch(filename,substr):
with open(filename,'r') as f:
for line in f:
if substr in line:
yield line
import os
import pathlib
workdir = os.getcwd()
datadir = workdir + r'\work\data'
quickLines(datadir+r'\portfolio.csv')
print('\n ----\n')
for x in filematch(datadir+r'\portfolio.csv','IBM'):
print(x)
########
####### 6.3 Producers, Consumers and Pipelines
######## producer → processing → processing → consumer
## Generator Pipelines
## Exercise 6.8: Setting up a simple pipeline
## producer → processing → processing → consumer
def filematch(lines,substr):
for line in lines:
if substr in line:
yield line
from follow import follow
import os
import time
datadir = os.getcwd() + r'\work\data'
stocklogFile = datadir + r'\stocklog.csv'
lines = follow(stocklogFile) #processing
ibm = filematch(lines,'IBM') #processing
for line in ibm: #consumer
print(line)
##Exercise 6.9: Setting up a more complex pipeline
from follow import follow
import csv
lines = follow(stocklogFile)
for row in csv.reader(lines):
print(row)