For cases when you don't necessarily control the data type (input from external system), you can use the parse method
which will attempt to make the correct determination for you. Please note that this will be slightly slower than creating
the instance from the respective from_* method as it needs to make a number of type/conditional checks.
Supports values as int, float, str, bytes, bytearray, memoryview, uuid.UUID, and ULID types.
>>> import ulid
>>> value = db.model.get_id() ## Unsure about datatype -- Could be int, UUID, or string?
>>> ulid.parse(value)
>>> <ULID('0K0EDFETFM8SH912DBBD4ABXSZ')>
Once you have a ULID object, there are a number of ways to interact with it.
The timestamp method will give you a snapshot view of the first 48-bits of the ULID while the randomness method
will give you a snapshot of the last 80-bits.
>>> import ulid
>>> u = ulid.new()
>>> u
<ULID('01BJQM7SC7D5VVTG3J68ABFQ3N')>
>>> u.timestamp()
<Timestamp('01BJQM7SC7')>
>>> u.randomness()
<Randomness('D5VVTG3J68ABFQ3N')>
The ULID, Timestamp, and Randomness classes all derive from the same base class, a MemoryView.
A MemoryView provides the bin, bytes, hex, int, oct, and str, methods for changing any values representation.
A fast implementation in pure python of the spec with binary format support.
Contributing
If you would like to contribute, simply fork the repository, push your changes and send a pull request.
Pull requests will be brought into the master branch via a rebase and fast-forward merge with the goal of having a linear branch history with no merge commits.
Cryptographically secure source of randomness, if possible
Sorting
The left-most character must be sorted first, and the right-most character sorted last (lexical order).
The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed
Encoding
Crockford's Base32 is used as shown. This alphabet excludes the letters I, L, O, and U to avoid confusion and abuse.
0123456789ABCDEFGHJKMNPQRSTVWXYZ
Binary Layout and Byte Order
The components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).