python 中的枚举转换器

发布于 2024-10-19 04:30:06 字数 725 浏览 2 评论 0原文

我有一个枚举

class Nationality:
        Poland='PL'
        Germany='DE'
        France='FR'
        ...
        Spain='ES'

我有2个方法原型:

# I want somethink like in c#        
def convert_country_code_to_country_name(country_code):
        print Enum.Parse(typeof(Nationality),country_code)

#this a second solution ,but it has a lot of ifs

def convert_country_code_to_country_name(country_code):
        if country_code=='DE':
                print Nationality.Germany #btw how to print here 'Germany', instead 'DE'

这就是我想要调用这个方法的方式:

convert_country_code_to_country_name('DE') # I want here to  print 'Germany'

如何在python中实现它?

I have an enum

class Nationality:
        Poland='PL'
        Germany='DE'
        France='FR'
        ...
        Spain='ES'

I have 2 prototypes of methods:

# I want somethink like in c#        
def convert_country_code_to_country_name(country_code):
        print Enum.Parse(typeof(Nationality),country_code)

#this a second solution ,but it has a lot of ifs

def convert_country_code_to_country_name(country_code):
        if country_code=='DE':
                print Nationality.Germany #btw how to print here 'Germany', instead 'DE'

This is how I want call this method:

convert_country_code_to_country_name('DE') # I want here to  print 'Germany'

How to implement it in python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笑饮青盏花 2024-10-26 04:30:06

最好的解决方案是从一开始就创建一本字典。你的枚举在Python中没有意义,它只是不必要地复杂。看起来您正在尝试编写 Java 代码,这是与 Python 代码的预期完全相反。

The best solution would be to create a dictionary right from the start. Your enum doesn't make sense in Python, its just unnecessarily complex. It looks like you are trying to write Java code, which is quite the opposite of what Python code is supposed to look like.

雨落□心尘 2024-10-26 04:30:06

Python 3.4 有一个新的 Enum 数据类型(已向后移植),它可以轻松支持您的用例:

class Nationality(enum.Enum):
    Poland = 'PL'
    Germany = 'DE'
    France = 'FR'
    Spain = 'ES'

从名称获取枚举成员:

--> Nationality['France']
<Nationality.France: 'FR'>

从值获取枚举成员:

--> Nationalatiy('FR')
<Nationality.France: 'FR'>

一旦获得枚举成员:

--> member = Nationality('FR')
--> member.name
'France'
--> member.value
'FR'

Python 3.4 has a new Enum data type (which has been backported), which easily supports your use case:

class Nationality(enum.Enum):
    Poland = 'PL'
    Germany = 'DE'
    France = 'FR'
    Spain = 'ES'

To get the enum member from the name:

--> Nationality['France']
<Nationality.France: 'FR'>

To get the enum member from the value:

--> Nationalatiy('FR')
<Nationality.France: 'FR'>

And once you have the enum member:

--> member = Nationality('FR')
--> member.name
'France'
--> member.value
'FR'
转身泪倾城 2024-10-26 04:30:06
class Nationality:
        Poland='PL'
        Germany='DE'
        France='FR'
        Spain='ES'

nationalityDict = {}
for k, v in Nationality.__dict__.iteritems():
    if not k.startswith('__'):
        nationalityDict[v] = k

现在:nationalityDict['DE'] 包含Germany

class Nationality:
        Poland='PL'
        Germany='DE'
        France='FR'
        Spain='ES'

nationalityDict = {}
for k, v in Nationality.__dict__.iteritems():
    if not k.startswith('__'):
        nationalityDict[v] = k

And now: nationalityDict['DE'] contains Germany.

二手情话 2024-10-26 04:30:06

你想用 dict 代替吗?

Nationality = { 
    "PL" : "Poland",
    "DE": "Germany"}

print Nationality["DE"] # prints 'Germany'

Would you like to use dict instead?

Nationality = { 
    "PL" : "Poland",
    "DE": "Germany"}

print Nationality["DE"] # prints 'Germany'
新雨望断虹 2024-10-26 04:30:06

我的方法就像这样(也许不完美,但你明白了):

class Nationality:
        Poland = 'PL'
        Germany = 'DE'
        France = 'FR'

        def convertToCodeFromName(name):
                return getattr(Nationality, name)

        def convertToNameFromCode(code):
                lookFor = None

                for member in dir(Nationality):
                        if (getattr(Nationality, member) == code):
                                lookFor = member
                                break
                return lookFor

print(Nationality.convertToCodeFromName("Poland"))
print(Nationality.convertToNameFromCode("PL"))

希望这会有所帮助。

My approach would be like this one (maybe not perfect, but you get the idea):

class Nationality:
        Poland = 'PL'
        Germany = 'DE'
        France = 'FR'

        def convertToCodeFromName(name):
                return getattr(Nationality, name)

        def convertToNameFromCode(code):
                lookFor = None

                for member in dir(Nationality):
                        if (getattr(Nationality, member) == code):
                                lookFor = member
                                break
                return lookFor

print(Nationality.convertToCodeFromName("Poland"))
print(Nationality.convertToNameFromCode("PL"))

Hope this helps.

他不在意 2024-10-26 04:30:06

转换为枚举的纯方法怎么样?只需将其存储在 util lib 中即可。或者您可以将其放在基 Enum 类上,并将 target_enum 默认为 self。

还可以调整它以引发异常,而不是使用默认值,但默认值更适合我的特定情况。

from enum import Enum
def to_enum(v,target_enum,default = None):
    '''
    if v is given enum, return it
    if v is an int, convert to enum via int convert
    if v is str convert to enum via string convert
    '''
    ret = default

    if v is None:
        return ret

    if v in target_enum:
        return v

    try:
        return target_enum(int(v))
    except Exception:
        pass

    try:
        return target_enum[str(v)]
    except Exception:
        pass

    return ret

这是它的单元测试类。

import unittest
from enum import Enum,unique
class EnumTest(Enum):
    T0 = 0
    T1 = 1

class EnumTest2(Enum):
    T0 = 0
    T1 = 1

class EnumUtilTest(unittest.TestCase):
    def test_to_enum(self):
        '''str, int, and Enum should be convertable '''
        r = Util.to_enum(1,EnumTest)
        self.assertEqual(r,EnumTest.T1)
        r = Util.to_enum('T1',EnumTest)
        self.assertEqual(r,EnumTest.T1)
        r = Util.to_enum(EnumTest.T1,EnumTest)
        self.assertEqual(r,EnumTest.T1)
        r = Util.to_enum('1',EnumTest)
        self.assertEqual(r,EnumTest.T1)

    def test_to_enum_fail(self): 
        '''Return None if convert fails'''       
        self.assertIsNone(Util.to_enum( None,EnumTest ))
        self.assertIsNone(Util.to_enum( 'abd',EnumTest ))
        self.assertIsNone(Util.to_enum( 123,EnumTest ))
        self.assertIsNone(Util.to_enum( EnumTest2.T1,EnumTest ))

    def test_to_enum_default(self):
        '''test default param'''
        r = Util.to_enum(EnumTest2.T1,EnumTest,EnumTest.T0)
        self.assertEqual(r,EnumTest.T0)

if __name__ == '__main__':
    unittest.main()

How about a pure method for converting to enum? Just stash this in a util lib. Or you could put it on a base Enum class and have target_enum default to self.

Could also tweak this to raise an exception instead of using the default value, but the default worked better for my specific case.

from enum import Enum
def to_enum(v,target_enum,default = None):
    '''
    if v is given enum, return it
    if v is an int, convert to enum via int convert
    if v is str convert to enum via string convert
    '''
    ret = default

    if v is None:
        return ret

    if v in target_enum:
        return v

    try:
        return target_enum(int(v))
    except Exception:
        pass

    try:
        return target_enum[str(v)]
    except Exception:
        pass

    return ret

Here is the unittest class for it.

import unittest
from enum import Enum,unique
class EnumTest(Enum):
    T0 = 0
    T1 = 1

class EnumTest2(Enum):
    T0 = 0
    T1 = 1

class EnumUtilTest(unittest.TestCase):
    def test_to_enum(self):
        '''str, int, and Enum should be convertable '''
        r = Util.to_enum(1,EnumTest)
        self.assertEqual(r,EnumTest.T1)
        r = Util.to_enum('T1',EnumTest)
        self.assertEqual(r,EnumTest.T1)
        r = Util.to_enum(EnumTest.T1,EnumTest)
        self.assertEqual(r,EnumTest.T1)
        r = Util.to_enum('1',EnumTest)
        self.assertEqual(r,EnumTest.T1)

    def test_to_enum_fail(self): 
        '''Return None if convert fails'''       
        self.assertIsNone(Util.to_enum( None,EnumTest ))
        self.assertIsNone(Util.to_enum( 'abd',EnumTest ))
        self.assertIsNone(Util.to_enum( 123,EnumTest ))
        self.assertIsNone(Util.to_enum( EnumTest2.T1,EnumTest ))

    def test_to_enum_default(self):
        '''test default param'''
        r = Util.to_enum(EnumTest2.T1,EnumTest,EnumTest.T0)
        self.assertEqual(r,EnumTest.T0)

if __name__ == '__main__':
    unittest.main()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文