Metadata-Version: 2.4
Name: setconstant
Version: 0.1.5
Summary: A Python package for managing constants effectively.
Home-page: https://github.com/Anuraj-CodeHaven
Author: Anuraj R
Author-email: anurajanu2883@gmail.com
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE.txt
Dynamic: author
Dynamic: author-email
Dynamic: classifier
Dynamic: description
Dynamic: description-content-type
Dynamic: home-page
Dynamic: license-file
Dynamic: requires-python
Dynamic: summary

# 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
---
