Changeset 85
- Timestamp:
- 08/11/05 18:11:04
- Files:
-
- trunk/__init__.py (modified) (7 diffs)
- trunk/doc/intro.html (modified) (1 diff)
- trunk/doc/managing.html (modified) (1 diff)
- trunk/doc/modeling.html (modified) (3 diffs)
- trunk/doc/storage.html (modified) (1 diff)
- trunk/engines.py (modified) (1 diff)
- trunk/storage/zoo_fixture.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/__init__.py
r84 r85 22 22 del newUnit # or otherwise release the reference. 23 23 24 Using recall(), you get a n iterator:24 Using recall(), you get a list; using xrecall(), you get an iterator: 25 25 for unit in sandbox.recall(cls, expr): 26 26 do_something_with(unit) … … 611 611 """migrate_class(cls, new_store). Copy all units of cls to new_store.""" 612 612 new_store.create_storage(cls) 613 for unit in self.new_sandbox(). recall(cls):613 for unit in self.new_sandbox().xrecall(cls): 614 614 new_store.reserve(unit) 615 615 new_store.save(unit, True) … … 683 683 684 684 def forget(self, unit): 685 """ forget(unit).Destroy unit, both in the cache and storage."""685 """Destroy unit, both in the cache and storage.""" 686 686 cls = unit.__class__ 687 687 store = self.arena.storage(cls) … … 696 696 unit.sandbox = None 697 697 698 def recall(self, cls, expr=None):699 """ recall(cls, expr=None) -> Recallunits of cls which match expr."""698 def xrecall(self, cls, expr=None): 699 """Iterator over units of cls which match expr.""" 700 700 701 701 store = self.arena.storage(cls) … … 746 746 if confirmed: 747 747 yield unit 748 749 def recall(self, cls, expr=None): 750 """List of units of class 'cls' which match expr.""" 751 return [x for x in self.xrecall(cls, expr)] 748 752 749 753 def multirecall(self, *pairs): … … 805 809 806 810 If you need a single Unit which matches a more complex 807 expression, use recall() .next().811 expression, use recall()[0] or xrecall().next(). 808 812 """ 809 813 expr = None … … 811 815 expr = logic.filter(**kwargs) 812 816 try: 813 return self. recall(cls, expr).next()817 return self.xrecall(cls, expr).next() 814 818 except StopIteration: 815 819 return None trunk/doc/intro.html
r84 r85 110 110 <pre>>>> import zookeeper 111 111 >>> box = zookeeper.arena.new_sandbox() 112 >>> [x for x in box.recall(zookeeper.Animal)]112 >>> box.recall(zookeeper.Animal) 113 113 [<zookeeper.Animal object at 0x013281F0>, <zookeeper.Animal object at 0x01328150>, 114 114 <zookeeper.Animal object at 0x01328130>, <zookeeper.Animal object at 0x01328230>] 115 >>> [x for x in box.recall(zookeeper.Zoo)]115 >>> box.recall(zookeeper.Zoo) 116 116 [] 117 117 >>> zoo = zookeeper.Zoo(Name='San Diego Zoo', Size='38') trunk/doc/managing.html
r84 r85 419 419 420 420 <h4>Sorting Units</h4> 421 <p>When you recall Units, you receive a generator, and must iterate over 422 the values in some way. Often, this is accomplished with a list 423 comprehension: 424 <pre>f = logic.Expression(lambda x: 'Aa' in x.Name) 425 people = [x for x in sandbox.recall(Person, f)] 426 </pre> 427 However, the <tt>recall</tt> method doesn't do any sorting; you must sort 428 your list in your Python code. Dejavu provides a <tt>sort(attrs, 429 descending=False)</tt> function to assist you. It returns a function, which 430 you can then use in Python's sort function. Continuing our example: 421 <p>When you recall Units, you receive a list. However, the <tt>recall</tt> 422 method doesn't do any sorting; you must sort your list in your Python code. 423 Dejavu provides a <tt>sort(attrs, descending=False)</tt> function to assist 424 you in sorting Units. It returns a function, which you can then use in 425 Python's sort function. Continuing our example: 431 426 <pre>sorted_people = people.sort(dejavu.sort('Size', 'Name'))</pre> 432 427 The most important issue (and the reason we don't just use 2.4's attrgetter), trunk/doc/modeling.html
r84 r85 275 275 <h4>Recalling</h4> 276 276 <p>Once you have memorized a Unit or two, you will probably want to 277 recall them at some point. Sandboxes possess twomember functions to277 recall them at some point. Sandboxes possess four member functions to 278 278 accomplish this.</p> 279 279 … … 293 293 </pre> 294 294 If you do not supply an Expression, all Units of the given Unit class 295 will be retrieved. Notice that the return value is *not* a list; it is a 296 generator (or other iterable). You must iterate over it to retrieve all 297 values. By returning an iterator, we allow some Storage Managers to load 298 Units in a more lazy fashion. If this is a huge burden for you, let me 299 know; I might be convinced to add a <tt>recall_list</tt> method.</p> 295 will be retrieved in a list.</p> 300 296 301 297 <p>If your Unit class defines an <tt>on_recall()</tt> method, it will be … … 306 302 unit will not be yielded back to the caller, nor placed in the Sandbox 307 303 cache.</p> 304 305 <h5>xrecall()</h5> 306 <p>Just like recall, but returns an iterator instead of a list. Use xrecall 307 to load Units in a more lazy fashion.</p> 308 308 309 309 <h5>unit()</h5> trunk/doc/storage.html
r84 r85 156 156 subclass.</li> 157 157 </ul> 158 <p>The shelve module does not yet support multirecall().</p>159 158 160 159 <h5>Common Database Configuration Entries</h5> trunk/engines.py
r84 r85 323 323 """Unit Collections obtained by executing the rules sometime in the past.""" 324 324 f = logic.filter(EngineID=self.ID) 325 allSnap = [x for x in self.sandbox.recall(UnitCollection, f)]325 allSnap = self.sandbox.recall(UnitCollection, f) 326 326 allSnap.sort(dejavu.sort(u'Timestamp')) 327 327 return allSnap trunk/storage/zoo_fixture.py
r84 r85 171 171 172 172 # Exhibits 173 exes = [x for x in box.recall(zoo.Exhibit)]173 exes = box.recall(zoo.Exhibit) 174 174 self.assertEqual(len(exes), 2) 175 175 if exes[0].Name == 'The Penguin Encounter': … … 197 197 # We flush_all to ensure a DB hit each time. 198 198 box.flush_all() 199 units = box.recall(cls, logic.Expression(lam)) 200 return len([x for x in units]) 201 202 zoos = [x for x in box.recall(zoo.Zoo)] 199 return len(box.recall(cls, logic.Expression(lam))) 200 201 zoos = box.recall(zoo.Zoo) 203 202 self.assertEqual(zoos[0].dirty(), False) 204 203 self.assertEqual(len(zoos), 4) … … 251 250 box.flush_all() 252 251 units = box.recall(zoo.Zoo, logic.Expression(lambda x: "_" in x.Name)) 253 self.assertEqual(len( [x for x in units]), 1)252 self.assertEqual(len(units), 1) 254 253 255 254 # This broke in MSAccess (storeado) in April 2005, due to a bug in … … 262 261 e.bind_args(Year=2004) 263 262 units = box.recall(zoo.Animal, e) 264 self.assertEqual(len( [x for x in units]), 1)263 self.assertEqual(len(units), 1) 265 264 266 265 def test_3_Aggregates(self): … … 326 325 # the order of thread execution can not be guaranteed. 327 326 box = zoo.arena.new_sandbox() 328 quadrupeds = [x for x in box.recall(zoo.Animal, f)]327 quadrupeds = box.recall(zoo.Animal, f) 329 328 self.assertEqual(len(quadrupeds), 4) 330 329 … … 383 382 box = zoo.arena.new_sandbox() 384 383 385 # Test box.unit inside of recall386 for ape in box. recall(zoo.Animal, logic.filter(Name='Ape')):384 # Test box.unit inside of xrecall 385 for ape in box.xrecall(zoo.Animal, logic.filter(Name='Ape')): 387 386 mother = box.unit(zoo.Animal, ID=ape.Mother) 388 387 if ape.ID == 11: … … 391 390 self.assertEqual(mother.ID, 11) 392 391 393 # Test recall inside of recall394 for ape in box. recall(zoo.Animal, logic.filter(Name='Ape')):392 # Test recall inside of xrecall 393 for ape in box.xrecall(zoo.Animal, logic.filter(Name='Ape')): 395 394 children = 0 396 395 for child in box.recall(zoo.Animal, logic.filter(Mother=ape.ID)): … … 401 400 self.assertEqual(children, 0) 402 401 403 # Test far associations inside of recall404 for ape in box. recall(zoo.Animal, logic.filter(Name='Ape')):402 # Test far associations inside of xrecall 403 for ape in box.xrecall(zoo.Animal, logic.filter(Name='Ape')): 405 404 mother = ape.first(zoo.Animal) 406 405 if ape.ID == 11:
