I created a nested dictionary based on AttrDict
found there :
Object-like attribute access for nested dictionary
I modified it to contain str
commands in "leaves" that gets executed when the value is requested/written to :
commands = {'root': {'com': {'read': 'READ_CMD', 'write': 'WRITE_CMD'} } }
class AttrTest()
def __init__:
self.__dict__['attr'] = AttrDict(commands)
test = AttrTest()
data = test.attr.root.com.read # data = value read with the command
test.attr.root.com.write = data # data = value written on the com port
While it works beautifully, I'd like to :
- Avoid people getting access to
attr
/root
/com
as these returns a sub-level dictonary - People accessing
attr.root.com
directly (through__getattribute__
/__setattr__
)
Currently, I'm facing the following problems :
- As said, when accessing the 'trunk' of the nested dict, I get a partial dict of the 'leaves'
- When accessing
attr.root.com
it returns{'read': 'READ_CMD', 'write': 'WRITE_CMD'}
- If detecting a
read
I do a forward lookup and return the value, but thenattr.root.com.read
fails
Is it possible to know what is the final level Python will request in the "path" ?
- To block access to
attr
/root
- To read/write the value accessing
attr.root.com
directly (using forward lookup) - To return the needed partial dict only if
attr.root.com.read
orattr.root.com.write
are requested
Currently I've found nothing that allows me to control how deep the lookup is expected to go.
Thanks for your consideration.