Package: maybe-else
OverviewProvides aMaybeclass which serves as a pseudo-implementation of null-aware operators in python:can be used to avoid commonvar if var is not None else handle_none(var)structuresNull-aware item accessNull-aware attribute accessNull-aware chained method callsCan be combined with all arithmetic and bitwise operatorsInstallationTo install use pip:$ pip install maybe-elseOr clone the repo:$ git clone https://github.com/matthewgdv/maybe.git
$ python setup.py installUsageThe simplest use-case is to wrap the value in theMaybeclass and callMaybe.else_()with an alternative value. If the initial value wasNone,
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 anIndexErrororKeyError(item access),AttributeError(attribute access), orTypeError(method call) is raised, the alternative value will be returned
upon callingMaybe.else_(). Other exception classes are not caught byMaybe(intentionally) and will have be to handled normally.Maybe("hi").monkeyweasel[3].else_("other")      # "other"
Maybe({"one": "1"})["one"].isnumeric().else_("other")   # TrueMost operators can be used withMaybe. Actions involving operators will ignoreTypeError, but other types of exceptions will have to be handled outside theMaybeconstruct.(Maybe(8) / 2).else_("other")                   # 4.0
(Maybe("hi").upper() + "!").else_("other")      # "HI!"
(Maybe(None) // 3).else_("other")               # "other"
(Maybe(11) % 4).else_("other")                  # 3If None would be returned as a result of operations performed on theMaybeobject, then None will be returned fromMaybe.else_(), rather than the alternative value.
This is becauseNoneis a legitimate output value, so long as it was not the original input value.Maybe({1: "1"}).get(2).else_("other")           # NoneTheMaybeclass will be truthy whenever it would return what it is currently holding on callingMaybe.else_(), and will be falsy when it would return the alternative.ContributingContributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.You can contribute in many ways:Report BugsReport bugs athttps://github.com/matthewgdv/maybe/issuesIf 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 BugsLook 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 FeaturesLook through the GitHub issues for features. Anything tagged with "enhancement" and "help wanted" is open to whoever wants to implement it.Write DocumentationThe 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 FeedbackThe best way to send feedback is to file an issue athttps://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: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.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.The pull request should work for the newest version of Python (currently 3.7). Older versions may incidentally work, but are not officially supported.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.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, W503This 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.