How to use the result.result.Ok function in result

To help you get started, we’ve selected a few result examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dbrgn / result / result / typetests.py View on Github external
from .result import Result, Ok, Err


res1 = Ok('hello')  # type: Result[str, int]
if isinstance(res1, Ok):
    ok = res1  # type: Ok[str]
    okValue = res1.ok()  # type: str
    mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5)  # type: float
else:
    err = res1  # type: Err[int]
    errValue = err.err()  # type: int
    mapped_to_list = res1.map_err(lambda e: [e]).err()  # type: Optional[List[int]]

# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
github dbrgn / result / result / typetests.py View on Github external
from .result import Result, Ok, Err


res1 = Ok('hello')  # type: Result[str, int]
if isinstance(res1, Ok):
    ok = res1  # type: Ok[str]
    okValue = res1.ok()  # type: str
    mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5)  # type: float
else:
    err = res1  # type: Err[int]
    errValue = err.err()  # type: int
    mapped_to_list = res1.map_err(lambda e: [e]).err()  # type: Optional[List[int]]

# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
github dbrgn / result / result / typetests.py View on Github external
from typing import List, Optional

from .result import Result, Ok, Err


res1 = Ok('hello')  # type: Result[str, int]
if isinstance(res1, Ok):
    ok = res1  # type: Ok[str]
    okValue = res1.ok()  # type: str
    mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5)  # type: float
else:
    err = res1  # type: Err[int]
    errValue = err.err()  # type: int
    mapped_to_list = res1.map_err(lambda e: [e]).err()  # type: Optional[List[int]]

# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
github dbrgn / result / result / typetests.py View on Github external
from typing import List, Optional

from .result import Result, Ok, Err


res1 = Ok('hello')  # type: Result[str, int]
if isinstance(res1, Ok):
    ok = res1  # type: Ok[str]
    okValue = res1.ok()  # type: str
    mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5)  # type: float
else:
    err = res1  # type: Err[int]
    errValue = err.err()  # type: int
    mapped_to_list = res1.map_err(lambda e: [e]).err()  # type: Optional[List[int]]

# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
github dbrgn / result / result / result.py View on Github external
def __eq__(self, other: Any) -> bool:
        return isinstance(other, Ok) and self.value == other.value
github dbrgn / result / result / result.py View on Github external
def map(self, op: Callable[[T], U]) -> 'Result[U, E]':
        """
        The contained result is `Ok`, so return `Ok` with original value mapped to
        a new value using the passed in function.
        """
        return Ok(op(self._value))
github dbrgn / result / result / result.py View on Github external
def map_err(self, op: Callable[[E], F]) -> 'Result[T, F]':
        """
        The contained result is `Err`, so return `Err` with original error mapped to
        a new value using the passed in function.
        """
        return Err(op(self._value))


# define Result as a generic type alias for use
# in type annotations
"""
A simple `Result` type inspired by Rust.
Not all methods (https://doc.rust-lang.org/std/result/enum.Result.html)
have been implemented, only the ones that make sense in the Python context.
"""
Result = Union[Ok[T], Err[E]]

"""
A type to use in `isinstance` checks.
This is purely for convenience sake, as you could also just write `isinstance(res, (Ok, Err))
"""
OkErr = (Ok, Err)


class UnwrapError(Exception):
    pass