Skip to content

Package provided none-aware wrapper for none-safety object manipulation

License

Notifications You must be signed in to change notification settings

dkotlyar/python-none-aware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python none-aware

Install

pip install none-aware

Usage

from none_aware import Maybe
obj = dict(foo='bar', baz=dict(foo='foo', bar='bar'))

maybe_obj = Maybe(obj)

assert maybe_obj['foo']() == 'bar'
assert maybe_obj['bar']() is None
assert maybe_obj['baz']['foo']() == 'foo'
assert maybe_obj['bar']['foo']() is None
assert maybe_obj['foo']['bar']() is None
assert maybe_obj.foo() == 'bar'
assert maybe_obj.bar() is None
assert maybe_obj.baz.foo() == 'foo'
assert maybe_obj.bar.foo() is None
assert maybe_obj.foo.bar() is None
assert maybe_obj.foo.upper() == 'BAR'
assert maybe_obj.foo.upper.lower() == 'bar'
assert maybe_obj.bar.foo.other.upper.lower() is None
assert maybe_obj.bar.foo.other.upper.lower.else_('Nothing') == 'Nothing'

maybe_strict = Maybe(obj, strict=True)
assert maybe_strict['foo'].else_('Other') == 'bar'
assert maybe_strict.foo.else_('Other') == 'Other'

maybe_none = Maybe(None)
print(maybe_none.foo.bar)  # None
print(maybe_none.is_none())  # True
print(maybe_none.bar.baz.is_none())  # True
print(maybe_obj.foo.is_none())  # False
print(maybe_obj.foo.bar.is_none())  # True

About

Package provided none-aware wrapper for none-safety object manipulation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages