Yashar Aliabbasi
Yashar Aliabbasi

Reputation: 2719

Incremental UUID generator in python

I'm working on project that need to make unique and incremental file names.I found UUID best way for making unique names but being incremental is already unanswered.We have such a situation that our list will recreate after reboot based on saved file.and creating new files by adding to previous list.
my tried program is simple:

import uuid
print(uuid.uuid4().hex)
print(uuid.uuid4().hex)
print(uuid.uuid4().hex)
print(uuid.uuid4().hex)

and sample output is this:

e497b7d7651341e0ac214fea8c408a7b
5d956a98724c459796ef1fce3818ff45
e60f2517ac1a430eab84768dae9e49a1
9df26e765dee4c3197c2f5a69e210046

(This result will totally different from run to run)
as you can see there is no guaranty that new UUID will be greater/smaller than previous one.

What I want is something like NEWSEQUENTIALID in Sql Server

Thank you in advance ;)

Upvotes: 1

Views: 2835

Answers (3)

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

You can use a file for saving the last number. Next time you can read this file and ++.

Upvotes: 2

frarugi87
frarugi87

Reputation: 2880

If I understood correctly, this is an example of XY Problem. What you want is that the filenames are unique and incremental (after the first one comes the second and so on).

This means you don't want a random file name, only one that is not reused.

I suggest a scheme such as

YYYYMMDD_HHmmSS_IDX

where YYYY is the year, MM is the month, DD the day, HH the hour (24-based), mm the minute, SS the seconds; all this data represent the start time of the instance of the program (so at the beginning you initialize a variable at the current time and never update it). All of these data are 0-padded if needed (for instance, if it is 9:08 in the morning the field will be 09 and 08).

The IDX field is an incremental index (for instance, a 0-padded 3-digits index) which gets updated every time you need to create a new file (000 at the beginning, 001 after, and so on).

This will be incremental, and moreover store also some other info (e.g. the start time of the date). It will be unique, unless you start two instances the very same second (then you can also add a millisecond counter)

EDIT:

If you don't have any means of getting the time (no RTC, no GPS, no internet, nothing) you just have to decide a pattern with a counter, for instance:

myfile_XXXXXX

where XXXXXX is a number with leading zeros and enough digits to let you never exceed the maximum.

You then have two options: list all files, pick the last and increment, or store the previous value of the counter.

In the first case you have some options. For instance, this answer uses glob (you just have to sort it, using for instance this syntax: sorted(glob.glob('/path/to/files/myfile_*')). This returns you a list; pick the last, remove the first part in order to have just the number, and increment it.

The second way is easier but requires you to create another file. In this file you simply store the current number, and save it back whenever you create a new file. You can even make it a settings file (where you also store the path to the files, and various options). If you want to put more info than just the number, think of using a more structured file such as an XML or a JSON file.

The first way is less error prone and more automatic. The second is easier to implement.

Upvotes: 3

Rafael Santos
Rafael Santos

Reputation: 21

Have you tried uuid.uuid1()?

It has a time component so the generated uuids can be ordered.

From the docs:

Generate a UUID from a host ID, sequence number, and the current time.

The default node parameter uses the MAC address of the machine, so multiple machines running your code at the same time could never generate the same uuid.

Upvotes: 2

Related Questions