Skip to content

Instantly share code, notes, and snippets.

@brettlangdon
Last active January 31, 2020 14:37
Show Gist options
  • Save brettlangdon/9b71d1fd37a60732cefa1c7eebb046f1 to your computer and use it in GitHub Desktop.
Save brettlangdon/9b71d1fd37a60732cefa1c7eebb046f1 to your computer and use it in GitHub Desktop.

Assume we have a file class with a single method called write, which persists bytes to disk:

f = File('/tmp/my/file.txt') f.write("hello world")

b = Buffer(f, bytes=1000) b.write("hello world") b.flush() Write a wrapper class for the file object which allows us to buffer the writes in-memory. The data should be flushed to disk when the buffer is full, or on demand with a method called flush. It should not use more memory than the max bytes allowed.

class BufferedFile(object):

 def __init__(self, file, buffer_size):
     # implement me ...

 def write(self, bytes):
     # implement me ...

 def flush(self):
     # implement me
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment