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

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.

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.5.tar.gz (9.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for constantia-0.0.5.tar.gz
Algorithm Hash digest
SHA256 5d91b2b07b9b6e6fab4ddeff9daf76c087e660c01e75485231e14c588b67e91a
MD5 9cc29853fb1d2e8fe14de63153c5ebcb
BLAKE2b-256 99b5cb9906ac12ca31d6744b34d408a3719c83af9eaa05ddb6bf58c034a981e2

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