Skip to main content

A Python package for managing constants effectively.

Project description

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


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

setconstant-1.3.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

setconstant-1.3.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file setconstant-1.3.0.tar.gz.

File metadata

  • Download URL: setconstant-1.3.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for setconstant-1.3.0.tar.gz
Algorithm Hash digest
SHA256 052ea2971694513c4258a21185f8af3383ef08b0473afb98cc4e2d81d55516a5
MD5 e1dae7a3125578520b2935d2e12ff75c
BLAKE2b-256 5072beab39fa517fc562109f7dbdeb71f08f42203f5558b0ad3d66fbfa35f3d9

See more details on using hashes here.

File details

Details for the file setconstant-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: setconstant-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for setconstant-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca4ab172b76c2b76027d24efb15e856542d40cd08acd06aac464df063e1cbaef
MD5 246a005a33a2d9a2ba7c3a03337ae82a
BLAKE2b-256 47c420e263a36f3bb99465c3996438867ea4ec76df064a15c2a1549bdc9f76a5

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