Skip to main content

Define const field and const class, customize it as You Like (Python)

Project description

kkconst is a constant-tools library.

Support Types:

int, str, bytes, datetime

In PY2: with unicode


int: status_code

from __future__ import print_function
import json
from collections import OrderedDict
from kkconst import BaseConst, ConstIntField

class BaseStatusCode(BaseConst):
    class Meta:
        allow_duplicated_value = False  # status_code should be no duplicated value

    def get_message(cls, status_code, default=None):
        return cls.get_verbose_name(status_code, default=default)

class StatusCodeField(ConstIntField):
    def __init__(self, status_code, message=u"", description=u""):
        ConstIntField.__init__(status_code, verbose_name=message, description=description)
        self.message = message

class ServiceStatusCode(BaseStatusCode):
    SERVICE_UNAVAILABLE = StatusCodeField(10001, u"service unavailable", description=u"server is sleeping/服务打盹了")

status_code = ServiceStatusCode.SERVICE_UNAVAILABLE
assert isinstance(status_code, ConstIntField)
assert isinstance(status_code, int)

print(status_code.verbose_name) # "service unavailable"
print(status_code.description)  # "server is sleeping"
print(ServiceStatusCode.get_verbose_name(status_code))  # "service unavailable"

# for restful response
response_data = OrderedDict()
response_data["status_code"] = status_code
response_data["message"] = status_code.verbose_name  # also status_code.message is the same value
response_data["description"] = status_code.description
response_data["extra_message"] = "may you live in an interesting time"
print(json.dumps(response_data, indent=2))
# {
#    "status_code": 10001,
#    "message": "service unavailable",
#    "description": "server is sleeping",
#    "extra_message": "may you live in an interesting time"
# }
# def views(request):
#     ...
#     return HttpResponse(200, response_data, "application/json")

float: math constant

from __future__ import print_function
from kkconst import (

class MathConst(BaseConst):
    PI = ConstFloatField(3.1415926, verbose_name=u"Pi")
    E = ConstFloatField(2.7182818284, verbose_name=u"mathematical constant")  # Euler's number"
    GOLDEN_RATIO = ConstFloatField(0.6180339887, verbose_name=u"Golden Ratio")

magic_num = MathConst.GOLDEN_RATIO
assert isinstance(magic_num, ConstFloatField)
assert isinstance(magic_num, float)

print(magic_num)  # 0.6180339887
print(magic_num.verbose_name)  # Golden Ratio
print(MathConst.get_verbose_name(magic_num))  # Golden Ratio
print(MathConst.get_verbose_name(magic_num.TYPE(magic_num)))  # Golden Ratio
print(MathConst.get_verbose_name(0.6180339887))  # Golden Ratio
print(MathConst.get_verbose_name(0.618033988))  # None
print(MathConst.get_verbose_name(0.618))  # None

str: system message

from kkconst import BaseConst, ConstStringField
# Python3 ConstStringField is equal to ConstUnicodeField
class SystemMessage(BaseConst):
    SERVICE_UNAVAILABLE = ConstStringField(u"service unavailable", verbose_name=u"Service is sleeping")
    PERMISSION_DENY = ConstStringField(u"permission deny", verbose_name=u"your have no permission")

datetime: released datetime

class ReleasedDatetime(BaseConst):
    PYTHON_2_0 = ConstDatetimeField("2000-10-16", verbose_name="PY2 Released")
    PYTHON_3_0 = ConstDatetimeField(datetime.datetime(year=2008, month=12, day=3), verbose_name="PY3 Released")

released_datetime = ReleasedDatetime.PYTHON_2_0
assert isinstance(field_value, ConstDatetimeField)
assert isinstance(field_value, datetime.datetime)

print(field_value.verbose_name)  # "PY2 Released"
print(ReleasedDatetime.get_verbose_name(field_value))  # "PY2 Released"

# param type is datetime
# output: PY2 Released

print(ReleasedDatetime.get_verbose_name(datetime.datetime(year=2000, month=10, day=16)))
# if raw_field_value is str, output None
# output: PY2 Released

Get It Now

$ pip install kkconst


MIT licensed. See the bundled LICENSE file for more details.


  • Python >= 2.7 or >= 3.4

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

kkconst-1.0.25.tar.gz (5.6 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page