In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to flexibly use Python enumeration class to achieve design status code information, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
In web projects, we often use custom status codes to inform the requestor of the request result and request status; how to design custom status code information in Python?
General class plus dictionary design status code #! / usr/bin/python3#-*-coding: utf-8-*-# @ Author: Hui# @ Desc: {Project response Code Module} # @ Date: 2021-09-22 23:37class RETCODE: OK = "0" ERROR = "- 1" IMAGECODEERR = "4001" THROTTLINGERR = "4002" NECESSARYPARAMERR = "4003" err_msg = {RETCODE.OK: "successful" RETCODE.IMAGECODEERR: "graphic CAPTCHA error", RETCODE.THROTTLINGERR: "access too frequently", RETCODE.NECESSARYPARAMERR: "missing required parameters",}
A separate dictionary is used to compare the status code information, so that once the design has more status codes, it is not easy to compare, and it is not so convenient to use it again. Simply try to organize a successful information.
Data = {'code': RETCODE.OK,' errmsg': err_ msg [RETCODE.OK]} skillfully using enumerated classes to design status code information
The status code information can be skillfully designed by using the enumeration class.
Definition of enumeration class #! / usr/bin/python3#-*-coding: utf-8-*-# @ Author: Hui# @ Desc: {Project enumeration class module} # @ Date: 2021-09-23 23:37from enum import Enumclass StatusCodeEnum (Enum): "status code enumeration class" OK = (0, 'success') ERROR = (- 1, 'error') SERVER_ERR = (500, 'server exception')
A normal class inherits the Enum class in the enum module and becomes an enumerated class.
The use of enumerated classes
Test and use in ipython
In [21]: ok = StatusCodeEnum.OKIn [22]: type (ok) Out [22]: In [23]: error = StatusCodeEnum.ERRORIn [24]: type (error) Out [24]: In [26]: ok.nameOut [26]: 'OK'In [27]: ok.valueOut [27]: (0,' success') In [28]: 'ERROR'In [29]: error.valueOut [29]: (- 1,' error')
Each property in the enumeration class returns an enumeration object, where the enumeration object has two important properties, name and value
Property names of name enumerated objects in enumerated classes
Value is the value of the enumeration object corresponding to the property name in the enumeration class.
# StatusCodeEnum.OK-> # name value# 'OK' (200,' success') # StatusCodeEnum.ERROR-> # name value# 'ERROR' (- 1,' error')
Organize a successful response message with enumerated class groups
Code = StatusCodeEnum.OK.value [0] errmsg = StatusCodeEnum.OK.value [1] data = {'code': code,' errmsg': errmsg}
At first glance, although the status code information is compared one by one, it is also very simple, but it is still a bit troublesome to use.
A grammar like StatusCodeEnum.OK.value [0] does not immediately know its meaning by name. Therefore, we also need to encapsulate the enumerated class.
Wrapper enumeration class #! / usr/bin/python3#-*-coding: utf-8-*-# @ Author: Hui# @ Desc: {Project enumeration class module} # @ Date: 2021-09-23 23:37from enum import Enumclass StatusCodeEnum (Enum): "" status code enumeration class "OK = (0, 'success') ERROR = (- 1, 'error') SERVER_ERR = 'server exception') @ property def code (self): "get status code"return self.value [0] @ property def errmsg (self):" get status code information "" return self.value [1]
Use the method of the type as a property through the @ property decorator, because the enumerated class. Attribute names correspond to different enumerated objects to encapsulate the status code and information. Look at the result of the external call
In [32]: StatusCodeEnum.OK.codeOut [32]: 0In [33]: StatusCodeEnum.OK.errmsgOut [33]: 'success' In [34]: StatusCodeEnum.ERROR.codeOut [34]:-1In [35]: StatusCodeEnum.ERROR.errmsgOut [35]: 'error'
For a detailed explanation of the use of @ property decorator, you can move to the skills of using property in Python
Continue to simulate organizational response data
Data = {'code': StatusCodeEnum.OK.code,' errmsg': StatusCodeEnum.OK.errmsg}
Now it's finally acceptable.
Status code information enumeration class
Share a wave of my usual status code information enumeration class for your reference.
#! / usr/bin/python3#-*-coding: utf-8-*-# @ Author: Hui# @ Desc: {Project enumeration class module} # @ Date: 2021-09-23 23:37from enum import Enumclass StatusCodeEnum (Enum): "" status code enumeration class "OK = (0, 'success') ERROR = (- 1, 'error') SERVER_ERR = (500) 'server exception') IMAGE_CODE_ERR = (4001, 'graphic CAPTCHA error') THROTTLING_ERR = (4002, 'access too frequently') NECESSARY_PARAM_ERR = (4003, 'missing required parameter') USER_ERR = (4004, 'wrong username') PWD_ERR = (4005, 'incorrect password') CPWD_ERR = (4006) 'inconsistent password') MOBILE_ERR = (4007, 'incorrect mobile number') SMS_CODE_ERR = (4008, 'incorrect SMS verification code') ALLOW_ERR = (4009, 'unchecked protocol') SESSION_ERR = (4010, 'user not logged in') DB_ERR = (5000, 'data error') EMAIL_ERR = (5001, 'incorrect mailbox') TEL_ERR = (5002) 'fixed phone error') NODATA_ERR = (5003,'no data') NEW_PWD_ERR = (5004, 'new password error') OPENID_ERR = (5005, 'invalid openid') PARAM_ERR = (5006,' parameter error') STOCK_ERR = (5007) 'insufficient inventory') @ property def code (self): "get status code"return self.value [0] @ property def errmsg (self):" get status code information "" return self.value [1]
After reading the above, have you mastered how to flexibly use the Python enumeration class to implement the design status code information? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.