epics-base / p4p

Python bindings for the PVAccess network client and server.
BSD 3-Clause "New" or "Revised" License
27 stars 38 forks source link

Implement todict for Value #16

Closed brunoseivam closed 6 years ago

brunoseivam commented 6 years ago

This might not be the best implementation but it demonstrates what I have in mind for #15

mdavidsaver commented 6 years ago

This certainly does the trick. Rather than PyDict_New() and separate construction though, it would be simpler to do the equivalent of dict([('a','b')]). This actually isn't difficult (although it wasn't initial obvious to me). Just call the type object. So something like:

PyObject *list = ...;
PyObject *dict = PyObject_CallFunction(&PyDict_Type, "O", list);
mdavidsaver commented 6 years ago

This change, along with adding a case in src/p4p/test/test_value.py should do the trick.

mdavidsaver commented 6 years ago

You could add to testSubStruct() after

https://github.com/mdavidsaver/p4p/blob/262f3a4c8ec96fda35f2f09270fcd550f30f3826/src/p4p/test/test_value.py#L134-L140

brunoseivam commented 6 years ago

I agree with your suggestions, it does look cleaner now. Do you think it would be possible to just call OrderedDict's constructor, instead?

mdavidsaver commented 6 years ago

would be possible to just call OrderedDict's constructor, instead?

Sure. Once you get hold of this type it's simple. I haven't found a concise equivalent for from collections import OrderedDict from extension code. fyi, I'd like to do something similar w/ logging.

I'm going to merge now, so this can be a later enhancement.