Skip to main content

setconstant

setconstant is a Python package designed to help developers manage constants with type safety, ensuring that constants are defined once and cannot be redefined. This package is perfect for maintaining clean and error-free code when working with immutable values.


Features

  • Type-safe constants: Supports integer, float, and string constants.
  • Prevents redefinition: Ensures constants cannot be reassigned after declaration.
  • Easy retrieval and deletion: Retrieve or delete constants as needed.
  • Custom error handling: Provides descriptive errors for invalid operations.

Install

pip install setconstant

Import

from setconstant import setconstant

Functions

const_i(name, value) — declare an integer constant

Use this to declare a whole number constant. setconstant.const_i("MAX_USERS", 100) setconstant.const_i("PORT", 8080) setconstant.const_i("MAX_RETRY", 3)

Rules:

• Value must be a whole number. 8.0 or "8" will be rejected.
• True / False are not allowed here. Use const_b() for booleans.

setconstant.const_i("PORT", 8.5) # TypeError — must be int setconstant.const_i("PORT", "8080") # TypeError — must be int setconstant.const_i("PORT", True) # TypeError — use const_b()

const_f(name, value) — declare a float constant

Use this to declare a decimal number constant. setconstant.const_f("PI", 3.14159) setconstant.const_f("TIMEOUT", 30.0) setconstant.const_f("THRESHOLD", 0.95)

Rules:

• Value must have a decimal point. Writing 3 instead of 3.0 will be rejected.

setconstant.const_f("PI", 3) # ❌ TypeError — write 3.0 not 3 setconstant.const_f("PI", "3.14") # ❌ TypeError — must be float not string

const_s(name, value) — declare a string constant

Use this to declare a text constant. setconstant.const_s("APP_NAME", "MyApp") setconstant.const_s("DB_HOST", "localhost") setconstant.const_s("ENV", "production")

Rules:

• Value must be text inside quotes.

setconstant.const_s("ENV", 100) # ❌ TypeError — must be a string

const_b(name, value) — declare a boolean constant

Use this to declare a True / False constant. setconstant.const_b("DEBUG", False) setconstant.const_b("VERBOSE", True)

Rules:

• Value must be exactly True or False.

setconstant.const_b("DEBUG", 1) # ❌ TypeError — must be True or False setconstant.const_b("DEBUG", "yes") # ❌ TypeError — must be True or False

get(name) — read a constant

Use this to get the value of a constant you declared earlier. setconstant.const_i("PORT", 8080)

port = setconstant.get("PORT") print(port) # 8080 Works for all types: setconstant.const_f("PI", 3.14159) setconstant.const_s("ENV", "production") setconstant.const_b("DEBUG", False)

print(setconstant.get("PI")) # 3.14159 print(setconstant.get("ENV")) # production print(setconstant.get("DEBUG")) # False If the constant does not exist: setconstant.get("MISSING") # AttributeError — tells you what constants ARE defined

get_constant(name) — same as get()

This is an older name for get(). Both do the same thing. setconstant.get_constant("PORT") # same as setconstant.get("PORT")

get_type(name) — check what type a constant is

Returns the type that was used when the constant was declared. setconstant.const_i("PORT", 8080) setconstant.const_f("PI", 3.14) setconstant.const_s("ENV", "production")

print(setconstant.get_type("PORT")) # <class 'int'> print(setconstant.get_type("PI")) # <class 'float'> print(setconstant.get_type("ENV")) # <class 'str'>

is_defined(name) — check if a constant exists

Returns True if the constant has been declared, False if not. Useful before trying to read a constant you are not sure about. setconstant.const_i("PORT", 8080)

print(setconstant.is_defined("PORT")) # True print(setconstant.is_defined("UNKNOWN")) # False Safe declare pattern — declare only if not already declared: if not setconstant.is_defined("PORT"): setconstant.const_i("PORT", 8080)

summary() — print a table of all constants

Prints all declared constants in a readable table. Useful for debugging. setconstant.const_i("PORT", 8080) setconstant.const_s("HOST", "localhost") setconstant.const_f("TIMEOUT", 30.0) setconstant.const_b("DEBUG", False)

setconstant.summary() Output: ┌──────────┬──────────┬─────────────┐ │ Name │ Type │ Value │ ├──────────┼──────────┼─────────────┤ │ PORT │ int │ 8080 │ │ HOST │ str │ 'localhost' │ │ TIMEOUT │ float │ 30.0 │ │ DEBUG │ bool │ False │ └──────────┴──────────┴─────────────┘

Important Rules Once declared, a constant can never be changed. setconstant.const_i("PORT", 8080) setconstant.const_i("PORT", 9090) # ValueError — already declared setconstant.PORT = 9090 # AttributeError — direct assignment blocked Re-declaring with the exact same value is fine (safe for Jupyter / Colab re-runs): setconstant.const_i("PORT", 8080) setconstant.const_i("PORT", 8080) # same value — no error const_s() and other declare methods return nothing — always use get() to read:

Wrong

print(setconstant.const_s("ENV", "production")) # prints None

Correct

setconstant.const_s("ENV", "production") print(setconstant.get("ENV")) # prints production


Full Example

from setconstant import setconstant

Declare

setconstant.const_i("MAX_USERS", 500) setconstant.const_f("PI", 3.14159) setconstant.const_s("APP_NAME", "MyApp") setconstant.const_b("DEBUG", False)

Read

print(setconstant.get("MAX_USERS")) # 500 print(setconstant.get("PI")) # 3.14159 print(setconstant.get("APP_NAME")) # MyApp print(setconstant.get("DEBUG")) # False

Check

print(setconstant.is_defined("PI")) # True

Type

print(setconstant.get_type("PI")) # <class 'float'>

View all

setconstant.summary()


Developer

Anuraj R


Metadata

Release files for setconstant 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for setconstant 1.3.0
File Size Uploaded Provenance Actions
setconstant-1.3.0.tar.gz 7.2 kB Without provenance Details for setconstant-1.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for setconstant 1.3.0
File Interpreter ABI Platform Actions
setconstant-1.3.0-py3-none-any.whl Python 3 none any Details for setconstant-1.3.0-py3-none-any.whl

Total release size: 14.5 kB

Release files / setconstant-1.3.0.tar.gz

Download URL setconstant-1.3.0.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
052ea2971694513c4258a21185f8af3383ef08b0473afb98cc4e2d81d55516a5
BLAKE2b-256 checksum
How to use checksums
5072beab39fa517fc562109f7dbdeb71f08f42203f5558b0ad3d66fbfa35f3d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / setconstant-1.3.0-py3-none-any.whl

Download URL setconstant-1.3.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ca4ab172b76c2b76027d24efb15e856542d40cd08acd06aac464df063e1cbaef
BLAKE2b-256 checksum
How to use checksums
47c420e263a36f3bb99465c3996438867ea4ec76df064a15c2a1549bdc9f76a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for setconstant 1.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page