On Tuesday 01 July 2008 13:56:26 Oleg Broytmann wrote:
> Or, if 'quantize' is always equal to precision
Since I believe the intent is to enforce consistency between the Python model
and the persistent storage, that should be the case.
> class Example(sqlobject.SQLObject):
> value = sqlobject.DecimalCol(size=10, precision=precision,
> quantize=True)
How about something like this?
class DecimalStringValidator(DecimalValidator):
def __init__(self, *args, **kw):
size = kw.pop('size', NoDefault)
precision = kw.pop('precision', NoDefault)
assert precision >= 0, \
"You must give a precision argument as a positive integer"
self.precision = Decimal(10) ** (-1 * int(precision))
super(DecimalStringValidator, self).__init__(*args, **kw)
def to_python(self, value, state):
value = super(DecimalStringValidator, self).to_python(value, state)
if 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):
value = value.quantize(self.precision)
return value
class SODecimalStringCol(SOStringCol):
def __init__(self, **kw):
self.size = kw.pop('size', NoDefault)
assert self.size >= 0, \
"You must give a size argument as a positive integer"
self.precision = kw.pop('precision', NoDefault)
assert 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):
validators = super(SODecimalStringCol, self).createValidators()
if self.quantize:
validators.insert(0,
DecimalStringValidator(precision=self.precision))
class DecimalStringCol(StringCol):
baseClass = SODecimalStringCol
-------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss