Skip to content

Latest commit

 

History

History
70 lines (44 loc) · 2.09 KB

README.rst

File metadata and controls

70 lines (44 loc) · 2.09 KB

sgol-python

image

image

Master Documentation Status

Description

This repo contains a nullsafe-like function for Python that can be used to extract data from dicts, lists, etc.

Installation

Install via PyPI with:

pip install pyextract

Or install directly from GitHub with:

pip install git+git://github.com/spatialcurrent/pyextract.git@master

Usage

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

Contributing

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.

License

This work is distributed under the MIT License. See LICENSE file.