GithubHelp home page GithubHelp logo

andrewgleave / shortuuid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from skorokithakis/shortuuid

1.0 2.0 0.0 75 KB

A generator library for concise, unambiguous and URL-safe UUIDs.

Home Page: http://www.stochastictechnologies.com/

License: BSD 3-Clause "New" or "Revised" License

shortuuid's Introduction

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.

Installation

To install shortuuid you need:

  • Python 2.5 or later in the 2.x line (3.x not supported, earlier than 2.5 not tested).

If you have the dependencies, you have multiple options of installation:

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

License

shortuuid is distributed under the BSD license.

shortuuid's People

Contributors

skorokithakis avatar mrjbq7 avatar fengsi avatar

Stargazers

Andrew Gleave avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.