[Zope] Which method to generate IDs for objects?

Maik Jablonski maik.jablonski@uni-bielefeld.de
Wed, 16 Jan 2002 15:13:18 +0100


hello juergen,

a simple approach: i would use hash() on a unique "property" of your 
objects... this can be the source of the document or a combination of 
unique properties...

for example: if you would like to store emails, you could use a hash of 
"sender","subject","date","body".

id=str(hash(sender+subject+date+body))

mails with exactly the same values for that would be identical (result 
will be the same hash-value)... and there's no need to store them.

this would fail if you have no unique properties... but that would mean 
to create thousands of documents with exactly the same contents & 
props... very unlikely i think, because one document would be enough in 
such a case.

another way could be:

id = "objId_%d" % len(container.objectValues())

this results in unique values if you don't delete an object out of the 
container...

i think there may be better ways, but hope this helps a bit...

mjablonski

> Hi!
>
> I am just experimenting with ZClasses and I ran into the following 
> problem: when I instantiate an object this object needs an unique ID. 
> There are some ways to generate this. First of all I thought of the 
> system time as ID, but this can make problems, even though not with 
> high probability. Does anybody out there have a better solution, I 
> think of an ID generation like in a sequence: object_id_001, 
> object_id_002, ... but how can I determine the last object ID?
>
> Other (better, more simple) ideas?
>
> Juergen
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )