tally-counter


Nametally-counter JSON
Version 0.0.8 PyPI version JSON
download
home_page
SummaryA Python tally counter class
upload_time2023-09-07 16:56:51
maintainer
docs_urlNone
author
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/)
[![Code Style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/houseman/tally-counter)
[![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": "",
    "name": "tally-counter",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "statistics,counter",
    "author": "",
    "author_email": "Scott Houseman <scott.houseman@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/22/99/7f71dd7193cae2184459d85dd7ebe129d371f97dce01ea098308f3ba0c05/tally-counter-0.0.8.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[![Code Style](https://img.shields.io/badge/code%20style-black-black)](https://github.com/houseman/tally-counter)\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.8",
    "project_urls": {
        "repository": "https://github.com/houseman/tally-counter"
    },
    "split_keywords": [
        "statistics",
        "counter"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "772a7cdc435c796c592399133a609e001f5e0ce18cec2a06a03427aa03958352",
                "md5": "942720c17312f4a4bd4168bac67940c8",
                "sha256": "f7aa0e53d91724adb9995b1683e3703074207be3aec561cdaf80a0218f1b9e24"
            },
            "downloads": -1,
            "filename": "tally_counter-0.0.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "942720c17312f4a4bd4168bac67940c8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 8046,
            "upload_time": "2023-09-07T16:56:49",
            "upload_time_iso_8601": "2023-09-07T16:56:49.578216Z",
            "url": "https://files.pythonhosted.org/packages/77/2a/7cdc435c796c592399133a609e001f5e0ce18cec2a06a03427aa03958352/tally_counter-0.0.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "22997f71dd7193cae2184459d85dd7ebe129d371f97dce01ea098308f3ba0c05",
                "md5": "945e30f1d496fefdd65d2b6378622afe",
                "sha256": "d3a8b10b73fa0452ab1376dac2cd79f1663f7a97d7b157808d3af0be5b2cf176"
            },
            "downloads": -1,
            "filename": "tally-counter-0.0.8.tar.gz",
            "has_sig": false,
            "md5_digest": "945e30f1d496fefdd65d2b6378622afe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 11892,
            "upload_time": "2023-09-07T16:56:51",
            "upload_time_iso_8601": "2023-09-07T16:56:51.150948Z",
            "url": "https://files.pythonhosted.org/packages/22/99/7f71dd7193cae2184459d85dd7ebe129d371f97dce01ea098308f3ba0c05/tally-counter-0.0.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-07 16:56:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "houseman",
    "github_project": "tally-counter",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "tally-counter"
}
        
Elapsed time: 0.12239s