]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commit
Changed DictModel to dict with attribute access
authorYves-Gwenael Bourhis <yves-gwenael.bourhis@cloudwatt.com>
Thu, 2 Jan 2014 14:19:17 +0000 (15:19 +0100)
committerYves-Gwenael Bourhis <yves-gwenael.bourhis@cloudwatt.com>
Wed, 4 Jun 2014 09:44:34 +0000 (11:44 +0200)
commit89f87c61e7e5c4fa69379e4039ad4b8221142a7c
tree30a0303144a1493bf6c332305e6957622c11cd67
parent53b701a3f91530c9462a9cb0690aaf68efd45f6d
Changed DictModel to dict with attribute access

DictModel is a dict where keys are accessible via attribute access.
The old version was an object with attributes created from dict keys and many
attributes where accessible only via getattr because they did not have a valid
python attribute naming (i.e.: 'provider:network_type' is not a valid
python variable/attribute name)::

    >>> d.provider:network_type
      File "<stdin>", line 1
        d.provider:network_type
              ^
    SyntaxError: invalid syntax

This time we just subclass dict with attribute access to keys.

So dict keys are accessible via attribute access but remain accessible via key
access::

    >>> d = DictModel({'foo': 'bar', 'provider:network_type': 'something'})
    >>> d.foo == d['foo']
    ... True
    >>> getattr(d, 'provider:network_type') == d.get('provider:network_type')
    ... True
    >>> getattr(d, 'provider:network_type') == d['provider:network_type']
    ... True

One of the big advantages when debugging is that now in pdb, pp(d) (where d is
a DictModel instance) shows a nice dictionary structure, while with the old
version whe had to perform a "dir(d)" and introspect each attribute...

Change-Id: I05fad7cd9763f97f61680d45e1b6592a80049541
Closes-Bug: 1251653
neutron/agent/linux/dhcp.py
neutron/tests/unit/test_dhcp_agent.py