def test_fold_loop_reader(iterable, sequence): """Ensures that ``.loop`` works for readers.""" assert Fold.loop( iterable, sequence.from_value(10), _sum_two, )(...) == sequence(...)
def run(target_agents: Set[Agent], enemy_agents: Set[Agent] = set()): agent_key_sources = ( try_to_submit_source(agent).map(lambda sources: (agent.key, sources)) for agent in chain(target_agents, enemy_agents)) source_map = (Fold.loop( agent_key_sources, Success([]), lambda key_source: lambda acc: acc + [key_source], ).map(lambda key_sources: dict(key_sources)).unwrap()) result_futures = {} with ProcessPoolExecutor() as executor: for lh, rh in product(target_agents, chain(target_agents, enemy_agents)): if lh == rh: continue lh, rh = _swap_agents_if_need(lh, rh) result_key = _get_result_key(lh, rh) lh_source = source_map[lh.key] rh_source = source_map[rh.key] result_futures[result_key] = executor.submit( _run_inner, lh_source, rh_source) return {k: f.result() for k, f in result_futures.items()}
async def main() -> IOResultE[Sequence[str]]: """ Main entrypoint for the async world. Let's fetch 3 titles of posts asynchronously. We use `gather` to run requests in "parallel". """ futures: Sequence[IOResultE[str]] = await asyncio.gather(*_show_titles(3)) return Fold.collect(futures, IOResultE.from_value(()))
def test_items(self): layout = BorderLayout() container = Frame(self.context, layout) child1 = Panel(self.context) child2 = Panel(self.context) child3 = Panel(self.context) child4 = Panel(self.context) child5 = Panel(self.context) container.add(child1) container.add(child2, Border.Left) container.add(child3, Border.Top, Insets(2, 2, 2, 2)) container.add(child4, Border.Right, padding=Insets(5, 5, 5, 5)) container.add(child5, region=Border.Bottom, padding=Insets(10, 10, 10, 10)) def assert_item(item: BorderItem, child: Component, border: Border, padding: Insets) -> None: self.assertEqual(child, item.component.unwrap()) self.assertEqual(border, item.border) self.assertEqual(padding, item.padding) assert_item(layout.areas[Border.Center], child1, Border.Center, Insets(0, 0, 0, 0)) assert_item(layout.areas[Border.Top], child3, Border.Top, Insets(2, 2, 2, 2)) assert_item(layout.areas[Border.Right], child4, Border.Right, Insets(5, 5, 5, 5)) assert_item(layout.areas[Border.Bottom], child5, Border.Bottom, Insets(10, 10, 10, 10)) assert_item(layout.areas[Border.Left], child2, Border.Left, Insets(0, 0, 0, 0)) container.add(child1, Border.Right) container.remove(child2) container.remove(child3) assert_item(layout.areas[Border.Right], child1, Border.Right, Insets(0, 0, 0, 0)) # noinspection PyTypeChecker children = Fold.collect_all( map(lambda a: a.component, layout.areas.values()), Some( ())).unwrap() self.assertEqual({child5, child1}, set(children)) self.assertEqual(Nothing, layout.areas[Border.Top].component) self.assertEqual(Some(child1), layout.areas[Border.Right].component) self.assertEqual(Some(child5), layout.areas[Border.Bottom].component) self.assertEqual(Nothing, layout.areas[Border.Left].component)
def _show_titles( number_of_posts: int, ) -> RequiresContextFutureResultE[Sequence[str], httpx.AsyncClient]: def factory(post: _Post) -> str: return post['title'] titles = [ # Notice how easily we compose async and sync functions: _fetch_post(post_id).map(factory) # TODO: try `for post_id in (2, 1, 0):` to see how errors work for post_id in range(1, number_of_posts + 1) ] return Fold.collect(titles, RequiresContextFutureResultE.from_value(()))
def test_fold_collect(iterable, sequence): """Iterable for regular types and ``Fold``.""" assert Fold.collect(iterable, sequence.from_value(())) == sequence
def test_fold_collect_recursion_limit(): """Ensures that ``.collect`` method is recurion safe.""" limit = sys.getrecursionlimit() + 1 iterable = (IO(1) for _ in range(limit)) expected = IO((1, ) * limit) assert Fold.collect(iterable, IO(())) == expected
def test_fold_collect_reader(iterable, sequence): """Ensures that ``.collect`` works for readers.""" assert Fold.collect( iterable, sequence.from_value(()), )(...) == sequence(...)
def test_fold_loop(iterable, sequence): """Iterable for ``Result`` and ``FailFast``.""" assert Fold.loop(iterable, sequence.from_value(10), _sum_two) == sequence
def test_fold_loop_recursion_limit(): """Ensures that ``.loop`` method is recurion safe.""" limit = sys.getrecursionlimit() + 1 iterable = (IO(1) for _ in range(limit)) assert Fold.loop(iterable, IO(0), _sum_two) == IO(limit)
def test_collect_all_reader_result(iterable, sequence): """Iterable for ``ReaderResult`` and ``Fold``.""" assert Fold.collect_all( iterable, sequence.from_value(()), )(...) == sequence(...)