reddit URL 中的字母数字 ID 是什么? [英] What is the alphanumeric id in a reddit URL?
问题描述
reddit URL中的7n5lu
是什么
http://www.reddit.com/r/reddit.com/comments/7n5lu/man_can_fly_if_you_watch_one_video_in_2
...它是如何生成的?
更新:@Gerald,我最初认为这是对 id 的一些混淆.它只是将整数转换为更紧凑的表示.我在想,为什么要这样做?为什么不使用原始整数本身!!
<预><代码>>>>to36(4000)'334'>>>to36(4001)'335'reddit 源代码 可用!这是我在生成该字符串时发现的内容:
def to_base(q,alphabet):如果 q<0:引发 ValueError,必须提供一个正整数"l = len(字母)转换 = []而 q != 0:q, r = divmod(q, l)转换.插入(0,字母[r])返回 "".join(converted) 或 '0'def to36(q):返回到_base(q,'0123456789abcdefghijklmnopqrstuvwxyz')
和其他地方,在链接"类下:
@propertydef _id36(self):返回到36(self._id)
What is the 7n5lu
in the reddit URL
http://www.reddit.com/r/reddit.com/comments/7n5lu/man_can_fly_if_you_watch_one_video_in_2
...and how is it generated?
Update: @Gerald, I initially thought this is some obfuscation of the id. It is just doing the conversion from integer to a more compact representation. I am thinking, why is this being done? why not use the original integer itself!!
>>> to36(4000)
'334'
>>> to36(4001)
'335'
The reddit source code is available! Here is what I found for generating that string:
def to_base(q, alphabet):
if q < 0: raise ValueError, "must supply a positive integer"
l = len(alphabet)
converted = []
while q != 0:
q, r = divmod(q, l)
converted.insert(0, alphabet[r])
return "".join(converted) or '0'
def to36(q):
return to_base(q, '0123456789abcdefghijklmnopqrstuvwxyz')
and elsewhere, under the "Link" class:
@property
def _id36(self):
return to36(self._id)
这篇关于reddit URL 中的字母数字 ID 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!