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.
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.
(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:
_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:
There is only one thing generating unique IDs
Your items have some concept of a title
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
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.
发布评论
评论(5)
您想要做的是将音节拼接在一起以创建可发音的伪单词。您可以用任何您喜欢的语言创建音节来组成可以发音和交流但实际上没有任何意义的单词。
这里有一篇文章介绍了一个人如何创建人类可读的 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.
对于电子邮件,我使用的是:
您可以通过更改数字来增加/减少长度。
有时您会得到 + 和 / 字符,如果您愿意,可以将它们删除。
编辑:
由于您还想通过电话传递它们,也许
b32encode(urandom(5))
会是更好的选择,因为它不会给您任何小写或不寻常的字符。For emails, what I use is:
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.像亚马逊的付费短语这样的东西怎么样?将二进制 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).
当然可以,但是它需要对您的问题空间进行更多限制,即:
然后您会执行以下操作:
Sure, but it requires a few more restrictions on your problem space, namely:
Then you'd do something like:
这是一个基于 uuid 的示例。调整 1000000 以增加或减少 id 的范围。由于您正在缩小 id 的范围,因此您可能必须检查该 ID 是否已存在。
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.