Changeset 107
- Timestamp:
- 11/26/05 03:52:01
- Files:
-
- trunk/arenas.py (modified) (2 diffs)
- trunk/doc/managing.html (modified) (1 diff)
- trunk/doc/storage.html (modified) (1 diff)
- trunk/engines.py (modified) (1 diff)
- trunk/logic.py (modified) (4 diffs)
- trunk/storage/__init__.py (modified) (3 diffs)
- trunk/storage/db.py (modified) (4 diffs)
- trunk/storage/storeshelve.py (modified) (3 diffs)
- trunk/test/test_storeshelve.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/arenas.py
r106 r107 350 350 351 351 for unit in cache.itervalues(): 352 if expr is None or expr .evaluate(unit):352 if expr is None or expr(unit): 353 353 yield tuple([getattr(unit, attr) for attr in attrs]) 354 354 … … 381 381 cache = self._cache(cls) 382 382 for unit in cache.itervalues(): 383 if expr is None or expr .evaluate(unit):383 if expr is None or expr(unit): 384 384 row = tuple([getattr(unit, attr) for attr in attrs]) 385 385 if row not in seen: trunk/doc/managing.html
r106 r107 37 37 always be bound to a Unit instance. In the example above, it's named 'x', 38 38 but 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 boolean39 can simply call <tt>Expression(unit)</tt>, and receive a boolean 40 40 value indicating whether our Unit "passes the test". Attribute lookups on 41 41 our 'x' object will apply to Unit Properties for that Unit object. trunk/doc/storage.html
r106 r107 146 146 <p>Persists Units to shelve-type db files. Extremely simple implementation; 147 147 everything 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>.148 one-by-one and tested in pure Python using <tt>Expression(unit)</tt>. 149 149 But for many applications, you don't need heavyweight query tools; 150 150 for example, an online forum may only need topic content looked up by ID. trunk/engines.py
r106 r107 481 481 for id in mem: 482 482 unit = self.sandbox.unit(cls, ID=id) 483 if unit and expr .evaluate(unit):483 if unit and expr(unit): 484 484 newset.append(id) 485 485 A.Members = newset trunk/logic.py
r106 r107 77 77 ... 78 78 >>> pass # Do some other things... 79 >>> e .evaluate(DumbObject())79 >>> e(DumbObject()) 80 80 True 81 81 82 The evaluate() method of an Expression accepts any object instance, and83 returns the truth value of itself, getting any named attributes from84 the passed-in object. Notice that the passed-in object does not need85 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. 86 86 87 87 … … 92 92 ... 93 93 >>> pass # Do some other things... 94 >>> e(DumbObject(), Size=3) 95 True 94 96 >>> e.bind_args(Size=3) 95 >>> e .evaluate(DumbObject())97 >>> e(DumbObject()) 96 98 True 97 99 98 100 If the lambda possesses a **kwargs argument in its signature, that 99 dictionary may be used to pass in late-bound locals. Before calling100 Expression.evaluate, callers should call .bind_args, passing a dict.101 E ach subscript of kwargs will be looked up in that dictionary, and102 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. 103 105 104 106 … … 242 244 def _load_func(self, func): 243 245 # 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]) 245 248 self.func = binder.function() 246 249 … … 274 277 self.kwargs.update(kwargs) 275 278 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 279 285 280 286 def __getstate__(self): trunk/storage/__init__.py
r106 r107 173 173 174 174 # Run through our cache first. Hopefully, this will save us 175 # calling expr .evaluatetwice for each unit.175 # calling expr(unit) twice for each unit. 176 176 for id, pickledUnit in cache.iteritems(): 177 177 unit = pickle.loads(pickledUnit) 178 if expr is None or expr .evaluate(unit):178 if expr is None or expr(unit): 179 179 matches[id] = unit 180 180 … … 243 243 244 244 # Run through our cache first. Hopefully, this will save us 245 # calling expr .evaluatetwice for each unit.245 # calling expr(unit) twice for each unit. 246 246 for id, pickledUnit in cache.iteritems(): 247 247 unit = pickle.loads(pickledUnit) 248 if expr is None or expr .evaluate(unit):248 if expr is None or expr(unit): 249 249 seen.append(tuple([getattr(unit, f) for f in attrs])) 250 250 … … 344 344 for data in cache.itervalues(): 345 345 unit = pickle.loads(data) 346 if expr .evaluate(unit):346 if expr(unit): 347 347 yield unit 348 348 trunk/storage/db.py
r106 r107 405 405 # cannot_represent exists so that a portion of an Expression can be 406 406 # 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). 410 409 cannot_represent = Sentinel('Cannot Repr') 411 410 … … 949 948 950 949 # If our SQL is imperfect, don't yield it to the 951 # caller unless it passes e valuate().952 if (not imperfect) or expr .evaluate(unit):950 # caller unless it passes expr(unit). 951 if (not imperfect) or expr(unit): 953 952 unit.cleanse() 954 953 yield unit … … 1213 1212 1214 1213 # If our SQL is imperfect, don't yield units to the 1215 # caller unless they pass e valuate().1214 # caller unless they pass expr(unit). 1216 1215 acceptable = True 1217 1216 unitset = [] … … 1221 1220 unit.cleanse() 1222 1221 if imp: 1223 acceptable &= e .evaluate(unit)1222 acceptable &= e(unit) 1224 1223 if not acceptable: 1225 1224 break trunk/storage/storeshelve.py
r106 r107 61 61 # Set the attribute directly to avoid __set__ overhead. 62 62 unit._properties = unitdict 63 if expr is None or expr .evaluate(unit):63 if expr is None or expr(unit): 64 64 unit.cleanse() 65 65 units.append(unit) … … 132 132 # Set the attributes directly to avoid __set__ overhead. 133 133 unit._properties = unitdict 134 if expr is None or expr .evaluate(unit):134 if expr is None or expr(unit): 135 135 globs.append(tuple([getattr(unit, field) for field in fields])) 136 136 return globs … … 150 150 # Set the attributes directly to avoid __set__ overhead. 151 151 unit._properties = unitdict 152 if expr is None or expr .evaluate(unit):152 if expr is None or expr(unit): 153 153 key = tuple([getattr(unit, field) for field in fields]) 154 154 globs[key] = None trunk/test/test_storeshelve.py
r106 r107 3 3 Notice that, since StorageManagerShelve doesn't decompile any Expressions, 4 4 this will also test all native dejavu logic functions and any other aspects 5 of Expression .evaluate(unit).5 of Expression(unit). 6 6 """ 7 7
