Utilities for traversing and transforming data structures
Project description
# schleppy
Utilities for traversing and transforming data structures
### reach(source, pattern, [options])
Converts an object key chain string to reference
- `options` - optional settings
- `separator` - string to split chain path on, defaults to '.'
- `default` - value to return if the path or value is not present, default is `None`
- `strict` - if `true`, will throw an error on missing member, default is `False`
A pattern including negative numbers will work like negative indices on an
array.
If chain is `False-y`, the object itself will be returned.
```python
from schleppy import reach
pattern = 'a.b.c'
source_obj = {'a' : {'b' : { 'c' : 1}}}
reach(source_obj, pattern) # returns 1
pattern = 'a.b.-1'
source_obj = {'a' : {'b' : [2,3,6]}}
reach(source_obj, pattern) # returns 6
```
### transform(source, transform, [options])
Transforms an existing object into a new one based on the supplied `obj` and `transform` map. `options` are the same as the `reach` options. The first argument can also be an array of objects. In that case the method will return an array of transformed objects. Note that `options.separator` will be respected for the keys in the transform object as well as values.
```python
source = {
'address': {
'one': '123 main street',
'two': 'PO Box 1234'
},
'title': 'Warehouse',
'state': 'CA'
}
result = transform(source, {
'person.address.lineOne': 'address.one',
'person.address.lineTwo': 'address.two',
'title': 'title',
'person.address.region': 'state'
})
# Results in
# {
# 'person': {
# 'address': {
# 'lineOne': '123 main street',
# 'lineTwo': 'PO Box 1234',
# 'region': 'CA'
# }
# },
# 'title': 'Warehouse'
# }
```
Utilities for traversing and transforming data structures
### reach(source, pattern, [options])
Converts an object key chain string to reference
- `options` - optional settings
- `separator` - string to split chain path on, defaults to '.'
- `default` - value to return if the path or value is not present, default is `None`
- `strict` - if `true`, will throw an error on missing member, default is `False`
A pattern including negative numbers will work like negative indices on an
array.
If chain is `False-y`, the object itself will be returned.
```python
from schleppy import reach
pattern = 'a.b.c'
source_obj = {'a' : {'b' : { 'c' : 1}}}
reach(source_obj, pattern) # returns 1
pattern = 'a.b.-1'
source_obj = {'a' : {'b' : [2,3,6]}}
reach(source_obj, pattern) # returns 6
```
### transform(source, transform, [options])
Transforms an existing object into a new one based on the supplied `obj` and `transform` map. `options` are the same as the `reach` options. The first argument can also be an array of objects. In that case the method will return an array of transformed objects. Note that `options.separator` will be respected for the keys in the transform object as well as values.
```python
source = {
'address': {
'one': '123 main street',
'two': 'PO Box 1234'
},
'title': 'Warehouse',
'state': 'CA'
}
result = transform(source, {
'person.address.lineOne': 'address.one',
'person.address.lineTwo': 'address.two',
'title': 'title',
'person.address.region': 'state'
})
# Results in
# {
# 'person': {
# 'address': {
# 'lineOne': '123 main street',
# 'lineTwo': 'PO Box 1234',
# 'region': 'CA'
# }
# },
# 'title': 'Warehouse'
# }
```
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
schleppy-0.3.tar.gz
(4.0 kB
view hashes)
Built Distribution
schleppy-0.3-py3-none-any.whl
(5.5 kB
view hashes)