Plucking (deep) keys/paths safely from python collections has never been easier.
Project description
plucky.pluck enables you to safely extract several-levels deep values by using a concise selector comprised of dictionary-like keys and list-like indices. Slices over list items are also supported.
plucky.merge facilitates recursive merging of two data structures, reducing leaf values with the provided binary operator.
Installation
plucky is available as a zero-dependency Python package. Install with:
$ pip install plucky
Usage
from plucky import pluck, merge
pluck(obj, 'selector.*.path.2')
merge({"x": 1, "y": 0}, {"x": 2})
Examples
obj = {
'users': [{
'uid': 1234,
'name': {
'first': 'John',
'last': 'Smith',
}
}, {
'uid': 2345,
'name': {
'last': 'Bono'
}
}]
}
pluck(obj, 'users.1.name')
# -> {'last': 'Bono'}
pluck(obj, 'users.*.name.last')
# -> ['Smith', 'Bono']
pluck(obj, 'users.*.name.first')
# -> ['John']
More Examples! :)
pluck([1,2,3], '2')
# -> 3
pluck([1,2,3], '-1')
# -> 3
pluck([1,2,3], '*')
# -> [1,2,3]
pluck([1,2,3], '-2:')
# -> [2,3]
pluck([1,2,3], '::-1')
# -> [3,2,1]
pluck([1, {'val': 2}, 3], '*.val')
# -> [2]
pluck([1, {'val': [1,2,3]}, 3], '1.val.-1')
# -> 3
merge({"x": 1, "y": 0}, {"x": 2})
# -> {"x": 3, "y": 0}
merge({"a": [1, 2], "b": [1, 2]}, {"a": [3, 4], "b": [3]})
# -> {"a": [4, 6], "b": [1, 2, 3]}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
plucky-0.3.1.tar.gz
(4.2 kB
view hashes)
Built Distribution
Close
Hashes for plucky-0.3.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75ab45336e09e9c854fcba7828eae01b73db639b43d6ef13490ad69b150dc01d |
|
MD5 | c86368d35f36a2e38e27586f3ff81c24 |
|
BLAKE2b-256 | 287f23494578c2b3806d6ff52224d85ed15f92f2878199a65b700df66d6c509c |