Skip to main content

KeeWee 🥝

Global application state management and recording.
The Keewee library implements an auxiliary class that can be used to track values assigned to class and instance variables at runtime.
One major usecase is to decouple your state-management from your business-logic and keep your code nice and concise.
You can also use it to record statistics about an attribute during runtime.
The library works with regular Python classes or dataclasses but needs little different configuration.

Installing

Install and update using pip

$ pip install -U keewee

A Simple Example

import random

from dataclasses import dataclass, field
from keewee import KeeWee


@dataclass
class PokemonTrainer:
    name: str
    skill_level: int | KeeWee = field(default=KeeWee(), repr=False)


if __name__ == "__main__":
    ash = PokemonTrainer(name="Ash Ketchum", skill_level=0)
    for _ in range(10):
        ash.skill_level = random.randint(1, 10)
    print(KeeWee.dumpd())
{
    'PokemonTrainer': {'skill_level': {"PokemonTrainer(name='Ash Ketchum')": [0, 5, 9, 6, 3, 6, 10, 8, 4, 2, 9]}}
}

Collection Record Modes

When assigning a KeeWee instance to an attribute, the user can customize its internal recording-behavior by providing the mode-option. Currently, there are four different modes, whereas the list-mode is the default setting.

Direct mode

When only the current or resp. the last value is of interest, one can choose the direct mode, where the attribute is directely mapped to its value.

{
    'PokemonTrainer': {'skill_level': {"PokemonTrainer(name='Ash Ketchum')": 3}}
}

List mode

The list-mode keeps all occurring values in an ordered list from the first to the last value this attribute was assigned.
Since this use-case is probably the most common it is also chosen to be the default record-behavior.

{
    'PokemonTrainer': {'skill_level': {"PokemonTrainer(name='Ash Ketchum')": [0, 5, 9, 6, 3, 6, 10, 8, 4, 2, 9]}}
}

Set mode

The set-modes only difference to the list-mode is that duplicates are not tracked.

{
    'PokemonTrainer': {'skill_level': {"PokemonTrainer(name='Ash Ketchum')": {0, 2, 3, 5, 7, 9}}}
}

Datetime to value

If one wants to know exactly at what timestamp the modification took place the dtv (datetime-value)-mode is the best to choose.
Here a new dictionary is created for every attribute and the current time is mapped onto the state change.

{'PokemonTrainer': {'skill_level': {"PokemonTrainer(name='Ash Ketchum')": {
    '15:11:44.976976': 0,
    '15:11:44.976985': 8,
    '15:11:44.976987': 6,
    '15:11:44.976990': 2,
    '15:11:44.976992': 6,
    '15:11:44.976994': 9,
    '15:11:44.976996': 8,
    '15:11:44.976998': 7,
    '15:11:44.977000': 3,
    '15:11:44.977002': 9,
    '15:11:44.977004': 7
}}}}

Numerical Record Modes

The following record modes only work for numerical values, e.g.int or float etc.
The result will look similar to the direct-mode.
Currently there are three numerical record modes

  1. sum the sum of all occurring values
  2. min the minimal value that has occurred
  3. max the maximum value that has occurred

An example usage for taking the sum over all values could look like the following.

@dataclass
class PokemonTrainer:
    name: str
    skill_level: int | KeeWee = field(default=KeeWee(mode='sum'), repr=False)
{
    'PokemonTrainer': {'skill_level': {"PokemonTrainer(name='Ash Ketchum')": 49}}
}

Metadata

Release files for keewee 0.2

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

Source distribution (sdist)

Source distribution for keewee 0.2
File Size Uploaded Provenance Actions
keewee-0.2.tar.gz 19.9 kB Without provenance Details for keewee-0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for keewee 0.2
File Interpreter ABI Platform Actions
keewee-0.2-py3-none-any.whl Python 3 none any Details for keewee-0.2-py3-none-any.whl

Total release size: 38.4 kB

Release files / keewee-0.2.tar.gz

Download URL keewee-0.2.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f02ada815922d1964d69ea261b29e01eb435bcc4ef52d19997bec7f52cfe2b41
BLAKE2b-256 checksum
How to use checksums
1f1ff08a4f5da561c104b81490feca970f1cc6aa85407a961323d4ea4a34c6ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Release files / keewee-0.2-py3-none-any.whl

Download URL keewee-0.2-py3-none-any.whl
Size 18.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
368d4fb737e7f2eb0a83ef71454a71e007e1d4ef4b3351912b4a3bc63b929daf
BLAKE2b-256 checksum
How to use checksums
535c301ccfba17ff1c97cb4612d9735669f5b4695914f8603c7c34e89d4787b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.12.0

Security for keewee 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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