tally-counter


Nametally-counter JSON
Version 0.0.14 PyPI version JSON
download
home_pageNone
SummaryA Python tally counter class
upload_time2024-08-29 07:29:48
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2022 Scott Houseman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords statistics counter
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # tally-counter
[![PyPI](https://img.shields.io/pypi/v/tally-counter.svg?logo=python)](https://pypi.python.org/pypi/tally-counter)
[![Downloads](https://static.pepy.tech/badge/tally-counter)](https://pepy.tech/project/tally-counter)
[![image](https://img.shields.io/pypi/pyversions/tally-counter.svg)](https://pypi.python.org/pypi/tally-counter)
[![GitHub](https://img.shields.io/github/v/release/houseman/tally-counter?logo=github&sort=semver)](https://github.com/houseman/tally-counter)
[![Build](https://github.com/houseman/tally-counter/actions/workflows/build.yml/badge.svg)](https://github.com/houseman/tally-counter/actions?query=workflow%3Abuild)
[![License](https://img.shields.io/github/license/houseman/tally-counter)](https://github.com/houseman/tally-counter)


[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)
[![Nox](https://img.shields.io/badge/%F0%9F%A6%8A-Nox-D85E00.svg)](https://github.com/houseman/tally-counter)
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)

A Python tally counter class

## Usage
This contrived sample counts numbers from 1 to 100. We count the following metrics
- an aggregate (sum) of all natural numbers from 1 to 100
- a separate aggregate sum of all even numbers from 1 to 100
- a separate aggregate sum of all odd numbers from 1 to 100
- a count of the numbers from 1 to 100

```python
>>> from tally_counter import Counter

>>> counter = Counter("numbers", "naturals", "odds", "evens")
>>> for x in range(1, 101):  # 1..100 inclusive
...     counter.naturals.incr(x)
...     if x % 2 == 0:
...         counter.evens.incr(x)
...     else:
...         counter.odds.incr(x)
...
...     counter.numbers.incr()  # Default increment value is 1

```

These metrics are now available to us, and may be accessed as attributes of the `Counter` instance:
### Sum of all natural numbers from 1 to 100
```python
>>> counter.naturals
5050
>>> counter.naturals.sum
5050

```

It is also possible to access these metrics using a key:
```python
>>> counter["naturals"]
5050
>>> counter["naturals"].sum
5050

```

### Count of all natural numbers from 1 to 100
```python
>>> counter.numbers
100
>>> counter.numbers.sum
100

```

### Sum
```python
>>> counter.evens
2550
>>> counter.odds
2500

```

### Mean
```python
>>> counter.naturals.mean()  # Returns a float type
50.5
>>> counter.naturals.mean(percentile=50)  # Supports percentiles
25.0
>>> counter.evens.mean()
51.0
>>> counter.odds.mean()
50.0

```

### Minimum
```python
>>> counter.odds.min()
1
>>> counter.evens.min()
2

```

### Maximum
```python
>>> counter.naturals.max()
100
>>> counter.naturals.max(percentile=95)  # Supports percentiles
94
>>> counter.odds.max()
99
>>> counter.evens.max()
100

```

### Length (number of data points in) of a data series
```python
>>> counter.numbers.len()
100

```

### Timing
#### Data series age
This is the time difference (in nanoseconds), between the current system time and the time that the first data point in the series was created.

```python
>>> counter.naturals.age()
750000

```

#### Data series time span
This is the time difference (in nanoseconds), between the first and the latest data points' timestamps.

```python
>>> counter.evens.span()
735000

```

### Adding or Subtracting
The `incr()` method should be used to add positive counter values to a data series
```python
>>> my_count = Counter("my")
>>> my_count.my.incr(1000)
>>> my_count.my
1000

```

To decrease a data series, use the `decr()` method
```python
>>> my_count.my.decr(100)
>>> my_count.my
900

```

### Setting a TTL for counters
It is possible to set a TTL (Time-To-Live) for a counter, through setting a `ttl` argument value in milliseconds.
If this is set, then counters that exceed that TTL in age are discarded.
This may be useful for things such as rate limits (a use case where counts should be made irrelevant once a certain amount of time has passed).

```python
>>> r_counter = Counter("requests", ttl=60000)  # Count requests for the past minute

```

### Setting a maximum series length

```python
>>> l_counter = Counter("latest", maxlen=100)
>>> for i in range(0, 1000):
...     l_counter.latest.incr(i)
...
>>> l_counter.latest.len()
100
>>> l_counter.latest
94950

```

### Setting an initial value for counters
It is possible to create the counters and set an initial data point at once
```python
>>> foo_counter = Counter(foo=100, bar=200)
>>> foo_counter.foo.incr(1)
>>> foo_counter.foo
101
>>> foo_counter.bar
200

```

### Counter auto-instantiation
By default, a counter data series will be created if it is accessed but does not yest
exist, and will be set to an initial value of zero.
```python
>>> bar_counter = Counter()
>>> bar_counter.bar
0

```

## Documentation for Contributors
- [Developer Notes](./docs/DEV.md)

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tally-counter",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "statistics, counter",
    "author": null,
    "author_email": "Scott Houseman <scott.houseman@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/33/97/d0e1be4d775c4a79090bc08959a253c51fda2f4f9a547449dfc769e44711/tally_counter-0.0.14.tar.gz",
    "platform": null,
    "description": "# tally-counter\n[![PyPI](https://img.shields.io/pypi/v/tally-counter.svg?logo=python)](https://pypi.python.org/pypi/tally-counter)\n[![Downloads](https://static.pepy.tech/badge/tally-counter)](https://pepy.tech/project/tally-counter)\n[![image](https://img.shields.io/pypi/pyversions/tally-counter.svg)](https://pypi.python.org/pypi/tally-counter)\n[![GitHub](https://img.shields.io/github/v/release/houseman/tally-counter?logo=github&sort=semver)](https://github.com/houseman/tally-counter)\n[![Build](https://github.com/houseman/tally-counter/actions/workflows/build.yml/badge.svg)](https://github.com/houseman/tally-counter/actions?query=workflow%3Abuild)\n[![License](https://img.shields.io/github/license/houseman/tally-counter)](https://github.com/houseman/tally-counter)\n\n\n[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)\n[![Checked with mypy](https://www.mypy-lang.org/static/mypy_badge.svg)](https://mypy-lang.org/)\n[![Nox](https://img.shields.io/badge/%F0%9F%A6%8A-Nox-D85E00.svg)](https://github.com/houseman/tally-counter)\n[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)\n\nA Python tally counter class\n\n## Usage\nThis contrived sample counts numbers from 1 to 100. We count the following metrics\n- an aggregate (sum) of all natural numbers from 1 to 100\n- a separate aggregate sum of all even numbers from 1 to 100\n- a separate aggregate sum of all odd numbers from 1 to 100\n- a count of the numbers from 1 to 100\n\n```python\n>>> from tally_counter import Counter\n\n>>> counter = Counter(\"numbers\", \"naturals\", \"odds\", \"evens\")\n>>> for x in range(1, 101):  # 1..100 inclusive\n...     counter.naturals.incr(x)\n...     if x % 2 == 0:\n...         counter.evens.incr(x)\n...     else:\n...         counter.odds.incr(x)\n...\n...     counter.numbers.incr()  # Default increment value is 1\n\n```\n\nThese metrics are now available to us, and may be accessed as attributes of the `Counter` instance:\n### Sum of all natural numbers from 1 to 100\n```python\n>>> counter.naturals\n5050\n>>> counter.naturals.sum\n5050\n\n```\n\nIt is also possible to access these metrics using a key:\n```python\n>>> counter[\"naturals\"]\n5050\n>>> counter[\"naturals\"].sum\n5050\n\n```\n\n### Count of all natural numbers from 1 to 100\n```python\n>>> counter.numbers\n100\n>>> counter.numbers.sum\n100\n\n```\n\n### Sum\n```python\n>>> counter.evens\n2550\n>>> counter.odds\n2500\n\n```\n\n### Mean\n```python\n>>> counter.naturals.mean()  # Returns a float type\n50.5\n>>> counter.naturals.mean(percentile=50)  # Supports percentiles\n25.0\n>>> counter.evens.mean()\n51.0\n>>> counter.odds.mean()\n50.0\n\n```\n\n### Minimum\n```python\n>>> counter.odds.min()\n1\n>>> counter.evens.min()\n2\n\n```\n\n### Maximum\n```python\n>>> counter.naturals.max()\n100\n>>> counter.naturals.max(percentile=95)  # Supports percentiles\n94\n>>> counter.odds.max()\n99\n>>> counter.evens.max()\n100\n\n```\n\n### Length (number of data points in) of a data series\n```python\n>>> counter.numbers.len()\n100\n\n```\n\n### Timing\n#### Data series age\nThis is the time difference (in nanoseconds), between the current system time and the time that the first data point in the series was created.\n\n```python\n>>> counter.naturals.age()\n750000\n\n```\n\n#### Data series time span\nThis is the time difference (in nanoseconds), between the first and the latest data points' timestamps.\n\n```python\n>>> counter.evens.span()\n735000\n\n```\n\n### Adding or Subtracting\nThe `incr()` method should be used to add positive counter values to a data series\n```python\n>>> my_count = Counter(\"my\")\n>>> my_count.my.incr(1000)\n>>> my_count.my\n1000\n\n```\n\nTo decrease a data series, use the `decr()` method\n```python\n>>> my_count.my.decr(100)\n>>> my_count.my\n900\n\n```\n\n### Setting a TTL for counters\nIt is possible to set a TTL (Time-To-Live) for a counter, through setting a `ttl` argument value in milliseconds.\nIf this is set, then counters that exceed that TTL in age are discarded.\nThis may be useful for things such as rate limits (a use case where counts should be made irrelevant once a certain amount of time has passed).\n\n```python\n>>> r_counter = Counter(\"requests\", ttl=60000)  # Count requests for the past minute\n\n```\n\n### Setting a maximum series length\n\n```python\n>>> l_counter = Counter(\"latest\", maxlen=100)\n>>> for i in range(0, 1000):\n...     l_counter.latest.incr(i)\n...\n>>> l_counter.latest.len()\n100\n>>> l_counter.latest\n94950\n\n```\n\n### Setting an initial value for counters\nIt is possible to create the counters and set an initial data point at once\n```python\n>>> foo_counter = Counter(foo=100, bar=200)\n>>> foo_counter.foo.incr(1)\n>>> foo_counter.foo\n101\n>>> foo_counter.bar\n200\n\n```\n\n### Counter auto-instantiation\nBy default, a counter data series will be created if it is accessed but does not yest\nexist, and will be set to an initial value of zero.\n```python\n>>> bar_counter = Counter()\n>>> bar_counter.bar\n0\n\n```\n\n## Documentation for Contributors\n- [Developer Notes](./docs/DEV.md)\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2022 Scott Houseman  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "A Python tally counter class",
    "version": "0.0.14",
    "project_urls": {
        "repository": "https://github.com/houseman/tally-counter"
    },
    "split_keywords": [
        "statistics",
        " counter"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "57b7ca2e6a007359ff04c0566f977f20e51be2bc67e8c126ff0633048472485f",
                "md5": "a741bc62c686cef09451c41e7dd2e096",
                "sha256": "7e5211f28161c441738936f482b2bfd0e0f930e4a429f6e33d29f7bd3fa5e66f"
            },
            "downloads": -1,
            "filename": "tally_counter-0.0.14-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "a741bc62c686cef09451c41e7dd2e096",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8251,
            "upload_time": "2024-08-29T07:29:46",
            "upload_time_iso_8601": "2024-08-29T07:29:46.998973Z",
            "url": "https://files.pythonhosted.org/packages/57/b7/ca2e6a007359ff04c0566f977f20e51be2bc67e8c126ff0633048472485f/tally_counter-0.0.14-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3397d0e1be4d775c4a79090bc08959a253c51fda2f4f9a547449dfc769e44711",
                "md5": "31f2d75a6113b77de3d9cddfc5f1628a",
                "sha256": "4c0f4a0b6eda224ae5ab7443d2c1bd543d27e19c7c1ee3d4a6e02ee6c7634954"
            },
            "downloads": -1,
            "filename": "tally_counter-0.0.14.tar.gz",
            "has_sig": false,
            "md5_digest": "31f2d75a6113b77de3d9cddfc5f1628a",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 12490,
            "upload_time": "2024-08-29T07:29:48",
            "upload_time_iso_8601": "2024-08-29T07:29:48.341736Z",
            "url": "https://files.pythonhosted.org/packages/33/97/d0e1be4d775c4a79090bc08959a253c51fda2f4f9a547449dfc769e44711/tally_counter-0.0.14.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-29 07:29:48",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "houseman",
    "github_project": "tally-counter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tally-counter"
}
        
Elapsed time: 4.36628s