git @ Cat's Eye Technologies Nhohnhehr / 5e7cc98
Make `bits` an instance attribute of BytesIO. Chris Pressey 7 months ago
1 changed file(s) with 13 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
324324
325325
326326 class BytesIO(NhohnhehrIO):
327 def units_in(self, bits=[[]]):
327 def __init__(self):
328 self.bits = [[]]
329
330 def units_in(self):
328331 # get data if necessary
329 if bits[0] == []:
332 if self.bits[0] == []:
330333 i = sys.stdin.read(1)
331334 if (i == ''):
332335 raise IOError() # eof
333336 else:
334 bits[0] = [int(bool(ord(i) & (1 << b))) for b in range(7, -1, -1)]
337 self.bits[0] = [int(bool(ord(i) & (1 << b))) for b in range(7, -1, -1)]
335338
336339 # return data
337 bit = bits[0][0]
338 bits[0] = bits[0][1:]
340 bit = self.bits[0][0]
341 self.bits[0] = self.bits[0][1:]
339342 return bit
340343
341 def units_out(self, bit, bits=[[]]):
342 bits[0].append(bit)
344 def units_out(self, bit):
345 self.bits[0].append(bit)
343346
344347 # if we have 8 bits, output
345 if len(bits[0]) == 8:
346 sys.stdout.write(chr(sum(bits[0][7 - b] << b for b in range(7, -1, -1))))
348 if len(self.bits[0]) == 8:
349 sys.stdout.write(chr(sum(self.bits[0][7 - b] << b for b in range(7, -1, -1))))
347350 sys.stdout.flush()
348 bits[0] = []
351 self.bits[0] = []
349352
350353
351354 def main(argv):