Skip to main content

Enforce constants at import time

Project description

constantia

test License Maintenance made-with-python PyPI pyversions PyPI version constantia PyPI status PyPI download month Maintainability 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

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

Dependencies

This package has no dependencies.

License

MIT license, but if you need any other contact me.

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

constantia-0.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

constantia-0.0.1-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file constantia-0.0.1.tar.gz.

File metadata

  • Download URL: constantia-0.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for constantia-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ab67fd3c5b88f08b031cf659b9695c1b726327a5d0a492dc7f4a1feaa99498b8
MD5 e39535a3b54d73b8ecb2a6b417d2a396
BLAKE2b-256 c7e84a745ccd578588b383a47f3ccbb96a4b3f23fcdc080125e2677c45123bc9

See more details on using hashes here.

File details

Details for the file constantia-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: constantia-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for constantia-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 045d7ebe1c76993e28757efa379dc86af0b095bf159eec4e1ef8f434cef07114
MD5 a82093084a675b2e341d8865b0925555
BLAKE2b-256 eb1132fc3a9b618b53ae3522ef155423bf027a9b906fc797ee670ab5114e02e1

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