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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file setconstant-0.1.5.tar.gz.
File metadata
- Download URL: setconstant-0.1.5.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e64f45e99fcd60ba0e9863de9053c99513348a0db8a7f2d87c76f0c37bcd772
|
|
| MD5 |
0e3d59ba9035b2b21e09c17a83008fde
|
|
| BLAKE2b-256 |
d687fbf8954fa004caf07ec0f0498fc917c3067a105be1a2de16e8eeadc0a0c3
|
File details
Details for the file setconstant-0.1.5-py3-none-any.whl.
File metadata
- Download URL: setconstant-0.1.5-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6da49c66f3670488a124ccdb7bd401e14d518e1986056e639157363826893167
|
|
| MD5 |
bdb7e094e29aeb31121dddbdad8f0dda
|
|
| BLAKE2b-256 |
52ed8ca35fed74d8013155805efeba227065f350de054145abb3e38791e752a7
|