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.2.tar.gz
(2.7 kB
view hashes)
Built Distribution
Close
Hashes for if_optional-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e7111a0afda2160e0d49c4f7182472624fd06fc984aa519bc33b4fdcd261a74 |
|
MD5 | d38d2ad9623c41072b1a432e72c3595f |
|
BLAKE2b-256 | 056a45f9411a68f607f4b9874a11df136affb343b565b9eac4899f118fab5f25 |