python手写一个Enum

因为目标机器没有enum模块,无法import Enum。但是代码里面用到了开发机器上的Enum。

没办法只能手写一个代替一下。

# It mainly implements three enum features
# 1. Return a enumhand class, has the key/enum class data: Task.DIEID
# 2. The enumhand class instance has enum class data according key: Task('DIEID')
# 3. enum class has class data name and value: Task.DIEID.name/Task.DIEID.value
# 4. And the enum class obejct should point to the same one with the same key in 1/2

def __new__(cls, name):
    if name in cls.meta.keys():
        return cls.meta[name]

def enum(name, **kwargs):
    data = {}
    meta = {}
    for kwg in kwargs:
        item = {}
        item['name']  = kwg
        item['value'] = kwargs[kwg]
        enum = type('enum', (), item)
        meta[kwg] = enum
    data['__new__'] = __new__
    data.update(meta)
    data['meta'] = meta.copy()
    return type('enumhand', (), data)


task = {
    'DIEID'      : 'DIEID',
    'BTIME'      : 'BUILDTIME',
    'FP'         : 'FP',
    'TRF'        : 'TRF',
    'PRBS'       : 'PRBS',
    'PRBS_LINE'  : 'PRBS_LINE',
    'L1'         : 'L1'
}
Task = enum('Task', **task)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值