Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added BaseDict to keep pointer to the initial input dict. #32
- Loading branch information
1 parent
ac3895a
commit eac655b
Showing
8 changed files
with
146 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from benedict.dicts.base.base_dict import BaseDict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from benedict.utils import type_util | ||
|
||
|
||
class BaseDict(dict): | ||
|
||
_dict = {} | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(BaseDict, self).__init__() | ||
if len(args) == 1 and type_util.is_dict(args[0]): | ||
self._dict = args[0] | ||
else: | ||
self._dict = dict(*args, **kwargs) | ||
|
||
def __bool__(self): | ||
return bool(self._dict) | ||
|
||
def __contains__(self, key): | ||
return key in self._dict | ||
|
||
def __delitem__(self, key): | ||
del self._dict[key] | ||
|
||
def __eq__(self, other): | ||
return self._dict == other | ||
|
||
def __getitem__(self, key): | ||
return self._dict[key] | ||
|
||
def __iter__(self): | ||
return iter(self._dict) | ||
|
||
def __len__(self): | ||
return len(self._dict) | ||
|
||
def __repr__(self): | ||
return repr(self._dict) | ||
|
||
def __setitem__(self, key, value): | ||
self._dict[key] = value | ||
|
||
def __str__(self): | ||
return str(self._dict) | ||
|
||
def clear(self): | ||
self._dict.clear() | ||
|
||
def copy(self): | ||
return self._dict.copy() | ||
|
||
def dict(self): | ||
return self._dict | ||
|
||
def get(self, key, default=None): | ||
return self._dict.get(key, default) | ||
|
||
def items(self): | ||
return self._dict.items() | ||
|
||
def keys(self): | ||
return self._dict.keys() | ||
|
||
def pop(self, key, *args): | ||
return self._dict.pop(key, *args) | ||
|
||
def setdefault(self, key, default=None): | ||
return self._dict.setdefault(key, default) | ||
|
||
def update(self, other): | ||
self._dict.update(other) | ||
|
||
def values(self): | ||
return self._dict.values() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from benedict import benedict | ||
|
||
import unittest | ||
|
||
|
||
class github_issue_0032_test_case(unittest.TestCase): | ||
|
||
""" | ||
https://github.com/fabiocaccamo/python-benedict/issues/32 | ||
To run this specific test: | ||
- For each method comment @unittest.skip decorator | ||
- Run python -m unittest tests.github.test_issue_0032 | ||
""" | ||
|
||
@staticmethod | ||
def load_dict(): | ||
return { | ||
'a': { | ||
'b': { | ||
'c': { | ||
'd': 10, | ||
}, | ||
'x': 100, | ||
} | ||
} | ||
} | ||
|
||
def test_pointers_with_dict(self): | ||
b = benedict(self.load_dict()) | ||
ab = benedict(b['a.b']) | ||
ab['c']['d'] = 20 | ||
ab['x'] = 200 | ||
r = { | ||
'a': { | ||
'b': { | ||
'c': { | ||
'd': 20, | ||
}, | ||
'x': 200, | ||
} | ||
} | ||
} | ||
self.assertEqual(b, r) |