Package: kkconst
kkconstis a constant-tools library.Project LinksGitHub:https://github.com/kaka19ace/kkconstPyPi:https://pypi.python.org/pypi/kkconstSupport Types:int, str, bytes, datetimeIn PY2: with unicodeNew Features1.1.3bugfix1.1.2fixed demos code1.1.0Meta support ‘strict_capital’ attribute to require const class’s const variable capital naming.Exampleint: status_codefrom__future__importprint_functionimportjsonfromcollectionsimportOrderedDictfromkkconstimportBaseConst,ConstIntFieldclassBaseStatusCode(BaseConst):classMeta:allow_duplicated_value=False# status_code should be no duplicated valueclassStatusCodeField(ConstIntField):def__init__(self,status_code,message=u"",description=u""):ConstIntField.__init__(status_code,verbose_name=message,description=description)self.message=messageclassServiceStatusCode(BaseStatusCode):SERVICE_UNAVAILABLE=StatusCodeField(10001,u"service unavailable",description=u"server is sleeping/服务打盹了")status_code=ServiceStatusCode.SERVICE_UNAVAILABLEassertisinstance(status_code,ConstIntField)assertisinstance(status_code,int)print(status_code.verbose_name)# "service unavailable"print(status_code.description)# "server is sleeping"# for restful responseresponse_data=OrderedDict()response_data["status_code"]=status_coderesponse_data["message"]=status_code.verbose_name# also status_code.message is the same valueresponse_data["description"]=status_code.descriptionresponse_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 constantfrom__future__importprint_functionfromkkconstimport(BaseConst,ConstFloatField,)classMathConst(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_RATIOassertisinstance(magic_num,ConstFloatField)assertisinstance(magic_num,float)print(magic_num)# 0.6180339887print(magic_num.verbose_name)# Golden Ratiostr: system messagefromkkconstimportBaseConst,ConstStringField# Python3 ConstStringField is equal to ConstUnicodeFieldclassSystemMessage(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 datetimeclassReleasedDatetime(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_0assertisinstance(field_value,ConstDatetimeField)assertisinstance(field_value,datetime.datetime)print(field_value)print(field_value.verbose_name)# "PY2 Released"BaseConst: Your Const Helperlike peewee model’ Meta, we can use Meta to help manage the const variabletheMetasupportsuchattributes:allow_duplicated_value-booldefaultTrue,whensetFalse,ifexistsavariablehasthesameastheanothervariabledefinedbefore,thenraiseAttributeErrorstrict_capital-booldefaultTrue,theConstClasswillchecktheconstvariablename,ifnameisnotupper,thenraiseAttributeErrorclassErrorCode(BaseConst):classMeta:allow_duplicated_value=Falsestrict_capital=TrueUSER_ID_NOT_EXISTS=ConstIntField(20001,verbose_name="USER_ID_ERROR")# will raise Error, because has the same with USER_ID_NOT_EXISTS# USER_EMAIL_INVALID = ConstIntField(20001, verbose_name="USER_EMAIL_ERROR")# will raise Error, because some letters are lowercase# User_Password_Invalid = ConstIntField(20003, verbose_name="USER_PASSWORD_ERROR")Get It Now$ pip install kkconstLicenseMIT licensed. See the bundledLICENSEfile for more details.RequirementsPython >= 2.7 or >= 3.4