예제 #1
0
def _create_event(team: Team, game: Game) -> Result[Event, ErrCode]:
    if not game.opening_whistle:
        return Result.from_failure(ErrCode.MISSING_OPENING_WHISTLE)

    venue = 'Heimspiel' if game.home_team == team else 'Auswärtsspiel'
    summary = f'{venue} - {game.opponent_of(team).short_name}'

    leg_title = game.leg_title()
    description = f'{leg_title} gegen {game.opponent_of(team).name}'
    if game.sports_hall:
        description += '\nSporthalle: ' + str(game.sports_hall.name)

    dated_games = game.other_games().filter(opening_whistle__isnull=False)
    for other in sorted(dated_games, key=lambda g: g.opening_whistle):
        if other.home_goals is not None:
            description += f'\n{other.leg_title()}: {other.home_goals}:{other.guest_goals} ({_outcome(other, team)})'

    start = game.opening_whistle
    end = start + timedelta(minutes=90)
    dtstamp = datetime.now()
    location = game.sports_hall.address if game.sports_hall else None
    uid = f'game/{game.number}@hbscorez.de'

    event = Event()
    event.add('summary', summary)
    event.add('description', description)
    event.add('dtstart', start)
    event.add('dtend', end)
    event.add('dtstamp', dtstamp)
    if location:
        event['location'] = vText(location)
    event['uid'] = uid

    return Result.from_value(event)
예제 #2
0
    def next_state(self, state: MouseState) -> ResultE[MouseState]:
        i = mouse.activeInputs

        pos = Point2D.from_tuple(mouse.position)
        buttons = set(filter(lambda b: b.event in i and KX_INPUT_ACTIVE in i[b.event].status, MouseButton))

        return Result.from_value(MouseState(pos, buttons))
예제 #3
0
async def async_bind_awaitable(
    function: Callable[[_ValueType], Awaitable[_NewValueType]],
    inner_value: Awaitable[Result[_ValueType, _ErrorType]],
) -> Result[_NewValueType, _ErrorType]:
    """Async binds a coroutine over a value."""
    container = await inner_value
    if isinstance(container, Result.success_type):
        return Result.from_value(await function(container.unwrap()))
    return container  # type: ignore
예제 #4
0
    def __call__(self, stream_id: UUID) -> Result[StreamDetails, FailureDetails]:
        stream = self.__find_stream_by_stream_id(stream_id).bind(
            self.__verify_if_stream_exist
        )
        partial_projects = partial(stream.bind, self.__find_projects_by_stream)
        partial_ksql = partial(
            stream.bind, lambda stream_: self.__get_stream_by_name(stream_.name)
        )

        # TODO: Removes every `type: ignore` after resolution of
        #  https://github.com/dry-python/returns/issues/410
        return flow(  # type: ignore
            Result.from_value(StreamDetails.build),  # type: ignore
            stream.apply,
            bind(self.__call_and_apply(partial_projects)),
            bind(self.__call_and_apply(partial_ksql)),
        )
예제 #5
0
    Reader,
    ReaderFutureResult,
    ReaderIOResult,
    ReaderResult,
)
from returns.contrib.pytest import ReturnsAsserts
from returns.future import Future, FutureResult
from returns.io import IO, IOResult
from returns.maybe import Maybe
from returns.primitives.asserts import assert_equal
from returns.primitives.container import BaseContainer
from returns.result import Result

_containers: Sequence[BaseContainer] = (
    Result.from_failure(1),
    Result.from_value(1),
    IO(1),
    IOResult.from_failure(1),
    IOResult.from_value(1),
    Maybe.from_value(1),
    Maybe.from_value(None),
    Maybe.from_optional(None),
    Future.from_value(1),
    FutureResult.from_value(1),
    FutureResult.from_failure(1),
    Reader.from_value(1),
    ReaderResult.from_value(1),
    ReaderResult.from_failure(1),
    ReaderIOResult.from_value(1),
    ReaderIOResult.from_failure(1),
    ReaderFutureResult.from_value(1),
예제 #6
0
 def next_state(self, state: int) -> ResultE[int]:
     return Result.from_value(state + len(input_text))
예제 #7
0
 def init_state(self) -> ResultE[int]:
     return Result.from_value(0)
예제 #8
0
파일: base.py 프로젝트: mysticfall/alleycat
    def start(self, args: OrderedDict) -> None:
        self._start_args = Result.from_value(args)

        self.logger.info("Created state: %s.", args)
예제 #9
0
 def state_of(x, y, *args):
     return Result.from_value(MouseState(Point2D(x, y), set(args)))
예제 #10
0
def assert_success(actual: Result[..., ...], expected: Any) -> None:
    assert actual == Result.from_value(expected)
예제 #11
0
 def init_state(self) -> ResultE[MouseState]:
     return Result.from_value(MouseState(Point2D(0.5, 0.5), set()))
예제 #12
0
파일: test_apply.py 프로젝트: t94j0/returns
def test_apply_with_result():
    """Ensures that functions can be composed and return type is correct."""
    applied = apply(Result.from_value(_function))

    assert applied(Success(1)) == Success('2')
    assert applied(Failure('s')) == Failure('s')
예제 #13
0
def require_type(obj: Any, expected: Type[T]) -> ResultE[T]:
    if not isinstance(obj, not_empty(expected)):
        return Failure(create_error(obj, expected))

    return Result.from_value(obj)