Optional chaining for Python
Project description
if_
Optional chaining for Python
Installation
python setup.py install
Usage
Call if_()
with a Python expression, and chain attribute and item accessors on
the return value, ending with .then
. If the initial expression evaluates to
None
, so will the whole chain; if not, each accessor will run normally but if
it fails the chain will return None
.
Examples
In [1]: from if_ import if_
In [2]: d = {
...: 'a': 123,
...: 'c': None,
...: 'd': {'this': 456},
...: 'l': [1, 2, 3],
...: 'f': lambda x: x * 11,
...: }
In [3]: if_(None).anything.then is None
Out[3]: True
In [4]: if_(d).anything.then is None
Out[4]: True
In [5]: if_(d)['a'].then == 123
Out[5]: True
In [6]: if_(d)['c']['anything'].then is None
Out[6]: True
In [7]: if_(d)['l'][1].then == 2
Out[7]: True
In [8]: if_(d)['l'][5].then is None
Out[8]: True
In [9]: if_(d)['f'](4).then == 44
Out[9]: True
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
if_optional-0.0.1.tar.gz
(2.4 kB
view hashes)
Built Distribution
Close
Hashes for if_optional-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f03ccf7570d6ff3b69c7c636630a1212f7237bcf0e9e2fde52a542b6f76566d2 |
|
MD5 | 81d8bf74fae6781db4a51b5a82178f00 |
|
BLAKE2b-256 | 1038dec4fdfa9ef1f02cfb476fb1884b745e6b8b0ec1caa7b7320fb8c34239b0 |