[python-sybase] GTK & Sybase

Sebo"k Norbert sebok1 at enternet.hu
Thu, 24 Feb 2005 17:50:56 +0100


This locale feature is started on 19-Jul-2003 in PEP 331, the latest
Sybase module is created on 27-Apr-2003. I think it's not use this
feature yet.

Anyway, I wrote my own converter.
It's ugly solution and maybe slow, but it's works...


def sybase_num_to_python_float(sybase_num):
    if sybase_num == None:
        return None

    elif not '.' in str(sybase_num):
        return int(sybase_num)

    else:
        if '-' in str(sybase_num):
            negative = True
            whole,decimal = str(sybase_num).replace('-','').split('.')
        else:
            negative = False
            whole,decimal = str(sybase_num).split('.')

        value = float(whole) + float(decimal) / (10 ** len(decimal))

        if negative: value = - value
        return value


Norbert Sebok

>Probably related to this:
>
>This is one of the new things in Python 2.4. Now there is a
>locale-independent (C locale) way of treating numbers. I bet the module
>has to be adapted to this change to take advantage of this fact (and
>letting users choose their own locale without breaking apps).
>
>http://www.python.org/doc/2.4/whatsnew/node11.html
>
>Andrew McNamara dijo:
>  
>
>>>I have some problem, when I use Sybase module with GTK:
>>>
>>> cursor.execute(" select 1.2 ")
>>> num = cursor.fetchone()[0]
>>> print num           # 1.2
>>> print float(1.2)    # 1.2
>>> print float(num)    # 1.2
>>>
>>> import gtk
>>> print num           # 1.2
>>> print float(1.2)    # 1.2
>>> print float(num)    # 1.0  <- not 1.2
>>>
>>>
>>>After import gtk, the float truncates the decimal.
>>>How can I fix this?
>>>      
>>>
>>This is a shot in the dark, but could gtk be setting the locale?
>>
>>It might also be useful to print the type of num, eg:
>>
>>    print type(num)
>>    
>>