Generate unique ID for URL

Steve Howell showell30 at yahoo.com
Tue Nov 13 21:32:40 EST 2012


On Nov 13, 6:04 pm, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
> On Tue, 13 Nov 2012 16:13:58 -0800, Miki Tebeka wrote:
> >> I want to create a URL-safe unique ID for URL's. What do you recommend?
> >> - Compression?
> > You can use base62 with a running counter, but then you'll need a (semi)
> > centralized entity to come up with the next id.
>
> > You can see one implementation athttp://bit.ly/PSJkHS(AppEngine
> > environment).
>
> Perhaps this is a silly question, but if you're using a running counter,
> why bother with base64? Decimal or hex digits are URL safe. If there are
> no concerns about predictability, why not just use the counter directly?
>
> You can encode a billion IDs in 8 hex digits compared to 16 base64
> characters:
>
> py> base64.urlsafe_b64encode('1000000000')
> 'MTAwMDAwMDAwMA=='
> py> "%x" % 1000000000
> '3b9aca00'
>
> Short and sweet and easy: no base64 calculation, no hash function, no
> database lookup, just a trivial int to string conversion.
>
> --
> Steven

If you're dealing entirely with integers, then this works too:

    import base64

    def encode(n):
        s = ''
        while n > 0:
            s += chr(n % 256)
            n //= 256
        return base64.urlsafe_b64encode(s)

    def test():
        seen = set()
        for i in range(999900000, 1000000000):
            s = encode(i)
            if s in seen:
                raise Exception('non-unique encoding')
            seen.add(s)
        print encode(1000000000)

    test()

It prints this for 1000000000:

    AMqaOw==




More information about the Python-list mailing list