Created
February 25, 2019 13:52
-
-
Save arjo129/47d76b7b7e284e50525de273f5913ccc to your computer and use it in GitHub Desktop.
A very simple class to conveniently emulate file operations in memory using python.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MockFile: | |
| def __init__(self): | |
| self.data = [] | |
| self.position = 0 | |
| def seek(self, offset, type=0): | |
| if type == SEEK_END: | |
| self.position = len(self.data) - offset | |
| else: | |
| self.position = offset | |
| def write(self, data): | |
| for d in data: | |
| if len(self.data) > self.position: | |
| self.data[self.position] = d | |
| elif len(self.data) == self.position: | |
| self.data += [d] | |
| self.position += 1 | |
| def read(self, n): | |
| data = self.data[self.position: self.position+n] | |
| if len(data) < n: | |
| raise Exception | |
| self.position += n | |
| return bytes(data) | |
| def tell(self): | |
| return self.position | |
| def get_bytes(self): | |
| return bytes(self.data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment