Skip to content

Instantly share code, notes, and snippets.

@arjo129
Created February 25, 2019 13:52
Show Gist options
  • Select an option

  • Save arjo129/47d76b7b7e284e50525de273f5913ccc to your computer and use it in GitHub Desktop.

Select an option

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.
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