From d888bdded70224822251163a99c0e73f521e751b Mon Sep 17 00:00:00 2001 From: "Ashok K. Pant" Date: Fri, 13 Jul 2018 15:06:09 +0545 Subject: [PATCH 1/3] Added pickle backend --- pickledb.py | 95 ++++++++++++++++++++++++++++++----------------------- setup.py | 4 +-- 2 files changed, 55 insertions(+), 44 deletions(-) diff --git a/pickledb.py b/pickledb.py index 3a15f0c..ad34ab4 100644 --- a/pickledb.py +++ b/pickledb.py @@ -27,175 +27,186 @@ # THE POSSIBILITY OF SUCH DAMAGE. import os +import pickle import simplejson -def load(location, option): - '''Return a pickledb object. location is the path to the json file.''' - return pickledb(location, option) + +def load(location, option, backend="pickle"): + """Return a pickledb object. location is the path to the backend file.""" + return pickledb(location, option, backend) + class pickledb(object): - def __init__(self, location, option): - '''Creates a database object and loads the data from the location path. - If the file does not exist it will be created on the first update.''' - self.load(location, option) + def __init__(self, location, option, backend="pickle"): + """Creates a database object and loads the data from the location path. + If the file does not exist it will be created on the first update. + :type backend: backend type [pickle, json]""" + self.fsave = option + self.loco = location + self.backend = backend + self.db = {} + self.load(location) - def load(self, location, option): - '''Loads, reloads or changes the path to the db file.''' + def load(self, location): + """Loads, reloads or changes the path to the db file.""" location = os.path.expanduser(location) - self.loco = location - self.fsave = option if os.path.exists(location): self._loaddb() else: - self.db = {} + pass return True def dump(self): - '''Force dump memory db to file.''' + """Force dump memory db to file.""" self._dumpdb(True) return True def set(self, key, value): - '''Set the (string,int,whatever) value of a key''' + """Set the (string,int,whatever) value of a key""" self.db[key] = value self._dumpdb(self.fsave) return True def get(self, key): - '''Get the value of a key''' + """Get the value of a key""" try: return self.db[key] except KeyError: return None def getall(self): - '''Return a list of all keys in db''' + """Return a list of all keys in db""" return self.db.keys() def rem(self, key): - '''Delete a key''' + """Delete a key""" del self.db[key] self._dumpdb(self.fsave) return True def lcreate(self, name): - '''Create a list''' + """Create a list""" self.db[name] = [] self._dumpdb(self.fsave) return True def ladd(self, name, value): - '''Add a value to a list''' + """Add a value to a list""" self.db[name].append(value) self._dumpdb(self.fsave) return True def lextend(self, name, seq): - '''Extend a list with a sequence''' + """Extend a list with a sequence""" self.db[name].extend(seq) self._dumpdb(self.fsave) return True def lgetall(self, name): - '''Return all values in a list''' + """Return all values in a list""" return self.db[name] def lget(self, name, pos): - '''Return one value in a list''' + """Return one value in a list""" return self.db[name][pos] def lrem(self, name): - '''Remove a list and all of its values''' + """Remove a list and all of its values""" number = len(self.db[name]) del self.db[name] self._dumpdb(self.fsave) return number def lpop(self, name, pos): - '''Remove one value in a list''' + """Remove one value in a list""" value = self.db[name][pos] del self.db[name][pos] self._dumpdb(self.fsave) return value def llen(self, name): - '''Returns the length of the list''' + """Returns the length of the list""" return len(self.db[name]) def append(self, key, more): - '''Add more to a key's value''' + """Add more to a key's value""" tmp = self.db[key] self.db[key] = ('%s%s' % (tmp, more)) self._dumpdb(self.fsave) return True def lappend(self, name, pos, more): - '''Add more to a value in a list''' + """Add more to a value in a list""" tmp = self.db[name][pos] self.db[name][pos] = ('%s%s' % (tmp, more)) self._dumpdb(self.fsave) return True def dcreate(self, name): - '''Create a dict''' + """Create a dict""" self.db[name] = {} self._dumpdb(self.fsave) return True def dadd(self, name, pair): - '''Add a key-value pair to a dict, "pair" is a tuple''' + """Add a key-value pair to a dict, "pair" is a tuple""" self.db[name][pair[0]] = pair[1] self._dumpdb(self.fsave) return True def dget(self, name, key): - '''Return the value for a key in a dict''' + """Return the value for a key in a dict""" return self.db[name][key] def dgetall(self, name): - '''Return all key-value pairs from a dict''' + """Return all key-value pairs from a dict""" return self.db[name] def drem(self, name): - '''Remove a dict and all of its pairs''' + """Remove a dict and all of its pairs""" del self.db[name] self._dumpdb(self.fsave) return True def dpop(self, name, key): - '''Remove one key-value pair in a dict''' + """Remove one key-value pair in a dict""" value = self.db[name][key] del self.db[name][key] self._dumpdb(self.fsave) return value def dkeys(self, name): - '''Return all the keys for a dict''' + """Return all the keys for a dict""" return self.db[name].keys() def dvals(self, name): - '''Return all the values for a dict''' + """Return all the values for a dict""" return self.db[name].values() def dexists(self, name, key): - '''Determine if a key exists or not''' + """Determine if a key exists or not""" if self.db[name][key] is not None: return 1 else: return 0 def deldb(self): - '''Delete everything from the database''' - self.db= {} + """Delete everything from the database""" self._dumpdb(self.fsave) return True def _loaddb(self): - '''Load or reload the json info from the file''' - self.db = simplejson.load(open(self.loco, 'rb')) + """Load or reload the db info from the file""" + if self.backend == "json": + self.db = simplejson.load(open(self.loco, 'rb')) + else: + self.db = pickle.load(open(self.loco, 'rb')) def _dumpdb(self, forced): - '''Write/save the json dump into the file''' + """Write/save the db dump into the file""" if forced: - simplejson.dump(self.db, open(self.loco, 'wt')) + if self.backend == "json": + simplejson.dump(self.db, open(self.loco, 'wb')) + else: + pickle.dump(self.db, open(self.loco, 'wb')) diff --git a/setup.py b/setup.py index 272c348..fe8e2b2 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ setup(name = "pickleDB", version="0.6.2", - description="A lightweight and simple database using simplejson.", + description="A lightweight and simple database with pickle or simplejson backend.", author="Harrison Erd", author_email="patx44@gmail.com", license="three-clause BSD", @@ -55,4 +55,4 @@ "Intended Audience :: Developers", "Topic :: Database" ], py_modules=['pickledb'], - install_requires=['simplejson']) + install_requires=['simplejson', 'pickle']) From 2b3f86066a6eb8fe758551b28835185594704f28 Mon Sep 17 00:00:00 2001 From: "Ashok K. Pant" Date: Fri, 13 Jul 2018 15:14:32 +0545 Subject: [PATCH 2/3] README --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 07de9fa..0c61bb9 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,7 @@ pickleDB -------- -pickleDB is lightweight, fast, and simple database based on the `simplejson `_ module. And it's BSD licensed! +pickleDB is lightweight, fast, and simple database with `simplejson `_ or `pickle `_ backend. And it's BSD licensed! pickleDB is Fun From 319fb416e7e06373715ed0ce4f1026bbc5efbba8 Mon Sep 17 00:00:00 2001 From: "Ashok K. Pant" Date: Fri, 13 Jul 2018 15:18:25 +0545 Subject: [PATCH 3/3] Refactored --- pickledb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pickledb.py b/pickledb.py index ad34ab4..26ec501 100644 --- a/pickledb.py +++ b/pickledb.py @@ -28,6 +28,7 @@ import os import pickle + import simplejson @@ -53,8 +54,7 @@ def load(self, location): location = os.path.expanduser(location) if os.path.exists(location): self._loaddb() - else: - pass + return True def dump(self):