Sthira
The word for "constant" in Sanskrit can be translated as "स्थिर" (sthira)
The Constant class is a metaclass for creating classes with constant attributes.
Once set, the attributes of a Constant class cannot be changed, and new attributes cannot be added.
This allows for creating classes that represent unchangeable values, such as constants, enums, and similar constructs.
The Constant class also provides a __str__and __repr__ implementation for convenient representation of the class.
Installtion
pip install sthira
Usage
from sthira import constant
@constant
class Mammal:
HUMAN = "human"
TIGER = "tiger"
LION = "lion"
@constant
class Bird:
CROW = "crow"
HAWK = "hawk"
@constant
class Fish:
TUNA = "tuna"
@constant
class Animal:
MAMMAL = Mammal
BIRD = Bird
FISH = Fish
print(f"{Animal.MAMMAL}")
print(f"{Animal.MAMMAL.HUMAN}")
print(f"{Animal.BIRD.CROW}")
Output
Mammal
human
crow
Cannot modify attributes
Animal.MAMMAL.HUMAN = "HomoSapiens"
# raise AttributeError("Cannot set or change the class attributes")
# AttributeError: Cannot set or change the class attributes
Dispatch
check
test_constant.py
from sthira import dispatch
@constant
class Red:
BRICK = "#AA4A44"
CADMIUM = "##D22B2B"
@constant
class Green:
LIME = "#32CD32"
LIGHT = "#90EE90"
@constant
class Color:
RED = Red
GREEN = Green
YELLOW = "not_there_yet"
@dispatch
def get_color(color, input_):
# Default implementation
raise NotImplementedError("Unsupported color!")
@get_color.register(Color.RED)
def _(input_):
return "I'm red"
@get_color.register(Color.GREEN)
def _(input_):
return "hulk out!"
print(get_color(Color.GREEN, "input"))
# hulk out!
Unit tests
python -m unittest test_constant.py
Metadata
Release files for sthira 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sthira-0.2.0.tar.gz | 3.5 kB | Without provenance | Details for sthira-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sthira-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for sthira-0.2.0-py3-none-any.whl |
Total release size: 7.0 kB
Release files / sthira-0.2.0.tar.gz
| Download URL | sthira-0.2.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0e4663fbf7146f7d6e992a174b852fc5b693c72e0f52b5e7b52ca2397a9881b7
|
|
BLAKE2b-256 checksum How to use checksums |
1c85994181fef6ae38caef93d8b2dc25c3fb35be6389c8e4ede03b36ac42c9d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-58-generic
|
Release files / sthira-0.2.0-py3-none-any.whl
| Download URL | sthira-0.2.0-py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a6f46b4d9a94855683eb980cc9902172783e24bbddd6977bb2489514ccfe8e7b
|
|
BLAKE2b-256 checksum How to use checksums |
c3a97fe10730f9c0ba51c8875ac228eaca66504b7409c92872056861e56e8be5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-58-generic
|
Security for sthira 0.2.0
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.