-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpistream.py
51 lines (36 loc) · 975 Bytes
/
pistream.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
"""pistream.py
Reads a random file in a loop to represent ongoing data
"""
class PiStream:
def __init__(self, filename="shortpi.bin"):
self.mfile = None
self.filename = filename
self.pos = 0
self.loopcount = 0
def seek(self, offset):
if self.mfile is not None:
self.mfile.seek(offset)
def __enter__(self):
self.mfile = open(self.filename, "rb")
def __exit__(self, type, value, traceback):
try:
self.mfile.close()
except Exception as ex:
print(ex)
def set_position(self, percent):
pass
def getBytes(self, num_bytes=1):
if self.mfile is not None:
bytes = self.mfile.read(num_bytes)
# At end of file? Go back to beginning!
while(len(bytes) < num_bytes):
self.seek(0)
self.loopcount += 1
bytes = bytes + self.mfile.read(num_bytes - len(bytes))
return bytes
else:
raise Exception("PiStream must be used in a with bock")
if __name__ == "__main__":
mpi = PiStream()
with mpi:
print(mpi.getBytes(10))