https://github.com/diegojromerolopez/constantia
Enforce constants in your code at import or run time
https://github.com/diegojromerolopez/constantia
const python
Last synced: 5 months ago
JSON representation
Enforce constants in your code at import or run time
- Host: GitHub
- URL: https://github.com/diegojromerolopez/constantia
- Owner: diegojromerolopez
- License: mit
- Created: 2025-01-20T22:06:19.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-26T22:30:47.000Z (over 1 year ago)
- Last Synced: 2025-03-20T12:05:13.321Z (over 1 year ago)
- Topics: const, python
- Language: Python
- Homepage:
- Size: 23.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# constantia

[](https://opensource.org/licenses/MIT)
[](https://github.com/diegojromerolopez/constantia/graphs/commit-activity)
[](https://www.python.org/)
[](https://pypi.python.org/pypi/constantia/)
[](https://pypi.python.org/pypi/constantia/)
[](https://pypi.python.org/pypi/constantia/)
[](https://pypi.python.org/pypi/constantia/)
[](https://codeclimate.com/github/diegojromerolopez/constantia/maintainability)
[](https://codeclimate.com/github/diegojromerolopez/constantia/test_coverage)
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
```python
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
```python
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
```python
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
```python
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
```python
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) license, but if you need any other contact me.