Changeset 349
- Timestamp:
- 12/13/06 12:55:49
- Files:
-
- trunk/codewalk.py (modified) (1 diff)
- trunk/storage/db.py (modified) (6 diffs)
- trunk/units.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/codewalk.py
r348 r349 56 56 (None,), ('cell',), (), '', '', 2, '', ('cell',)) 57 57 def deref_cell(cell): 58 """Return the value of 'cell' (an object from a func_closure).""" 58 59 # FunctionType(code, globals[, name[, argdefs[, closure]]]) 59 60 return FunctionType(_derefblock, {}, "", (), (cell,))() trunk/storage/db.py
r348 r349 133 133 """Set 'value' (of type 'dbtype') as a valid property on the unit.""" 134 134 try: 135 # Skip the "property" classmethod to save a function call136 # (which is ~4x slower).137 # If someone overrides Unit.property, this will break138 # their app, but it's called *so* often it seems worth it.139 ## prop = unit.__class__.property(key)140 135 prop = getattr(unit.__class__, key) 141 136 cvalue = self.db.adapterfromdb.coerce(value, dbtype, prop.type) … … 169 164 for key in idnames + [x for x in cls.properties if x not in idnames]: 170 165 col = t.columns[key] 171 prop = cls.property(key)166 prop = getattr(cls, key) 172 167 props.append((key, colnames.index(col.name), 173 168 col.dbtype, prop.type, prop.coerce)) … … 297 292 data, _ = self.db.fetch(sql, self.db.get_transaction()) 298 293 t = self.db[cls.__name__] 299 dbtypes = [t.columns[f].dbtype for f in fields] 300 props = [cls.property(x) for x in fields] 294 295 # Pre-calc inner loop data. 296 metadata = [] 297 for i, f in enumerate(fields): 298 prop = getattr(cls, f) 299 metadata.append((i, t.columns[f].dbtype, prop.type, prop.coerce)) 301 300 302 301 coerce = self.db.adapterfromdb.coerce … … 304 303 for row in data: 305 304 coerced_row = [] 306 for i, val in enumerate(row): 307 prop = props[i] 308 val = coerce(val, dbtypes[i], prop.type) 309 if decimal and prop.type == decimal.Decimal and val is not None: 310 scale = prop.hints.get('scale', None) 311 if scale: 312 val = val.quantize(decimal.Decimal("." + ("0" * scale))) 305 for i, dbtype, ptype, propcoerce in metadata: 306 val = coerce(row[i], dbtype, ptype) 307 if propcoerce: 308 val = propcoerce(None, val) 313 309 coerced_row.append(val) 314 310 yield tuple(coerced_row) … … 590 586 pytype1 = self.db.python_type(oldcol.dbtype) 591 587 # Note we use newname, which assumes that property is in the class. 592 pytype2 = cls.property(newname).type588 pytype2 = getattr(cls, newname).type 593 589 oldcol.imperfect_type = not self.db.isrelatedtype(pytype1, pytype2) 594 590 # Use the superclass call to avoid DROP COLUMN/ADD COLUMN. … … 634 630 c = c[0] 635 631 pytype1 = self.db.python_type(c.dbtype) 636 pytype2 = cls.property(ckey).type632 pytype2 = getattr(cls, ckey).type 637 633 c.imperfect_type = not self.db.isrelatedtype(pytype1, pytype2) 638 634 # Use the superclass call to avoid ALTER TABLE trunk/units.py
r348 r349 354 354 355 355 def coerce(self, unit, value): 356 """Coerce the given value to the proper type for this property. 357 358 In the base class, the 'unit' arg is not used. When overriding 359 this class, you should allow for meaningful results even if 360 the supplied 'unit' arg is None. 361 """ 356 362 if value is not None: 357 363 selftype = self.type … … 546 552 # instead of: 547 553 # unit = UnitSubClass() 554 # unit._properties = {...} 548 555 # If done this way, the caller must make CERTAIN that all of 549 # the values in _properties are set .556 # the values in _properties are set, and must call cleanse(). 550 557 self._properties = dict.fromkeys(cls.properties, None) 551 552 # We assume the caller will cleanse once they've set _properties.553 558 else: 554 559 # Copy the class properties into self._properties, … … 645 650 return tuple(product) 646 651 indices = classmethod(indices) 647 648 def property(cls, key):649 """Return the specified UnitProperty object (not its value)."""650 # Retrieving from the class gives us651 # the UnitProperty object, not its value.652 return getattr(cls, key)653 property = classmethod(property)654 652 655 653 def adjust(self, **values):
