We'll see that dynamic manipulation of an object's properties and relationships can be completely generic, using the techniques we saw in the previous chapters.
Since we know how model's properties can be retrieved, and how an object can be generically asked for a given property, we can combine these two techniques to generically manipulate any object, for example to print informations on a list of different objects:
>>> ms=defaultModelSet() >>> objs=ec.fetch('Writer', qualifier='lastName=="Cleese"') >>> objs.extend(ec.fetch('Book')) >>> # ... # Now we will manipulate 'objs' without explicitly referring ... # to its methods ... >>> for o in objs: ... print 'Object ',o ... for cp in ms.entityNamed(o.entityName()).classProperties_attributes(): ... print ' %s: %s'%(cp.name(),o.valueForKey(cp.name())) Object (<Writer.Writer instance at 0x8485a04>) John Cleese age: 24 lastName: Cleese firstName: John birthday: 1939-10-27 08:31:15.00 Object <Book.Book instance at 0x8491f94> title: Gargantua id: 1 price: None Object <Book.Book instance at 0x848a1fc> title: Bouge ton pied que je voie la mer id: 2 price: None Object <Book.Book instance at 0x848f50c> title: Le coup du pere Francois id: 3 price: None Object <Book.Book instance at 0x84a38b4> title: T'assieds pas sur le compte-gouttes id: 4 price: None
We can achieve the same thing with valuesForKeys():
>>> for o in objs: ... print 'Object ',o ... cp_attrs=ms.entityNamed(o.entityName()).classProperties_attributes() ... cp_attrs_names=[cp.name() for cp in cp_attrs] ... print ' ',o.valuesForKeys(cp_attrs_names) Object (<Writer.Writer instance at 0x8485a04>) John Cleese [24, 'Cleese', 'John', <DateTime object for '1939-10-27 08:31:15.00' at 81984f0>] Object <Book.Book instance at 0x8491f94> ['Gargantua', 1, None] Object <Book.Book instance at 0x848a1fc> ['Bouge ton pied que je voie la mer', 2, None] Object <Book.Book instance at 0x848f50c> ['Le coup du pere Francois', 3, None] Object <Book.Book instance at 0x84a38b4> ["T'assieds pas sur le compte-gouttes", 4, None]
Comments are welcome: Sebastien Bigaret / Modeling Home Page