Note that an an entire file can be trivially read as a single integer as follows:
data.n_ = sum(b*256**i for i, b in enumerate(open(filename, 'rb').read()))
Of course this introduces O(n^2) complexity so it's not desirable for large files (though surprisingly quick for smaller chunks). So the above should be seen as revealing the abstraction, not the actual implementation. Also it would be more typical to read a sequence of small chunks.
Performance could be greatly increased using ctypes to directly populate the long int data structure. This removes the n^2 problem.
This model completely hides the byte stream nature of the file. We see a file as a stream of bits. This unusual perspective is central to btypes.
peek(self, n: int) -> int:
'''read the next n bits as an integer, but don't consume'''
consume(self, n: int):
'''consume n bits'''
read(self, n: int) -> int:
'''read the next n bits as an integer and consume the bits'''
Abstraction to read bits from a file.
Note that an an entire file can be trivially read as a single integer as follows:
data.n_ = sum(b*256**i for i, b in enumerate(open(filename, 'rb').read()))
Of course this introduces O(n^2) complexity so it's not desirable for large files (though surprisingly quick for smaller chunks). So the above should be seen as revealing the abstraction, not the actual implementation. Also it would be more typical to read a sequence of small chunks.
Performance could be greatly increased using ctypes to directly populate the long int data structure. This removes the n^2 problem.
This model completely hides the byte stream nature of the file. We see a file as a stream of bits. This unusual perspective is central to btypes.