I would like to create a number like:
000000000001
to save to the database. I obviously cannot increment in this fashion (I don't think) in a database, so I'm looking for the most efficient method for pulling the previous number from the database and incrementing it by 1 to create the next record:
000000000002
and so on...
If I store the first number manually, can I do some sort of manual typing to make it hold its number of zeros? I don't even know where to start.
There's actually a super tricky way to do this using the itertools library and a generator function.
This creates an iterator which will return the "zero-padded string form" that you need. It works using the product function which iteratively returns repeated combinations from an iterable in "sorted order". The num_digits argument specifies how many total digits you would like returned.
start
specifies a place to begin the iteration from (say if you wanted to start from 1111111).product
comes with the python 2.6 release. If your using something before that for some reason then use this as the product definition. Taken from the docs here.You can use this function in in a for-loop as an interator:
Hope that helps. -Will
All the leading zeroes are just formatting.
Use an ordinary integer and format it to have lots of leading zeroes.