From 26b1150214ad539f5fe0a2327a6e688f38341dca Mon Sep 17 00:00:00 2001 From: khamidou Date: Mon, 8 Mar 2010 21:34:53 +0100 Subject: [PATCH] first commit --- README | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ cabinet.py | 27 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 README create mode 100644 cabinet.py diff --git a/README b/README new file mode 100644 index 0000000..a4f0c67 --- /dev/null +++ b/README @@ -0,0 +1,48 @@ +Cabinet is a very-thin layer other berkeley db. It serializes python objects to json using simplejson and then saves them to a bsddb backend. +The library is really simple (at the moment it's less than 30 loc!) so don't hesitate to modify it and send me back patches! + +Example session +================ + +import cabinet + +cab = cabinet.Cabinet("mydb.db") +cab["lou"] = {"name" : "lou", "surname": "reed", "albums" : ["metal machine music"]} + +# Save the current state of the database : +cab.sync() + +print cab["lou"] + +# That's it. + +Copying +======= + +Cabinet, Copyright (c) 2010 Karim Hamidou +All rights reserved. +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + Neither the name of the author nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 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. diff --git a/cabinet.py b/cabinet.py new file mode 100644 index 0000000..c6aaba0 --- /dev/null +++ b/cabinet.py @@ -0,0 +1,27 @@ +# A very thin layer other berkeley db - it mostly serializes JSON back and forth. + +import bsddb +try: + import json +except ImportError: + import simplejson as json + + +class Cabinet(object): + def __init__(self, filename, *args): + self.db = bsddb.hashopen(filename) + + def __getitem__(self, key): + if self.db.has_key(key): + return json.loads(self.db[key]) + else: + raise KeyError + + def __setitem__(self, key, item): + self.db[key] = json.dumps(item) + + def __delitem__(self, key): + del self.db[key] + + def sync(self): + self.db.sync() -- 2.43.0