what's the 5 character alphanumeric id in redd

2019-02-03 06:41发布

问题:

Whats 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

how is it generated?

update: @Gerald, Thanks for the code. I initially thought this is some obfuscation of the id. But, 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'

回答1:

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)


回答2:

That looks like a unique id for the thread. It's most likely used to find the thread in the database.



回答3:

Little remark.

It is not sufficient for this example but usually appending to lists

a = []
for i in range(NNN): a.append(i)
a.reverse()

really more efficient than inserting at head.

a = []
for i in range(NNN): a.insert(0,i)

.