def eff_flat_io_empty(self): t = IO.now(List(Right(Empty()))) target = List(Right(Empty())) res = (t.effs( 4, List, Either, Maybe, Either).flat_map(lambda x: IO.now(List(Right(Just(Right(1))))))) res.value.run().should.equal(target)
def eff_flat(self): a, b = self._r t = List(Just(Right(Just(a)))) target = List(Just(Right(Just(a + b)))) res = (t.effs( 3, Maybe, Either, Maybe).flat_map(lambda x: List(Just(Right(Just(x + b)))))) res.value.should.equal(target)
def decode(self, tpe: Type[Maybe], data: Json) -> Either[JsonError, Maybe[A]]: inner = Lists.wrap(tpe.__args__).head return (Right(Nothing) if data.absent else maybe_from_object( data, inner) if data.object else decode.match(data) / Just if data.array else Right(Nothing) if data.null else inner.cata( lambda a: decode_json_type_json(data, a) / Just, lambda: data.scalar.e(f'invalid type for `Maybe`: {data}', Maybe.check(data.data))))
def eff(self) -> None: def f(a: int) -> EvalState[int, Either[str, int]]: return EvalState.pure(Right(2)) s0 = EvalState.pure(Right(1)) s0.eff(Either).flat_map(f).value.run(1)._value().should.equal( (1, Right(2))) (s0 // EvalState.modify(I).replace ).eff(Either).flat_map(f).value.run(1)._value().should.equal( (1, Right(2)))
def read_conf(self) -> Expectation: rplugins = conf_data.map(Rplugin.from_config) successes = List( Right( DirRplugin.cons('null', '/dev/null', True, List('/some/path'))), Right(SiteRplugin.cons('plug', 'plug')), ) return ((k(rplugins[:2]) == successes) & k(rplugins.lift(2)).must(be_just(be_left)) & k(rplugins.lift(3)).must(be_just(be_left)))
def gather(self, po: ProgGather, output: Gather[A]) -> Prog[List[Either[IOException, A]]]: return Prog.pure( List( Right( GatherSubprocessResult( SubprocessResult(0, Nil, Nil, self.venv.name)))))
def staticmethod(self) -> None: @do(Either[str, int]) def run() -> Do: decoded = yield _code_json(_SM.cons) return decoded() run().should.equal(Right(65))
def function(self) -> None: @do(Either[str, int]) def run() -> Do: decoded = yield _code_json(Fun(encode_me)) return decoded.f() run().should.equal(Right(5))
def extract(match: Match) -> Generator: path = yield match.group('path') lnum = yield match.group('lnum') lnum_i = yield parse_int(lnum) col = match.group('col') // parse_int error = yield match.group('error') yield Right((Path(path), lnum_i, col, error))
def read_result(result: TmuxCmdResult) -> Either[List[str], List[str]]: return ( Right(result.output.output) if isinstance(result, TmuxCmdSuccess) else Left(result.output.output.cons(f'tmux command {result.cmd} failed:')) if isinstance(result, TmuxCmdError) else Left( result.output.output.cons( f'fatal tmux command execution in {result.cmds}:')))
def parse_magnet(magnet) -> Either[str, Magnet]: pr = urlparse(magnet) if pr.scheme == 'magnet': q = Map(parse_qs(pr.query)).valmap(List.wrap) name = q.get('dn') // _.head return Right(Magnet(name, q)) else: return Left('not a magnet')
def substitute(files: Files, path: Path, lnum: int, col: Either[str, int], error: str, coco_path: Path) -> Generator: lines = yield files.lift(path).to_either('corrupt state') line = yield lines.lift(lnum - 1).to_either(f'invalid line number {lnum} for {path}') lnum_match = yield lnum_rex.search(line) coco_lnum = yield lnum_match.group('lnum') coco_lnum_i = yield parse_int(coco_lnum) col_map = col / (lambda a: Map(col=a)) | Map() yield Right(Map(lnum=coco_lnum_i, text=error, valid=1, maker_name='mypy') ** col_map)
def optional(self) -> None: a = 'a' b = 'b' Maybe.optional(a).to_maybe.should.just_contain(a) Empty().to_maybe.should.be.a(Empty) Maybe.optional(a).to_either(b).should.equal(Right(a)) Empty().to_either(b).should.equal(Left(b)) Empty().to_either(lambda: b).should.equal(Left(b))
def single_venv_config(name: str, spec: str, **extra_vars: Any) -> Tuple[Rplugin, Venv, TestConfig]: rplugin = simple_rplugin(name, spec) dir = temp_dir('rplugin', 'venv') vars = Map(chromatin_venv_dir=str(dir))**Map(extra_vars) conf = lens.basic.state_ctor.set(LogBufferEnv.cons)(chromatin_config) venv = Venv( rplugin.name, VenvMeta(name, dir / name, Right(Path('/dev/null')), Right(Path('/dev/null')))) return rplugin, venv, TestConfig.cons( conf, vars=vars, io_interpreter=single_venv_io_interpreter(venv), logger=buffering_logger, function_handler=test_function_handler(exists=1), command_handler=test_command_handler(), )
def type_arg(tpe: type, index: int) -> Do: def error() -> str: return f'{tpe} has no type args' raw = yield Maybe.getattr(tpe, '__args__').to_either_f(error) types = yield Right(Lists.wrap(raw)) if isinstance( raw, Iterable) else Left(error()) yield types.lift(index).to_either_f( lambda: f'{tpe} has less than {index + 1} args')
def run(data: Json) -> Do: mod_field = yield data.field('mod') mod_path = yield mod_field.as_scalar names_field = yield data.field('names') names_json = yield names_field.as_array names = Lists.wrap(names_json.native) mod = yield (Either.import_module(mod_path.native) if isinstance( mod_path.native, str) else Left( JsonError(data, 'module is not a string'))) yield names.fold_m(Right(mod))(Either.getattr)
def traverse(self) -> None: def f(a: int) -> Either[str, int]: return Right(a * 2) Map({ 1: 2, 3: 4 }).traverse(f, Either).should.equal(Right(Map({ 1: 4, 3: 8 })))
def eff_flat_task(self): a, b = self._r t = Task.now(List(Right(Just(Right(a))))) target = List(Right(Just(Right(a + b)))) res = (t.effs(4, List, Either, Maybe, Either).flat_map( lambda x: Task.now(List(Right(Just(Right(x + b))))))) res.value.run().should.equal(target)
def eff_map(self): a, b = self._r c = -1 t = Right(List(Just(Right(a)), Just(Left(c)), Empty())) target = Right(List(Just(Right(a + b)), Just(Left(c)), Empty())) res = t.effs(3).map(_ + b) res.value.should.equal(target)
def from_tmux( pane_id: str, pane_width: str, pane_height: str, pane_top: str, pane_pid: str, window_id: str, session_id: str, ) -> Do: id = yield parse_pane_id(pane_id) width = yield parse_int(pane_width) height = yield parse_int(pane_height) top = yield parse_int(pane_top) pid = yield parse_int(pane_pid) wid = yield parse_window_id(window_id) sid = yield parse_session_id(session_id) yield Right(PaneData(id, width, height, top, pid, wid, sid))
def as_array(self) -> Either[JsonError, 'JsonArray']: return Right(self) if self.array else Left(self.error('not an array'))
def encode(self, a: Maybe[A]) -> Either[JsonError, Json]: return Right(JsonScalar(a | None))
def encode(self, a: Union[Number, str, None]) -> Either[JsonError, Json]: return Right(JsonScalar(a))
def either_from_object(data: JsonObject, ltype: Type[A], rtype: Type[B]) -> Do: value = yield data.field('value') decoded = yield decode.match(value) tpe = yield data.tpe yield Right(tpe(decoded))
def decode_type(data: Json) -> Do: mod = yield data.field('mod') names = yield data.field('names') yield (Right(type(None)) if mod.native == 'builtins' and names.native == ['NoneType'] else decode_instance(data, 'type'))
def fix(s: Map, r: Map) -> Id[Tuple[Map, Either[str, Map]]]: return Id((s, Right(r)))
def eff_flat_empty(self): t = List(Right(Empty())) target = List(Right(Empty())) res = (t.effs(3, Either, Maybe, Maybe).flat_map(lambda x: List(Right(Just(Just(1)))))) res.value.should.equal(target)
def as_object(self) -> Either[JsonError, 'JsonObject']: return Right(self) if self.object else Left( self.error('not an object'))
def eff_flat_io_left(self): a, b = self._r t = IO.now(Left(Just(a))) target = Left(Just(a)) res = t.effs(1, Either, Maybe) // (lambda x: IO.now(Right(Just(b)))) res.value.run().should.equal(target)
def field(self, key: str) -> Either[JsonError, Json]: return Right( self.data.lift(key) | JsonAbsent(self.error(f'no field `{key}`')))