Skip to main content

A Python tally counter class

Project description

tally-counter

A Python tally counter class

Supported Python version Code Style Linting

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

Count of all natural numbers from 1 to 100

>>> counter.numbers
100

Sum of all even numbers in range 1 to 100

>>> counter.evens
2550

Sum of all odd numbers in range 1 to 100

>>> counter.odds
2500

Average of all natural numbers 1 to 100

>>> counter.naturals.average()
50.5

Average of all even numbers in range 1 to 100

>>> counter.evens.average()  # Returns a float type
51.0

Average of all odd numbers in range 1 to 100

>>> counter.odds.average()
50.0

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 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-1.0.0a0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

tally_counter-1.0.0a0-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file tally-counter-1.0.0a0.tar.gz.

File metadata

  • Download URL: tally-counter-1.0.0a0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for tally-counter-1.0.0a0.tar.gz
Algorithm Hash digest
SHA256 ba81cb2be46807bce136ab02ee61b37eeb8e859f90495802d9498ad377dbc161
MD5 d84106bd7de4ddcc2d98321f8d779be4
BLAKE2b-256 69d2ac1f010fcae8aac7ea5b8fcd449e5f55cb48565b529506ae3461960671a7

See more details on using hashes here.

File details

Details for the file tally_counter-1.0.0a0-py3-none-any.whl.

File metadata

File hashes

Hashes for tally_counter-1.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a3713a590cca3f6f7c81dac9bf3c0910cbbb17edab7859a42e7f30d8e8f7f22
MD5 6d2043bf1cc5d3349842a346cafa202a
BLAKE2b-256 d8e0a03bad4f555a6f1b1f000f58a3bc33d1098625a8ba70af584f2e352a0e51

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