Skip to content

Instantly share code, notes, and snippets.

@kaanberke
Created February 24, 2020 14:19
Show Gist options
  • Select an option

  • Save kaanberke/8305c4c5e530c9cc190d4a48225bc96d to your computer and use it in GitHub Desktop.

Select an option

Save kaanberke/8305c4c5e530c9cc190d4a48225bc96d to your computer and use it in GitHub Desktop.
A class named Seizer which helps us to create pickle-like files.
import shelve
class Seizer:
def __init__(self, filename, writeback=False):
self.filename = filename
self.writeback = writeback
self.last_get = None
self.last_add = None
self.open()
def __exit__(self):
self.close()
def get_item(self, key):
if key in self.data_file:
self.last_get = {key: self.data_file[key]}
return self.data_file[key]
else:
return None
def add_item(self, key, value):
self.data_file[key] = value
self.last_add = {key: value}
def remove_item(self, key):
del self.data_file[key]
def open(self):
self.data_file = shelve.open(self.filename, self.writeback)
def close(self):
self.data_file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment