kwcoco.util.dict_proxy2 module¶
- class kwcoco.util.dict_proxy2.DictInterface[source]¶
Bases:
object
- An inherited class must specify the
getitem
,setitem
, and keys
methods.
A class is dictionary like if it has:
__iter__
,__len__
,__contains__
,__getitem__
,items
,keys
,values
,get
,and if it should be writable it should have:
__delitem__
,__setitem__
,update
,And perhaps:
copy
,__iter__
,__len__
,__contains__
,__getitem__
,items
,keys
,values
,get
,and if it should be writable it should have:
__delitem__
,__setitem__
,update
,And perhaps:
copy
,Example
from scriptconfig.dict_like import DictLike class DuckDict(DictLike):
- def __init__(self, _data=None):
- if _data is None:
_data = {}
self._data = _data
- def getitem(self, key):
return self._data[key]
- def keys(self):
return self._data.keys()
self = DuckDict({1: 2, 3: 4}) print(f’self._data={self._data}’) cast = dict(self) print(f’cast={cast}’) print(f’self={self}’)
- An inherited class must specify the
- class kwcoco.util.dict_proxy2.DictProxy2[source]¶
Bases:
DictInterface
Allows an object to proxy the behavior of a _proxy dict attribute
- class kwcoco.util.dict_proxy2._AliasMetaclass(name, bases, namespace, *args, **kwargs)[source]¶
Bases:
type
Populates the __alias_to_aliases__ field at class definition time to reduce the overhead of instance creation.
- class kwcoco.util.dict_proxy2.AliasedDictProxy[source]¶
Bases:
DictProxy2
Can have a class attribute called ``__alias_to_primary__ `` which is a Dict[str, str] mapping alias-keys to primary-keys.
Need to handle cases:
- image dictionary contains no primary / aliased keys
primary keys used
- image dictionary only has aliased keys
aliased keys are updated
- image dictionary only has primary keys
primary keys are updated
- image dictionary only both primary and aliased keys
both keys are updated
Example
>>> from kwcoco.util.dict_proxy2 import * # NOQA >>> class MyAliasedObject(AliasedDictProxy): >>> __alias_to_primary__ = { >>> 'foo_alias1': 'foo_primary', >>> 'foo_alias2': 'foo_primary', >>> 'bar_alias1': 'bar_primary', >>> } >>> def __init__(self, obj): >>> self._proxy = obj >>> def __repr__(self): >>> return repr(self._proxy) >>> def __str__(self): >>> return str(self._proxy) >>> # Test starting from empty >>> obj = MyAliasedObject({}) >>> obj['regular_key'] = 'val0' >>> assert 'foo_primary' not in obj >>> assert 'foo_alias1' not in obj >>> assert 'foo_alias2' not in obj >>> obj['foo_primary'] = 'val1' >>> assert 'foo_primary' in obj >>> assert 'foo_alias1' in obj >>> assert 'foo_alias2' in obj >>> obj['foo_alias1'] = 'val2' >>> obj['foo_alias2'] = 'val3' >>> obj['bar_alias1'] = 'val4' >>> obj['bar_primary'] = 'val5' >>> assert obj._proxy == { >>> 'regular_key': 'val0', >>> 'foo_primary': 'val3', >>> 'bar_primary': 'val5'} >>> # Test starting with primary keys >>> obj = MyAliasedObject({ >>> 'foo_primary': 123, >>> 'bar_primary': 123, >>> }) >>> assert 'foo_alias1' in obj >>> assert 'bar_alias1' in obj >>> obj['bar_alias1'] = 456 >>> obj['foo_primary'] = 789 >>> assert obj._proxy == { >>> 'foo_primary': 789, >>> 'bar_primary': 456} >>> # Test that if aliases keys are existant we dont add primary keys >>> obj = MyAliasedObject({ >>> 'foo_alias1': 123, >>> }) >>> assert 'foo_alias1' in obj >>> assert 'foo_primary' in obj >>> obj['foo_alias1'] = 456 >>> obj['foo_primary'] = 789 >>> assert obj._proxy == { >>> 'foo_alias1': 789, >>> } >>> # Test that if primary and aliases keys exist, we update both >>> obj = MyAliasedObject({ >>> 'foo_primary': 3, >>> 'foo_alias2': 5, >>> }) >>> # We do not attempt to detect conflicts >>> assert obj['foo_primary'] == 3 >>> assert obj['foo_alias1'] == 3 >>> assert obj['foo_alias2'] == 5 >>> obj['foo_alias1'] = 23 >>> assert obj['foo_primary'] == 23 >>> assert obj['foo_alias1'] == 23 >>> assert obj['foo_alias2'] == 23 >>> obj['foo_primary'] = -12 >>> assert obj['foo_primary'] == -12 >>> assert obj['foo_alias1'] == -12 >>> assert obj['foo_alias2'] == -12 >>> assert obj._proxy == { >>> 'foo_primary': -12, >>> 'foo_alias2': -12}