Sometimes you need more than this. Let's take the test package AuthorBooks, and suppose we want to use FixedPoint9.1.
string
/VARCHAR
(was: a
float
/NUMERIC(10,2)
): we will store the FixedPoint
object as a string, not as a float, because of the inherent imprecision
which goes any (binary) representation of float9.2
PRECISION=2 def _setPrice(self, value): if value is None: self._price=None else: self._price = FixedPoint(value, PRECISION) def _getPrice(self): if self._price: return None else: return str(self._price)
Now let's test this: (remember to change the DB schema so that table BOOK's
attribute price
is a VARCHAR
)
>>> from fixedpoint import FixedPoint >>> from AuthorBooks.Book import Book >>> from Modeling.EditingContext import EditingContext >>> ec=EditingContext() >>> book=Book() >>> book.setTitle('Test FixedPoint') >>> book.setPrice(FixedPoint("3.341")) >>> book.getTitle(), book.getPrice() ('Test FixedPoint', FixedPoint('3.34', 2)) # precision=2 >>> ec.insert(book) >>> ec.saveChanges() >>> book.getTitle(), book.getPrice() ('Test FixedPoint', FixedPoint('3.34', 2))
Here you can check in you db that it was stored as a varchar
, as
expected. Start a new python and test the fetch:
>>> from fixedpoint import FixedPoint >>> from Modeling.EditingContext import EditingContext >>> ec=EditingContext() >>> books=ec.fetch('Book') >>> books[0].getTitle(), books[0].getPrice() ('Test FixedPoint', FixedPoint('3.34', 2))
As you can see, FixedPoint is now correctly and automatically handled by the framework.
This technique can be used for any custom type you want to use. The next section gives some details on how this works.
FixedPoint
package can be found on
sourceforge
0.7
' in a python interpreter:
>>> 0.7
0.69999999999999996