shortuuid-0.3.1/0000775000175000017500000000000012207621450014766 5ustar stavrosstavros00000000000000shortuuid-0.3.1/README.rst0000664000175000017500000000612312207621267016465 0ustar stavrosstavros00000000000000=========== Description =========== ``shortuuid`` is a simple python library that generates concise, unambiguous, URL-safe UUIDs. Often, one needs to use non-sequential IDs in places where users will see them, but the IDs must be as concise and easy to use as possible. ``shortuuid`` solves this problem by generating uuids using Python's built-in ``uuid`` module and then translating them to base57 using lowercase and uppercase letters and digits, and removing similar-looking characters such as l, 1, I, O and 0. .. image:: https://travis-ci.org/stochastic-technologies/shortuuid.png :target: https://travis-ci.org/stochastic-technologies/shortuuid Installation ------------ To install ``shortuuid`` you need: * Python 2.5 or later in the 2.x line (earlier than 2.5 not tested). If you have the dependencies, you have multiple options of installation: * With pip (preferred), do ``pip install shortuuid``. * With setuptools, do ``easy_install shortuuid``. * To install the source, download it from https://github.com/stochastic-technologies/shortuuid and do ``python setup.py install``. Usage ----- To use ``shortuuid``, just import it in your project like so: >>> import shortuuid You can then generate a short UUID: >>> shortuuid.uuid() 'vytxeTZskVKR7C7WgdSP3d' If you prefer a version 5 UUID, you can pass a name (DNS or URL) to the call and it will be used as a namespace (uuid.NAMESPACE_DNS or uuid.NAMESPACE_URL) for the resulting UUID: >>> shortuuid.uuid(name="example.com") 'wpsWLdLt9nscn2jbTD3uxe' >>> shortuuid.uuid(name="http://example.com") 'c8sh5y9hdSMS6zVnrvf53T' To see the alphabet that is being used to generate new UUIDs: >>> shortuuid.get_alphabet() '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' If you want to use your own alphabet to generate UUIDs, use ``set_alphabet()``: >>> shortuuid.set_alphabet("aaaaabcdefgh1230123") >>> shortuuid.uuid() '0agee20aa1hehebcagddhedddc0d2chhab3b' ``shortuuid`` will automatically sort and remove duplicates from your alphabet to ensure consistency: >>> shortuuid.get_alphabet() '0123abcdefgh' If the default 22 digits are too long for you, you can get shorter IDs by just truncating the string to the desired length. The IDs won't be universally unique any longer, but the probability of a collision will still be very low. To serialize existing UUIDs, use ``encode()`` and ``decode()``: >>> import uuid ; u = uuid.uuid4() ; u UUID('6ca4f0f8-2508-4bac-b8f1-5d1e3da2247a') >>> s = shortuuid.encode(u) ; s 'cu8Eo9RyrUsV4MXEiDZpLM' >>> shortuuid.decode(s) == u True >>> short = s[:7] ; short 'cu8Eo9R' >>> h = shortuuid.decode(short) UUID('00000000-0000-0000-0000-00b8c0b9f952') >>> shortuuid.decode(shortuuid.encode(h)) == h True Class-based usage ----------------- If you need to have various alphabets per-thread, you can use the `ShortUUID` class, like so: >>> su = shortuuid.ShortUUID(alphabet="01345678") >>> su.uuid() '034636353306816784480643806546503818874456' >>> su.get_alphabet() '01345678' >>> su.set_alphabet("21345687654123456") >>> su.get_alphabet() '12345678' License ------- ``shortuuid`` is distributed under the BSD license. shortuuid-0.3.1/setup.cfg0000664000175000017500000000007312207621450016607 0ustar stavrosstavros00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 shortuuid-0.3.1/PKG-INFO0000664000175000017500000000073312207621450016066 0ustar stavrosstavros00000000000000Metadata-Version: 1.0 Name: shortuuid Version: 0.3.1 Summary: A generator library for concise, unambiguous and URL-safe UUIDs. Home-page: https://github.com/stochastic-technologies/shortuuid/ Author: Stochastic Technologies Author-email: info@stochastictechnologies.com License: BSD Description: A library that generates short, pretty, unambiguous unique IDs by using an extensive, case-sensitive alphabet and omitting similar-looking letters and numbers. Platform: UNKNOWN shortuuid-0.3.1/shortuuid/0000775000175000017500000000000012207621450017014 5ustar stavrosstavros00000000000000shortuuid-0.3.1/shortuuid/main.py0000664000175000017500000000476312207621267020332 0ustar stavrosstavros00000000000000""" Concise UUID generation. """ import uuid as _uu class ShortUUID(object): def __init__(self, alphabet=None): if alphabet is None: alphabet = list("23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz") # Define our alphabet. self._alphabet = alphabet self._alpha_len = len(self._alphabet) def encode(self, uuid): """ Encodes a UUID into a string (LSB first) according to the alphabet If leftmost (MSB) bits 0, string might be shorter """ unique_id = uuid.int output = "" while unique_id: unique_id, digit = divmod(unique_id, self._alpha_len) output += self._alphabet[digit] return output def decode(self, string): """ Decodes a string according to the current alphabet into a UUID Raises ValueError when encountering illegal characters or too long string If string too short, fills leftmost (MSB) bits with 0. """ number = 0 for char in string[::-1]: number = number * self._alpha_len + self._alphabet.index(char) return _uu.UUID(int=number) def uuid(self, name=None): """ Generate and return a UUID. If the name parameter is provided, set the namespace to the provided name and generate a UUID. """ # If no name is given, generate a random UUID. if name is None: uuid = _uu.uuid4() elif not "http" in name.lower(): uuid = _uu.uuid5(_uu.NAMESPACE_DNS, name) else: uuid = _uu.uuid5(_uu.NAMESPACE_URL, name) return self.encode(uuid) def get_alphabet(self): """Return the current alphabet used for new UUIDs.""" return ''.join(self._alphabet) def set_alphabet(self, alphabet): """Set the alphabet to be used for new UUIDs.""" # Turn the alphabet into a set and sort it to prevent duplicates # and ensure reproducibility. new_alphabet = list(sorted(set(alphabet))) if len(new_alphabet) > 1: self._alphabet = new_alphabet self._alpha_len = len(self._alphabet) else: raise ValueError("Alphabet with more than one unique symbols required.") # For backwards compatibility _global_instance = ShortUUID() encode = _global_instance.encode decode = _global_instance.decode uuid = _global_instance.uuid get_alphabet = _global_instance.get_alphabet set_alphabet = _global_instance.set_alphabet shortuuid-0.3.1/shortuuid/tests.py0000664000175000017500000000560412207621267020543 0ustar stavrosstavros00000000000000import os import sys import unittest from uuid import UUID, uuid4 sys.path.insert(0, os.path.abspath(__file__ + "/../..")) from shortuuid.main import * class LegacyShortUUIDTest(unittest.TestCase): def test_generation(self): self.assertTrue(20 < len(uuid()), 24) self.assertTrue(20 < len(uuid("http://www.example.com/")) < 24) self.assertTrue(20 < len(uuid("HTTP://www.example.com/")) < 24) self.assertTrue(20 < len(uuid("example.com/")) < 24) def test_encoding(self): u = UUID('{12345678-1234-5678-1234-567812345678}') self.assertEquals(encode(u), "VoVuUtBhZ6TvQSAYEqNdF5") def test_decoding(self): u = UUID('{12345678-1234-5678-1234-567812345678}') self.assertEquals(decode("VoVuUtBhZ6TvQSAYEqNdF5"), u) def test_alphabet(self): backup_alphabet = get_alphabet() alphabet = "01" set_alphabet(alphabet) self.assertEquals(alphabet, get_alphabet()) set_alphabet("01010101010101") self.assertEquals(alphabet, get_alphabet()) self.assertEquals(set(uuid()), set("01")) self.assertTrue(116 < len(uuid()) < 140) u = uuid4() self.assertEquals(u, decode(encode(u))) u = uuid() self.assertEquals(u, encode(decode(u))) self.assertRaises(ValueError, set_alphabet, "1") self.assertRaises(ValueError, set_alphabet, "1111111") set_alphabet(backup_alphabet) class ClassShortUUIDTest(unittest.TestCase): def test_generation(self): su = ShortUUID() self.assertTrue(20 < len(su.uuid()) < 24) self.assertTrue(20 < len(su.uuid("http://www.example.com/")) < 24) self.assertTrue(20 < len(su.uuid("HTTP://www.example.com/")) < 24) self.assertTrue(20 < len(su.uuid("example.com/")) < 24) def test_encoding(self): su = ShortUUID() u = UUID('{12345678-1234-5678-1234-567812345678}') self.assertEquals(su.encode(u), "VoVuUtBhZ6TvQSAYEqNdF5") def test_decoding(self): su = ShortUUID() u = UUID('{12345678-1234-5678-1234-567812345678}') self.assertEquals(su.decode("VoVuUtBhZ6TvQSAYEqNdF5"), u) def test_alphabet(self): alphabet = "01" su1 = ShortUUID(alphabet) su2 = ShortUUID() self.assertEquals(alphabet, su1.get_alphabet()) su1.set_alphabet("01010101010101") self.assertEquals(alphabet, su1.get_alphabet()) self.assertEquals(set(su1.uuid()), set("01")) self.assertTrue(116 < len(su1.uuid()) < 140) self.assertTrue(20 < len(su2.uuid()) < 24) u = uuid4() self.assertEquals(u, su1.decode(su1.encode(u))) u = su1.uuid() self.assertEquals(u, su1.encode(su1.decode(u))) self.assertRaises(ValueError, su1.set_alphabet, "1") self.assertRaises(ValueError, su1.set_alphabet, "1111111") if __name__ == '__main__': unittest.main() shortuuid-0.3.1/shortuuid/__init__.py0000664000175000017500000000012712207621267021133 0ustar stavrosstavros00000000000000from shortuuid.main import encode, decode, uuid, get_alphabet, set_alphabet, ShortUUID shortuuid-0.3.1/shortuuid.egg-info/0000775000175000017500000000000012207621450020506 5ustar stavrosstavros00000000000000shortuuid-0.3.1/shortuuid.egg-info/top_level.txt0000664000175000017500000000001212207621450023231 0ustar stavrosstavros00000000000000shortuuid shortuuid-0.3.1/shortuuid.egg-info/PKG-INFO0000664000175000017500000000073312207621450021606 0ustar stavrosstavros00000000000000Metadata-Version: 1.0 Name: shortuuid Version: 0.3.1 Summary: A generator library for concise, unambiguous and URL-safe UUIDs. Home-page: https://github.com/stochastic-technologies/shortuuid/ Author: Stochastic Technologies Author-email: info@stochastictechnologies.com License: BSD Description: A library that generates short, pretty, unambiguous unique IDs by using an extensive, case-sensitive alphabet and omitting similar-looking letters and numbers. Platform: UNKNOWN shortuuid-0.3.1/shortuuid.egg-info/dependency_links.txt0000664000175000017500000000000112207621450024554 0ustar stavrosstavros00000000000000 shortuuid-0.3.1/shortuuid.egg-info/SOURCES.txt0000664000175000017500000000034612207621450022375 0ustar stavrosstavros00000000000000COPYING MANIFEST.in README.rst setup.py shortuuid/__init__.py shortuuid/main.py shortuuid/tests.py shortuuid.egg-info/PKG-INFO shortuuid.egg-info/SOURCES.txt shortuuid.egg-info/dependency_links.txt shortuuid.egg-info/top_level.txtshortuuid-0.3.1/setup.py0000664000175000017500000000130612207621430016476 0ustar stavrosstavros00000000000000#!/usr/bin/env python import sys assert sys.version >= '2.5', "Requires Python v2.5 or above." from setuptools import setup setup( name = "shortuuid", version = "0.3.1", author = "Stochastic Technologies", author_email = "info@stochastictechnologies.com", url = "https://github.com/stochastic-technologies/shortuuid/", description = "A generator library for concise, unambiguous and URL-safe UUIDs.", long_description = "A library that generates short, pretty, unambiguous unique IDs " " by using an extensive, case-sensitive alphabet and omitting " "similar-looking letters and numbers.", license = "BSD", packages = ["shortuuid"], ) shortuuid-0.3.1/MANIFEST.in0000664000175000017500000000013012207621430016514 0ustar stavrosstavros00000000000000include MANIFEST.in include COPYING include README.rst recursive-include shortuuid *.py shortuuid-0.3.1/COPYING0000664000175000017500000000272412207621267016034 0ustar stavrosstavros00000000000000Copyright (c) 2011, Stochastic Technologies All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of Stochastic Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.