No decimal field?

22 views
Skip to first unread message

Italo Maia

unread,
Dec 6, 2009, 10:32:09 PM12/6/09
to buzhug
I'm having trouble with buzhug + decimal fields. Is there a way to
make it work? Using buzhug from ubuntu karmic repository.

Pierre Quentel

unread,
Dec 19, 2009, 2:54:43 AM12/19/09
to buzhug
Hi,

On 7 déc, 04:32, Italo Maia <italo.m...@gmail.com> wrote:
> I'm having trouble with buzhug + decimal fields. Is there a way to
> make it work? Using buzhug from ubuntu karmic repository.

For the moment the decimal type is not supported. As for all types,
there must be a function that transforms a value to a string,
preserving the order. I haven't spent much time on it, but with all
the available options for the decimal type I wonder if such a function
can be found. Ideas, anyone ?

- Pierre

Jim Eggleston

unread,
Dec 19, 2009, 7:50:05 AM12/19/09
to buz...@googlegroups.com
Possibly the FloatFile class in buzhug_classes.py could be adapted. Here is rough outline:

Copy FloatFile  to create a new class called DecimalFile

Put this somewhere convenient:
from decimal import Decimal

In the to_block() method:
...
i = value.to_integral()
j = (value - i) * Decimal(10**9)
j = j.to_integral()
i = long(i)
j = long(j)
...

Do the reverse in from_block. Need to modify the type checking code as well.

This will preserve sort order. Without putting a lot of thought into it, I _think_ this will encode and decode Decimals accurately.

Cheers,
Jim




2009/12/19 Pierre Quentel <quentel...@wanadoo.fr>

- Pierre

--

Vous recevez ce message, car vous êtes abonné au groupe Google Groupes buzhug.
Pour envoyer un message à ce groupe, adressez un e-mail à buz...@googlegroups.com.
Pour vous désabonner de ce groupe, envoyez un e-mail à l'adresse buzhug+un...@googlegroups.com.
Pour plus d'options, consultez la page de ce groupe : http://groups.google.com/group/buzhug?hl=fr



Jim Eggleston

unread,
Dec 19, 2009, 4:10:11 PM12/19/09
to buz...@googlegroups.com
The to_integral method doesn't truncate, it rounds, so this line:

i = value.to_integral()

needs to be changed to:

i = (value - Decimal('0.5')).to_integral()

Another thing to note is that the default precision in the decimal module is 28 places. The DecimalFile code snippet I gave only stores 9 places. Not sure if this is an issue in practice. It should be fine for monetary applications.

Cheers,
Jim




2009/12/19 Jim Eggleston <jim.eg...@gmail.com>
Possibly the FloatFile class in buzhug_classes.py could be adapted. Here is rough outline:

Copy FloatFile  to create a new class called DecimalFile

Put this somewhere convenient:
from decimal import Decimal

In the to_block() method:
...
i = value.to_integral()
j = (value - i) * Decimal('10**9')

Jim Eggleston

unread,
Dec 21, 2009, 2:05:52 PM12/21/09
to buz...@googlegroups.com
BTW, I am not going to code this up. I don't actually use buzhug, even though I've been following it for a long time.

Italo, why not give it a go yourself? If you have a need for a decimal type then you most likely have good test cases as well.

Cheers,
Jim




2009/12/20 Jim Eggleston <jim.eg...@gmail.com>

Nicolas Pinault

unread,
Dec 21, 2009, 3:04:29 PM12/21/09
to buz...@googlegroups.com


Pierre Quentel a écrit :
Looking at Decimal doc, and after very few tests, it appears that converting to a string is as easy as calling the str() function. The reverse is done by calling decimal.Decimal(string).

Regards,
Nicolas

Jim Eggleston

unread,
Dec 21, 2009, 3:41:00 PM12/21/09
to buz...@googlegroups.com
Yes but the value stored in the database must compare/sort correctly, which won't happen automatically if a string is saved. You could pad the string with zeroes, but then you have to make a decision on how many significant digits you want to store. The struct value only takes 13 bytes and stores a long for the wholenumber part and nine decimal places for the fraction.

Cheers,
Jim




2009/12/22 Nicolas Pinault <nic...@famillepinault.fr>

Nicolas Pinault

unread,
Dec 21, 2009, 3:59:25 PM12/21/09
to buz...@googlegroups.com
Hi Jim,

Jim Eggleston a écrit :
Yes but the value stored in the database must compare/sort correctly, which won't happen automatically if a string is saved. You could pad the string with zeroes, but then you have to make a decision on how many significant digits you want to store. The struct value only takes 13 bytes and stores a long for the wholenumber part and nine decimal places for the fraction.

Right, the decimal part is a problem.

Regards,
Nicolas
Reply all
Reply to author
Forward
0 new messages