constantia
Enforce constants in your code at import or at run time.
Usage
Use the consts decorator and pass as parameters a list of
variables you want to treat as constants.
The variables in the constant list cannot be re-assigned nor they can be assigned a mutable object. They can only be assigned:
- strings
- integers
- floats
- tuples
This is done to avoid indirect modifications.
Choose if you want to do the checks at runtime or when importing the function by setting the check_at argument.
Examples
Function
Checking the constants at import time
from constantia import consts
@consts(['x', 'y', 'z'], check_at='import')
def func():
x = [1, 2, 3] # this will crash at import time as the constant does not have an immutable value
y = 20
y = 30 # this will raise an exception at import time as the constant is reassigned
Checking the constants at run time
from constantia import consts
@consts(['x', 'y', 'z'], check_at='runtime')
def func():
x = [1, 2, 3] # this will rais an exception when trying to run the function
y = 20
y = 30 # this will rais an exception when trying to run the function
Class constants
Ensuring every uppercase attribute is a constant
from constantia import consts
@consts('uppercase', check_at='import')
class Example:
X = 9999
Y = 'other constant'
X = 888 # this will raise an exception
Ensuring a constant is not re-assignable
from constantia import consts
@consts(['X'], check_at='import')
class Example:
X = 9999
X = 888 # this will raise an exception
@classmethod
def change_x1(cls):
cls.X = 8888 # this will raise an exception
@staticmethod
def change_x2():
Example.X = 8888 # this will raise an exception
Sensible defaults: constants are uppercase and check the constants at import time
from constantia import consts
@consts
class Example:
X = 9999
Y = 'other constant'
X = 888 # this will raise an exception
Dependencies
This package has no dependencies.
License
MIT license, but if you need any other contact me.
Metadata
Release files for constantia 0.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| constantia-0.0.5.tar.gz | 9.6 kB | Details |
Release files / constantia-0.0.5.tar.gz
| Download URL | constantia-0.0.5.tar.gz |
|---|---|
| Size | 9.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5d91b2b07b9b6e6fab4ddeff9daf76c087e660c01e75485231e14c588b67e91a
|
|
BLAKE2b-256 checksum How to use checksums |
99b5cb9906ac12ca31d6744b34d408a3719c83af9eaa05ddb6bf58c034a981e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|