Created
November 23, 2014 22:14
-
-
Save EnriqueSoria/2d08ca8dadc169ad531f to your computer and use it in GitHub Desktop.
Very simple database for Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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