Python - 来自另一个枚举的枚举键
Python - Enum Keys From Another Enum
我有一个枚举 class,代码如下:
# enums.py
class AuthCode(Enum):
ALREADY_AUTHENTICATED = 1
MISSING_EMAIL = 2
MISSING_PASSWORD = 3
MISSING_REGISTRATION_TOKEN_CODE = 4
INVALID_EMAIL = 5
REDEEMED_EMAIL = 6
INVALID_PASSWORD = 7
INVALID_REGISTRATION_TOKEN_CODE = 8
REDEEMED_REGISTRATION_TOKEN_CODE = 9
INVALID_CREDENTIALS = 10
SIGNIN_SUCCESSFUL = 11
REGISTRATION_SUCCESSFUL = 12
class AuthCodeMessage(Enum):
AuthCode.ALREADY_AUTHENTICATED = "User was already logged in"
AuthCode.MISSING_EMAIL = "Email was not specified"
AuthCode.MISSING_PASSWORD = "Password was not specified"
AuthCode.MISSING_REGISTRATION_TOKEN_CODE = "Registration token code was not specified"
AuthCode.INVALID_EMAIL = "Email is invalid"
AuthCode.REDEEMED_EMAIL = "An account with this email has been created already"
AuthCode.INVALID_PASSWORD = "Password does not meet minimum password requirements"
AuthCode.INVALID_REGISTRATION_TOKEN_CODE = "Registration token code is invalid"
AuthCode.REDEEMED_REGISTRATION_TOKEN_CODE = "Registration token code has been redeemed already"
AuthCode.INVALID_CREDENTIALS = "Account with these credentials does not exist"
AuthCode.SIGNIN_SUCCESSFUL = "User has been signed in successfully"
AuthCode.REGISTRATION_SUCCESSFUL = "User account has been created successfully"
是否可以按照定义的方式定义第二个枚举 (AuthCodeMessage)(即,使用另一个枚举作为键)。如何检索枚举值?
编辑 1:
我想使用类似于以下的方法从我的代码中调用我的枚举:
from enums import AuthCode, AuthCodeMessage
def authResponse(authCode):
AuthCode.ALREADY_AUTHENTICATED
return jsonify({
"code": authCode,
"message": AuthCodeMessage.authCode
})
authResponse(AuthCode.ALREADY_AUTHENTICATED)
您真正想要的是将 AuthCode
映射到字符串。在 Python 中执行此操作的常用方法是使用字典,如下所示:
AUTHCODE_MESSAGES = {
AuthCode.ALREADY_AUTHENTICATED: "User was already logged in",
# copy the rest of the code/message pairs here
# make sure to replace the = with :
# and add a , on the end of every line
}
然后您可以像这样使用它:
# FIXME: enums is not a very clear name for what it does.
from enums import AuthCode, AUTHCODE_MESSAGES
def authResponse(authCode):
return jsonify({
"code": authCode,
"message": AUTHCODE_MESSAGES[authCode]
})
不用两个枚举,只用一个。使用文档(第二个)中的 AutoNumber recipe,您的代码将如下所示:
class AuthCode(AutoNumber):
ALREADY_AUTHENTICATED = "User was already logged in"
MISSING_EMAIL = "Email was not specified"
MISSING_PASSWORD = "Password was not specified"
...
def __init__(self, description):
self.description = description
及以后:
def authResponse(authCode):
return jsonify({
"code": authCode.value,
"message": authCode.description,
})
--
披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。
我有一个枚举 class,代码如下:
# enums.py
class AuthCode(Enum):
ALREADY_AUTHENTICATED = 1
MISSING_EMAIL = 2
MISSING_PASSWORD = 3
MISSING_REGISTRATION_TOKEN_CODE = 4
INVALID_EMAIL = 5
REDEEMED_EMAIL = 6
INVALID_PASSWORD = 7
INVALID_REGISTRATION_TOKEN_CODE = 8
REDEEMED_REGISTRATION_TOKEN_CODE = 9
INVALID_CREDENTIALS = 10
SIGNIN_SUCCESSFUL = 11
REGISTRATION_SUCCESSFUL = 12
class AuthCodeMessage(Enum):
AuthCode.ALREADY_AUTHENTICATED = "User was already logged in"
AuthCode.MISSING_EMAIL = "Email was not specified"
AuthCode.MISSING_PASSWORD = "Password was not specified"
AuthCode.MISSING_REGISTRATION_TOKEN_CODE = "Registration token code was not specified"
AuthCode.INVALID_EMAIL = "Email is invalid"
AuthCode.REDEEMED_EMAIL = "An account with this email has been created already"
AuthCode.INVALID_PASSWORD = "Password does not meet minimum password requirements"
AuthCode.INVALID_REGISTRATION_TOKEN_CODE = "Registration token code is invalid"
AuthCode.REDEEMED_REGISTRATION_TOKEN_CODE = "Registration token code has been redeemed already"
AuthCode.INVALID_CREDENTIALS = "Account with these credentials does not exist"
AuthCode.SIGNIN_SUCCESSFUL = "User has been signed in successfully"
AuthCode.REGISTRATION_SUCCESSFUL = "User account has been created successfully"
是否可以按照定义的方式定义第二个枚举 (AuthCodeMessage)(即,使用另一个枚举作为键)。如何检索枚举值?
编辑 1: 我想使用类似于以下的方法从我的代码中调用我的枚举:
from enums import AuthCode, AuthCodeMessage
def authResponse(authCode):
AuthCode.ALREADY_AUTHENTICATED
return jsonify({
"code": authCode,
"message": AuthCodeMessage.authCode
})
authResponse(AuthCode.ALREADY_AUTHENTICATED)
您真正想要的是将 AuthCode
映射到字符串。在 Python 中执行此操作的常用方法是使用字典,如下所示:
AUTHCODE_MESSAGES = {
AuthCode.ALREADY_AUTHENTICATED: "User was already logged in",
# copy the rest of the code/message pairs here
# make sure to replace the = with :
# and add a , on the end of every line
}
然后您可以像这样使用它:
# FIXME: enums is not a very clear name for what it does.
from enums import AuthCode, AUTHCODE_MESSAGES
def authResponse(authCode):
return jsonify({
"code": authCode,
"message": AUTHCODE_MESSAGES[authCode]
})
不用两个枚举,只用一个。使用文档(第二个)中的 AutoNumber recipe,您的代码将如下所示:
class AuthCode(AutoNumber):
ALREADY_AUTHENTICATED = "User was already logged in"
MISSING_EMAIL = "Email was not specified"
MISSING_PASSWORD = "Password was not specified"
...
def __init__(self, description):
self.description = description
及以后:
def authResponse(authCode):
return jsonify({
"code": authCode.value,
"message": authCode.description,
})
--
披露:我是 Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) 库的作者。