Skip to main content

The `Constant` class is a metaclass for creating classes with constant attributes

Project description

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

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

sthira-0.2.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sthira-0.2.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file sthira-0.2.0.tar.gz.

File metadata

  • Download URL: sthira-0.2.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-58-generic

File hashes

Hashes for sthira-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0e4663fbf7146f7d6e992a174b852fc5b693c72e0f52b5e7b52ca2397a9881b7
MD5 63d138e296c66da19bc237cc3d6707cb
BLAKE2b-256 1c85994181fef6ae38caef93d8b2dc25c3fb35be6389c8e4ede03b36ac42c9d1

See more details on using hashes here.

File details

Details for the file sthira-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: sthira-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.6 Linux/5.15.0-58-generic

File hashes

Hashes for sthira-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6f46b4d9a94855683eb980cc9902172783e24bbddd6977bb2489514ccfe8e7b
MD5 d3e2acbe41f9c5e949012b60529edc01
BLAKE2b-256 c3a97fe10730f9c0ba51c8875ac228eaca66504b7409c92872056861e56e8be5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page