Ejemplo n.º 1
0
def splice_out_successor(
    m: MapTree[Key,
               Value]) -> Tuple[Key, Value, Option[MapTreeLeaf[Key, Value]]]:
    for m2 in m.to_list():
        if isinstance(m2, MapTreeNode):
            mn = cast(MapTreeNode[Key, Value], m2)
            if is_empty(mn.left):
                return (mn.key, mn.value, mn.right)
            else:
                k3, v3, l_ = splice_out_successor(mn.left)
                return (k3, v3, mk(l_, mn.key, mn.value, mn.right))
        else:
            return (m2.key, m2.value, empty)
    else:  # Nothing
        failwith("internal error: Map.splice_out_successor")
Ejemplo n.º 2
0
    def __next__(self) -> Tuple[Key, Value]:
        if not self.stack:
            raise StopIteration

        rest = self.stack.tail()
        for m in self.stack.head():
            if isinstance(m, MapTreeNode):
                failwith(
                    "Please report error: Map iterator, unexpected stack for next()"
                )
            else:
                self.stack = collapseLHS(rest)
                return m.key, m.value
        else:
            failwith(
                "Please report error: Map iterator, unexpected stack for next()"
            )
Ejemplo n.º 3
0
def rebalance(t1: MapTree[Key, Value], k: Key, v: Value,
              t2: MapTree[Key, Value]) -> MapTree[Key, Value]:
    t1h = height(t1)
    t2h = height(t2)
    if t2h > t1h + TOLERANCE:  # right is heavier than left
        if isinstance(t2.value, MapTreeNode):
            t2_ = t2.value
            # One of the nodes must have height > height t1 + 1
            if height(t2_.left) > t1h + 1:  # balance left: combination
                if isinstance(t2_.left.value, MapTreeNode):
                    t2l = t2_.left.value
                    return mk(
                        mk(t1, k, v, t2l.left),
                        t2l.key,
                        t2l.value,
                        mk(t2l.right, t2_.key, t2_.value, t2_.right),
                    )
                else:
                    failwith("internal error: Map.rebalance")
            else:  # Rotate left
                return mk(mk(t1, k, v, t2_.left), t2_.key, t2_.value,
                          t2_.right)
        else:
            failwith("internal error: Map.rebalance")
    else:
        if t1h > t2h + TOLERANCE:  # left is heavier than right
            if isinstance(t1.value, MapTreeNode):
                t1_ = t1.value
                # One of the nodes must have height > height t2 + 1
                if height(t1_.right) > t2h + 1:  # balance right: combination
                    if isinstance(t1_.right.value, MapTreeNode):
                        t1r = t1_.right.value
                        return mk(
                            mk(t1_.left, t1_.key, t1_.value, t1r.left),
                            t1r.key,
                            t1r.value,
                            mk(t1r.right, k, v, t2),
                        )
                    else:
                        failwith("internal error: Map.rebalance")
                else:
                    return mk(t1_.left, t1_.key, t1_.value,
                              mk(t1_.right, k, v, t2))
            else:
                failwith("internal error: Map.rebalance")
        else:
            return mk(t1, k, v, t2)
Ejemplo n.º 4
0
def already_finished():
    failwith("enumeration already finished")
Ejemplo n.º 5
0
def not_started() -> None:
    failwith("enumeration not started")
Ejemplo n.º 6
0
def default_session() -> ClientSession:
    return failwith("Must set HTTP session")
Ejemplo n.º 7
0
# This is usually the context used until we decode a fetched result into some custom result type.
HttpContext = Context[Option[ClientResponse]]


def default_session() -> ClientSession:
    return failwith("Must set HTTP session")


default_result = Nothing
default_request = HttpRequest(
    SessionFactory=default_session,
    Method=HttpMethod.GET,
    Headers=seq.empty,
    ContentBuilder=Nothing,
    Query=seq.empty,
    UrlBuilder=lambda _: failwith("Url not set"),  # type: ignore
)
default_context: HttpContext = Context(Request=default_request,
                                       Response=default_result)


def with_http_session(
        session: ClientSession) -> Callable[[HttpContext], HttpContext]:
    """Set the HTTP client to useH for the requests."""
    def _(context: HttpContext) -> HttpContext:
        return context.replace(Request=context.Request.replace(
            SessionFactory=(lambda: session)))

    return _