如何在Python中生成一个人类友好的唯一ID?

发布于 2024-11-25 11:51:31 字数 1431 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

总攻大人 2024-12-02 11:51:31

您想要做的是将音节拼接在一起以创建可发音的伪单词。您可以用任何您喜欢的语言创建音节来组成可以发音和交流但实际上没有任何意义的单词。

这里有一篇文章介绍了一个人如何创建人类可读的 UID 来进行语音发音以及一些陷阱。

阅读上面的链接,了解采用此类方法时应考虑的一些陷阱。

您可以只使用一串字母,但将它们显示为 NATO 语音字母,而不仅仅是字母。

What you want to do is stitch together syllables to create pronounceable pseudo words. You can create syllables in any language you like to make up words that can be pronounced and communicated but don't actually mean anything.

Here is an article about how one person created human readable UIDs for speaking them phonetically and some of the pitfalls.

Read the above link for just some of the pitfalls you should consider when taking an approach like this.

You could just use a string of alphabetic letters but present them as the NATO phonetic alphabet instead of just the alphabet.

错々过的事 2024-12-02 11:51:31

对于电子邮件,我使用的是:

from base64 import b64encode
from os import urandom
key = b64encode(urandom(9))

您可以通过更改数字来增加/减少长度。
有时您会得到 + 和 / 字符,如果您愿意,可以将它们删除。

编辑:
由于您还想通过电话传递它们,也许 b32encode(urandom(5)) 会是更好的选择,因为它不会给您任何小写或不寻常的字符。

For emails, what I use is:

from base64 import b64encode
from os import urandom
key = b64encode(urandom(9))

You can increase/decrease the length by changing the number.
Sometimes you will get + and / characters and you can strip them out if you like.

Edit:
Since you also want to pass them over the phone maybe b32encode(urandom(5)) would be a better choice since it wont give you any lowercase or unusual characters.

帅哥哥的热头脑 2024-12-02 11:51:31

像亚马逊的付费短语这样的东西怎么样?将二进制 ID 转换为英文单词序列。

如果您想要与 UUID 具有相同范围的内容,则需要表示 16 个字节。
为了保持合理,将短语限制为 4 个单词,因此每个单词代表 4 个字节,或 65536 种可能性,因此您需要一个包含 262,144 个单词的字典。

编辑:
实际上,经过反思,更好的可能是一种疯狂的 lib 句子 - 它将限制所需单词的数量,并且可能使其更容易记住,因为它具有语法结构。当然,它需要更长,也许像这样:

(a/an/the/#) (adj) (名词) (动词)(时态) (副词) while (a/an/the/#) ( adj)(名词)(动词)(副词)。

How about something like Amazon's payphrases? Convert the binary ID to a sequence of english words.

If you want something with the same range as a UUID, you need to represent 16 bytes.
To keep it reasonable, restrict the phrase to 4 words, so each word represents 4 bytes, or 65536 possibilities, so you'll need a dictionary of 262,144 words.

EDIT:
Actually on reflection what might be better is a sort of mad lib sentence - it will restrict the number of needed words and may make it easier to remember since it has a grammatical structure. It will need to be longer, of course, perhaps something like this:

(a/an/the/#) (adj) (noun) (verb)(tense) (adverb) while (a/an/the/#) (adj) (noun) (verb) (adverb).

萤火眠眠 2024-12-02 11:51:31

当然可以,但是它需要对您的问题空间进行更多限制,即:

  1. 只有一件事生成唯一的 ID
  2. 您的项目有一些标题的概念
  3. 您可以保留一个字符串列表

然后您会执行以下操作:

_UID_INTERNALS = set()

def getID(obj):
    if hasattr(obj, 'UID'):
        return obj.UID
    title = obj.title.encode("ascii", errors="ignore")
    title = title.lower()
    title = "-".join(title.split())
    if not title:
        title = "unnamed-object"
    UID = title
    num = 1
    while UID in _UID_INTERNALS:
        UID = title + str(num)
        num += 1
    _UID_INTERNALS.add(UID)
    obj.UID = UID
    return UID

Sure, but it requires a few more restrictions on your problem space, namely:

  1. There is only one thing generating unique IDs
  2. Your items have some concept of a title
  3. You can persist a list of strings

Then you'd do something like:

_UID_INTERNALS = set()

def getID(obj):
    if hasattr(obj, 'UID'):
        return obj.UID
    title = obj.title.encode("ascii", errors="ignore")
    title = title.lower()
    title = "-".join(title.split())
    if not title:
        title = "unnamed-object"
    UID = title
    num = 1
    while UID in _UID_INTERNALS:
        UID = title + str(num)
        num += 1
    _UID_INTERNALS.add(UID)
    obj.UID = UID
    return UID
潇烟暮雨 2024-12-02 11:51:31

这是一个基于 uuid 的示例。调整 1000000 以增加或减少 id 的范围。由于您正在缩小 id 的范围,因此您可能必须检查该 ID 是否已存在。

>>> import uuid
>>> hash(str(uuid.uuid1())) % 1000000
380539
>>> hash(str(uuid.uuid1())) % 1000000
411563

Here's a uuid-based example. Adjust the 1000000 to increase or decrease the range of your ids. Since you're reducing the range of the id, you'll probably have to check to see if the ID already exists.

>>> import uuid
>>> hash(str(uuid.uuid1())) % 1000000
380539
>>> hash(str(uuid.uuid1())) % 1000000
411563
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文