Ivan, Thanks for this explanation! The pointer to the difference between class-level variable and instance-level variables was the missing link. The magic behind this two variables with identical names but different content wasn't obvious.
Setting the instance-level variable options did the job. Best regards, Helge ------------------------------------------------------------------------ *From:* Ivan Wick [mailto:ivanwick+sourceforge....@gmail.com] *Sent:* Monday, November 1, 2021, 8:42 PM *To:* Helge Kruse *Cc:* sigrok-devel@lists.sourceforge.net *Subject:* [sigrok-devel] Accessing protocol decoder options
The "options" in the Decoder class is a class-level variable, shared by all instances of the Decoder class (like static in C++ or Java). It is meant to be an abstract description of the options, it does not store the concrete values of the options for any one instance. When you instantiate a Decoder object, it must take the tuple of class-level options (really, a description of options) and create an instance-level dictionary for the concrete options of that instance. In Python, you must use "self" to refer to the instance. So you would need code like: class Decoder: # class options description options = ({'id': 'baudrate', 'desc':'Baud rate', 'default':115200},) def __init__(self): self.options = {} # new instance options for opt_spec in Decoder.options: opt_id = opt_spec['id'] opt_default = opt_spec['default'] self.options[opt_id] = opt_default def start(self): print('baudrate: %d' % self.options['baudrate']) if __name__ == '__main__': d = Decoder() # expect baudrate to be default 115200 unless changed # d.options['baudrate'] = 9600 d.start() For reference, this happens in function srd_inst_option_set of instance.c around line 142 https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blob;f=instance.c;hb=02aa01ad5f05f2730309200abda0ac75d3721e1d#l133 <https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blob;f=instance.c;hb=02aa01ad5f05f2730309200abda0ac75d3721e1d#l133> On Mon, Nov 1, 2021 at 9:47 AM Helge Kruse <helge.kr...@gmx.net <mailto:helge.kr...@gmx.net>> wrote: Hello, I am not an experienced Python programmer. So I’d like to ask some detail to the syntax I found in sigrok protocol decoders. I wonder about the syntax or probably syntactic sugar in the libsigrokdecode library. The sigrok protocol decoder API (https://www.sigrok.org/wiki/Protocol_decoder_API <https://www.sigrok.org/wiki/Protocol_decoder_API>) includes the attribute “options” as a tuple consisting of dictionaries. The decoder code itself reads the current options value with a “string index” like in the UART decoder: self.bw <http://self.bw> = (self.options['data_bits'] + 7) // 8 I want to run my decoder in a plain Python environment to make some tests. Instead of deriving from “srd.Decoder” I can write the decoder class like this: class Decoder: options = ( {'id': 'baudrate', 'desc': 'Baud rate', 'default': 115200}, ) def __init__(self): pass def start(self): if options['baudrate'] == 9600: pass # test the class if __name__ == "__main__": d = Decoder() d.start() If I instantiate the class I get an exception for the first line in the start method: “tuple indices must be integer or slices, not str”. What is the magic behind the libsigrok implementation, that allows to use a string to access a specific options value? Is there some Python magic that helps to make the start method running in a plain Python environment? Best regards, Helge _______________________________________________ sigrok-devel mailing list sigrok-devel@lists.sourceforge.net <mailto:sigrok-devel@lists.sourceforge.net> https://lists.sourceforge.net/lists/listinfo/sigrok-devel <https://lists.sourceforge.net/lists/listinfo/sigrok-devel>
_______________________________________________ sigrok-devel mailing list sigrok-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sigrok-devel