Skip to main content

Fast key search in python

Project description

Dict picker (dictionary picker)

Retrieve data from Python dictionaries and lists.

Installation

With pip:

pip install -U dict-picker

Or with conda:

conda install "dict-picker" -c conda-forge

Usage

pick_by_path

The pick_by_path function is used to extract values from nested dictionaries using a path-like string to identify the desired value. The function takes three arguments: a dictionary object, a string representing the path to the desired value and optionally a delimiter.

The path string consists of a sequence of keys, separated by the delimiter character ('/' by default), where each key represents a level in the nested dictionary. If the key has the value '*', it matches any key on that level, or sets it to check all items in the list.

The function returns the value at the end of the path or a list of values if the path contains a wildcard. If the path is not found, the function returns None.

from dict_picker import pick_by_path

example_dict = {
    "foo": "bar",
    "baz": {
        "qux": "quux",
        "quuux": "corge"
    },
    "fred": {
        "wilma": "betty",
        "barney": "pebbles"
    },
    "arr": [
        { 'id': 123 },
        { 'id': 456 },
        { 'id': 789 },
        { 'name': 'bubbles' },
    ],
}

assert pick_by_path(example_dict, "foo") == "bar"
assert pick_by_path(example_dict, "baz/qux") == "quux"
assert pick_by_path(example_dict, "fred/wilma") == "betty"
assert pick_by_path(example_dict, "*/qux") == "quux"
assert pick_by_path(example_dict, "fred/*") == {"wilma": "betty", "barney": "pebbles"}
assert pick_by_path(example_dict, "*/quux") is None
assert pick_by_path(example_dict, "arr/*/id") == [123, 456, 789]

# slice syntax
assert pick_by_path(example_dict, "arr/0") == {'id': 123}
assert pick_by_path(example_dict, "arr/1/id") == 456
assert pick_by_path(example_dict, "arr/-1") == {'name': 'bubbles'}
assert pick_by_path(example_dict, "arr/1:/id") == [456, 789, None]
assert pick_by_path(example_dict, "arr/::-2/id") == [789, 123]

Parameters:

  • obj -- A dictionary object to search for the desired value.
  • path: str -- A string representing the path to the desired value.
  • delimiter: str -- A string used to separate keys in the path string. Default is "/".

Return value:

  • Returns the value found at the end of the path string, or a list of values if the path contains a wildcard. If the path is not found, None is returned.

pick_by_paths

The pick_by_paths function is similar to pick_by_path, but can extract values from multiple paths at once. It takes a dictionary object and a list of path strings as arguments, and returns a list of values found at the end of each path string or None if a path is not found. This works faster than running pick_by_path in a loop.

from dict_picker import pick_by_path, pick_by_paths

example_dict = {
    "foo": "bar",
    "baz": {
        "qux": "quux",
        "quuux": "corge"
    },
    "fred": {
        "wilma": "betty",
        "barney": "pebbles"
    },
    "arr": [
        { 'id': 123 },
        { 'id': 456 },
        { 'id': 789 },
        { 'name': 'bubbles' },
    ],
}

assert pick_by_paths(example_dict, ["fred/*","*/quux", "arr/*/id",]) == [{"wilma": "betty", "barney": "pebbles"}, None, [123, 456, 789]]

Build from sources

Cargo

curl --proto '=https' --tlsv1.2 -sSf <https://sh.rustup.rs> | sh

For other installation options, see the official website.

Maturin

To bind python and rust, pyo3 is used. The Maturin library is used to make it easy to work with.

python -m pip install maturin

Build

python -m maturin build --release

Wheel will be released under the system and python in which it will be built. Read more about the compilation.

The finished wheel can be found in the target/wheels folder.

Build with docker

docker run --rm -v $(pwd):/io ghcr.io/pyo3/maturin build --release  # or other maturin arguments

On macOS, it is better to use docker instead of podman.

Roadmap

  • Arbitrary levels skip operator **;

  • Search inside an array of dictionaries. For example:

    {
        arr:  [
            { id: 123 },
            { id: 456 },
            { id: 789 },
        ]
    }
    'arr/*/id' -> [123, 456, 789]
    
  • Search by tuple of patterns. Returns the result as a list of found values.

  • add option to access lists via python slice syntax (issue #2):

    {
        arr:  [
            { id: 123 },
            { id: 456 },
            { id: 789 },
        ]
    }
    'arr/1/id' -> 456
    'arr/1' -> { id: 123 }
    'arr/:2/id' -> [123, 456]
    'arr/1:' -> [{ id: 456 },{ id: 789 }]
    'arr/::2/id' -> [123, 789]
    

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

dict_picker-0.1.5.tar.gz (9.4 kB view hashes)

Uploaded Source

Built Distributions

dict_picker-0.1.5-pp39-pypy39_pp73-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-pp39-pypy39_pp73-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-pp39-pypy39_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

dict_picker-0.1.5-pp38-pypy38_pp73-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-pp38-pypy38_pp73-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-pp38-pypy38_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

dict_picker-0.1.5-pp37-pypy37_pp73-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded PyPy musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-pp37-pypy37_pp73-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded PyPy musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-pp37-pypy37_pp73-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded PyPy manylinux: glibc 2.5+ i686

dict_picker-0.1.5-cp311-none-win_amd64.whl (115.1 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

dict_picker-0.1.5-cp311-none-win32.whl (113.8 kB view hashes)

Uploaded CPython 3.11 Windows x86

dict_picker-0.1.5-cp311-cp311-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-cp311-cp311-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-cp311-cp311-manylinux_2_24_s390x.whl (1.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.24+ s390x

dict_picker-0.1.5-cp311-cp311-manylinux_2_24_ppc64le.whl (1.2 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.24+ ppc64le

dict_picker-0.1.5-cp311-cp311-manylinux_2_24_armv7l.whl (1.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.24+ ARMv7l

dict_picker-0.1.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.5+ i686

dict_picker-0.1.5-cp311-cp311-macosx_11_0_arm64.whl (231.6 kB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

dict_picker-0.1.5-cp311-cp311-macosx_10_7_x86_64.whl (237.8 kB view hashes)

Uploaded CPython 3.11 macOS 10.7+ x86-64

dict_picker-0.1.5-cp310-none-win_amd64.whl (115.1 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

dict_picker-0.1.5-cp310-none-win32.whl (113.8 kB view hashes)

Uploaded CPython 3.10 Windows x86

dict_picker-0.1.5-cp310-cp310-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-cp310-cp310-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-cp310-cp310-manylinux_2_24_s390x.whl (1.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.24+ s390x

dict_picker-0.1.5-cp310-cp310-manylinux_2_24_ppc64le.whl (1.2 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.24+ ppc64le

dict_picker-0.1.5-cp310-cp310-manylinux_2_24_armv7l.whl (1.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.24+ ARMv7l

dict_picker-0.1.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.5+ i686

dict_picker-0.1.5-cp310-cp310-macosx_11_0_arm64.whl (231.6 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

dict_picker-0.1.5-cp310-cp310-macosx_10_7_x86_64.whl (237.8 kB view hashes)

Uploaded CPython 3.10 macOS 10.7+ x86-64

dict_picker-0.1.5-cp39-none-win_amd64.whl (115.1 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

dict_picker-0.1.5-cp39-none-win32.whl (113.8 kB view hashes)

Uploaded CPython 3.9 Windows x86

dict_picker-0.1.5-cp39-cp39-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-cp39-cp39-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-cp39-cp39-manylinux_2_24_s390x.whl (1.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.24+ s390x

dict_picker-0.1.5-cp39-cp39-manylinux_2_24_ppc64le.whl (1.2 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.24+ ppc64le

dict_picker-0.1.5-cp39-cp39-manylinux_2_24_armv7l.whl (1.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.24+ ARMv7l

dict_picker-0.1.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.5+ i686

dict_picker-0.1.5-cp38-none-win_amd64.whl (115.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

dict_picker-0.1.5-cp38-none-win32.whl (113.5 kB view hashes)

Uploaded CPython 3.8 Windows x86

dict_picker-0.1.5-cp38-cp38-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-cp38-cp38-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-cp38-cp38-manylinux_2_24_s390x.whl (1.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.24+ s390x

dict_picker-0.1.5-cp38-cp38-manylinux_2_24_ppc64le.whl (1.2 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.24+ ppc64le

dict_picker-0.1.5-cp38-cp38-manylinux_2_24_armv7l.whl (1.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.24+ ARMv7l

dict_picker-0.1.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ i686

dict_picker-0.1.5-cp37-none-win_amd64.whl (115.0 kB view hashes)

Uploaded CPython 3.7 Windows x86-64

dict_picker-0.1.5-cp37-none-win32.whl (113.4 kB view hashes)

Uploaded CPython 3.7 Windows x86

dict_picker-0.1.5-cp37-cp37m-musllinux_1_1_x86_64.whl (1.2 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

dict_picker-0.1.5-cp37-cp37m-musllinux_1_1_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ ARM64

dict_picker-0.1.5-cp37-cp37m-manylinux_2_24_s390x.whl (1.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.24+ s390x

dict_picker-0.1.5-cp37-cp37m-manylinux_2_24_ppc64le.whl (1.2 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.24+ ppc64le

dict_picker-0.1.5-cp37-cp37m-manylinux_2_24_armv7l.whl (1.0 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.24+ ARMv7l

dict_picker-0.1.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

dict_picker-0.1.5-cp37-cp37m-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ ARM64

dict_picker-0.1.5-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.whl (1.1 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.5+ i686

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page