Mailing List Archive

L postfix char on longs - what is the best way to discard it.?
Hi folks - I'm batting to find a nice clean way to remove the 'L' from long
repr strings .. (eg, my SQL queries end up like "SELECT * FROM blah WHERE
guff = 23423L"..)

I can fiddle with every bit of my code that attempts to display a long, such
as

query = "... WHERE guff = %s" % repr(long(x))[0:-1]

-- its ugly when you use alot of longs, but does the job..

I can bugger around with long_format() to remove the postix 'L' altogether,
or build a % substitution to print a long without the L - uglier for
distribution, but nice for writing code - I have no idea what repercussions
this has on the library code - probably bad.. I'm assuiming the actual
reason for the 'L' is so a string could be then fed back as a literal
without overflow the default integer conversion. Couldn't the integer
conversion build a long on overflow, or would that get uglier?

Does anyone have some suggestions on better methods?

Thanks.

Jim Crumpler.
L postfix char on longs - what is the best way to discard it.? [ In reply to ]
Jim Crumpler writes:
> query = "... WHERE guff = %s" % repr(long(x))[0:-1]
>
> -- its ugly when you use alot of longs, but does the job..

So pack it up in a function:

def l2a(x):
return `long(x)[:-1]`

query = "... WHERE guff = %s" % l2a(x)

It's still not pretty, but it's more clear than coding it inline
every place you need it!


-Fred

--
Fred L. Drake, Jr. <fdrake@acm.org>
Corporation for National Research Initiatives
L postfix char on longs - what is the best way to discard it.? [ In reply to ]
Jim Crumpler wrote:
>
> Hi folks - I'm batting to find a nice clean way to remove the 'L' from long
> repr strings .. (eg, my SQL queries end up like "SELECT * FROM blah WHERE
> guff = 23423L"..)
>
> I can fiddle with every bit of my code that attempts to display a long, such
> as
>
> query = "... WHERE guff = %s" % repr(long(x))[0:-1]
>
> -- its ugly when you use alot of longs, but does the job..
>
> I can bugger around with long_format() to remove the postix 'L' altogether,
> or build a % substitution to print a long without the L - uglier for
> distribution, but nice for writing code - I have no idea what repercussions
> this has on the library code - probably bad.. I'm assuiming the actual
> reason for the 'L' is so a string could be then fed back as a literal
> without overflow the default integer conversion. Couldn't the integer
> conversion build a long on overflow, or would that get uglier?
>
> Does anyone have some suggestions on better methods?

You could use mxODBC which takes care of all this for you: just
pass in the integer or long object as variable, e.g.
cursor.execute('SELECT ... WHERE id = ?',(x,)).

If the id column is a BIGINT column, x is converted to a string before
passing it to the database. For long integer objects, the trailing 'L'
is removed automagically. In case id is a normal INT column,
the equivalent of int(x) is passed to the database as C long integer.
Overflows raise an exception. Note that SQL BIGINT columns usually
only support 64bit integer values.

mxODBC can be found on my Python Pages.

--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 140 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
L postfix char on longs - what is the best way to discard it.? [ In reply to ]
On Fri, 13 Aug 1999 13:22:22 +0100, "Jim Crumpler" <Jim.Crumpler@unicity.com.au> wrote:

>Hi folks - I'm batting to find a nice clean way to remove the 'L' from long
>repr strings .. (eg, my SQL queries end up like "SELECT * FROM blah WHERE
>guff = 23423L"..)
>
>I can fiddle with every bit of my code that attempts to display a long, such
>as
>
>query = "... WHERE guff = %s" % repr(long(x))[0:-1]
>
> -- its ugly when you use alot of longs, but does the job..
>
[snip]
>
>Does anyone have some suggestions on better methods?

Printing a long without the L-suffix would be a good candidate for an
additional format specifier of the %-Operator IHMO.

Stefan