[PyGreSQL] PyGreSQL Digest, Vol 84, Issue 3

Christoph Zwerschke cito at online.de
Sat Jul 16 13:48:14 EDT 2011


Am 16.07.2011 18:59 schrieb Maxim Yegorushkin:
> +        elif isinstance(val, float):
> +            if math.isnan(val):
> +                val = "'NaN'"
> +            elif math.isinf(val):
> +                val = "'Infinity'" if val>  0 else "'-Infinity'"
>           elif val is None:
>               val = 'NULL'
>           elif isinstance(val, (list, tuple)):

Yes, something along these lines will be better, with some changes to 
make this work on older Python versions which do not have isinf, isnan 
and the ternary syntax. Like so:

try:
     from math import isnan, isinf
except ImportError: # Python < 2.6
     isnan = lambda x: x != x
     isinf = lambda x: not isnan(x) and isnan(x * 0)

> It relies on the fact that python can handle "Infinity" and
> "-Infinity" as well as "inf" and "-inf". I can't double check that in
> python documentation right now since python.org does not respond at
> present.
>
> Another thing worth mentioning is that it would break user code that
> uses float formatting in sql strings, like "%.2f" instead of simple
> "%s". Are float formats allowed in sql strings according to dbapi
> spec?

Good question. Pygres supports "pyformat", which means "extended" format 
codes. Not sure whether the "extended" refers only to dictionary style 
parameters or also other format codes - the specs are unclear. But we 
should clarify this first.

-- Christoph




More information about the PyGreSQL mailing list