Singularity/lib/db.py

31 lines
746 B
Python

import pickle
from .abc import IOStream, IStream, OStream
class Database:
def __init__(self, fn) -> None:
self.fn = fn
try:
with open(fn, "rb+") as stream_out:
self.data = pickle.load(stream_out)
except:
self.data = {}
def write(self, key, entry):
self.data[key] = entry
def update(self):
with open(self.fn, "rb+") as stream_out:
self.data = pickle.load(stream_out)
def get(self, key, default=None):
return self.data.get(key, default)
def commit(self):
with open(self.fn, "wb+") as stream_in:
pickle.dump(self.data, stream_in)
@classmethod
def connect(cls, fname):
return cls(fname)