Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

optiver

Kinda nice (and not ugly) optional chaining for Python. Access nested attributes without crashing on Nones.

Example

from optiver import opt

user = get_user()  # .profile might be None

theme = ~opt(user).profile.settings.theme  # theme is str | None, won't crash

API

opt(value)  # wrap a value
~wrapped    # unwrap to get result

Chain any attributes. If any value in the chain is None, the rest short-circuits and returns None. Missing attrs still raise AttributeError.

Install

pip install optiver  # TODO: publish to PyPI

Notes

  • ~ is the unwrap operator (Python's __invert__)
  • Crashes on missing attrs (like normal Python)
  • Safely chains through None values
  • No runtime dependencies
  • Unfortunately kinda hard to type-check this but if you really cared about that would you really be using Python. Just add unit tests!

About

Kinda nice optional chaining for Python.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages