Skip to main content

MID module

Project description

python-mid

Efficient ID generation, lexicographically sortable, contains time information.

Structure

  • 4 bytes for the timestamp
  • 8 bytes of randomness
  • encoded as urlsafe base64
  • 16 characters
  • Example: ZEDpWWfGaXNR_0rs

Usage

>>> import mid
>>> mid.generate_mid()
'ZEDpWWfGaXNR_0rs'
>>> mid.get_date_time("ZEDpWWfGaXNR_0rs")
datetime.datetime(2017, 12, 28, 20, 22, 24)
>>> mid.get_time("ZEDpWWfGaXNR_0rs")
1514474944

Performance

Bench #1: Generate 1.000.000 IDs

UUID1 UUID4 ULID ObjectID MID
Execution Time 2.96s 3.55s 5.88s 5.43s 0.28s
Executions/s 337612.24 281647.68 170118.74 184101.31 3556948.61

FAQ

Are the resulting IDs always lexographically sortable?

Yes, the resulting MIDs generated by the generate_mid function in the C extension code I provided earlier are always lexicographically sortable.

The first four bytes of each MID represent the current time in seconds since the epoch. These bytes are stored in big-endian order, which means that MIDs generated at later times will have a larger numerical value for these first four bytes. Since the base64 encoding used to encode the MIDs preserves the lexicographical order of the input data, this means that MIDs generated at later times will also be lexicographically greater than MIDs generated at earlier times.

The remaining eight bytes of each MID are filled with random data and do not affect the lexicographical order of the MIDs.

Therefore, if you generate multiple MIDs using the generate_mid function and sort them lexicographically, they will be sorted in chronological order based on their creation time.

What about January 19, 2038?

The generate_mid function stores the current time in seconds since the epoch as a 4-byte integer in big-endian order. This means that the maximum value that can be represented by these 4 bytes is 2^32 - 1, or 4294967295.

Since the epoch is defined as January 1, 1970, this means that the latest time that can be represented by these 4 bytes is 4294967295 seconds after the epoch, or approximately 07:14:07 UTC on February 7, 2106.

After this time, the value of the current time in seconds since the epoch will no longer fit into 4 bytes and will overflow. This will cause the time value stored in the MIDs to wrap around and start counting from zero again.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

python_mid-1.0.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.0 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_mid-1.0.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.0 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_mid-1.0.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.8 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_mid-1.0.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.6 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_mid-1.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.3 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

File details

Details for the file python_mid-1.0.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_mid-1.0.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 48df79fa34c1c5c35b2f4a5740e8a34a85d1cf73e95fa38b953d5916d25c743e
MD5 a3ee13311ffcc9363462b07a3d2e42be
BLAKE2b-256 9bdbb5ac3b175343351d4c1e8999262a907cd5ba327296c132e3d02f79b5acc9

See more details on using hashes here.

File details

Details for the file python_mid-1.0.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_mid-1.0.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 33e0c16d3bfd0a944e892bc4de88752fb2a851143859eed011bb0eb7ae31e256
MD5 6c4d88d33743c92652d646a22d700eaf
BLAKE2b-256 a6be169d8cc3e24e4e525a5a0a343a2c408d28c8a711e181fa0a9610f4828411

See more details on using hashes here.

File details

Details for the file python_mid-1.0.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_mid-1.0.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0313d04a1fd0ad0e66b12b945f178fb09cb5d1fc5c46b205c1c2bfa2d342df97
MD5 477d8848c498f5f8fd6e48a6cdda7de8
BLAKE2b-256 bd0d7899170a8022de819a04a7e082e3b91e7a31ab258b70b8e698d66eb8ced1

See more details on using hashes here.

File details

Details for the file python_mid-1.0.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_mid-1.0.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 49b2d40bde28ffb8fd2af10921e10d76154380ead098339fba6f369d15316f3f
MD5 81b9b8207dd92d7764f0564786c65cbb
BLAKE2b-256 372b92a54dc74dfd962e742e2615a63e44902b2806b6a8e37431184c84a1c9b3

See more details on using hashes here.

File details

Details for the file python_mid-1.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_mid-1.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6d7a3244ab6f8ad3de75dd71fb1e3c23dff369498b06ef85cff02840e877e1e1
MD5 d2d493f9f2c5ed5de51d648d54bb80dd
BLAKE2b-256 b6d580e71d1722426902e64878f34e7765d95b96e5d186bdc38b3a1e62bc0a12

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page