Skip to main content

Overview

Provides a Maybe class which serves as a pseudo-implementation of null-aware operators in python:

  • can be used to avoid common var if var is not None else handle_none(var) structures
  • Null-aware item access
  • Null-aware attribute access
  • Null-aware chained method calls
  • Can be combined with all arithmetic and bitwise operators

Installation

To install use pip:

$ pip install maybe-else

Or clone the repo:

$ git clone https://github.com/matthewgdv/maybe.git
$ python setup.py install

Usage

The simplest use-case is to wrap the value in the Maybe class and call Maybe.else_() with an alternative value. If the initial value was None, the alternative value will be returned. Otherwise the original value will be returned.

Maybe(None).else_("other")                      # "other"
Maybe("hi").else_("other")                      # "hi"

More complex uses involve chaining item/attribute access and method calls off the initial value. If at any point an IndexError or KeyError (item access), AttributeError (attribute access), or TypeError (method call) is raised, the alternative value will be returned upon calling Maybe.else_(). Other exception classes are not caught by Maybe (intentionally) and will have be to handled normally.

Maybe("hi").monkeyweasel[3].else_("other")      # "other"
Maybe({"one": "1"})["one"].isnumeric().else_("other")   # True

Most operators can be used with Maybe. Actions involving operators will ignore TypeError, but other types of exceptions will have to be handled outside the Maybe construct.

(Maybe(8) / 2).else_("other")                   # 4.0
(Maybe("hi").upper() + "!").else_("other")      # "HI!"
(Maybe(None) // 3).else_("other")               # "other"
(Maybe(11) % 4).else_("other")                  # 3

If None would be returned as a result of operations performed on the Maybe object, then None will be returned from Maybe.else_(), rather than the alternative value. This is because None is a legitimate output value, so long as it was not the original input value.

Maybe({1: "1"}).get(2).else_("other")           # None

The Maybe class will be truthy whenever it would return what it is currently holding on calling Maybe.else_(), and will be falsy when it would return the alternative.

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Report Bugs

Report bugs at https://github.com/matthewgdv/maybe/issues

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your local setup that might be helpful in troubleshooting.
  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with "bug" and "help wanted" is open to whoever wants to implement a fix for it.

Implement Features

Look through the GitHub issues for features. Anything tagged with "enhancement" and "help wanted" is open to whoever wants to implement it.

Write Documentation

The repository could always use more documentation, whether as part of the official docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/matthewgdv/maybe/issues.

If you are proposing a new feature:

  • Explain in detail how it would work.
  • Keep the scope as narrow as possible, to make it easier to implement.
  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Before you submit a pull request, check that it meets these guidelines:

  1. If the pull request adds functionality, it should include tests and the docs should be updated. Write docstrings for any functions that are part of the external API, and add the feature to the README.md.

  2. If the pull request fixes a bug, tests should be added proving that the bug has been fixed. However, no update to the docs is necessary for bugfixes.

  3. The pull request should work for the newest version of Python (currently 3.7). Older versions may incidentally work, but are not officially supported.

  4. Inline type hints should be used, with an emphasis on ensuring that introspection and autocompletion tools such as Jedi are able to understand the code wherever possible.

  5. PEP8 guidelines should be followed where possible, but deviations from it where it makes sense and improves legibility are encouraged. The following PEP8 error codes can be safely ignored: E121, E123, E126, E226, E24, E704, W503

  6. This repository intentionally disallows the PEP8 79-character limit. Therefore, any contributions adhering to this convention will be rejected. As a rule of thumb you should endeavor to stay under 200 characters except where going over preserves alignment, or where the line is mostly non-algorythmic code, such as extremely long strings or function calls.

Release files for maybe-else 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for maybe-else 0.2.1
File Size Uploaded
maybe-else-0.2.1.tar.gz 4.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for maybe-else 0.2.1
File Interpreter ABI Platform
maybe_else-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.3 kB

Release files / maybe-else-0.2.1.tar.gz

Download URL maybe-else-0.2.1.tar.gz
Size 4.8 kB
Tags Source
SHA-256 checksum
How to use checksums
61340e0e6641eb5e14ed9080bce45e42a452e18a6c3f003a32680dfab422113e
BLAKE2b-256 checksum
How to use checksums
b9f3bbe1fc7b1bd11a5a016b123b5159a00eb54785d5368c46c00676c24e25c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

Release files / maybe_else-0.2.1-py3-none-any.whl

Download URL maybe_else-0.2.1-py3-none-any.whl
Size 5.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
aa1434f24b60d64b1476cc56d9bc69cfd2df6045bfac41f4249d8f1cf6a93940
BLAKE2b-256 checksum
How to use checksums
e6904d0f6ef7c843c4a35d622004e0342d1ad7af0b0ea3d5a2496227dbc5f5b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.0.5

2 release files

0.0.4

1 release file

0.0.3

2 release files

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page