A Python tally counter class
Project description
tally-counter
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tally_counter-0.0.14.tar.gz
.
File metadata
- Download URL: tally_counter-0.0.14.tar.gz
- Upload date:
- Size: 12.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c0f4a0b6eda224ae5ab7443d2c1bd543d27e19c7c1ee3d4a6e02ee6c7634954 |
|
MD5 | 31f2d75a6113b77de3d9cddfc5f1628a |
|
BLAKE2b-256 | 3397d0e1be4d775c4a79090bc08959a253c51fda2f4f9a547449dfc769e44711 |
File details
Details for the file tally_counter-0.0.14-py3-none-any.whl
.
File metadata
- Download URL: tally_counter-0.0.14-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e5211f28161c441738936f482b2bfd0e0f930e4a429f6e33d29f7bd3fa5e66f |
|
MD5 | a741bc62c686cef09451c41e7dd2e096 |
|
BLAKE2b-256 | 57b7ca2e6a007359ff04c0566f977f20e51be2bc67e8c126ff0633048472485f |