def check_nKeys(self): "check numeric keys" idx = IdxDict() idx[0] = "a" idx[1] = "b" assert idx.keys() == [0, 1], \ "numeric keys are wrong: %s" % idk.keys()
def check_lshift(self): idx = IdxDict() idx << "x" idx << "y" idx << "z" assert idx.keys() == [0, 1, 2], \ "keys are wrong: %s" % str(idx.keys()) assert idx.values() == ["x", "y", "z"], \ "values are wrong: %s" % str(idx.values())
def check_looping(self): idx = IdxDict() for item in idx: assert 0, "there shouldn't be anything in idx" idx << 1 for item in idx: assert item==1, "wrong item" idx.clear() for item in idx: assert 0, "there shouldn't be anything in idx after .clear()"
def check_IdxDict(self): idx = IdxDict() idx["a"] = 1 idx["b"] = 2 idx["c"] = 2 idx["a"] = 0 idx[1] = 1 assert idx.keys() == ['a', 'b', 'c'], \ "keys are wrong: %s" % str(idx.keys()) assert idx[0] == 0, "index is wrong" assert idx[0:2] == [0, 1], \ "slicing is wrong: %s" % str(idx[0:2])
def check_looping(self): idx = IdxDict() for item in idx: assert 0, "there shouldn't be anything in idx" idx << 1 for item in idx: assert item == 1, "wrong item" idx.clear() for item in idx: assert 0, "there shouldn't be anything in idx after .clear()"
def check_repr(self): """ really, this just exposes a bug if the keys are numbers... """ idx = IdxDict() idx << "zero" assert repr(idx) == "{0: 'zero'}", \ "wrong representation: %s" % repr(idx)
def __init__(self, table=None, **data): ## we're a new instance by default.. self.isNew = 0 ## A record's table can be passed in the constructor or ## defined a subclass's definition... Most likely, you won't ## create records directly, but call someTable.fetch(key) if table: self.table = table assert self.table is not None, "Record must have an associated Table!" # populate the data.. you probably don't want to do this, # either.. Rather, go through Table. self.data = IdxDict() if data: self.data.update(data) else: self._new()
def toListDict(cur): """converts cursor.fetchall() results into a list of IdxDicts""" #@TODO: (is this still needed?) from pytypes import IdxDict res = [] for row in cur.fetchall(): dict = IdxDict() for i in range(len(cur.description)): dict[cur.description[i][0]] = row[i] res.append(dict) return res
def check_negative(self): idx = IdxDict() idx << "abc" idx << "xyz" assert idx[-1] == "xyz", "-1 broke" assert idx[-2] == "abc", "-2 broke" try: bad = idx[-3] gotError = 0 except IndexError: gotError = 1 assert gotError, "-3 worked but should not have!"
class Record(UserDict.UserDict): """ Record - makes it easy to edit records in a database. """ __ver__ = "$Id$" ## class attributes ########################################## table = None ## constructor ############################################### def __init__(self, table=None, **data): ## we're a new instance by default.. self.isNew = 0 ## A record's table can be passed in the constructor or ## defined a subclass's definition... Most likely, you won't ## create records directly, but call someTable.fetch(key) if table: self.table = table assert self.table is not None, "Record must have an associated Table!" # populate the data.. you probably don't want to do this, # either.. Rather, go through Table. self.data = IdxDict() if data: self.data.update(data) else: self._new() ## public methods ############################################### def delete(self): """ Deletes the record. """ self.table.delete(self[self.table.rowid]) def save(self): """ Inserts or Updates the record. """ if self.isNew: self._insert() self.isNew = 0 else: self._update() ## private methods ################################################# def _new(self): """ Prepare to add a new record. This is called by default. """ self.isNew = 1 for f in self.table.fields: self.data[f.name] = f.default def _update(self): """ called when saving a record that's already in the table. """ self.table.update(self[self.table.rowid], self) def _insert(self): """ calld when saving a record that's not already in the table. """ self.table.insert(self)