-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathtwo_arg_iter.py
72 lines (49 loc) · 1.44 KB
/
two_arg_iter.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
72
import functools
import time
def something():
return 42
def useless_example():
it = iter(something, 42)
print(next(it))
print(next(it))
def two_arg_iter(f, sentinel):
while True:
val = f()
if val == sentinel:
return
yield val
class ChunkedReader:
def __init__(self, f, chunk_size=64):
self.f = f
self.chunk_size = chunk_size
def read_chunk(self):
return self.f.read(self.chunk_size)
def chunk_example():
with open('example.txt', 'rb') as f:
reader = ChunkedReader(f, 4)
for chunk in iter(reader, b''):
print(f"chunk: {chunk}")
def alternate_way_to_chunk_example():
with open('example.txt', 'rb') as f:
for chunk in iter(functools.partial(f.read, 4), b''):
print(f"chunk: {chunk}")
def preferred_way_to_do_chunk_example():
with open('example.txt', 'rb') as f:
while (chunk := f.read(4)) != b'':
print(f"chunk: {chunk}")
def beyond_what_two_arg_iter_can_do():
t0 = time.perf_counter()
while (t := time.perf_counter()) < t0 + 1.:
print(f"it has been {t - t0} seconds")
time.sleep(.1)
def thx():
pass
def main():
# useless_example()
chunk_example()
# alternate_way_to_chunk_example()
# preferred_way_to_do_chunk_example()
# beyond_what_two_arg_iter_can_do()
# async iter does NOT have 2-arg form
if __name__ == '__main__':
main()