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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for constantia-0.0.4.tar.gz
Algorithm Hash digest
SHA256 3d3645ba0553277d21fa9d0a5c75f556eac8c9537aeb416945334278675d1d38
MD5 6acf863d03a90ba632a71a8a293dfe09
BLAKE2b-256 8a2f00c5a32b7acf851652e025d8973dbb2567fd464e70c6918b1baded969479

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