Coder Social home page Coder Social logo

ulid2's Introduction

ulid2

MIT License Build Status codecov

ULID (Universally Unique Lexicographically Sortable Identifier) encoding and decoding for Python.

  • Python 2.7 and 3.x compatible
  • Supports binary ULIDs
  • Supports bidirectional conversion of base32 ULIDs
  • Supports expressing ULIDs as UUIDs

Usage

Generating ULIDs

  • Use ulid2.generate_binary_ulid() to generate a raw binary ULID
  • Use ulid2.generate_ulid_as_uuid() to generate an ULID as an uuid.UUID
  • Use ulid2.generate_ulid_as_base32() to generate an ULID as ASCII

These functions accept optional arguments:

  • timestamp: a datetime.datetime or integer UNIX timestamp to base the ULID on.
  • monotonic: boolean; whether to attempt to ensure ULIDs are monotonically increasing. Monotonic behavior is not guaranteed when used from multiple threads.

Parsing ULIDs

  • Use ulid2.get_ulid_time(ulid) to get the time from an ULID (in any format)

Converting ULIDs

  • Use ulid2.ulid_to_base32(ulid) to convert an ULID to its ASCII representation
  • Use ulid2.ulid_to_uuid(ulid) to convert an ULID to its UUID representation
  • Use ulid2.ulid_to_binary(ulid) to convert an ULID to its binary representation

Base32

  • Use ulid2.encode_ulid_base32(binary) to convert 16 bytes to 26 ASCII characters
  • Use ulid2.decode_ulid_base32(ascii) to convert 26 ASCII characters to 16 bytes

Django compatibility

As ulid2 is capable of expressing ULIDs as Python UUIDs, it's directly compatible with Django's UUIDFields. For instance, to ULID-ify a model's primary key, simply

from django.db import models
from ulid2 import generate_ulid_as_uuid

class MyModel(models.Model):
    id = models.UUIDField(default=generate_ulid_as_uuid, primary_key=True)

and you're done!

Why the 2 in the name?

ulid is already taken by mdipierro's implementation. :)

Prior Art

ulid2's People

Contributors

akx avatar itdaniher avatar ruksi avatar larsyencken avatar

Watchers

 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.