-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
413 additions
and
474 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,5 @@ | ||
include LICENSE | ||
include transmogrifydict.png | ||
include README.md | ||
include requirements.txt | ||
global-exclude *.pyc |
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 |
---|---|---|
@@ -1,16 +1,147 @@ | ||
# transmogrifydict | ||
|
||
The "turn a dict from one API into a dict for another" python module. | ||
The "map a dict from one API into a dict for another" python module. | ||
|
||
![That dict is so cool...](/transmogrifydict.png) | ||
|
||
Py2: [![Python 2 Build Status](https://semaphoreci.com/api/v1/emergence/transmogrifydict-py2/branches/master/shields_badge.svg)](https://semaphoreci.com/emergence/transmogrifydict-py2) | ||
Py3: [![Python 3 Build Status](https://semaphoreci.com/api/v1/emergence/transmogrifydict-py3/branches/master/shields_badge.svg)](https://semaphoreci.com/emergence/transmogrifydict-py3) | ||
|
||
Py2: [![Python 2 Coverage](https://docs.emergence.com/transmogrifydict/py2-coverage/coverage.svg)](https://docs.emergence.com/transmogrifydict/py2-coverage/) | ||
Py3: [![Python 3 Coverage](https://docs.emergence.com/transmogrifydict/py3-coverage/coverage.svg)](https://docs.emergence.com/transmogrifydict/py3-coverage/) | ||
| Python | Branch | Build Status | Coverage Status | | ||
| ------ | ------ | ------------ | --------------- | | ||
| 2.7 | master | [![Python 2 Build Status](https://semaphoreci.com/api/v1/emergence/transmogrifydict-py2/branches/master/shields_badge.svg)](https://semaphoreci.com/emergence/transmogrifydict-py2/branches/master) | [![Python 2 Coverage](https://docs.emergence.com/transmogrifydict/htmlcov_py2_master/coverage.svg)](https://docs.emergence.com/transmogrifydict/htmlcov_py2_master/) | | ||
| 2.7 | develop | [![Python 2 Build Status](https://semaphoreci.com/api/v1/emergence/transmogrifydict-py2/branches/develop/shields_badge.svg)](https://semaphoreci.com/emergence/transmogrifydict-py2/branches/develop) | [![Python 2 Coverage](https://docs.emergence.com/transmogrifydict/htmlcov_py2_develop/coverage.svg)](https://docs.emergence.com/transmogrifydict/htmlcov_py2_develop/) | | ||
| 3.5 | master | [![Python 3 Build Status](https://semaphoreci.com/api/v1/emergence/transmogrifydict-py3/branches/master/shields_badge.svg)](https://semaphoreci.com/emergence/transmogrifydict-py3/branches/master) | [![Python 3 Coverage](https://docs.emergence.com/transmogrifydict/htmlcov_py3_master/coverage.svg)](https://docs.emergence.com/transmogrifydict/htmlcov_py3_master/) | | ||
| 3.5 | develop | [![Python 3 Build Status](https://semaphoreci.com/api/v1/emergence/transmogrifydict-py3/branches/develop/shields_badge.svg)](https://semaphoreci.com/emergence/transmogrifydict-py3/branches/develop) | [![Python 3 Coverage](https://docs.emergence.com/transmogrifydict/htmlcov_py3_develop/coverage.svg)](https://docs.emergence.com/transmogrifydict/htmlcov_py3_develop/) | | ||
|
||
## methods | ||
|
||
* `resolve_path_to_value(source, path)` - fetch a value out of `source` using `path` as the pointer to the desired value. see docstring for path string formats. | ||
* `resolve_mapping_to_dict(mapping, source)` - move values from `source` into a returned dict, using `mapping` for paths and returned keys. see `resolve_path_to_value`'s docstring for path string formats. | ||
* `resolve_mapping_to_dict(mapping, source)` - move values from `source` into a returned dict, using `mapping` for paths and returned keys. | ||
|
||
```python | ||
from transmogrifydict import resolve_mapping_to_dict | ||
|
||
mapping = { | ||
'a': 'd', | ||
'b': 'e', | ||
'c': 'f' | ||
} | ||
|
||
source = { | ||
'd': 1, | ||
'e': 2, | ||
'f': 3 | ||
} | ||
|
||
resolve_mapping_to_dict(mapping, source) | ||
# { | ||
# 'a': 1, | ||
# 'b': 2, | ||
# 'c': 3, | ||
# } | ||
``` | ||
|
||
* `resolve_path_to_value(source, path)` - fetch a value out of `source` using `path` as the pointer to the desired value. see docstring for path string formats. | ||
|
||
```python | ||
from transmogrifydict import resolve_path_to_value | ||
|
||
source = { | ||
'd': 1, | ||
'e': 2, | ||
'f': 3 | ||
} | ||
|
||
found, value = resolve_path_to_value(source, 'e') | ||
|
||
print((found, value)) | ||
# (True, 2) | ||
``` | ||
|
||
## `path` or `mapping` value format | ||
```python | ||
from transmogrifydict import resolve_path_to_value | ||
|
||
source = { | ||
'some-key': { | ||
'another-key': '123' | ||
} | ||
} | ||
|
||
# dot notation can be used to descend into dictionaries. | ||
resolve_path_to_value(source, 'some-key.another-key') | ||
# (True, '123') | ||
|
||
source = { | ||
'some-key': '{"another-key":"123"}' | ||
} | ||
|
||
# dot notation can also be used to descend into json strings that are dictionary like | ||
resolve_path_to_value(source, 'some-key.another-key') | ||
# (True, '123') | ||
|
||
source = { | ||
'some-key': { | ||
'another-key': ['1', '2', '3'] | ||
} | ||
} | ||
|
||
# square brackets can be used to get specific indexes from a list | ||
resolve_path_to_value(source, 'some-key.another-key[1]') | ||
# (True, '2') | ||
|
||
source = { | ||
'some-key': { | ||
'another-key': [ | ||
{ | ||
'filter-key': 'yeah', | ||
'each-key': 'a', | ||
}, | ||
{ | ||
'filter-key': 'yeah', | ||
'each-key': 'b', | ||
}, | ||
{ | ||
'filter-key': 'nah', | ||
'each-key': 'c', | ||
} | ||
] | ||
} | ||
} | ||
|
||
# dot notation can be used after square brackets if the list contains dict-like values | ||
resolve_path_to_value(source, 'some-key.another-key[1].each-key') | ||
# (True, ['b']) | ||
|
||
# square brackets can be used to iterate over arrays to descend into the items | ||
resolve_path_to_value(source, 'some-key.another-key[].each-key') | ||
# (True, ['a', 'b', 'c']) | ||
|
||
# when iterating over a list, a filter can be applied using [key=value] | ||
resolve_path_to_value(source, 'some-key.another-key[filter-key=yeah].each-key') | ||
# (True, ['a', 'b']) | ||
|
||
source = { | ||
'a-key': [ | ||
{ | ||
'b-key': { | ||
'c-key': 1, | ||
'd-key': 2, | ||
} | ||
}, | ||
{ | ||
'b-key': { | ||
'c-key': 1, | ||
'd-key': 3, | ||
} | ||
}, | ||
{ | ||
'b-key': { | ||
'c-key': 0, | ||
'd-key': 4, | ||
} | ||
} | ||
] | ||
} | ||
# tidle notation can be used to filter on sub keys of dict list items. | ||
resolve_path_to_value(source, 'a-key[b-key~c-key=1].b-key.d-key') | ||
# (True, [2, 3, 4]) | ||
# | ||
``` |
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
setup( | ||
name='transmogrifydict', | ||
url='https://github.com/emergence/transmogrifydict', | ||
version='1.1.0', | ||
version='1.1.1', | ||
description='The "turn a dict from one API into a dict for another" python module.', | ||
author='Emergence by Design', | ||
author_email='[email protected]', | ||
|
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,68 @@ | ||
import doctest | ||
|
||
from collections import OrderedDict | ||
|
||
from tests.helpers import BaseUnitTest, KwargsToOutputDynamicTestsMetaClass | ||
import transmogrifydict | ||
from six import with_metaclass | ||
|
||
|
||
def load_tests(loader, tests, ignore): | ||
tests.addTests(doctest.DocTestSuite(transmogrifydict)) | ||
return tests | ||
|
||
|
||
class ResolvePathToValueTestCase(with_metaclass(KwargsToOutputDynamicTestsMetaClass, BaseUnitTest)): | ||
func = transmogrifydict.resolve_path_to_value | ||
tests = OrderedDict(( | ||
( | ||
'simple', | ||
{ | ||
'kwargs': { | ||
'source': { | ||
'one': 1, | ||
'two': 2, | ||
'three': 3 | ||
}, | ||
'path': 'two' | ||
}, | ||
'output': (True, 2) | ||
} | ||
), | ||
( | ||
'with_sub_keys', | ||
{ | ||
'kwargs': { | ||
'source': { | ||
'one': 1, | ||
'two': { | ||
'sleeping': 'bereft of life', | ||
'pining for the fjords': 'has ceased to be', | ||
'stunned': 'an ex-parrot', | ||
}, | ||
'three': 3 | ||
}, | ||
'path': 'two.sleeping' | ||
}, | ||
'output': (True, 'bereft of life') | ||
} | ||
), | ||
( | ||
'with_sub_keys_with_spaces', | ||
{ | ||
'kwargs': { | ||
'source': { | ||
'one': 1, | ||
't w o': { | ||
'sleeping': 'bereft of life', | ||
'pining for the fjords': 'has ceased to be', | ||
'stunned': 'an ex-parrot', | ||
}, | ||
'three': 3 | ||
}, | ||
'path': 't w o.pining for the fjords' | ||
}, | ||
'output': (True, 'has ceased to be') | ||
} | ||
), | ||
)) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.