Skip to content

Instantly share code, notes, and snippets.

@EnriqueSoria
Created November 23, 2014 22:14
Show Gist options
  • Save EnriqueSoria/2d08ca8dadc169ad531f to your computer and use it in GitHub Desktop.
Save EnriqueSoria/2d08ca8dadc169ad531f to your computer and use it in GitHub Desktop.
Very simple database for Python
# -*- coding: utf-8 -*-
class DataBase:
'''
Database is a dictionary that is stored and read from a file.
'''
def __init__(self, fileName):
import cPickle as pickle
self.pickle = pickle
self.fileName = fileName
self.data = {}
def load(self):
''' Try to load the data sotred in *fileName*.'''
with open(self.fileName, 'rb') as fh:
obj = self.pickle.load(fh)
self.data = obj
def save(self):
''' Saves the data to a file '''
with open(self.fileName, 'wb') as fh:
self.pickle.dump(self.data, fh, self.pickle.HIGHEST_PROTOCOL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment