From bf995fa0e4cf050e6264a557fe94c4f75b7a5a11 Mon Sep 17 00:00:00 2001 From: arkturian Date: Sun, 20 Mar 2016 14:07:10 +0700 Subject: [PATCH 1/6] pythonic class names (PEP-0008) https://www.python.org/dev/peps/pep-0008/#class-names --- pickledb.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pickledb.py b/pickledb.py index 1023ef2..562a28a 100644 --- a/pickledb.py +++ b/pickledb.py @@ -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. From 319d1482538f041b59e2587d580af0a24804fe5e Mon Sep 17 00:00:00 2001 From: arkturian Date: Sun, 20 Mar 2016 14:09:08 +0700 Subject: [PATCH 2/6] move the append function to a more appropriate place. --- pickledb.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pickledb.py b/pickledb.py index 562a28a..b214665 100644 --- a/pickledb.py +++ b/pickledb.py @@ -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: @@ -123,13 +130,6 @@ 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] From c636d0d240dfd3b2d4bdaa945d1e937ded8dbc22 Mon Sep 17 00:00:00 2001 From: arkturian Date: Sun, 20 Mar 2016 14:44:24 +0700 Subject: [PATCH 3/6] new functions: lupdate and lfind. --- pickledb.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pickledb.py b/pickledb.py index b214665..fe3e42e 100644 --- a/pickledb.py +++ b/pickledb.py @@ -137,6 +137,23 @@ def lappend(self, name, pos, 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 dcreate(self, name): '''Create a dict''' self.db[name] = {} From 2aab015b098bc40c3f02db02eb3142c8d822d483 Mon Sep 17 00:00:00 2001 From: arkturian Date: Sun, 20 Mar 2016 15:03:35 +0700 Subject: [PATCH 4/6] new function: ldfind. --- pickledb.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pickledb.py b/pickledb.py index fe3e42e..d93523d 100644 --- a/pickledb.py +++ b/pickledb.py @@ -154,6 +154,15 @@ def lfind(self, name, value): except StopIteration: return -1 + 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(lname) if d[key] == value ) + except StopIteration: + return None + def dcreate(self, name): '''Create a dict''' self.db[name] = {} From 58d659d4712ab0c0ba729f74beddb26553e06f29 Mon Sep 17 00:00:00 2001 From: Arkturian Date: Tue, 10 May 2016 21:51:55 +0700 Subject: [PATCH 5/6] fix a typo --- pickledb.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pickledb.py b/pickledb.py index d93523d..8f3cdd0 100644 --- a/pickledb.py +++ b/pickledb.py @@ -159,7 +159,7 @@ def ldfind(self, name, key, value): 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(lname) if d[key] == value ) + return next( d for d in self.lgetall(name) if d[key] == value ) except StopIteration: return None From 1e050f05d40f4cba121bc8e98152f8eb38d9fc52 Mon Sep 17 00:00:00 2001 From: arkturian Date: Wed, 15 Jun 2016 08:57:40 +0700 Subject: [PATCH 6/6] new method lfind_all A new method lfind_all() that returns a list of all indexes where given value was found in a list. --- pickledb.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pickledb.py b/pickledb.py index 8f3cdd0..8b4e106 100644 --- a/pickledb.py +++ b/pickledb.py @@ -153,6 +153,12 @@ def lfind(self, name, value): 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