Working with Enums

31 views
Skip to first unread message

Thomas Bowley

unread,
Feb 12, 2016, 2:16:42 AM2/12/16
to Construct
Hi guys

Please excuse me if this sounds stupid, I've only just taken up Python and came across Construct.
It already seems perfect for my task of defining command/response frames in a serial protocol I'm working with.

My problem is that I don't know how to use Enums in the way that I am used to in other languages eg. C#, Java or C for that matter.
It might be explained in the documentation - if so, I'm just to thick to understand it. :)

Having the different possible commands defined like this:

FrameCmd = Enum(Byte('Command'),
                   
ReadAbsoluteData=0x00,
                   
WriteAbsoluteData=0x01,
                   
ReadDataBlock=0x04,
                   
WriteDataBlock=0x05,
                   
ReadNewTelegram=0x07,
                   
MakeNewDataBlock=0x08,
                   
DeleteDataBlock=0x09,
               
)

I would expect to be able to
use it as such:

TransmitCommand(FrameCmd.ReadDataBlock)

or in a test like such:
if FrameCmd.ReadDataBlock == .... :


Does anyone know how to use Enums in Construct like so?

Benjamin

unread,
Feb 12, 2016, 2:44:14 AM2/12/16
to construct3
Construct isn't really designed for this particular usage, so while it's possible, it's clunky:
TransmitCommand(FrameCmd.build("ReadDataBlock"))

For your usage here, you probably just want to make FrameCmd a simple class:

class FrameCmd:
    ReadAbsoluteData=0x00
    WriteAbsoluteData=0x01
 
   ReadDataBlock=0x04
    WriteDataBlock=0x05
    ReadNewTelegram=0x07
    MakeNewDataBlock=0x08
    DeleteDataBlock=0x09


--
You received this message because you are subscribed to the Google Groups "Construct" group.
To unsubscribe from this group and stop receiving emails from it, send an email to construct3+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arek Bulski

unread,
Sep 26, 2016, 10:31:57 AM9/26/16
to Construct
Reply all
Reply to author
Forward
0 new messages