diff --git a/pickledb.py b/pickledb.py index 160b0d5..c45db28 100644 --- a/pickledb.py +++ b/pickledb.py @@ -29,8 +29,7 @@ # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - +import atexit import sys import os import signal @@ -56,6 +55,8 @@ def __init__(self, location, auto_dump, sig): if sig: self.set_sigterm_handler() + atexit.register(self._autodumpdb) + def __getitem__(self, item): '''Syntax sugar for get()''' return self.get(item) @@ -68,6 +69,10 @@ def __delitem__(self, key): '''Sytax sugar for rem()''' return self.rem(key) + def __contains__(self, key): + '''Sytax sugar for exists()''' + return self.exists(key) + def set_sigterm_handler(self): '''Assigns sigterm_handler for graceful shutdown during dump()''' def sigterm_handler(): @@ -89,10 +94,12 @@ def load(self, location, auto_dump): def dump(self): '''Force dump memory db to file''' - json.dump(self.db, open(self.loco, 'wt')) + json.dump(self.db, open(self.loco, 'wt'), ensure_ascii=False, indent=4) self.dthread = Thread( target=json.dump, - args=(self.db, open(self.loco, 'wt'))) + args=(self.db, open(self.loco, 'wt')), + kwargs=dict(ensure_ascii=False, indent=4) + ) self.dthread.start() self.dthread.join() return True diff --git a/tests.py b/tests.py index 7db5a6d..dddd94b 100644 --- a/tests.py +++ b/tests.py @@ -26,6 +26,10 @@ def test_sugar_rem(self): del self.db["foo"] assert "foo" not in self.db.db + def test_sugar_exists(self): + self.db["foo"] = "bar" + assert "foo" in self.db + def test_set(self): self.db.set('key', 'value') x = self.db.get('key')