PK ! uh dry_monads/__init__.py# -*- coding: utf-8 -*- PK ! ~B< < dry_monads/do_notation.py# -*- coding: utf-8 -*- from functools import wraps from typing import Callable from dry_monads.primitives.exceptions import UnwrapFailedError from dry_monads.primitives.types import MonadType def do_notation( function: Callable[..., MonadType], ) -> Callable[..., MonadType]: """ Decorator to enable 'do-notation' context. See example usages below. .. code:: python See also: - https://dry-rb.org/gems/dry-monads/do-notation/ - https://en.wikibooks.org/wiki/Haskell/do_notation - https://wiki.haskell.org/Do_notation_considered_harmful """ @wraps(function) def decorator(*args, **kwargs) -> MonadType: try: return function(*args, **kwargs) except UnwrapFailedError as exc: return exc.halted_monad return decorator PK ! W9l l dry_monads/either.py# -*- coding: utf-8 -*- from abc import ABCMeta, abstractmethod from typing import Any, Callable, Generic, NoReturn, TypeVar, Union from typing_extensions import final from dry_monads.primitives.exceptions import UnwrapFailedError from dry_monads.primitives.monad import Monad, NewValueType, ValueType ErrorType = TypeVar('ErrorType') # That's the most ugly part. # We need to express `Either` with two type parameters and # Left and Right with just one parameter. # And that's how we do it. Any other and more cleaner ways are appreciated. class Either(Generic[ValueType, ErrorType], metaclass=ABCMeta): """ Represents a calculation that may either fail or succeed. An alternative to using exceptions. 'Either' (or its alias 'Result') is an abstract type and should not be instantiated directly. Instead use 'Right' (or its alias 'Success') and 'Left' (or its alias 'Failure'). """ _inner_value: Union[ValueType, ErrorType] @abstractmethod def unwrap(self) -> ValueType: # pragma: no cover """ Custom magic method to unwrap inner value from monad. Should be redefined for ones that actually have values. And for ones that raise an exception for no values. """ raise NotImplementedError() @final class Left(Either[Any, ErrorType], Monad[ErrorType]): """ Represents a calculation which has failed. It should contain an error code or message. To help with readability you may alternatively use the alias 'Failure'. """ def __init__(self, inner_value: ErrorType) -> None: """ Wraps the given value in the Container. 'value' is any arbitrary value of any type including functions. """ self._inner_value = inner_value def fmap(self, function) -> 'Left[ErrorType]': """Returns the 'Left' instance that was used to call the method.""" return Left(self._inner_value) def bind(self, function) -> 'Left[ErrorType]': """Returns the 'Left' instance that was used to call the method.""" return Left(self._inner_value) def value_or(self, default_value: NewValueType) -> NewValueType: """Returns the value if we deal with 'Right' or default if 'Left'.""" return default_value def unwrap(self) -> NoReturn: """Raises an exception, since it does not have a value inside.""" raise UnwrapFailedError(self) @final class Right(Either[ValueType, Any], Monad[ValueType]): """ Represents a calculation which has succeeded and contains the result. To help with readability you may alternatively use the alias 'Success'. """ def __init__(self, inner_value: ValueType) -> None: """ Wraps the given value in the Container. 'value' is any arbitrary value of any type including functions. """ self._inner_value = inner_value def fmap( self, function: Callable[[ValueType], NewValueType], ) -> 'Right[NewValueType]': """ Applies function to the inner value. Applies 'function' to the contents of the 'Right' instance and returns a new 'Right' object containing the result. 'function' should accept a single "normal" (non-monad) argument and return a non-monad result. """ return Right(function(self._inner_value)) def bind( self, function: Callable[[ValueType], Either[NewValueType, ErrorType]], ) -> Either[NewValueType, ErrorType]: """ Applies 'function' to the result of a previous calculation. 'function' should accept a single "normal" (non-monad) argument and return either a 'Left' or 'Right' type object. """ return function(self._inner_value) def value_or(self, default_value: NewValueType) -> ValueType: """Returns the value if we deal with 'Right' or default if 'Left'.""" return self._inner_value def unwrap(self) -> ValueType: """Returns the unwrapped value from the inside of this monad.""" return self._inner_value # Useful aliases for end users: Result = Either Success = Right Failure = Left PK ! uh ! dry_monads/primitives/__init__.py# -*- coding: utf-8 -*- PK ! ?_= = # dry_monads/primitives/exceptions.py# -*- coding: utf-8 -*- from typing import TYPE_CHECKING if TYPE_CHECKING: # pragma: no cover from dry_monads.primitives.types import MonadType # noqa: Z435, F401 class UnwrapFailedError(Exception): """Raised when a monad can not be unwrapped into a meaningful value.""" def __init__(self, monad: 'MonadType') -> None: """ Saves halted monad in the inner state. So, this monad can later be unpacked from this exception and used as a regular value. """ super().__init__() self.halted_monad = monad PK ! ch dry_monads/primitives/monad.py# -*- coding: utf-8 -*- from abc import ABCMeta, abstractmethod from typing import Any, Generic, TypeVar ValueType = TypeVar('ValueType') NewValueType = TypeVar('NewValueType') class Monad(Generic[ValueType], metaclass=ABCMeta): """ Represents a "context" in which calculations can be executed. You won't create 'Monad' instances directly. Instead, sub-classes implement specific contexts. Monads allow you to bind together a series of calculations while maintaining the context of that specific monad. """ _inner_value: Any @abstractmethod def fmap(self, function): # pragma: no cover """ Applies 'function' to the contents of the functor. And returns a new functor value. """ raise NotImplementedError() @abstractmethod def bind(self, function): # pragma: no cover """ Applies 'function' to the result of a previous calculation. And returns a new monad. """ raise NotImplementedError() @abstractmethod def value_or(self, default_value): # pragma: no cover """Forces to unwrap value from monad or return a default.""" raise NotImplementedError() @abstractmethod def unwrap(self) -> ValueType: # pragma: no cover """ Custom magic method to unwrap inner value from monad. Should be redefined for ones that actually have values. And for ones that raise an exception for no values. """ raise NotImplementedError() def __str__(self) -> str: """Converts to string.""" return '{0}: {1}'.format( self.__class__.__qualname__, str(self._inner_value), ) def __eq__(self, other) -> bool: """Used to compare two 'Monad' objects.""" if not isinstance(other, Monad): return False return self._inner_value == other._inner_value # noqa: Z441 PK ! Rh dry_monads/primitives/types.py# -*- coding: utf-8 -*- from typing import TYPE_CHECKING, TypeVar, Union if TYPE_CHECKING: # pragma: no cover from dry_monads.either import Either # noqa: Z435, F401 from dry_monads.primitives.monad import Monad # noqa: Z435, F401 # We need to have this ugly type because there is no other way around it: MonadType = TypeVar('MonadType', bound=Union['Monad', 'Either']) PK ! R0 0 " dry_monads-0.1.1.dist-info/LICENSEMIT License Copyright (c) 2019 wemake.services Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK !HڽT U dry_monads-0.1.1.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK !H-̶H # dry_monads-0.1.1.dist-info/METADATAWrHM T!g ر &HjMwYyy}=[%R>ϝsiD5u?3Hdѹ Hn"R)瞺I$T]rnd!$J)IhĈI3Bӈ(`Q$TyIF#bDžsC*`=?v>2R]PLIe{e\vRfe[Rt,,r?ԲKD6iu?i^1+)\K~=c_p؆"K"hsE]G\ي>=Ti"@V x+ hrB.ѮVM~$2%TeɤT%VF,C~~rFeQC{as{_-HZd