atomic-counter


Nameatomic-counter JSON
Version 0.1.4 PyPI version JSON
download
home_pageNone
SummaryAtomic Counters
upload_time2024-12-12 05:06:38
maintainerNone
docs_urlNone
authorTim Paine
requires_python>=3.8
licenseNone
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Atomic Counter

[![Build Status](https://github.com/timkpaine/atomic-counter/workflows/Build%20Status/badge.svg?branch=main)](https://github.com/timkpaine/atomic-counter/actions?query=workflow%3A%22Build+Status%22)
[![Coverage](https://codecov.io/gh/timkpaine/atomic-counter/branch/main/graph/badge.svg)](https://codecov.io/gh/timkpaine/atomic-counter)
[![License](https://img.shields.io/github/license/timkpaine/atomic-counter.svg)](https://pypi.python.org/pypi/atomic-counter)
[![PyPI](https://img.shields.io/pypi/v/atomic-counter.svg)](https://pypi.python.org/pypi/atomic-counter)

## Overview
`atomic-counter` is a rust library for generating a monotonically increasing sequence of integers. Depending on the particular configuration of the counter, the generated sequence will be produce unique numbers down to the nanosecond, regardless of memory state.
E.g. if you quit the process and recreate a new counter `>1ns` later, your sequence is guaranteed to still be monotonically increasing (but with a gap).

## Usage

```python
from atomic_counter import Counter

c = Counter(base_in_nanos)

c.next()  # generate next number in sequence
```

To create e.g. a daily counter, pass in `base=today in nanos`. As this is a common occurrence for sequences that reset daily, a convenience function `def daily() -> Counter:` is provided.


There is also a `TimeCounter` class provided. A 64 bit unsigned integer is created that is monotonically increasing, and allows for converting to microseconds to serve as a timestamp (up to the year 2112). Will break if more than 4096 calls to "next" are called within a single microsecond (which is almost assuredly never going to be physically possible, every call makes a system call to get the current time).

```python
from atomic_counter import TimeCounter
from datetime import datetime, timezone

c = TimeCounter()

x = c.next()  # generate id
x_time = TimeCounter.to_datetime(x)  # generates the datetime where the value was called.
now = datetime.now(timezone.utc)
assert x_time <= now
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "atomic-counter",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Tim Paine",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/23/5c/ddd8b4179739dd1eccad4d60ad7aba0353cecdb63b51d5309f99e9f028a0/atomic_counter-0.1.4.tar.gz",
    "platform": null,
    "description": "# Atomic Counter\n\n[![Build Status](https://github.com/timkpaine/atomic-counter/workflows/Build%20Status/badge.svg?branch=main)](https://github.com/timkpaine/atomic-counter/actions?query=workflow%3A%22Build+Status%22)\n[![Coverage](https://codecov.io/gh/timkpaine/atomic-counter/branch/main/graph/badge.svg)](https://codecov.io/gh/timkpaine/atomic-counter)\n[![License](https://img.shields.io/github/license/timkpaine/atomic-counter.svg)](https://pypi.python.org/pypi/atomic-counter)\n[![PyPI](https://img.shields.io/pypi/v/atomic-counter.svg)](https://pypi.python.org/pypi/atomic-counter)\n\n## Overview\n`atomic-counter` is a rust library for generating a monotonically increasing sequence of integers. Depending on the particular configuration of the counter, the generated sequence will be produce unique numbers down to the nanosecond, regardless of memory state.\nE.g. if you quit the process and recreate a new counter `>1ns` later, your sequence is guaranteed to still be monotonically increasing (but with a gap).\n\n## Usage\n\n```python\nfrom atomic_counter import Counter\n\nc = Counter(base_in_nanos)\n\nc.next()  # generate next number in sequence\n```\n\nTo create e.g. a daily counter, pass in `base=today in nanos`. As this is a common occurrence for sequences that reset daily, a convenience function `def daily() -> Counter:` is provided.\n\n\nThere is also a `TimeCounter` class provided. A 64 bit unsigned integer is created that is monotonically increasing, and allows for converting to microseconds to serve as a timestamp (up to the year 2112). Will break if more than 4096 calls to \"next\" are called within a single microsecond (which is almost assuredly never going to be physically possible, every call makes a system call to get the current time).\n\n```python\nfrom atomic_counter import TimeCounter\nfrom datetime import datetime, timezone\n\nc = TimeCounter()\n\nx = c.next()  # generate id\nx_time = TimeCounter.to_datetime(x)  # generates the datetime where the value was called.\nnow = datetime.now(timezone.utc)\nassert x_time <= now\n```\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Atomic Counters",
    "version": "0.1.4",
    "project_urls": {
        "homepage": "https://github.com/timkpaine/atomic-counter",
        "repository": "https://github.com/timkpaine/atomic-counter"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "de1864be6a8c74e1ecd6739432c7e7834506d55a3693c88877f0b3683f380d40",
                "md5": "6ca4d3a85de0e532af4baa5f664ba83e",
                "sha256": "2bf58b0762048ba021c7a6fe6c20d44a970c11896189b88f7aae18f9af38a388"
            },
            "downloads": -1,
            "filename": "atomic_counter-0.1.4-cp37-abi3-macosx_11_0_arm64.whl",
            "has_sig": false,
            "md5_digest": "6ca4d3a85de0e532af4baa5f664ba83e",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.8",
            "size": 209443,
            "upload_time": "2024-12-12T05:06:28",
            "upload_time_iso_8601": "2024-12-12T05:06:28.615142Z",
            "url": "https://files.pythonhosted.org/packages/de/18/64be6a8c74e1ecd6739432c7e7834506d55a3693c88877f0b3683f380d40/atomic_counter-0.1.4-cp37-abi3-macosx_11_0_arm64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2ad11035e5ca71740a9553fcc1e07144c29ff9a50d63405d298498b55f8d713e",
                "md5": "40343ad952f3b7307af59e079a4e9a8d",
                "sha256": "7a44180f20658079eb5f2847f851fcf42b4518d2d39bcbf147bcd18b7211e5aa"
            },
            "downloads": -1,
            "filename": "atomic_counter-0.1.4-cp37-abi3-macosx_11_0_x86_64.whl",
            "has_sig": false,
            "md5_digest": "40343ad952f3b7307af59e079a4e9a8d",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.8",
            "size": 220537,
            "upload_time": "2024-12-12T05:06:29",
            "upload_time_iso_8601": "2024-12-12T05:06:29.939018Z",
            "url": "https://files.pythonhosted.org/packages/2a/d1/1035e5ca71740a9553fcc1e07144c29ff9a50d63405d298498b55f8d713e/atomic_counter-0.1.4-cp37-abi3-macosx_11_0_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f870e706b92cd9be248ff61d8b739b6298f13df9505025a8aff7dc2fc11aa6d7",
                "md5": "8f448212c2893471b99dcb86bde3f7d1",
                "sha256": "358d42034de019d90acd5a3440d643eaa5acf9fe52097c1e7d473d90d62148a7"
            },
            "downloads": -1,
            "filename": "atomic_counter-0.1.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "has_sig": false,
            "md5_digest": "8f448212c2893471b99dcb86bde3f7d1",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.8",
            "size": 241188,
            "upload_time": "2024-12-12T05:06:31",
            "upload_time_iso_8601": "2024-12-12T05:06:31.140623Z",
            "url": "https://files.pythonhosted.org/packages/f8/70/e706b92cd9be248ff61d8b739b6298f13df9505025a8aff7dc2fc11aa6d7/atomic_counter-0.1.4-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c8bc37a4de3f7029f314e85f7638eb2d679c91065d6cab6d7f797e79c5fa2a17",
                "md5": "abe2c7a1bf35b0abe6857948ff8a1a45",
                "sha256": "1b165e154ac8fd91bc30c10158706946cb8f1296a43f7e450bb09e2f6cc9bfd0"
            },
            "downloads": -1,
            "filename": "atomic_counter-0.1.4-cp37-abi3-win32.whl",
            "has_sig": false,
            "md5_digest": "abe2c7a1bf35b0abe6857948ff8a1a45",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.8",
            "size": 117026,
            "upload_time": "2024-12-12T05:06:33",
            "upload_time_iso_8601": "2024-12-12T05:06:33.260239Z",
            "url": "https://files.pythonhosted.org/packages/c8/bc/37a4de3f7029f314e85f7638eb2d679c91065d6cab6d7f797e79c5fa2a17/atomic_counter-0.1.4-cp37-abi3-win32.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ce868f818b379752dd8f0f40e1128fca8454b74c80228f497fa005bc54d39724",
                "md5": "c45e0c8f9f70cd1d3cbf82576f28bbd4",
                "sha256": "1ac8dfc801b14c9759c0e105593765f260abb90ec7d72c0d3be9bc696a621f3a"
            },
            "downloads": -1,
            "filename": "atomic_counter-0.1.4-cp37-abi3-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "c45e0c8f9f70cd1d3cbf82576f28bbd4",
            "packagetype": "bdist_wheel",
            "python_version": "cp37",
            "requires_python": ">=3.8",
            "size": 121478,
            "upload_time": "2024-12-12T05:06:36",
            "upload_time_iso_8601": "2024-12-12T05:06:36.246305Z",
            "url": "https://files.pythonhosted.org/packages/ce/86/8f818b379752dd8f0f40e1128fca8454b74c80228f497fa005bc54d39724/atomic_counter-0.1.4-cp37-abi3-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "235cddd8b4179739dd1eccad4d60ad7aba0353cecdb63b51d5309f99e9f028a0",
                "md5": "100a96293eb7b3b9a560efd3f535d56a",
                "sha256": "c221aa80581b98c288c6833ac06e0cf74e2081bd40e0e5dcda67f590bc2739bb"
            },
            "downloads": -1,
            "filename": "atomic_counter-0.1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "100a96293eb7b3b9a560efd3f535d56a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 19880,
            "upload_time": "2024-12-12T05:06:38",
            "upload_time_iso_8601": "2024-12-12T05:06:38.301472Z",
            "url": "https://files.pythonhosted.org/packages/23/5c/ddd8b4179739dd1eccad4d60ad7aba0353cecdb63b51d5309f99e9f028a0/atomic_counter-0.1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-12 05:06:38",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "timkpaine",
    "github_project": "atomic-counter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "atomic-counter"
}
        
Elapsed time: 1.10499s