Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 76

Show
Ignore:
Timestamp:
06/27/05 05:24:59
Author:
fumanchu
Message:

1. Refactored name coercion in db.py (Christian Holtje)
2. Finally implemented multirecall for storeshelve.py.
3. Stripped newlines.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/storage/db.py

    r75 r76  
    4747import warnings 
    4848import threading 
     49 
     50 
     51def getCoerceName(value, valuetype=None): 
     52    mod = valuetype.__module__ 
     53    if mod == "__builtin__": 
     54        xform = "coerce_%s" % valuetype.__name__ 
     55    else: 
     56        xform = "coerce_%s_%s" % (mod, valuetype.__name__) 
     57    xform = xform.replace(".", "_") 
     58    return xform 
     59 
     60def getCoerceMethod(obj, value, valuetype=None): 
     61    if valuetype is None: 
     62        valuetype = type(value) 
     63     
     64    meth = getCoerceName(value, valuetype) 
     65    if hasattr(obj, meth): 
     66        return getattr(obj, meth) 
     67     
     68    methods = [] 
     69    for base in valuetype.__bases__: 
     70        meth = getCoerceName(value, base) 
     71        methods.append(meth) 
     72        if hasattr(obj, meth): 
     73            return getattr(obj, meth) 
     74     
     75    raise TypeError("'%s' is not handled by %s.  Looked for: %s " % 
     76                    (valuetype, obj.__class__, ",".join(methods))) 
    4977 
    5078 
     
    200228    def coerce(self, value, valuetype=None): 
    201229        """coerce(value, valuetype=None) -> value, coerced by valuetype.""" 
    202         if valuetype is None: 
    203             valuetype = type(value) 
    204          
    205         mod = valuetype.__module__ 
    206         if mod == "__builtin__": 
    207             xform = "coerce_%s" % valuetype.__name__ 
    208         else: 
    209             xform = "coerce_%s_%s" % (mod, valuetype.__name__) 
    210         xform = xform.replace(".", "_") 
    211         try: 
    212             xform = getattr(self, xform) 
    213         except AttributeError: 
    214             raise TypeError("'%s' is not handled by %s." % 
    215                             (valuetype, self.__class__)) 
    216         return xform(value) 
     230        meth = getCoerceMethod(self, value, valuetype) 
     231        return meth(value) 
    217232     
    218233    def tostr(self, value): 
     
    288303            return None 
    289304         
    290         if valuetype is None: 
    291             valuetype = type(value) 
    292          
    293         mod = valuetype.__module__ 
    294         if mod == "__builtin__": 
    295             xform = "coerce_%s" % valuetype.__name__ 
    296         else: 
    297             xform = "coerce_%s_%s" % (mod, valuetype.__name__) 
    298         xform = xform.replace(".", "_") 
    299         try: 
    300             xform = getattr(self, xform) 
    301         except AttributeError: 
    302             raise TypeError("'%s' is not handled by %s." % 
    303                             (valuetype, self.__class__)) 
    304         return xform(value, coltype) 
     305        meth = getCoerceMethod(self, value, valuetype) 
     306        return meth(value, coltype) 
    305307     
    306308    def consume(self, unit, key, value, coltype): 
  • trunk/storage/storeshelve.py

    r75 r76  
    135135    def multirecall(self, *pairs): 
    136136        """multirecall(*pairs) -> Full inner join units for each (cls, expr) pair.""" 
    137         raise NotImplementedError("This method doesn't yet work for shelve.") 
    138         unitsets = [] 
     137##        raise NotImplementedError("This method doesn't yet work for shelve.") 
     138        pairs = list(pairs) 
     139        firstcls, firstexpr = pairs.pop(0) 
     140        masterset = [[x] for x in self.recall(firstcls, firstexpr)] 
    139141         
    140         firstcls = pairs[0][0] 
    141142        for cls, expr in pairs: 
    142             if cls is not firstcls: 
    143                 spath = self.arena.associations.shortest_path(firstcls, cls) 
    144                 # This should be firstcls in every case. 
    145                 cls1 = spath.pop(0) 
    146                 for cls2 in spath: 
    147                     leftkey, rightkey = cls1._associations[cls2] 
    148                     wheres.append("(%s.%s = %s.%s)" % 
    149                                   (t(cls1), i(leftkey), 
    150                                    t(cls2), i(rightkey))) 
    151                     cls1 = cls2 
    152                 join = logic.Expression() 
    153                 expr = expr + join 
     143            tests = [] 
     144            spath = self.arena.associations.shortest_path(firstcls, cls) 
     145            cls1 = spath.pop(0) # This should be firstcls in every case. 
     146            for cls2 in spath: 
     147                subset = [x for x in self.recall(cls2, expr)] 
     148                leftkey, rightkey = cls1._associations[cls2] 
     149                tests.append((subset, leftkey, rightkey)) 
     150                cls1 = cls2 
    154151             
    155             unitset = [unit for unit in self.recall(cls, expr)] 
    156             unitsets.append(unitset) 
     152            newmasterset = [] 
     153            for row in masterset: 
     154                list1 = [row[0]] 
     155                for subset, leftkey, rightkey in tests: 
     156                    matches = [] 
     157                    for unit1 in list1: 
     158                        matches.extend([unit2 for unit2 in subset 
     159                                        if getattr(unit1, leftkey) 
     160                                        == getattr(unit2, rightkey)]) 
     161                    list1 = matches 
     162                 
     163                # Take the final matches and join each with the current row. 
     164                for unit in matches: 
     165                    newmasterset.append(row + [unit]) 
     166            masterset = newmasterset 
    157167         
    158         for unitset in unitsets: 
    159             yield unitset 
     168        return masterset 
    160169