On Tuesday 01 July 2008 11:29:56 Oleg Broytmann wrote: > On Thu, Feb 14, 2008 at 07:31:10PM +0300, Oleg Broytmann wrote: > > DecimalCol will not be changed. I will add documentation explaining > > the type affinity problem in SQLite. > > There will be a new DecimalStringCol that stores Decimals as text. > > It is now in the trunk. > > > If > > you want quantization please write a validator class, and I will include > > it into SQLObject. Initially it will be off, but a user can easily use it > > - every column has a "validator" keyword argument. > > Oleg.
Oleg, Thank you. I'd be happy to write a validator, if I can figure out how it should work. For the special case of CurrencyCol, which has precision known in advance, this is easy: ===== class CurrencyValidator(sqlobject.col.DecimalValidator): HUNDREDTHS = Decimal('9.99') def to_python(self, value, state): value = super(CurrencyValidator, self).to_python(value, state) if isinstance(value, Decimal): value = value.quantize(self.HUNDREDTHS) return value def from_python(self, value, state): value = super(CurrencyValidator, self).from_python(value, state) if isinstance(value, Decimal): value = value.quantize(self.HUNDREDTHS) return value ====== But for a general DecimalCol, where the precision isn't known in advance, obviously this simple strategy won't work. An alternative strategy would be like this: ====== class PreciseDecimalValidator(sqlobject.col.DecimalValidator): def __init__(self, *args, **kw): size = kw.pop('size', None) precision = kw.pop('precision', None) if precision: self.PRECISION = Decimal(10) ** (-1 * int(precision)) super(PreciseDecimalValidator, self).__init__(*args, **kw) def to_python(self, value, state): value = super(PreciseDecimalValidator, self).to_python(value, state) if self.PRECISION and isinstance(value, Decimal): value = value.quantize(self.PRECISION) return value def from_python(self, value, state): value = super(PreciseDecimalValidator, self).from_python(value, state) if self.PRECISION and isinstance(value, Decimal): value = value.quantize(self.PRECISION) return value precision = 4 class Example(sqlobject.SQLObject): value = sqlobject.DecimalCol(size=10, precision=precision, validator=PreciseDecimalValidator(precision=precision)) ======= But this is very inelegant. Is there a way to use introspection instead? Thanks, cs ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08 _______________________________________________ sqlobject-discuss mailing list sqlobject-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss