定义const字段和const类,根据需要进行自定义(python)

kkconst的Python项目详细描述


kkconst是一个常量工具库。

支撑类型:

int,str,字节,日期时间

在py2中:使用unicode

新功能

1.1.3
  1. 错误修复
1.1.2
  1. 固定演示代码
1.1.0
  1. 元支持“strict_capital”属性,要求const类的const变量大写命名。

示例

int:状态代码

from__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:数学常数

from__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 Ratio

str:系统消息

fromkkconstimportBaseConst,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:发布日期时间

classReleasedDatetime(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:您的const助手
就像peewee model的meta一样,我们可以使用meta来帮助管理const变量
theMetasupportsuchattributes:allow_duplicated_value-booldefaultTrue,whensetFalse,ifexistsavariablehasthesameastheanothervariabledefinedbefore,thenraiseAttributeErrorstrict_capital-booldefaultTrue,theConstClasswillchecktheconstvariablename,ifnameisnotupper,thenraiseAttributeError
classErrorCode(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")

立即获取

$ pip install kkconst

许可证

麻省理工学院许可。有关详细信息,请参阅捆绑的LICENSE文件。

要求

  • python>;=2.7或>;=3.4

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java getResourceAsStream()返回内容为空的流   java用小数组替换大数组或映射的一部分   java如何获得安卓的运营商功能   在Java中,可以从数组中的值一次分配给多个变量吗?   java Spring透视图没有在eclipse中显示   Java中的通用LinkedList克隆   java确实避开了线程的分析处理。holdsLock()正确吗?   java SocketInputStream。客户端使用jax rs向服务器发送请求时,socketRead0挂起   java从扫描仪读取多个输入   java Siddhi是否无法按多个变量分组?   java字符串将不会绘制   如何在Java中遍历属性文件中的值   对象列表的java JSON字符串   JavaGridLayout不考虑给定的行数和列数   java SSL:解析证书时出错:确定   java检查Hashmap中的键是否在AutoCompleteTextView中键入   java如何从HTML服务器获取JSON对象作为响应   Java对于类型列表<Object>   在AnyLogic中与外部java应用程序一起使用ExperimentRunFast