def outcome(self) -> GameOutcome: if self.home_goals is None and self.guest_goals is None: return GameOutcome.OPEN home_goals: int = Maybe.from_optional(self.home_goals).unwrap() guest_goals: int = Maybe.from_optional(self.guest_goals).unwrap() if home_goals > guest_goals \ or self.forfeiting_team == self.guest_team: return GameOutcome.HOME_WIN if home_goals < guest_goals \ or self.forfeiting_team == self.home_team: return GameOutcome.AWAY_WIN if home_goals == guest_goals: return GameOutcome.TIE raise ValueError('no matching outcome')
def __init__(self, context: Context, image: Optional[Image] = None, padding: Insets = Insets(0, 0, 0, 0), visible: bool = True) -> None: self.image = Maybe.from_optional(image) self.padding = padding super().__init__(context, visible)
def __init__(self, toolkit: Toolkit, look_and_feel: Optional[LookAndFeel] = None, font_options: Optional[FontOptions] = None, window_manager: Optional[WindowManager] = None, error_handler: Optional[ErrorHandler] = None) -> None: if toolkit is None: raise ValueError("Argument 'toolkit' is required.") super().__init__() from alleycat.ui import WindowManager from alleycat.ui.glass import GlassLookAndFeel self._toolkit = toolkit self._look_and_feel = Maybe.from_optional(look_and_feel).or_else_call(lambda: GlassLookAndFeel(toolkit)) self._font_options = Maybe.from_optional(font_options).or_else_call( lambda: FontOptions(antialias=ANTIALIAS_SUBPIXEL, hint_style=HINT_STYLE_FULL)) self._error_handler = Maybe.from_optional(error_handler).value_or(toolkit.error_handler) self._window_manager = Maybe.from_optional(window_manager) \ .or_else_call(lambda: WindowManager(self.error_handler)) inputs = toolkit.create_inputs(self) assert inputs is not None self._inputs = {i.id: i for i in inputs} self._pollers = [i for i in inputs if isinstance(i, EventLoopAware)] # noinspection PyTypeChecker self.surface = self.observe("window_size").pipe(ops.map(self.toolkit.create_surface)) old_surface = self.observe("surface").pipe( ops.pairwise(), ops.map(lambda s: s[0]), ops.take_until(self.on_dispose)) old_surface.subscribe(Surface.finish, on_error=self.error_handler)
def __init__(self, context: Context, layout: Optional[Layout] = None, visible: bool = True): from .layout import AbsoluteLayout self._layout = Maybe.from_optional(layout).or_else_call(AbsoluteLayout) self._layout_pending = True self._layout_running = False # noinspection PyTypeChecker self.children = self.layout.observe("children").pipe( ops.map( lambda children: tuple(map(lambda c: c.component, children)))) super().__init__(context, visible) self.observe("size") \ .pipe(ops.filter(lambda _: self.visible), ops.distinct_until_changed()) \ .subscribe(lambda _: self.request_layout(), on_error=self.error_handler)
def resize_handle_at(self, component: Frame, location: Point) -> Maybe[Direction]: (px, py) = (location - component.offset).tuple (x, y, w, h) = component.bounds.tuple handle: Optional[Direction] = None if y <= py <= y + self.resize_handle_size: handle = Direction.North elif y + h - self.resize_handle_size <= py <= y + h: handle = Direction.South if x + w - self.resize_handle_size <= px <= x + w: if handle is None: handle = Direction.East else: handle = Direction.Northeast if handle == Direction.North else Direction.Southeast elif x <= px <= x + self.resize_handle_size: if handle is None: handle = Direction.West else: handle = Direction.Northwest if handle == Direction.North else Direction.Southwest return Maybe.from_optional(handle)
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), ReaderFutureResult.from_failure(1), ) @pytest.mark.parametrize('container', _containers) def test_assert_equal(container, anyio_backend_name: str):
def get_street_address(order: 'shopping.order.Order') -> Maybe[str]: return Maybe.from_optional( order.user).bind_optional(lambda user: user.address).bind_optional( lambda address: address.street, )