Beispiel #1
0
 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)
Beispiel #2
0
 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)
Beispiel #3
0
def add_crm_venv() -> Do:
    handle = yield Ribo.setting(handle_crm)
    if handle:
        log.debug('adding chromatin venv')
        plugin = Rplugin.simple('chromatin')
        yield Ribo.modify_main(lambda a: a.set.chromatin_rplugin(Just(plugin)))
        yield Ribo.modify_main(
            lambda a: a.set.chromatin_venv(Just('chromatin')))
Beispiel #4
0
 def find(self):
     k1 = 'key'
     v1 = 'value'
     k2 = 'key2'
     v2 = 'value2'
     m = Map({k1: v1, k2: v2})
     m.find(_ == v1).should.equal(Just((k1, v1)))
     m.find_key(_ == k2).should.equal(Just((k2, v2)))
     m.find(_ == 'invalid').should.equal(Empty())
     m.find_key(_ == 'invalid').should.equal(Empty())
Beispiel #5
0
 def _getitem(self) -> None:
     f = __[1]
     a = 13
     f((1, a, 2)).should.equal(a)
     g = self.__.filter(self._ > 1)[1]
     b = 6
     g(List(4, 1, b)).should.equal(b)
     h = self._.x[0]
     h(Just(List(a))).should.equal(a)
     i = self._.x[0].length
     l = 3
     i(Just(List(Lists.range(3)))).should.equal(l)
Beispiel #6
0
 def is_handler(name: str, f: Callable) -> Do:
     effective = getattr(f, '__do_original', f)
     hints = yield Try(get_type_hints, effective).to_maybe
     spec = yield Try(inspect.getfullargspec, effective).to_maybe
     param_name = yield Lists.wrap(spec.args).lift(1)
     param_type = yield Map(hints).lift(param_name)
     yield (Just(
         (normalize_type(param_type), f)) if isinstance(param_type, type)
            and issubclass(param_type, alg) else Just(
                (normalize_type(param_type),
                 f)) if isinstance(param_type, _GenericAlias)
            and issubclass(param_type.__origin__, alg) else Nothing)
Beispiel #7
0
 def search(monitor) -> None:
     r = monitor['release']
     date = Try(datetime.strptime, r['airdate'], '%Y-%m-%d') | None
     release = Release(name=r['series'], season=r['season'], episode=r['episode'], search_name=r['search_name'],
                       airdate=date)
     query = cmd.rest.filter_not(_.empty) / _.join_tokens | release.search_query_no_res
     query1 = release.search_query_date if query == 'date' else query
     self.log.info(f'Searching torrent for "{release}" with query "{query1}"...')
     dl = TorrentDownloader([query1])
     res = Boolean(dl._search()).m(dl.search_results) / Lists.wrap // _.head
     return (
         res /
         (lambda l: cmd.put(data=(lambda i: Just(dict(url=l))), path=Just('link'))) |
         IO.failed('no search results')
     )
Beispiel #8
0
 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)
Beispiel #9
0
 def is_handler(name: str, f: Callable) -> Do:
     effective = getattr(f, '__do_original', f)
     spec = yield Try(inspect.getfullargspec, effective).to_maybe
     param_name = yield Lists.wrap(spec.args).lift(1)
     param_type = yield Map(spec.annotations).lift(param_name)
     yield (Just((normalize_type(param_type),
                  f)) if issubclass(param_type, alg) else Nothing)
Beispiel #10
0
def parse_size(size_str: str) -> Generator:
    match = yield size_re.match(size_str).to_maybe
    num = yield match.group('num').to_maybe
    prefix = yield match.group('prefix').to_maybe
    mult = 1e6 if prefix == 'M' else 1e9
    num_i = yield Try(float, num).to_maybe
    yield Just(num_i * mult)
Beispiel #11
0
    def join_maybe(self, err: str) -> 'IO[B]':
        def f(a: Union[A, Maybe[B]]) -> 'IO[B]':
            return (IO.from_maybe(a, err) if isinstance(a, Maybe) else
                    IO.failed(f'`IO.join_maybe` called on {self}'))

        return self.flat_map(f,
                             fs=Just(Eval.later('join_maybe({})'.format, err)))
Beispiel #12
0
    def loop(head: A, tail: List[A]) -> Eval[Maybe[List[A]]]:
        def flatten(
                memla: Maybe[Eval[Maybe[List[A]]]]) -> Eval[Maybe[List[A]]]:
            return (memla.map(lambda emla: emla.map(lambda mla: mla.map(
                lambda la: la.cons(head)))) | (lambda: Eval.now(Nothing)))

        return (Eval.now(Just(tail.cons(new))) if pred(head) else
                Eval.later(tail.detach_head.map2, loop) // flatten)
Beispiel #13
0
    def flat_map(self) -> None:
        s = MaybeState.pure(1)

        def f(a: int) -> None:
            return MaybeState.inspect(lambda s: len(s) + a)

        s1 = s.flat_map(f)
        return s1.run('str').should.equal(Just(('str', 4)))
Beispiel #14
0
    def create(self, data: Callable = None, path=Empty()):
        cb = (lambda a: Just(dict())) if data is None else data
        sub = path / '/{}'.format | ''

        def assemble(path):
            return (cb(self.rest).io('invalid arguments') /
                    L(self.client.post)('{}{}'.format(path, sub), _))

        return self._data_path // assemble
Beispiel #15
0
 def add(self):
     key = 'key'
     val = 'value'
     k2 = 'key2'
     v2 = 'value2'
     m = Map({key: val})
     m2 = m + (k2, v2)
     m2.lift(k2).should.equal(Just(v2))
     m.lift(k2).should.equal(Empty())
Beispiel #16
0
def setup_venvs(names: List[str]) -> Do:
    venvs = yield setup_venv_dir(Just(venvs_path))
    create = names.exists(lambda a: not venv_path(a).exists())
    if create:
        yield N.from_io(clear_cache())
    plugins = yield names.traverse(setup_venv, NvimIO)
    yield nvim_sync_command('CrmSetupPlugins')
    yield plugins.traverse(venv_existent(venvs, 30), NvimIO)
    yield plugins.traverse(package_installed(venvs), NvimIO)
Beispiel #17
0
 def _instantiate_type(self) -> None:
     a, b, c = 1, 2, 3
     class T:
         def __init__(self, a) -> None:
             self.a = a
         def __call__(self, a, b):
             return self.a, a, b
     l = Just(T) / self.__(a)
     (l / self.__(b, c)).should.contain((a, b, c))
Beispiel #18
0
 def add_multi(self):
     key = 'key'
     val = 'value'
     k2 = 'key2'
     v2 = 'value2'
     m = Map({key: val})
     m2 = m**Map({k2: v2})
     m2.lift(k2).should.equal(Just(v2))
     m.lift(k2).should.equal(Empty())
Beispiel #19
0
 def flat_map(self):
     k1 = 'key'
     v1 = 'value'
     k2 = 'key2'
     v2 = 'value2'
     m = Map({k1: v1, k2: v2})
     res = m.flat_map(lambda a, b: Just((a, b)) if a == k1 else Empty())
     res.should.have.key(k1).being.equal(v1)
     res.should_not.have.key(k2)
Beispiel #20
0
    def _complex_placeholders(self) -> None:
        v1 = 13
        v2 = 29

        def f(a, b, c, d):
            return b * d

        Just((v1, v2)).map2(self.L(f)(2, self._, 4,
                                      self._)).should.contain(v1 * v2)
Beispiel #21
0
    def just(self) -> None:
        @do
        def run(i: int) -> Generator[Maybe[int], int, Maybe[int]]:
            a = yield Just(i)
            b = yield Just(a + 5)
            c = yield Just(b + 7)
            d = yield Just(c * 3)
            yield Just(d)

        run(3).should.equal(Just(45))
Beispiel #22
0
    def just(self) -> None:
        @do(Maybe[int])
        def run(i: int) -> Do:
            a = yield Just(i)
            b = yield Just(a + 5)
            c = yield Just(b + 7)
            d = yield Just(c * 3)
            return d

        run(3).should.equal(Just(45))
Beispiel #23
0
    def foreach(self) -> None:
        a = 1
        b = 6

        def setter(c: int) -> None:
            nonlocal a
            a = c + 1

        (Just(b) % setter).should.contain(b)
        a.should.equal(b + 1)
Beispiel #24
0
 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)
Beispiel #25
0
def parse_row(row: etree.Element) -> Generator:
    texts = cell_texts(row)
    title = yield texts.head
    size_str, seeders_str = yield texts.lift_all(-3, -2)
    link = yield sub(row, 'div[@class="tt-name"]/a').head
    url = yield Maybe.optional(link.get('href'))
    hash_match = yield url_re.search(url).to_maybe
    hash = yield hash_match.group('hash').to_maybe
    size = yield parse_size(size_str)
    seeders = yield Try(int, seeders_str.replace(',', '')).to_maybe
    yield Just(
        dict(
            title=title,
            size=size,
            size_str=size_str,
            seeders=seeders,
            magnet_link=magnet(hash),
        ))
Beispiel #26
0
    def _req(self, meth, path=Empty(), data=None):
        sub = path / '/{}'.format | ''
        cb = (lambda a: Just(dict())) if data is None else data

        def io(i, body):
            return IO.delay(meth(self.client),
                            '{}/{}{}'.format(self._type, i, sub),
                            body=body)

        def assemble(i, a):
            return cb(a).io('invalid arguments') // L(io)(i, _)

        def check_error(response):
            if isinstance(response, dict) and 'error' in response:
                return IO.failed(response['error'])
            else:
                return IO.pure(response)

        return self.run(assemble) // check_error
Beispiel #27
0
 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)
Beispiel #28
0
 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)
Beispiel #29
0
 def eff_map_io(self):
     a, b = self._r
     t = IO.now(List(Just(a), Just(b), Empty()))
     target = List(Just(a + b), Just(b + b), Empty())
     res = t.effs(2).map(_ + b)
     res.value.run().should.equal(target)
Beispiel #30
0
 def lift_tuple(data: Tuple[A, ...]) -> Maybe[A]:
     return Just(data[index]) if len(data) > index else Nothing