11-digit integer literal on a 32-bit CPU

Tim Peters tim_one at email.msn.com
Mon Mar 6 23:13:14 EST 2000


[posted & mailed]

[Patrick Phalen]
> I need to assign an 11-digit Transaction ID, according to specs I'm
> given by a client.
>
> Begin with 10000000000
>
> Increment by 1 for each new transaction -- 10000000001, etc.
>
> (Store as string in a file between transactions)
>
> As an integer literal, this overflows on an Intel CPU.
>
> What's a nice trick for dealing with this?
>
> Total transactions aren't likely to exceed 999,999

[and later]

> Oh. I should add that the ID needs to be included in a delimited
> text data file, so 10000000000L doesn't work.

>>> id = long("10000000000")
>>> id = id + 1
>>> str(id)
'10000000001L'
>>> str(id)[:-1]
'10000000001'
>>>

That is, converting strings to and from longs is easy.  You just have to
strip the trailing "L" on output.  Note that this is changing in 1.6, though
(i.e., the trailing "L" is going away via str(), but staying via repr()).

don't-let-one-character-drive-you-from-the-obvious-approach-ly y'rs  - tim






More information about the Python-list mailing list