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, and may be accessed as attributes of the Counter instance:

Sum of all natural numbers from 1 to 100

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

It is also possible to access these metrics using a key:

>>> 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.8.tar.gz (11.9 kB view hashes)

Uploaded Source

Built Distribution

tally_counter-0.0.8-py3-none-any.whl (8.0 kB view hashes)

Uploaded Python 3

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