Option

Note

Usage on each methods are valid on the premise that the following classes is defined in advance.

# coding: utf-8

from owlmixin import OwlMixin, TOption
from owlmixin.owlcollections import TDict, TList
from owlmixin.owlenum import OwlEnum, OwlObjectEnum


class Animal(OwlObjectEnum):  # pragma: no cover
    DOG = ("dog", {"cry": "bow-wow"})
    CAT = ("cat", {"cry": "mewing"})

    def cry(self):
        return self.object["cry"]


class Color(OwlEnum):  # pragma: no cover
    RED = "red"
    GREEN = "green"
    BLUE = "blue"


class Food(OwlMixin):  # pragma: no cover
    name: str
    names_by_lang: TOption[TDict[str]]


class Human(OwlMixin):  # pragma: no cover
    id: int
    name: str
    favorites: TList[Food]


class Machine(OwlMixin):  # pragma: no cover
    id: int
    name: str


class Japanese(OwlMixin):  # pragma: no cover
    name: str
    language: str = "japanese"

TOption

class owlmixin.owloption.TOption(value)[source]
get() T[source]

Usage:

>>> TOption(3).get()
3
>>> TOption(0).get()
0
>>> TOption(None).get()
get_or(default: T) T[source]

Usage:

>>> TOption(3).get_or(999)
3
>>> TOption(0).get_or(999)
0
>>> TOption(None).get_or(999)
999
is_none() bool[source]

Usage:

>>> TOption(3).is_none()
False
>>> TOption(0).is_none()
False
>>> TOption(None).is_none()
True
any() bool[source]

Usage:

>>> TOption(3).any()
True
>>> TOption(0).any()
True
>>> TOption(None).any()
False
map(func: Callable[[T], U]) TOption[U][source]

Usage:

>>> TOption(3).map(lambda x: x+1).get()
4
>>> TOption(None).map(lambda x: x+1).get_or(999)
999
flat_map(func: Callable[[T], TOption[U]]) TOption[U][source]

Usage:

>>> TOption(3).flat_map(lambda x: TOption(str(x+1))).get()
'4'
>>> TOption(3).flat_map(lambda x: TOption(None)).get_or("none")
'none'
>>> TOption(None).flat_map(lambda x: TOption(str(x+1))).get_or("none")
'none'