From: Oleg B. <ph...@ph...> - 2008-07-11 16:23:10
|
On Fri, Jul 11, 2008 at 10:34:07AM -0500, Christopher Singley wrote: > > ? Without str() the query is > > > > INSERT INTO test (d) VALUES (10.0000) > > > > but converting to floats is exactly what we want to prevent. With str() > > the query is > > > > INSERT INTO test (d) VALUES ('10.0000') > > Of course you are correct. I apologize for the careless error. > > How about this: It requires a bit more work because we need to to always convert the value to a string. To do this the column always creates DecimalStringValidator: class DecimalStringValidator(DecimalValidator): def to_python(self, value, state): value = super(DecimalStringValidator, 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(DecimalStringValidator, self).from_python(value, state) if isinstance(value, Decimal): if self.precision: value = value.quantize(self.precision) value = value.to_eng_string() return value class SODecimalStringCol(SOStringCol): def __init__(self, **kw): self.size = kw.pop('size', NoDefault) assert (self.size is not NoDefault) and (self.size >= 0), \ "You must give a size argument as a positive integer" self.precision = kw.pop('precision', NoDefault) assert (self.precision is not NoDefault) and (self.precision >= 0), \ "You must give a precision argument as a positive integer" kw['length'] = int(self.size) + int(self.precision) self.quantize = kw.pop('quantize', False) assert isinstance(self.quantize, bool), \ "quantize argument must be Boolean True/False" super(SODecimalStringCol, self).__init__(**kw) def createValidators(self): if self.quantize: v = DecimalStringValidator(precision=Decimal(10) ** (-1 * int(self.precision))) else: v = DecimalStringValidator(precision=0) return [v] + super(SODecimalStringCol, self).createValidators() class DecimalStringCol(StringCol): baseClass = SODecimalStringCol (Now I have to copy your self.max to my implementation...) Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |