From 5871da7f8d1db69c2fca18f21f3e983a526ce274 Mon Sep 17 00:00:00 2001 From: Joel Hillacre Date: Tue, 15 Dec 2015 15:23:22 -0700 Subject: [PATCH 1/5] ignore python virtual environment. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 49066d9..73cd9bd 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .idea dist *.egg-info +venv From 5adf0598579bcbd4666e2365768084e2f6767db5 Mon Sep 17 00:00:00 2001 From: Joel Hillacre Date: Tue, 15 Dec 2015 16:23:04 -0700 Subject: [PATCH 2/5] pep8 --- transmogrifydict.py | 1 + 1 file changed, 1 insertion(+) diff --git a/transmogrifydict.py b/transmogrifydict.py index dd6f954..685b703 100644 --- a/transmogrifydict.py +++ b/transmogrifydict.py @@ -1,5 +1,6 @@ import json + def resolve_path_to_value(source, path): """ fetch a value out of `source` using `path` as the pointer to the desired value. From 67deb3b43bdfdd4b52a950d453146559c9efdc26 Mon Sep 17 00:00:00 2001 From: Joel Hillacre Date: Tue, 15 Dec 2015 16:24:06 -0700 Subject: [PATCH 3/5] indicate that we work with unicode too... --- transmogrifydict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/transmogrifydict.py b/transmogrifydict.py index 685b703..7095fca 100644 --- a/transmogrifydict.py +++ b/transmogrifydict.py @@ -70,7 +70,7 @@ def resolve_path_to_value(source, path): :param source: potentially holds the desired value :type source: dict :param path: points to the desired value - :type path: str + :type path: basestring :returns: a boolean indicating found status, the value that was found :rtype: tuple :raises ValueError: if we don't understand what went inside some square brackets. From 30c84701dcd72b9326b00481b0f282563d505208 Mon Sep 17 00:00:00 2001 From: Joel Hillacre Date: Tue, 15 Dec 2015 16:25:18 -0700 Subject: [PATCH 4/5] make tests work. add promised ability to look for keys with values of ints. --- transmogrifydict.py | 48 ++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/transmogrifydict.py b/transmogrifydict.py index 7095fca..cb6842f 100644 --- a/transmogrifydict.py +++ b/transmogrifydict.py @@ -15,7 +15,10 @@ def resolve_path_to_value(source, path): - find dict in array using sub keys key[Key~SubKey=Value] - example: + if the substring `Value` `isdigit()`, we look for an `int` version. You can wrap `'8'` into `'"8"'` to find the + basestring version. + + examples: >>> source_dict = { ... 'first_key': 'a', ... 'second_key' : [ @@ -26,11 +29,13 @@ def resolve_path_to_value(source, path): ... 'third_key' : [ ... { ... 'b': 1, - ... 'c': 2 + ... 'c': 2, + ... 'h': 'asdf' ... }, ... { ... 'b': 3, - ... 'c': 4 + ... 'c': 4, + ... 'h': 'qw"er' ... } ... ], ... 'fourth_key': [ @@ -57,15 +62,17 @@ def resolve_path_to_value(source, path): ... ] ... } >>> resolve_path_to_value(source_dict, 'first_key') - 'a' + (True, 'a') >>> resolve_path_to_value(source_dict, 'second_key[1]') - True, 'y' + (True, 'y') >>> resolve_path_to_value(source_dict, 'third_key[b=3]') - {'c': 2, 'b': 1} - >>> resolve_path_to_value(source_dict, 'third_key[b=3].c') - 2 - >>> resolve_path_to_value(source_dict, 'fourth_key[d~g=8].d.f') - 7 + (True, {'h': 'qw"er', 'c': 4, 'b': 3}) + >>> resolve_path_to_value(source_dict, 'third_key[h="qw"er"]') + (True, {'h': 'qw"er', 'c': 4, 'b': 3}) + >>> resolve_path_to_value(source_dict, 'third_key[h=asdf].c') + (True, 2) + >>> resolve_path_to_value(source_dict, 'fourth_key[d~g=6].e.f') + (True, 7) :param source: potentially holds the desired value :type source: dict @@ -102,13 +109,22 @@ def resolve_path_to_value(source, path): elif '=' in array_part: # [Key=Value] or [Key~SubKey=Value] find_key, find_value = array_part.split('=') + if find_value.isdigit(): + find_value = int(find_value) + elif find_value.startswith('"') and find_value.endswith('"'): + find_value = find_value[1:-1] for item in hasattr(mapped_value, 'keys') and [mapped_value] or mapped_value: sub_item = item - for sub_key in find_key.split('~'): - sub_item = sub_item[sub_key] - if sub_item == find_value: - mapped_value = item - break + sub_keys = find_key.split('~') + try: + while sub_keys: + sub_item = sub_item[sub_keys.pop(0)] + except KeyError, IndexError: + pass + else: + if sub_item == find_value: + mapped_value = item + break else: # raise KeyError('no item with %r == %r' % (find_key, find_value)) found_value = False @@ -148,7 +164,7 @@ def resolve_mapping_to_dict(mapping, source): ... ] ... } >>> resolve_mapping_to_dict(mapping_dict, source_dict) - {'a': '1', 'b': '2', 'c': '3'} + {'a': '1', 'c': '3', 'b': '2'} :param mapping: values are paths to find the corresponding value in `source`, keys are were to store said values :type mapping: dict From 016042716ffcee9009607cc4a55303945c567d14 Mon Sep 17 00:00:00 2001 From: Joel Hillacre Date: Tue, 15 Dec 2015 16:26:47 -0700 Subject: [PATCH 5/5] bump version number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 1710849..9dad9ac 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name='transmogrifydict', url='https://github.com/emergence/transmogrifydict', - version='0.0.2', + version='1.0.0', description='The "turn a dict from one API into a dict for another" python module.', author='Emergence by Design', author_email='support@emergence.com',