python-mid


Namepython-mid JSON
Version 1.0.0 PyPI version JSON
download
home_pagehttps://github.com/klamar/python-mid
SummaryMID module
upload_time2023-04-20 09:44:52
maintainer
docs_urlNone
authorMartin Klapproth
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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

```python
>>> 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.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/klamar/python-mid",
    "name": "python-mid",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "martin.klapproth@makrotan.com",
    "keywords": "",
    "author": "Martin Klapproth",
    "author_email": "",
    "download_url": "",
    "platform": null,
    "description": "# python-mid\nEfficient ID generation, lexicographically sortable, contains time information.\n\n## Structure\n\n* 4 bytes for the timestamp\n* 8 bytes of randomness\n* encoded as urlsafe base64\n* 16 characters\n* Example: `ZEDpWWfGaXNR_0rs`\n\n## Usage\n\n```python\n>>> import mid\n>>> mid.generate_mid()\n'ZEDpWWfGaXNR_0rs'\n>>> mid.get_date_time(\"ZEDpWWfGaXNR_0rs\")\ndatetime.datetime(2017, 12, 28, 20, 22, 24)\n>>> mid.get_time(\"ZEDpWWfGaXNR_0rs\")\n1514474944\n```\n\n## Performance\n\nBench #1: Generate 1.000.000 IDs\n\n|                | UUID1     | UUID4     | ULID      | ObjectID   | MID        |\n|----------------|-----------|-----------|-----------|------------|------------|\n| Execution Time | 2.96s     | 3.55s     | 5.88s     | 5.43s      | 0.28s      |\n| Executions/s   | 337612.24 | 281647.68 | 170118.74 | 184101.31  | 3556948.61 |\n\n## FAQ\n\n### Are the resulting IDs always lexographically sortable?\n\nYes, the resulting MIDs generated by the generate_mid function in the C extension code I provided earlier are always lexicographically sortable.\n\nThe 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.\n\nThe remaining eight bytes of each MID are filled with random data and do not affect the lexicographical order of the MIDs.\n\nTherefore, 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.\n\n### What about January 19, 2038?\n\nThe 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.\n\nSince 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.\n\nAfter 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.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "MID module",
    "version": "1.0.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a6be169d8cc3e24e4e525a5a0a343a2c408d28c8a711e181fa0a9610f4828411",
                "md5": "6c4d88d33743c92652d646a22d700eaf",
                "sha256": "33e0c16d3bfd0a944e892bc4de88752fb2a851143859eed011bb0eb7ae31e256"
            },
            "downloads": -1,
            "filename": "python_mid-1.0.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "6c4d88d33743c92652d646a22d700eaf",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": null,
            "size": 12992,
            "upload_time": "2023-04-20T09:44:52",
            "upload_time_iso_8601": "2023-04-20T09:44:52.551112Z",
            "url": "https://files.pythonhosted.org/packages/a6/be/169d8cc3e24e4e525a5a0a343a2c408d28c8a711e181fa0a9610f4828411/python_mid-1.0.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9bdbb5ac3b175343351d4c1e8999262a907cd5ba327296c132e3d02f79b5acc9",
                "md5": "a3ee13311ffcc9363462b07a3d2e42be",
                "sha256": "48df79fa34c1c5c35b2f4a5740e8a34a85d1cf73e95fa38b953d5916d25c743e"
            },
            "downloads": -1,
            "filename": "python_mid-1.0.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "a3ee13311ffcc9363462b07a3d2e42be",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": null,
            "size": 13003,
            "upload_time": "2023-04-20T09:44:54",
            "upload_time_iso_8601": "2023-04-20T09:44:54.400838Z",
            "url": "https://files.pythonhosted.org/packages/9b/db/b5ac3b175343351d4c1e8999262a907cd5ba327296c132e3d02f79b5acc9/python_mid-1.0.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b6d580e71d1722426902e64878f34e7765d95b96e5d186bdc38b3a1e62bc0a12",
                "md5": "d2d493f9f2c5ed5de51d648d54bb80dd",
                "sha256": "6d7a3244ab6f8ad3de75dd71fb1e3c23dff369498b06ef85cff02840e877e1e1"
            },
            "downloads": -1,
            "filename": "python_mid-1.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "d2d493f9f2c5ed5de51d648d54bb80dd",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": null,
            "size": 13274,
            "upload_time": "2023-04-20T09:44:55",
            "upload_time_iso_8601": "2023-04-20T09:44:55.839722Z",
            "url": "https://files.pythonhosted.org/packages/b6/d5/80e71d1722426902e64878f34e7765d95b96e5d186bdc38b3a1e62bc0a12/python_mid-1.0.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "372b92a54dc74dfd962e742e2615a63e44902b2806b6a8e37431184c84a1c9b3",
                "md5": "81b9b8207dd92d7764f0564786c65cbb",
                "sha256": "49b2d40bde28ffb8fd2af10921e10d76154380ead098339fba6f369d15316f3f"
            },
            "downloads": -1,
            "filename": "python_mid-1.0.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "81b9b8207dd92d7764f0564786c65cbb",
            "packagetype": "bdist_wheel",
            "python_version": "cp38",
            "requires_python": null,
            "size": 13570,
            "upload_time": "2023-04-20T09:44:57",
            "upload_time_iso_8601": "2023-04-20T09:44:57.702317Z",
            "url": "https://files.pythonhosted.org/packages/37/2b/92a54dc74dfd962e742e2615a63e44902b2806b6a8e37431184c84a1c9b3/python_mid-1.0.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "bd0d7899170a8022de819a04a7e082e3b91e7a31ab258b70b8e698d66eb8ced1",
                "md5": "477d8848c498f5f8fd6e48a6cdda7de8",
                "sha256": "0313d04a1fd0ad0e66b12b945f178fb09cb5d1fc5c46b205c1c2bfa2d342df97"
            },
            "downloads": -1,
            "filename": "python_mid-1.0.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "477d8848c498f5f8fd6e48a6cdda7de8",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": null,
            "size": 12825,
            "upload_time": "2023-04-20T09:44:59",
            "upload_time_iso_8601": "2023-04-20T09:44:59.239148Z",
            "url": "https://files.pythonhosted.org/packages/bd/0d/7899170a8022de819a04a7e082e3b91e7a31ab258b70b8e698d66eb8ced1/python_mid-1.0.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-20 09:44:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "klamar",
    "github_project": "python-mid",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "python-mid"
}
        
Elapsed time: 0.06030s