Last active
February 5, 2017 20:38
-
-
Save AndresMWeber/76cb74a18ed7d4c682d9cafdbe3b86a4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def gen_dict_key_matches(key, dictionary, path=None, full_path=False): | |
if path is None: | |
path = [] | |
for k, v in iteritems(dictionary): | |
path.append(k) | |
if k == key: | |
yield (path, v) if full_path else v | |
elif isinstance(v, dict): | |
for result in gen_dict_key_matches(key, v, path): | |
yield result | |
from tools import gen_dict_key_matches as ge | |
print list(ge('nf', {'nf':{'blah':4}})) # no worky | |
print list(ge('nf', {'nf':5})) # worky |
This file contains hidden or 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
class TestGetKeysContaining(TestBase): | |
def test_simple(self): | |
self.assertEquals(get_keys_containing('test', {'test': 1}), 1) | |
def test_not_existing(self): | |
self.assertEquals(get_keys_containing('mest', {'test': 1}), None) | |
def test_mock_config_find(self): | |
self.checkEqual(list(get_keys_containing('naming_formats', OrderedDict([('overall_config', {'version_padding': 3}), | |
('options', { | |
'discipline': {'animation': 'AN ANI ANIM ANIMN', 'lighting': 'LT LGT LGHT LIGHT', | |
'compositing': 'CM CMP COMP COMPG', 'rigging': 'RG RIG RIGG RIGNG', | |
'modeling': 'MD MOD MODL MODEL', 'matchmove': 'MM MMV MMOV MMOVE'}, | |
'side': ['left', 'right', 'center']}), | |
('naming_formats', { | |
'node': {'default': 'side_location_nameDecoratorVar_childtype_purpose_type', | |
'format_archive': 'side_name_space_purpose_decorator_childtype_type', | |
'format_lee': 'type_childtype_space_purpose_name_side'}, | |
'texturing': {'shader': 'side_name_type'}})]))), | |
['node', 'texturing']) |
ssttuu
commented
Feb 5, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment