Python question: "observer pattern" actions for dictionary?

Hi all,

I’m learning to write classes in python and am trying to understand better default actions:

In particular, I’m working on a class for set of instruments. It turns out that this class of instruments has a help message that documents the full command interface, and the structure of this help is the same for all instruments from that manufacturer (“Windfreak Tech”, cool USB microwave generators and up/down converters).

My idea is that rather than hardcoding the parameters of the class, I can parse them from the help message into a dictionary. So my class has basically just a dictionary with entries like:

wft.value["Frequency (MHz)"]

I would like be able to type something like this:

wft.value["Frequency (MHz)"] = 1000.3

and then have this automatically run the write() code to set this value (and query it back maybe).

As I’m new to using observer properties in python, I was wondering: what is the best way to do this? I would somehow need to pass both the key and the value to my setter function?

Thanks in advance!
Gary

In case you’re interested, you can see the code here:

Instead of storing this information in a barebones dictionary, you can implement your custom container that executes the necessary code when modified. An observer pattern is more complex.

Here’s how it could work.

class VerboseDict(dict):
    def __setitem__(self, key, value):
        print(f"Setting {key} to {value}")
        # Call the dictionary method using the super built-in
        super().__setitem__(key, value)

    def __getitem__(self, key):
        print(f"You have requested {key}, here you go.")
        return super().__getitem__(key)


data = VerboseDict()
data["Frequency"] = 100
print(data['Frequency'])

prints

Setting Frequency to 100
You have requested Frequency, here you go.
100

yeah, that’s a good solution!

(as I was writing my post I was already thinking something like that might be best :slight_smile:…sometimes just formulating the question you come up with the answer…)

Do I understand correctly that:

class VerboseDict(dict):

is the way to specify that Verbose dict is a child (subclass? not sure I know the right words…) of dict, and inherits all of it’s methods / data?

and then super() is the way you access the data / methods of the parent class?

(and I guess I cannot write super[key] = value, but instead need to use the “internal” methods __settitem__?)

Thanks from a python-object-oriented-programming-noob…

Indeed. VerboseDict is a child of dict.

super() is a weird object, so I’m not extremely certain why you can’t do super()[key] = value, but it seemed to not work. :man_shrugging:

1 Like

May be I am late to the party, but I would use Traitlets for implementing data structures with observer
https://traitlets.readthedocs.io/en/stable/api.html#callbacks-when-trait-attributes-change

1 Like

Thanks Slava! I keep running into the word “traitlets” and I had no idea what they were :slight_smile: They sound awesome, I’ll have to do some reading.

Cheers,
Gary