生成唯一标识符 [英] Generating a unique identifier

查看:106
本文介绍了生成唯一标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序将生成许多实例,使用它们

一段时间,然后扔掉它们,我希望每个实例都有一个独特的

在Python会话的生命周期内不会重复使用的标识符。


我不能使用对象的id(),因为这只能保证在对象的生命周期中,
是唯一的。


对于我的应用程序,如果id是可预测的并不重要,所以我
可以做一些简单的事情:


def unique_id():

n = 1234567890

而True :

收益率n

n + = 1


unique_id = unique_id()


同时Application_Is_Running:

make_an_object(id = unique_id())

do_stuff_with_objects()

delete_some_of_them()


这很容易,但我想我会检查标准库中是否有现有的

解决方案我错过了此外,对于其他

应用程序,我可能希望它们更难以预测。


-

Steven。

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_objects()
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.

推荐答案

2007年9月7日星期五下午12:03:23 -0000,Steven D''Aprano写道:

[...]
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包括用于RFC 4122普遍唯一ID的uuid模块:

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


-


[Will Maier] ----------------- [wi ******* @ ml1 .net | http://www.lfod.us/]


On Fri,2007年9月7日12:03:23 +0000,Steven D''Aprano写道:
On Fri, 07 Sep 2007 12:03:23 +0000, Steven D''Aprano wrote:

我有一个应用程序将生成许多实例,使用它们暂停一段时间,然后扔掉它们,然后将它们扔掉,我希望每个实例都有一个独特的

标识符,赢得''在Python会话的生命周期中重复使用。


我不能使用对象的id(),因为这只能保证是

在对象的生命周期内是唯一的。


对于我的应用程序,它是d如果id是可预测的那么无关紧要,所以我可以做一些像这样简单的事情:


def unique_id():

n = 1234567890

而True:

收益率n

n + = 1


unique_id = unique_id()


而Application_Is_Running:

make_an_object(id = unique_id())

do_stuff_with_objects()

delete_some_of_them()


这很容易,但我想我会检查标准中是否有现有的

解决方案我错过的图书馆。
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_objects()
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.



对于这个简单的解决方案,你可以使用`itertools.count()`。


Ciao,

Marc''BlackJack''Rintsch

For that easy solution you can use `itertools.count()`.

Ciao,
Marc ''BlackJack'' Rintsch


9月7日上午7:03,Steven D''Aprano< st ... @ REMOVE-THIS-

cybersource.com.auwrote:
On Sep 7, 7:03 am, Steven D''Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:

我有一个应用程序将生成许多实例,使用它们

一段时间,然后扔掉它们,我希望每个人都有一个唯一的

标识符,在Python会话的生命周期内不会被重复使用。


我不能使用对象的id(),因为在对象的生命周期中,它只能保证是唯一的。


对于我的应用程序,如果id是可预测的并不重要,所以我

可以做一些简单的事情:


def unique_id():

n = 1234567890

而True:

收益率n

n + = 1


unique_id = unique_id()


而Application_Is_Running:

make_an_object(id = unique_id())

do_stuff_with_objects()

delete_some_of_them()


这很容易,但我想我'' d检查我错过的标准库中是否存在现有的

解决方案。此外,对于其他

应用程序,我可能希望它们更难以预测。


-

史蒂文。
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_objects()
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.



您可以随时使用md5模块和time.time()来生成

a唯一ID。

Mike

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

Mike


这篇关于生成唯一标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆