使用枚举类属性的正确方法

发布于 2025-02-08 13:11:16 字数 698 浏览 1 评论 0原文

很少有.xlsx文件存储我指的数据。 将它们添加到枚举课程中。在源代码文件中,我需要在此处获取此枚举类的成员的一些属性,例如此处的文件名。 以下代码是否可以很好地解决问题,还是有根据最佳实践对其进行重新处理的途径?谢谢你!

from enum import Enum
class Data(Enum):
    TYPE_A = 1
    TYPE_B = 2
    TYPE_C = 3
    TYPE_D = 4
    TYPE_E = 5
    TYPE_F = 6
    TYPE_G = 7

    @property
    def file_name(cls):
        FILE_NAMES_DATA = (
            'TYPE_A.xlsx',
            'TYPE_B.xlsx',
            'TYPE_C.xlsx',
            'TYPE_D.xlsx',
            'TYPE_E.xlsx',
            'TYPE_F.xlsx',
            'TYPE_G.xlsx',
        )
        MAP_DATA = {
            member: file_name for member, file_name in zip(Data, FILE_NAMES_DATA)
        }
        return MAP_DATA[cls]


Has few .xlsx files storing the data I refer to.
Added them up to Enum Class. Within the source code file I need to obtain some properties of the members of this Enum class, like file name here.
Would the below code serve the problem well or there is an avenue to rework it in accordance with the best practices? Thank you!

from enum import Enum
class Data(Enum):
    TYPE_A = 1
    TYPE_B = 2
    TYPE_C = 3
    TYPE_D = 4
    TYPE_E = 5
    TYPE_F = 6
    TYPE_G = 7

    @property
    def file_name(cls):
        FILE_NAMES_DATA = (
            'TYPE_A.xlsx',
            'TYPE_B.xlsx',
            'TYPE_C.xlsx',
            'TYPE_D.xlsx',
            'TYPE_E.xlsx',
            'TYPE_F.xlsx',
            'TYPE_G.xlsx',
        )
        MAP_DATA = {
            member: file_name for member, file_name in zip(Data, FILE_NAMES_DATA)
        }
        return MAP_DATA[cls]


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

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

发布评论

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

评论(2

假装爱人 2025-02-15 13:11:16

1 - 7毫无意义吗?文件名与成员名称相同吗? ('type_a''type_b'等)在这种情况下,您可以做:

class Data(Enum):
    #
    def _generate_next_value_(name, *args):
        return name
    #
    TYPE_A = auto()
    TYPE_B = auto()
    TYPE_C = auto()
    TYPE_D = auto()
    TYPE_E = auto()
    TYPE_F = auto()
    TYPE_G = auto()
    #
    @property
    def file_name(self):
        return self.value + '.xlsx'

Are the values 1-7 meaningless? Are the file names the same as the member names? ('TYPE_A', 'TYPE_B', etc.) In that case you can do:

class Data(Enum):
    #
    def _generate_next_value_(name, *args):
        return name
    #
    TYPE_A = auto()
    TYPE_B = auto()
    TYPE_C = auto()
    TYPE_D = auto()
    TYPE_E = auto()
    TYPE_F = auto()
    TYPE_G = auto()
    #
    @property
    def file_name(self):
        return self.value + '.xlsx'
新雨望断虹 2025-02-15 13:11:16

您可以通过定义枚举@propertyfile_names_data中手动创建文件名。

from enum import Enum

class Data(Enum):
    TYPE_A = 1
    TYPE_B = 2
    TYPE_C = 3
    TYPE_D = 4
    TYPE_E = 5
    TYPE_F = 6
    TYPE_G = 7
    @property
    def file_name(self):
        return self.name + '.xlsx'

for x in Data:
    print(x.file_name)    

Instead of creating file names manually in FILE_NAMES_DATA, You can create them dynamically by defining a enum @property.

from enum import Enum

class Data(Enum):
    TYPE_A = 1
    TYPE_B = 2
    TYPE_C = 3
    TYPE_D = 4
    TYPE_E = 5
    TYPE_F = 6
    TYPE_G = 7
    @property
    def file_name(self):
        return self.name + '.xlsx'

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