Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

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

Changeset 107

Show
Ignore:
Timestamp:
11/26/05 03:52:01
Author:
fumanchu
Message:

logic.Expression changes:

  1. expr(unit) is now synonymous with expr.evaluate(unit).
  2. You can now pass multiple objects into evaluate.
  3. You can now pass kwargs to evaluate, rather than binding them to the Expression. You can still bind them, but passed-in kwargs will overwrite bound ones.
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/arenas.py

    r106 r107  
    350350         
    351351        for unit in cache.itervalues(): 
    352             if expr is None or expr.evaluate(unit): 
     352            if expr is None or expr(unit): 
    353353                yield tuple([getattr(unit, attr) for attr in attrs]) 
    354354         
     
    381381        cache = self._cache(cls) 
    382382        for unit in cache.itervalues(): 
    383             if expr is None or expr.evaluate(unit): 
     383            if expr is None or expr(unit): 
    384384                row = tuple([getattr(unit, attr) for attr in attrs]) 
    385385                if row not in seen: 
  • trunk/doc/managing.html

    r106 r107  
    3737always be bound to a Unit instance. In the example above, it's named 'x', 
    3838but you can use any name you like. Using lambdas as a base means that we 
    39 can simply call <tt>Expression.evaluate(unit)</tt>, and receive a boolean 
     39can simply call <tt>Expression(unit)</tt>, and receive a boolean 
    4040value indicating whether our Unit "passes the test". Attribute lookups on 
    4141our 'x' object will apply to Unit Properties for that Unit object. 
  • trunk/doc/storage.html

    r106 r107  
    146146<p>Persists Units to shelve-type db files. Extremely simple implementation; 
    147147everything is pickled. Querying will be slow--every Unit is sucked in 
    148 one-by-one and tested in pure Python using <tt>Expression.evaluate()</tt>. 
     148one-by-one and tested in pure Python using <tt>Expression(unit)</tt>. 
    149149But for many applications, you don't need heavyweight query tools; 
    150150for example, an online forum may only need topic content looked up by ID. 
  • trunk/engines.py

    r106 r107  
    481481                for id in mem: 
    482482                    unit = self.sandbox.unit(cls, ID=id) 
    483                     if unit and expr.evaluate(unit): 
     483                    if unit and expr(unit): 
    484484                        newset.append(id) 
    485485                A.Members = newset 
  • trunk/logic.py

    r106 r107  
    7777    ...      
    7878    >>> pass # Do some other things... 
    79     >>> e.evaluate(DumbObject()) 
     79    >>> e(DumbObject()) 
    8080    True 
    8181     
    82     The evaluate() method of an Expression accepts any object instance, and 
    83     returns the truth value of itself, getting any named attributes from 
    84     the passed-in object. Notice that the passed-in object does not need 
    85     to be instantiated prior to the formation of the Expression. 
     82    When calling an Expression, it accepts any object instance(s), 
     83    and returns the truth value of itself, getting any named attributes 
     84    from the passed-in object(s). Notice that the passed-in objects do not 
     85    need to be instantiated prior to the construction of the Expression. 
    8686 
    8787 
     
    9292    ...      
    9393    >>> pass # Do some other things... 
     94    >>> e(DumbObject(), Size=3) 
     95    True 
    9496    >>> e.bind_args(Size=3) 
    95     >>> e.evaluate(DumbObject()) 
     97    >>> e(DumbObject()) 
    9698    True 
    9799     
    98100    If the lambda possesses a **kwargs argument in its signature, that 
    99     dictionary may be used to pass in late-bound locals. Before calling 
    100     Expression.evaluate, callers should call .bind_args, passing a dict. 
    101     Each subscript of kwargs will be looked up in that dictionary, and 
    102     the mapped value will replace the operand in the evaluation step
     101    dictionary may be used to pass in late-bound locals. They may either 
     102    be passed when calling the Expression, or may be bound to the 
     103    Expression using the 'bind_args' method. If both are provided, 
     104    the passed-in kwargs will overwrite any bound kwargs
    103105 
    104106 
     
    242244    def _load_func(self, func): 
    243245        # Early-bind as much as possible. 
    244         binder = codewalk.EarlyBinder(func, bind_late=[datetime.datetime.now, datetime.date.today]) 
     246        binder = codewalk.EarlyBinder(func, bind_late=[datetime.datetime.now, 
     247                                                       datetime.date.today]) 
    245248        self.func = binder.function() 
    246249     
     
    274277        self.kwargs.update(kwargs) 
    275278     
    276     def evaluate(self, obj): 
    277         kw = self.kwargs 
    278         return self.func(obj, **kw) 
     279    def evaluate(self, *args, **kwargs): 
     280        kw = {} 
     281        kw.update(self.kwargs) 
     282        kw.update(kwargs) 
     283        return self.func(*args, **kw) 
     284    __call__ = evaluate 
    279285     
    280286    def __getstate__(self): 
  • trunk/storage/__init__.py

    r106 r107  
    173173             
    174174            # Run through our cache first. Hopefully, this will save us 
    175             # calling expr.evaluate twice for each unit. 
     175            # calling expr(unit) twice for each unit. 
    176176            for id, pickledUnit in cache.iteritems(): 
    177177                unit = pickle.loads(pickledUnit) 
    178                 if expr is None or expr.evaluate(unit): 
     178                if expr is None or expr(unit): 
    179179                    matches[id] = unit 
    180180             
     
    243243             
    244244            # Run through our cache first. Hopefully, this will save us 
    245             # calling expr.evaluate twice for each unit. 
     245            # calling expr(unit) twice for each unit. 
    246246            for id, pickledUnit in cache.iteritems(): 
    247247                unit = pickle.loads(pickledUnit) 
    248                 if expr is None or expr.evaluate(unit): 
     248                if expr is None or expr(unit): 
    249249                    seen.append(tuple([getattr(unit, f) for f in attrs])) 
    250250             
     
    344344            for data in cache.itervalues(): 
    345345                unit = pickle.loads(data) 
    346                 if expr.evaluate(unit): 
     346                if expr(unit): 
    347347                    yield unit 
    348348     
  • trunk/storage/db.py

    r106 r107  
    405405# cannot_represent exists so that a portion of an Expression can be 
    406406# labeled imperfect. For example, the function dejavu.iscurrentweek 
    407 # rarely has an SQL equivalent. All Units (which match the rest of  
    408 # the Expression) will be recalled; they can then be compared in 
    409 # expr.evaluate(unit). 
     407# rarely has an SQL equivalent. All Units (which match the rest of the 
     408# Expression) will be recalled; they can then be compared in expr(unit). 
    410409cannot_represent = Sentinel('Cannot Repr') 
    411410 
     
    949948             
    950949            # If our SQL is imperfect, don't yield it to the 
    951             # caller unless it passes evaluate(). 
    952             if (not imperfect) or expr.evaluate(unit): 
     950            # caller unless it passes expr(unit). 
     951            if (not imperfect) or expr(unit): 
    953952                unit.cleanse() 
    954953                yield unit 
     
    12131212             
    12141213            # If our SQL is imperfect, don't yield units to the 
    1215             # caller unless they pass evaluate(). 
     1214            # caller unless they pass expr(unit). 
    12161215            acceptable = True 
    12171216            unitset = [] 
     
    12211220                unit.cleanse() 
    12221221                if imp: 
    1223                     acceptable &= e.evaluate(unit) 
     1222                    acceptable &= e(unit) 
    12241223                    if not acceptable: 
    12251224                        break 
  • trunk/storage/storeshelve.py

    r106 r107  
    6161                    # Set the attribute directly to avoid __set__ overhead. 
    6262                    unit._properties = unitdict 
    63                     if expr is None or expr.evaluate(unit): 
     63                    if expr is None or expr(unit): 
    6464                        unit.cleanse() 
    6565                        units.append(unit) 
     
    132132                # Set the attributes directly to avoid __set__ overhead. 
    133133                unit._properties = unitdict 
    134                 if expr is None or expr.evaluate(unit): 
     134                if expr is None or expr(unit): 
    135135                    globs.append(tuple([getattr(unit, field) for field in fields])) 
    136136            return globs 
     
    150150                # Set the attributes directly to avoid __set__ overhead. 
    151151                unit._properties = unitdict 
    152                 if expr is None or expr.evaluate(unit): 
     152                if expr is None or expr(unit): 
    153153                    key = tuple([getattr(unit, field) for field in fields]) 
    154154                    globs[key] = None 
  • trunk/test/test_storeshelve.py

    r106 r107  
    33Notice that, since StorageManagerShelve doesn't decompile any Expressions, 
    44this will also test all native dejavu logic functions and any other aspects 
    5 of Expression.evaluate(unit). 
     5of Expression(unit). 
    66""" 
    77