473,569 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generating a unique identifier

I have an application that will be producing many instances, using them
for a while, then tossing them away, and I want each one to have a unique
identifier that won't be re-used for the lifetime of the Python session.

I can't use the id() of the object, because that is only guaranteed to be
unique during the lifetime of the object.

For my application, it doesn't matter if the ids are predictable, so I
could do something as simple as this:

def unique_id():
n = 1234567890
while True:
yield n
n += 1

unique_id = unique_id()

while Application_Is_ Running:
make_an_object( id=unique_id())
do_stuff_with_o bjects()
delete_some_of_ them()

which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed. Also, for other
applications, I might want them to be rather less predictable.

--
Steven.
Sep 7 '07 #1
14 7526
On Fri, Sep 07, 2007 at 12:03:23PM -0000, Steven D'Aprano wrote:
[...]
which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed. Also, for other
applications, I might want them to be rather less predictable.
2.5 includes the uuid module for RFC 4122 universally-unique IDs:

http://docs.python.org/lib/module-uuid.html

--

[Will Maier]-----------------[wi*******@ml1.n et|http://www.lfod.us/]
Sep 7 '07 #2
On Fri, 07 Sep 2007 12:03:23 +0000, Steven D'Aprano wrote:
I have an application that will be producing many instances, using them
for a while, then tossing them away, and I want each one to have a unique
identifier that won't be re-used for the lifetime of the Python session.

I can't use the id() of the object, because that is only guaranteed to be
unique during the lifetime of the object.

For my application, it doesn't matter if the ids are predictable, so I
could do something as simple as this:

def unique_id():
n = 1234567890
while True:
yield n
n += 1

unique_id = unique_id()

while Application_Is_ Running:
make_an_object( id=unique_id())
do_stuff_with_o bjects()
delete_some_of_ them()

which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed.
For that easy solution you can use `itertools.coun t()`.

Ciao,
Marc 'BlackJack' Rintsch
Sep 7 '07 #3
On Sep 7, 7:03 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
I have an application that will be producing many instances, using them
for a while, then tossing them away, and I want each one to have a unique
identifier that won't be re-used for the lifetime of the Python session.

I can't use the id() of the object, because that is only guaranteed to be
unique during the lifetime of the object.

For my application, it doesn't matter if the ids are predictable, so I
could do something as simple as this:

def unique_id():
n = 1234567890
while True:
yield n
n += 1

unique_id = unique_id()

while Application_Is_ Running:
make_an_object( id=unique_id())
do_stuff_with_o bjects()
delete_some_of_ them()

which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed. Also, for other
applications, I might want them to be rather less predictable.

--
Steven.
You could always use the md5 module along with time.time() to generate
a unique id.

Mike

Sep 7 '07 #4
Will Maier wrote:
On Fri, Sep 07, 2007 at 12:03:23PM -0000, Steven D'Aprano wrote:
[...]
>which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed. Also, for other
applications , I might want them to be rather less predictable.

2.5 includes the uuid module for RFC 4122 universally-unique IDs:

http://docs.python.org/lib/module-uuid.html
Jesus Christ! What in all the world *doesn't* Python do already?!

Can't we now just write up a module for the standard lib that takes care
of writing all the other code as well and just forget about getting our
hands dirty with programming altogether?

/W
(just in case: ;)!)
Sep 7 '07 #5
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrites:
def unique_id():
n = 1234567890
while True:
yield n
n += 1
unique_id = itertools.count (1234567890)
which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed. Also, for other
applications, I might want them to be rather less predictable.
def unique_id():
return os.urandom(10). encode('hex')
Sep 7 '07 #6
Paul Rubin <http://ph****@NOSPAM.i nvalidwrites:
def unique_id():
return os.urandom(10). encode('hex')
Sorry, make that 32 or 40 instead of 10, if the number of id's is large,
to make birthday collisions unlikely.

If you don't want the id's to be that large, you can implement a
Feistel cipher using md5 or sha as the round function pretty
straightforward ly, then just feed successive integers through it.
That also guarantees uniqueness, at least within one run of the
program. I have some sample code around for that, let me know if you
need it.
Sep 7 '07 #7
Will Maier <wi*******@ml1. netwrites:
On Fri, Sep 07, 2007 at 12:03:23PM -0000, Steven D'Aprano wrote:
[...]
which is easy enough, but I thought I'd check if there was an
existing solution in the standard library that I missed. Also, for
other applications, I might want them to be rather less
predictable.

2.5 includes the uuid module for RFC 4122 universally-unique IDs:

http://docs.python.org/lib/module-uuid.html
I second this recommendation. If you want arbitrary unique IDs that
are not a function of the data they identify, the simplest solution is
a monotonically-increasing serial number. If you want more than that,
you might as well go straight to the standard UUIDs.

Even if you're not using Python 2.5, grab the external 'python-uuid'
package for your operating system and use that. If you use the options
for including random elements, the generated UUIDs will even satisfy
your "unpredicta ble" requirement.

--
\ "This sentence contradicts itself -- no actually it doesn't." |
`\ -- Douglas Hofstadter |
_o__) |
Ben Finney
Sep 7 '07 #8
On Fri, 07 Sep 2007 08:42:45 -0700, Paul Rubin wrote:
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com .auwrites:
>def unique_id():
n = 1234567890
while True:
yield n
n += 1

unique_id = itertools.count (1234567890)
Sweet!

I really must make itertools second-nature. I always forget it.

>which is easy enough, but I thought I'd check if there was an existing
solution in the standard library that I missed. Also, for other
applications , I might want them to be rather less predictable.

def unique_id():
return os.urandom(10). encode('hex')
Any time I see something using a random number to generate IDs, I worry
about collisions. Am I being paranoid? (But even paranoids write code
with bugs...)
Here's something which is a little less predictable than a straight
counter:

def unpredictable_c ounter(n=123456 78):
while True:
n += random.randint( 1, 68)
yield n

def more_unpredicta ble_counter(n=1 234567):
uc = unpredictable_c ounter(n)
pool = []
while True:
if not pool:
pool = [None]*99
for i in range(99):
pool[i] = uc.next()
random.shuffle( pool)
yield pool.pop()


--
Steven.
Sep 8 '07 #9
Ben Finney <bi************ ****@benfinney. id.auwrites:
http://docs.python.org/lib/module-uuid.html
I second this recommendation. If you want arbitrary unique IDs that
are not a function of the data they identify, the simplest solution is
a monotonically-increasing serial number. If you want more than that,
you might as well go straight to the standard UUIDs.
The standard UUID's as described in that url don't make any serious
attempt to be unguessable. It's better to use a string from os.urandom().

To Stephen: double oops, I was thinking of hex digits rather than bytes
when I suggested reading 32 (length of an md5 hash) or 40 (length of
an sha1 hash) from os.urandom. That should have said 16 or 20 bytes.

If k is the number of unique id's you'll actually use, and N is the
number of possible random uid's (so for 16 bytes, N=2**128 since
16 bytes is 128 bits), the probability of a collision occuring is
approximately exp(-k**2 / (2*N)), assuming k is small compared
with sqrt(N).
Sep 8 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1974
by: Mary | last post by:
Hi, I need some assistance with a query. To be honest, I'm not even sure it can be done. I'll try to keep the information limited to only what's relevant to what I have and what I am trying to achieve with this query. I have a table that contains around 100,000 records. For the sake of this discussion, assume just two columns: ID Data
3
2700
by: Kilroy Programmer | last post by:
Is there a way to store a unique numeric identifier (say, for example, an int) into a TreeNode, so that when the TreeNode is checked (since CheckBoxes is enabled) the eventhandler AfterCheck() can examine the responsible Node's identifier to see which TreeNode triggered the event? Analyzing the Node's Text string is undesirable because it...
2
1251
by: sparks | last post by:
ok I was generating a combo of things into a unique identifier. Private Sub generatenumber() Dim db As DAO.Database Dim rs As DAO.Recordset Dim StudentNumber As Integer Set db = CurrentDb Set rs = db.OpenRecordset("SELECT Max(qryParticipantids.gennumber) AS MaxofGenNUMBER FROM qryParticipantids;") If rs.RecordCount > 0 Then
4
10115
by: ba.hons | last post by:
Hello all, Was wondering if anyone could provide some info on what could be a possible solution to a problem am having. I have to generate a Unique Identifier in C# which I will use to assign to a user, i dont really need the Unique Identifier's to be sequential so i was considering using system.time.hour + system.time.minute. +...
2
6196
by: Ken | last post by:
Hi, I have a form whose control source is a view from SQL server 2005 database. The view has a primary key that is a unique identifier field with keys generated by newid() function from SQL server 2005. Anyhow when I try to view/assign the unique identifier field and debug/ use watchlist I get this "????????" for the value of the unique...
4
4234
by: Rob Stevens | last post by:
Is there some sort of unique identifier in every treenode that is consistent? I was looking at the handle of every treenode, but it appears that the handle changes everytime the tree is built. I have seen in some databases where they store the treenodes they are using some serial number of the node. I don't have a clue what that is, but...
2
3578
by: Gary Hasler | last post by:
Does anyone have any suggestions on generating GUID (Globally Unique Identifier) tags with php? They would need to be in the format 4402bd8a-cd51-40ea-99d7-b510e89e344b Specifically this is for adding accessibility for the PicLens browser add-on (which uses an .rss file) to some of our photo gallery pages. It seems to require each...
4
2365
by: Mufasa | last post by:
I'm looking for a way to get a truly unique identifier for a machine for our client software. I'd like to have it so that there's little or no setup by the end user. (We set up the machines and then ship them out for most cases but some of our customers do provide their own machines.) Our concern is if somebody takes one of our machines and...
13
3693
by: mliptak | last post by:
I'm trying to implement logging in my application, so that each log message has its unique identifier, e.g. log(identifier, text) What I want to achieve is that the compiler screams if the log() with 'identifier' is also used in some other place in the code which would make the 'identifier' not unique. Is that something that can be achieved...
0
7628
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7938
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8145
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6317
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5245
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3679
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1236
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.