Skip to main content

Hetman Codetable provides centralized, standardized response codes for APIs and applications. It ensures consistency by treating codes as a single source of truth.

Project description

Hetman Logo

Hetman 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 hetman-codetable

Why use this?

Managing response codes and messages across an application can become chaotic. Hetman Codetable allows you to:

  1. Centralize all your application codes and messages in one place.
  2. Standardize API responses with consistent error codes and messages.
  3. Internationalize (i18n) messages easily, switching languages dynamically.

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.")

    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.'}

print(UserSuccess.CREATED)
# Output: {'code': 'user.created', 'msg': 'User created successfully.'}

# --- 2. Accessing i18n Codes (Default: English, i18n.set_base_locale("en")) ---
print(UserError.ALREADY_EXISTS)
# Output: {'code': 'user.already_exists', 'msg': 'User already exists.'}

# --- 3. Switching Locales (for all i18n codes) ---
i18n.set_locale("pl")

print(UserError.ALREADY_EXISTS)
# Output: {'code': 'user.already_exists', 'msg': 'Użytkownik już istnieje.'}

print(UserSuccess.UPDATED)
# Output: {'code': 'user.updated', 'msg': 'Użytkownik został zaktualizowany pomyślnie.'}

Customizing Key Maps

You can custom 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.ALREADY_EXISTS)
# Output: {'error_code': 'user.already_exists', 'message': 'Użytkownik już istnieje.'}

Core Features

  • Namespace Management: Automatically prefixes codes with the table's namespace (e.g., user.not_found).
  • 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 contextvars to handle per-request locales safely in async environments.

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

hetman_codetable-1.0.1.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

hetman_codetable-1.0.1-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file hetman_codetable-1.0.1.tar.gz.

File metadata

  • Download URL: hetman_codetable-1.0.1.tar.gz
  • Upload date:
  • Size: 8.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hetman_codetable-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2bbe033001685dc48e0bd42fb3e0d59a7cf8979f6bc016e46de2b335bc0773fd
MD5 a3c6e0248058dd6f3220154c354b2785
BLAKE2b-256 3c9ba9c1e243c1b71c94593c12c13d03fffc0ac87c595797507e34e15640f743

See more details on using hashes here.

Provenance

The following attestation bundles were made for hetman_codetable-1.0.1.tar.gz:

Publisher: python-publish.yml on hetman-app/hetman-codetable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hetman_codetable-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for hetman_codetable-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b019c47058667b6995fefa169d2c72f530cde1de8925a5982fb875a7a03c9005
MD5 3a9b0ea34a826c6bb3f89c813e5443e9
BLAKE2b-256 63d1132f8547519b11032e29f1b8f419adda6d110dd7e1b3efc14bd7c7b8b238

See more details on using hashes here.

Provenance

The following attestation bundles were made for hetman_codetable-1.0.1-py3-none-any.whl:

Publisher: python-publish.yml on hetman-app/hetman-codetable

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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