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

new methods: lfind, lfind_all, ldfind, lupdate + PEP 0008 #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
50 changes: 41 additions & 9 deletions pickledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

def load(location, option):
'''Return a pickledb object. location is the path to the json file.'''
return pickledb(location, option)
return PickleDB(location, option)

class pickledb(object):
class PickleDB(object):

def __init__(self, location, option):
'''Creates a database object and loads the data from the location path.
Expand Down Expand Up @@ -62,6 +62,13 @@ def set(self, key, value):
self._dumpdb(self.fsave)
return True

def append(self, key, more):
'''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 get(self, key):
'''Get the value of a key'''
try:
Expand Down Expand Up @@ -123,20 +130,45 @@ def llen(self, name):
'''Returns the length of the list'''
return len(self.db[name])

def append(self, key, more):
'''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'''
tmp = self.db[name][pos]
self.db[name][pos] = ('%s%s' % (tmp, more))
self._dumpdb(self.fsave)
return True

def lupdate(self, name, seq):
'''Remove the list, create a new list with the same name
and populate it with the sequence'''
self.lrem(name)
self.lcreate(name)
self.lextend(name, seq)
self._dumpdb(self.fsave)
return True

def lfind(self, name, value):
'''Returns the lowest index in a list where value is found,
returns -1 if it wasn't found. Works just like str.find()'''
try:
return next( i for i,el in enumerate(self.db[name]) if el == value )
except StopIteration:
return -1

def lfind_all(self, name, value):
'''Returns a list of all indexes where `value` is found in a list,
returns `None` if `value` wasn't found.'''
indexes = [ i for i,el in enumerate(self.db[name]) if el == value ]
return indexes if len(indexes)>0 else None

def ldfind(self, name, key, value):
'''In a list of dictionaries, find and return the first dict
that has dict[key] == value, otherwise return None.
Example: ldfind(name="famous_quotes", key="id", value=100)'''
try:
return next( d for d in self.lgetall(name) if d[key] == value )
except StopIteration:
return None

def dcreate(self, name):
'''Create a dict'''
self.db[name] = {}
Expand Down