在Python中创建像枚举一样的C [英] Creating C like Enum in Python

查看:46
本文介绍了在Python中创建像枚举一样的C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我已经找到了这个问题的答案,因为这是不可能的,但是如果有一个绝妙的把戏……我全神贯注.我正在尝试在python中重现以下C枚举列表:

Maybe I have already found the answer to this question in that it's not possible, but if there's a nifty trick... I'm all ears. I'm trying to reproduce the following C enumeration list in python:

enum Id
{
   NONE = 0,
   HEARTBEAT, //0x1
   FLUID_TRANSFER_REQUEST,
   FLUID_TRANSFER_STATUS_MSG,
   FLUID_TRANSFER_ERROR_MSG,
   FLUID_TRANSFER_RESUME,
   EMERGENCY_STOP_MSG,
   LOG_MSG,
   VERSION_REQUEST,
   VERSION_RESPONSE,
   CHANNEL_INFORMATION_REQUEST,
   CHANNEL_INFORMATION_RESPONSE,
   TEST_REQUEST,
   LED_CONTROL_REQ,
   RESET_REQ,

   // Camera App Messages
   START_SENDING_PICTURES = 0x010000,
   STOP_SENDING_PICTURES,
   START_RECORDING_VIDEO_REQ,
   STOP_RECORDING_VIDEO_REQ,
   TAKE_PICTURE_REQ,
   SET_VOLUME_LIMIT,         
   VIDEO_FRAME_MSG,
   PICTURE_MSG,
   I_FRAME_REQUEST,
   CURRENT_VOLUME,
   START_ANALYZING_IMAGES_REQ,
   STOP_ANALYZING_IMAGES_REQ,
   SET_FILE_PATH,

   //Sensor Calibration
   VOLUME_REQUEST = 0x020000,
   START_CAL,
   CLI_COMMAND_REQUEST,
   CLI_COMMAND_RESPONSE,

   // File Mananger
   NEW_DELIVERY_REQ = 0x30000,
   GET_DELIVERY_FILE_REQ,
   GET_FILE_REQ,

   ACK_NACK,
   RESPONSE,

   LAST_ID
};

但是,我不想为列表指定每个值,因为它经常更改.由于我还在各个部分中都将其设置为新值,因此无法使用自动编号方法(例如VOLUME_REQUEST = 0x020000).任何人都有巧妙的技巧可以在python中重现C风格的枚举,还是我坚持用困难的方式重现它?

However, I don't want to have to specify every value for the list because it's changing often. Since I also have it set to a new value in the various sections, I can't use the AutoNumber methodology (e.g. VOLUME_REQUEST = 0x020000). Anyone got a clever trick to reproduce C style enums in python, or am I stuck with reproducing it the hard way?

推荐答案

有一个新的 aenum库 enum34 的作者撰写好东西(例如基于类的 NamedTuple Constant 类).

There is a new aenum library by the author of enum34 which has a few extra goodies (such as a class-based NamedTuple and a Constant class).

如果您使用的是Python 3,那么很酷的功能之一就是内置的自动编号功能:

One of the cool features available if you are using Python 3 is the built-in auto-numbering:

from aenum import Enum

class Id(Enum, start=0):
    #
    _auto_on_                   # needed since aenum 3.0
    #
    NONE  # 0x0
    HEARTBEAT  # 0x1
    FLUID_TRANSFER_REQUEST
    FLUID_TRANSFER_STATUS_MSG
    FLUID_TRANSFER_ERROR_MSG
    # ...
    # Camera App Messages
    START_SENDING_PICTURES = 0x010000
    STOP_SENDING_PICTURES
    START_RECORDING_VIDEO_REQ
    STOP_RECORDING_VIDEO_REQ
    # ...
    # Sensor Calibration
    VOLUME_REQUEST = 0x020000
    START_CAL
    CLI_COMMAND_REQUEST
    CLI_COMMAND_RESPONSE
    #
    # File Mananger
    NEW_DELIVERY_REQ = 0x30000
    GET_DELIVERY_FILE_REQ
    GET_FILE_REQ
    #
    ACK_NACK
    RESPONSE
    #
    LAST_ID

并在使用中:

print(repr(Id.HEARTBEAT))
# <Id.HEARTBEAT: 1>

print(repr(Id.STOP_SENDING_PICTURES))
# <Id.STOP_SENDING_PICTURES: 65537>

print(repr(Id.VOLUME_REQUEST))
# <Id.VOLUME_REQUEST: 131072>

这篇关于在Python中创建像枚举一样的C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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