Skip to main content

Codetable is a lightweight package for seamlessly setting up codes, such as those used in API responses.

Project description

from codetable import Code, Codes, CodesStorage, msg

codes_storage = CodesStorage()


class UserErrorCodes(Codes):
    NAMESPACE: str = "user"

    EXPORT_TO: CodesStorage = codes_storage  # For example, export status codes to global storage for use in frontend translations.

    ALREADY_EXISTS: str
    DOES_NOT_EXIST: Code = msg("User does not exist.")


print("# UserErrorCodes.ALREADY_EXISTS\n")
print("code:", UserErrorCodes.ALREADY_EXISTS)

print("\n# UserErrorCodes.DOES_NOT_EXIST\n")
print("obj:", UserErrorCodes.DOES_NOT_EXIST)
print("code:", UserErrorCodes.DOES_NOT_EXIST.code)
print("msg:", UserErrorCodes.DOES_NOT_EXIST.msg)

print("\n# UserErrorCodes.export_codes()\n")
print(UserErrorCodes.export_codes())


class CatCodes(Codes):
    NAMESPACE: str = "cat"

    EXPORT_TO: CodesStorage = codes_storage  # We don't call export_codes here, but it will still be exported due to the presence of EXPORT_TO. export_codes is run automatically when EXPORT_TO is present.

    NOT_HUNGRY: Code = msg("Cat is not hungry!")


print("\n# codes_storage\n")
print(codes_storage)

print("\n# codes_storage.to_json()")
print("# !!! You can change ident by passing an argument !!!\n")
print(codes_storage.to_json())

print("\n# codes_storage.to_dict()\n")
print(codes_storage.to_dict())

# # UserErrorCodes.ALREADY_EXISTS

# code: user_already_exists

# # UserErrorCodes.DOES_NOT_EXIST

# obj: Code(code='user_does_not_exist', msg='User does not exist.')
# code: user_does_not_exist
# msg: User does not exist.

# # UserErrorCodes.export_codes()

# {'user_already_exists': None, 'user_does_not_exist': 'User does not exist.'}

# # codes_storage

# CodesStorage(storage={'user_already_exists': None, 'user_does_not_exist': 'User does not exist.', 'cat_not_hungry': 'Cat is not hungry!'})

# # codes_storage.to_json()
# # !!! You can change ident by passing an argument !!!

# {
#     "user_already_exists": null,
#     "user_does_not_exist": "User does not exist.",
#     "cat_not_hungry": "Cat is not hungry!"
# }

# # codes_storage.to_dict()

# {'user_already_exists': None, 'user_does_not_exist': 'User does not exist.', 'cat_not_hungry': 'Cat is not hungry!'}

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

codetable-1.0.5.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

codetable-1.0.5-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file codetable-1.0.5.tar.gz.

File metadata

  • Download URL: codetable-1.0.5.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for codetable-1.0.5.tar.gz
Algorithm Hash digest
SHA256 39d248a1c69bb55ad96fa2a615492538742b5ffbe725c1b6d518fb0831b82e58
MD5 9ed7584a01abcc06dba7e5757d60938b
BLAKE2b-256 fa00c70b349ffc38800940ad072597f6c7012376c392b883f24ad6d9ea6c9770

See more details on using hashes here.

File details

Details for the file codetable-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: codetable-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.11

File hashes

Hashes for codetable-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 544dea0c4d101cbde9f525c0e8e389421f9618237a8104be74591d928b903260
MD5 79e5d76e2bb9323209c231ae669fefcc
BLAKE2b-256 4d92c53a8d790d8c6d8fbebc8281535b18155f9c012b37b9deeb606be6b6d074

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