Skip to main content

tally-counter

PyPI Downloads image GitHub Build License

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

Release files for tally-counter 0.0.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tally-counter 0.0.14
File Size Uploaded
tally_counter-0.0.14.tar.gz 12.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tally-counter 0.0.14
File Interpreter ABI Platform
tally_counter-0.0.14-py3-none-any.whl Python 3 none any Details

Total release size: 20.7 kB

Release files / tally_counter-0.0.14.tar.gz

Download URL tally_counter-0.0.14.tar.gz
Size 12.5 kB
Tags Source
SHA-256 checksum
How to use checksums
4c0f4a0b6eda224ae5ab7443d2c1bd543d27e19c7c1ee3d4a6e02ee6c7634954
BLAKE2b-256 checksum
How to use checksums
3397d0e1be4d775c4a79090bc08959a253c51fda2f4f9a547449dfc769e44711
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release files / tally_counter-0.0.14-py3-none-any.whl

Download URL tally_counter-0.0.14-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e5211f28161c441738936f482b2bfd0e0f930e4a429f6e33d29f7bd3fa5e66f
BLAKE2b-256 checksum
How to use checksums
57b7ca2e6a007359ff04c0566f977f20e51be2bc67e8c126ff0633048472485f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.0 CPython/3.12.5

Release history Release notifications | RSS feed

This release

0.0.14 This release

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page