예제 #1
0
class AsyncHandler(ReleaseHandler):
    def __init__(self, timeout, *a, **kw) -> None:
        super().__init__(*a, **kw)
        self._async = Empty()
        self._timeout = timedelta(minutes=timeout)

    def _handle(self, monitor):
        if self._async.empty:
            self._async = (self._create_async(monitor) /
                           amino.L(self._adapter)(_, monitor) % __.start())

    def _cleanup(self):
        def timeout(a):
            if datetime.now() - a.created > self._timeout:
                self._clean_timeout(a)
                a.kill()
                return True

        if (self._async.exists(self._clean_done)
                or self._async.exists(timeout)):
            self._async = Empty()

    @abc.abstractproperty
    def _adapter(self) -> type:
        ...

    @abc.abstractmethod
    def _clean_done(self, proc) -> bool:
        ...

    @abc.abstractmethod
    def _clean_timeout(self, proc) -> None:
        ...
예제 #2
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)
예제 #3
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)
예제 #4
0
 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))
예제 #5
0
    def _cleanup(self):
        def timeout(a):
            if datetime.now() - a.created > self._timeout:
                self._clean_timeout(a)
                a.kill()
                return True

        if (self._async.exists(self._clean_done)
                or self._async.exists(timeout)):
            self._async = Empty()
예제 #6
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())
예제 #7
0
 def run(names, ids):
     return (
         Empty()
         if ids.empty else
         ids.head
         if ids.length == 1 else
         self._ask_show(names, ids)
     )
예제 #8
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
예제 #9
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())
예제 #10
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())
예제 #11
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)
예제 #12
0
 def __init__(self, library) -> None:
     self._library = library
     if self._display:
         os.environ['DISPLAY'] = self._display
     self._Player = None
     self._mplayer = None
     self._target = None
     self._observer = None
     self._extra_args = []  # type: list
     self._sub_fps = Empty()
     self._setup_player_type()
예제 #13
0
def from_tree(
        tree: Node[A, Any],
        f: Callable[[Node[A, Any], Maybe[RoseTree[B]]], B],
        cons_root: Callable[[B, Callable[[RoseTree[Node[A, Any]]], LazyList[RoseTree[Node[A, Any]]]]], RoseTree[B]],
        cons_node: Callable[
            [B, RoseTree[Node[A, Any]], Callable[[RoseTree[Node[A, Any]]], LazyList[RoseTree[Node[A, Any]]]]],
            RoseTree[B]
        ]
) -> RoseTree[B]:
    def sub(node: Node[A, Any]) -> Callable[[RoseTree[Node[B, Any]]], LazyList[RoseTree[Node[B, Any]]]]:
        def cons_sub(parent: RoseTree[Node[B, Any]], a: A) -> RoseTree[B]:
            return cons_node(f(a, Just(parent)), parent, sub(a))
        return lambda parent: node.sub_l.map(lambda a: cons_sub(parent, a))
    return cons_root(f(tree, Empty()), sub(tree))
예제 #14
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
예제 #15
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)
예제 #16
0
 def join_maybes(self):
     List(Just(4), Empty(), Just(5), Empty())\
         .join\
         .should.equal(List(4, 5))
예제 #17
0
 def resolve():
     return self._resolve_show(name) if self._query_etvdb else Empty()
예제 #18
0
 def flat_map_maybe(self):
     List(1, 2, 3)\
         .flat_map(lambda a: Empty() if a % 2 == 0 else Just(a + 1))\
         .should.equal(List(2, 4))
예제 #19
0
 def __init__(self, timeout, *a, **kw) -> None:
     super().__init__(*a, **kw)
     self._async = Empty()
     self._timeout = timedelta(minutes=timeout)
예제 #20
0
파일: maybe.py 프로젝트: yuhangwang/amino
 def filter(self, fa: Maybe[A], f: Callable[[A], bool]):
     return fa // (lambda a: Just(a) if f(a) else Empty())
예제 #21
0
파일: maybe.py 프로젝트: yuhangwang/amino
 def index_where(self, fa: Maybe[A], f: Callable[[A], bool]):
     return fa / f // (lambda a: Just(0) if a else Empty())
예제 #22
0
파일: maybe.py 프로젝트: yuhangwang/amino
 def flat_map(self, fa: Maybe[A], f: Callable[[A], Maybe[B]]) -> Maybe[B]:
     return fa.cata(lambda v: f(v), Empty())
예제 #23
0
파일: maybe.py 프로젝트: yuhangwang/amino
 def traverse(self, fa: Maybe[A], f: Callable, tpe: type):
     monad = Applicative.fatal(tpe)
     r = lambda a: monad.map(f(a), Just)
     return fa.cata(r, monad.pure(Empty()))
예제 #24
0
 def find(self):
     l = List(1, 6, 9)
     l.find(_ % 2 == 0).should.equal(Just(6))
     l.find(_ == 3).should.equal(Empty())
예제 #25
0
 def lift(self):
     l = List(1, 4, 7)
     l.lift(1).should.equal(Just(4))
     l.lift(3).should.equal(Empty())
예제 #26
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)
예제 #27
0
 def __init__(self, releases, shows, *a, **kw):
     RestApiBase.__init__(self, 'get')
     self._releases = releases
     self._shows = shows
     self._getd = Empty()
예제 #28
0
 def __init__(self, releases, *a, **kw):
     super().__init__(self._download_timeout, releases, 5,
                      'download manager')
     self._min_failed = 0
     self._proc = Empty()
예제 #29
0
 def result(self) -> Maybe[List]:
     return (Just(List.wrap(self._result.get_nowait()))
             if self.result_present else Empty())
예제 #30
0
 def post(self, data: Callable = None, path=Empty()):
     return self._req(_.post, path=path, data=data)