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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file if_optional-0.0.3.tar.gz.

File metadata

  • Download URL: if_optional-0.0.3.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.17

File hashes

Hashes for if_optional-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f461d62d9f3c81f229e419ba3e05a05fad99d774d1926cdaf533523e42663032
MD5 78c05f50f788cdc4d83df6ca3f89ec79
BLAKE2b-256 469c5b36ef4ef4355eb481c842d0b0335cf4bdf3a27293c633309ae57c97844d

See more details on using hashes here.

File details

Details for the file if_optional-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: if_optional-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 2.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.17

File hashes

Hashes for if_optional-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 73cea1f601fcbecdb7263b96fc5041236282273175833963ca55b538bb63337d
MD5 9d18fb0bca661bde3b93d5e95704c824
BLAKE2b-256 9df464efcb9dfb9f0e35b54fe2bbe52cc2a8f5ab8cf9e11eba457f72bd1f9673

See more details on using hashes here.

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