Krywok Codetable provides centralized, standardized response codes for APIs and applications. It ensures consistency by treating codes as a single source of truth.
Project description
Krywok Codetable provides centralized, standardized response codes for APIs and applications. It ensures consistency by treating codes as a single source of truth.
Installation
pip install krywok-codetable
Why use this?
Managing response codes and messages across an application can become chaotic. Krywok Codetable allows you to:
- Centralize all your application codes and messages in one place.
- Standardize API responses with consistent error codes and messages.
- Internationalize (i18n) messages easily, switching languages dynamically.
- Inject dynamic data into messages using a simple formatting API.
Usage Example
Defining Codetables
You can organize your codes into multiple classes for better structure, even while sharing the same namespace.
from codetable import Codetable, msg, i18n
# 1. Set the base locale (Critical for i18n)
i18n.set_base_locale("en")
class UserError(Codetable):
NAMESPACE = "user"
NOT_FOUND = msg("User not found.")
LOCKED = msg("Account locked for {minutes} minutes.")
ALREADY_EXISTS = i18n(
en="User already exists.",
pl="Użytkownik już istnieje.",
)
class UserSuccess(Codetable):
NAMESPACE = "user"
CREATED = msg("User created successfully.")
UPDATED = i18n(
en="User updated successfully.",
pl="Użytkownik został zaktualizowany pomyślnie.",
)
Accessing Codes
# --- 1. Accessing Standard Codes ---
print(UserError.NOT_FOUND)
# Output: {'code': 'user.not_found', 'msg': 'User not found.'}
# --- 2. Dynamic Formatting ---
print(UserError.format(UserError.LOCKED, minutes=15))
# Output: {'code': 'user.locked', 'msg': 'Account locked for 15 minutes.'}
# --- 3. Accessing i18n Codes (Default: English, i18n.set_base_locale("en")) ---
print(UserError.ALREADY_EXISTS)
# Output: {'code': 'user.already_exists', 'msg': 'User already exists.'}
# --- 4. Switching Locales (for all i18n codes) ---
i18n.set_locale("pl")
print(UserError.ALREADY_EXISTS)
# Output: {'code': 'user.already_exists', 'msg': 'Użytkownik już istnieje.'}
Lazy Load
i18n.set_locale("en")
error = UserError.lazy("ALREADY_EXISTS")
print(error())
# Output: {'code': 'user.already_exists', 'msg': 'User already exists.'}
success = UserSuccess.lazy("UPDATED")
print(success())
# Output: {'code': 'user.updated', 'msg': 'User updated successfully.'}
Customizing Key Maps
You can customize output keys globally or per-table by modifying the key_map.
# Change default keys globally
Codetable.key_map = {"code": "error_code", "value": "message"}
print(UserError.NOT_FOUND)
# Output: {'error_code': 'user.not_found', 'message': 'User not found.'}
Core Features
- Namespace Management: Automatically prefixes codes with the table's namespace (e.g.,
user.not_found). - Dynamic Formatting: Supports injecting data into messages via
*argsand**kwargsusing the.format()method. - Strict Typing: Built with Python type hints for excellent IDE support.
- Dynamic Descriptor Protocol: Codes know their own variable names and parent tables automatically.
- Context-Aware I18n: Uses
contextvarsto handle per-request locales safely in async environments (e.g., FastAPI). - Customizable Output: Easily remap dictionary keys to match your existing API schema.
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 krywok_codetable-1.0.1.tar.gz.
File metadata
- Download URL: krywok_codetable-1.0.1.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a323d8fb8b3d8af2ad5bdcc5a285fbc72e8a8435ca5d790f9b00ec938164e86
|
|
| MD5 |
7398da977133b64307a0de8a37f9bf62
|
|
| BLAKE2b-256 |
1696b755d661de81cc83c5ee46d11c28deb0080698d1e48f539ecc3df767c7d3
|
Provenance
The following attestation bundles were made for krywok_codetable-1.0.1.tar.gz:
Publisher:
python-publish.yml on Krywok/krywok-codetable
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
krywok_codetable-1.0.1.tar.gz -
Subject digest:
9a323d8fb8b3d8af2ad5bdcc5a285fbc72e8a8435ca5d790f9b00ec938164e86 - Sigstore transparency entry: 1204331487
- Sigstore integration time:
-
Permalink:
Krywok/krywok-codetable@0e7c9d107624e358d9433a54e5d1db7e5c596388 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/Krywok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0e7c9d107624e358d9433a54e5d1db7e5c596388 -
Trigger Event:
release
-
Statement type:
File details
Details for the file krywok_codetable-1.0.1-py3-none-any.whl.
File metadata
- Download URL: krywok_codetable-1.0.1-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0c61f4c8f91caa024ed8435715747aa2638136ad509d3772fec0251e6bc3189
|
|
| MD5 |
ec2b90f2ad7d599e4ae33727814d8512
|
|
| BLAKE2b-256 |
77b6a46891a3ffbf502753438cff2f5f171e33a5d0ac855b1e185f1bc5bad794
|
Provenance
The following attestation bundles were made for krywok_codetable-1.0.1-py3-none-any.whl:
Publisher:
python-publish.yml on Krywok/krywok-codetable
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
krywok_codetable-1.0.1-py3-none-any.whl -
Subject digest:
a0c61f4c8f91caa024ed8435715747aa2638136ad509d3772fec0251e6bc3189 - Sigstore transparency entry: 1204331489
- Sigstore integration time:
-
Permalink:
Krywok/krywok-codetable@0e7c9d107624e358d9433a54e5d1db7e5c596388 -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/Krywok
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0e7c9d107624e358d9433a54e5d1db7e5c596388 -
Trigger Event:
release
-
Statement type: