Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pickle backend #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added pickle backend
ashokpant committed Jul 13, 2018
commit d888bdded70224822251163a99c0e73f521e751b
95 changes: 53 additions & 42 deletions pickledb.py
Original file line number Diff line number Diff line change
@@ -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'))
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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'])