This repo contains a nullsafe-like function for Python that can be used to extract data from dicts, lists, etc.
Install via PyPI with:
pip install pyextract
Or install directly from GitHub with:
pip install git+git://github.com/spatialcurrent/pyextract.git@master
The position args are keyChain, node, and fallback. If the keyChain is invalid, then the fallback value is returned. The fallback is only returned if (1) the location described by the keyChan is invalid or (2) the value located is None. If the value located is a blank string, zero, false, etc., then it will return. If handling for blank strings, then use the 3rd pattern below to fallback even if the value found is a blank string.
from pyextract.extract import extract
x = { "a": {"b": "c"} }
y = extract(["a", "b"], x, None)
# y == c
x = { "a": {"b": None} }
y = extract(["a", "b"], x, None)
# y == None
x = { "a": {"b": None} }
y = extract(["a", "b"], x, "") or "c"
# y == c
Spatial Current, Inc. is currently accepting pull requests for this repository. We'd love to have your contributions! Please see Contributing.rst for how to get started.
This work is distributed under the MIT License. See LICENSE file.