Skip to main content

A Python tally counter class

Project description

tally-counter

PyPI Downloads image GitHub Build License

Ruff Checked with mypy Code Style Nox 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
>>> 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

Sum of all natural numbers from 1 to 100

>>> counter.naturals
5050
>>> counter.naturals.sum
5050

Count of all natural numbers from 1 to 100

>>> counter.numbers
100
>>> counter.numbers.sum
100

Sum

>>> counter.evens
2550
>>> counter.odds
2500

Mean

>>> 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

>>> counter.odds.min()
1
>>> counter.evens.min()
2

Maximum

>>> 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

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

>>> counter.naturals.age()
750000

Data series time span

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

>>> counter.evens.span()
735000

Adding or Subtracting

The incr() method should be used to add positive counter values to a data series

>>> my_count = Counter("my")
>>> my_count.my.incr(1000)
>>> my_count.my
1000

To decrease a data series, use the decr() method

>>> 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).

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

Setting a maximum series length

>>> 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

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

>>> bar_counter = Counter()
>>> bar_counter.bar
0

Documentation for Contributors

Project details


Download files

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

Source Distribution

tally-counter-0.0.7.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

tally_counter-0.0.7-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file tally-counter-0.0.7.tar.gz.

File metadata

  • Download URL: tally-counter-0.0.7.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for tally-counter-0.0.7.tar.gz
Algorithm Hash digest
SHA256 eaec5e857be6795c38d273ca1650c9942068cf160b7b3a285e19c005f3244589
MD5 546442dd39c5d3a916f3fb98718ff980
BLAKE2b-256 1bd3cb346e57ef881259fc551e9f9ba1dd38517ebce7e45f45beb14b6ccfd7ff

See more details on using hashes here.

File details

Details for the file tally_counter-0.0.7-py3-none-any.whl.

File metadata

File hashes

Hashes for tally_counter-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 74980478bfb57eda65533fa5ccd4cb3c22f8856b62db4aa78955b50cf39d9fb7
MD5 b39168d2c0c44170b59db20213fc9104
BLAKE2b-256 378511daa6ed867a4baadfc8ac5fe4e14d6262866e3afbcdb40d7b9adbcf02ad

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