Skip to content

kephircheek/python-constancy

Repository files navigation

python-constancy

Container for constants.

test PyPI version license: MIT code style: black

Installing

pip install constancy

Example:

from constancy import Constants

Create container for constants

>>> DAYS = Constants(
...     MON=0,
...     TUE=1,
...     WED=2,
...     THU=3,
...     FRI=4,
...     SAT=5,
...     SUN=6
... )

Get value from container

>>> DAYS.MON
0
>>> DAYS['MON']
0

Represent with pure python data structures

>>> list(DAYS)
['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
>>> dict(DAYS)
{'WED': 2, 'SUN': 6, 'FRI': 4, 'THU': 3, 'MON': 0, 'TUE': 1, 'SAT': 5}

All constants are immutable

>>> DAYS.MON = 7
...
AttributeError: Immutable attribute

>>> del DAYS.MON
...
AttributeError: Immutable attribute

Autocomplete only for constants

>>> dir(DAYS)
['FRI', 'MON', 'SAT', 'SUN', 'THU', 'TUE', 'WED']

Sorting like list.sort

>>> DAYS.sort(key=lambda k, v: v, reverse=True)
>>> list(DAYS)
['SUN', 'SAT', 'FRI', 'THU', 'WED', 'TUE', 'MON']

About

Simple immutable container for constants

Resources

License

Stars

Watchers

Forks

Languages