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.pluckable will happily wrap dictionary- or list-like objects and allow for chained soft plucking with attribute and item getters (e.g. .attr, ["key"], [idx], [a:b], or a combination: ["key1", "key2"], and [0, 3:7, ::-1])
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, pluckable
pluck(obj, 'selector.*.path.2')
merge({"x": 1, "y": 0}, {"x": 2})
pluckable(obj).users[2:5, 10:15].name["first", "middle"]
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]}
pluckable(obj).users.name.last.value
# -> ['Smith', 'Bono']
pluckable(obj).users[:, ::-1].name.last.value
# -> ['Smith', 'Bono', 'Bono', 'Smith']
pluckable(obj).users[:, ::-1].name.last[0, -1].value
# -> ['Smith', 'Smith']
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
Built Distribution
Hashes for plucky-0.3.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41fd8d9f55371f8c987b0a2b47d33be0c7874f399219cb49ce7a44297475d061 |
|
MD5 | 1315e689994abbecf9def63459678e29 |
|
BLAKE2b-256 | 75c00f1110ab1ded242dd34cf98dcd4dea4e9fa96f42a07b4d5f2f7bd949ef51 |