Python Enum类(带有tostring fromstring) [英] Python Enum class (with tostring fromstring)

查看:173
本文介绍了Python Enum类(带有tostring fromstring)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个简单的方法来实现(hack)一个枚举到Python中:

  class MyEnum:
VAL1,VAL2,VAL3 = range(3)

我可以这么称呼: p>

  bob = MyEnum.VAL1 

性感!



好的,现在我想要获得两个数值,如果给定一个字符串,或一个字符串,如果给定一个数值。假设我想要的字符串完全匹配到枚举键的



最好的我可以想到的是这样的:

  class MyEnum:
VAL1,VAL2,VAL3 = range(3)
@classmethod
def tostring(cls,val):
if(val == cls.VAL1):
returnVAL1
elif(val == cls.VAL2):
returnVAL2
elif(val == cls.VAL3):
返回VAL3
其他:
返回无
@classmethod
def fromstring(cls,str):
if (str.upper()==VAL1):
return cls.VAL1
elif(str.upper()==VAL2):
return cls.VAL2
elif(str.upper()==VAL2):
返回cls.VAL2
其他:
返回无

或类似的东西(忽略我如何捕捉无效案例)



有没有更好的,更多的python以中心的方式做我在做的事情上面?或者是上面已经很简洁了。



似乎有一个更好的方法来做。

解决方案

这是你要求的:

  class MyEnum :
VAL1,VAL2,VAL3 = range(3)
@classmethod
def tostring(cls,val):
for k,v in vars(cls).iteritems() :
if v == val:
return k

@classmethod
def fromstring(cls,str):
return getattr(cls,str。上(),无)

打印MyEnum.fromstring('Val1')
打印MyEnum.tostring(2)

但我真的没有得到Python中的枚举点。它具有如此丰富的系统以及发电机和协同管理国家。



我知道我没有在Python中使用Enums超过12年,也许你可以摆脱它们; - )


I've found a simply way to implement(hack) an enum into Python:

class MyEnum:
  VAL1, VAL2, VAL3 = range(3)

I can then call this as such:

bob = MyEnum.VAL1

Sexy!

Alright, now I want to be able to get both the numerical value if given a string, or a string if given a numerical value. Let's say I want the strings to exactly match up to the Enum key's

The best I could think of is something like this:

class MyEnum:
  VAL1, VAL2, VAL3 = range(3)
  @classmethod
  def tostring(cls, val):
    if (val == cls.VAL1):
      return "VAL1"
    elif (val == cls.VAL2):
      return "VAL2"
    elif (val == cls.VAL3):
      return "VAL3"
    else:
      return None
  @classmethod
  def fromstring(cls, str):
    if (str.upper() == "VAL1"):
      return cls.VAL1
    elif (str.upper() == "VAL2"):
      return cls.VAL2
    elif (str.upper() == "VAL2"):
      return cls.VAL2
    else:
      return None

or something like that (ignore how i'm catching invalid cases)

Is there a better, more python centric way to do what I'm doing above? Or is the above already as concise as it gets.

It seems like there's got to be a better way to do it.

解决方案

Well, here is what you asked for:

class MyEnum:
  VAL1, VAL2, VAL3 = range(3)
  @classmethod
  def tostring(cls, val):
    for k,v in vars(cls).iteritems():
        if v==val:
            return k

  @classmethod
  def fromstring(cls, str):
      return getattr(cls, str.upper(), None)

print MyEnum.fromstring('Val1')
print MyEnum.tostring(2)

But I really don't get the point of Enums in Python. It has such a rich type system as well as generators and coroutines to manage states.

I know I've not been using Enums in Python for more than 12 years, maybe you can get rid of them too ;-)

这篇关于Python Enum类(带有tostring fromstring)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆