Skip to main content

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 ._. 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.

In a boolean or equality test you can leave off the ._.

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._ is None
Out[3]: True

In [4]: if_(d).anything._ is None
Out[4]: True

In [5]: if_(d)['a']._ == 123
Out[5]: True

In [6]: if_(d)['c']['anything']._ is None
Out[6]: True

In [7]: if_(d)['l'][1]._ == 2
Out[7]: True

In [8]: if_(d)['l'][5]._ is None
Out[8]: True

In [9]: if_(d)['f'](4) == 44
Out[9]: True

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

if_optional-0.0.3.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

if_optional-0.0.3-py3-none-any.whl (2.8 kB view hashes)

Uploaded Python 3

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