FixedPoint

Gerhard Häring gh_pythonlist at gmx.de
Thu Feb 7 16:16:14 EST 2002


Le 07/02/02 à 08:22, Mark McEahern écrivit:
> Steve Holden wrote:
> > Since the *intent* for None is that there should only ever be one
> > object of type None, the better test would be
> >
> >     if something is None
> 
> Thanks, that's how I generally test for None--I didn't know the bit about
> "there should only ever be one."
> 
> However, you wrote:
> 
> > which can easily be used without requiring any changes to FixedPoint.
> 
> Well, sure, I could easily change my local copy of PyPgSql's PgSQL.py.

Nah, that sucks. We can do better than that :-)

> [...] Of course, I could ask the PyPgSql folks not to test for None
> like that.

Ok, I'm listening :-)

This is a quick shot, but what about changing the _quote function in
PgSQL.py to this:

##########################################################################
def _quote(value):
    """
_quote(value) -> string
    This function transforms the Python value into a string suitable to send
    to the PostgreSQL database in a insert or update statement.  This function
    is automatically applied to all parameter sent vis an execute() call.
    Because of this an update/insert statement string in an execute() call
    should only use '%s' [or '%(name)s'] for variable subsitution without any
    quoting."""

    if hasattr(value, '_quote'):
	return value._quote()

    if value is None:
	return 'NULL'

    if type(value) in [ListType, TupleType]:
	return _handleArray(list(value))

    if type(value) in [DateTimeType, DateTimeDeltaType]:
	return "'%s'" % value

    if type(value) == StringType:
	return PgQuoteString(value)

    return repr(value)
##########################################################################

There are two changes:

1) it really tests for "is None", which should be ok for all cases I can
currently imagine.

2) It also tests for a _quote() method first, so users can more easily
override the default quoting behaviour.

I can also can't see where this would break existing code.  But I would
probably ask Bill Allie before committing a subtle change like this one.

Hmm., while we're at it, we should probably change this test too: "in
[ListType, TupleType]" Does anybody know how I can reliably test if
something is a Sequence or is iterable?

(Also will have to change the StringType test anyway for Unicode,
anyway)

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 3.2 °C      Wind: 2.4 m/s




More information about the Python-list mailing list