Skip to content

Commit

Permalink
graveldb: assign default per-property
Browse files Browse the repository at this point in the history
  • Loading branch information
zielmicha committed Jul 15, 2013
1 parent 3269ae8 commit 0b1510f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dbtest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import graveldb

class Screw(graveldb.Table('Screw', '/tmp')):
default = lambda self: graveldb.Object(size=1)
default = dict(size=1)

s = Screw('foo')
with s:
Expand Down
10 changes: 8 additions & 2 deletions graveldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ def Table(name, path):
class _Table(object):
def __init__(self, name):
self.name = name
self.data = self.table.get(name, self.default())
self.data = self.table.get(name, Object())
self.setup()

def setup(self):
for k, v in self.default.items():
if not hasattr(self.data, k):
setattr(self.data, k, v)

def save(self):
self.table[self.name] = self.data
Expand All @@ -30,7 +36,7 @@ def __enter__(self):
def __exit__(self, *args):
self.table.unlock_all()

default = lambda self: Object()
default = {}

class Object(object):
def __init__(self, **kwargs):
Expand Down

0 comments on commit 0b1510f

Please sign in to comment.