venv added, updated
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
from .basic import default_comparer, default_error, noop
|
||||
from .concurrency import default_thread_factory, synchronized
|
||||
from .constants import DELTA_ZERO, UTC_ZERO
|
||||
from .exceptions import (
|
||||
ArgumentOutOfRangeException,
|
||||
DisposedException,
|
||||
SequenceContainsNoElementsError,
|
||||
)
|
||||
from .priorityqueue import PriorityQueue
|
||||
from .utils import NotSet, add_ref, alias, infinite
|
||||
|
||||
__all__ = [
|
||||
"add_ref",
|
||||
"alias",
|
||||
"ArgumentOutOfRangeException",
|
||||
"DisposedException",
|
||||
"default_comparer",
|
||||
"default_error",
|
||||
"infinite",
|
||||
"noop",
|
||||
"NotSet",
|
||||
"SequenceContainsNoElementsError",
|
||||
"concurrency",
|
||||
"DELTA_ZERO",
|
||||
"UTC_ZERO",
|
||||
"synchronized",
|
||||
"default_thread_factory",
|
||||
"PriorityQueue",
|
||||
]
|
||||
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
@@ -0,0 +1,36 @@
|
||||
from datetime import datetime
|
||||
from typing import Any, NoReturn, TypeVar, Union
|
||||
|
||||
_T = TypeVar("_T")
|
||||
|
||||
|
||||
def noop(*args: Any, **kw: Any) -> None:
|
||||
"""No operation. Returns nothing"""
|
||||
|
||||
|
||||
def identity(x: _T) -> _T:
|
||||
"""Returns argument x"""
|
||||
return x
|
||||
|
||||
|
||||
def default_now() -> datetime:
|
||||
return datetime.utcnow()
|
||||
|
||||
|
||||
def default_comparer(x: _T, y: _T) -> bool:
|
||||
return x == y
|
||||
|
||||
|
||||
def default_sub_comparer(x: Any, y: Any) -> Any:
|
||||
return x - y
|
||||
|
||||
|
||||
def default_key_serializer(x: Any) -> str:
|
||||
return str(x)
|
||||
|
||||
|
||||
def default_error(err: Union[Exception, str]) -> NoReturn:
|
||||
if isinstance(err, BaseException):
|
||||
raise err
|
||||
|
||||
raise Exception(err)
|
||||
@@ -0,0 +1,26 @@
|
||||
from threading import RLock, Thread
|
||||
from typing import Any, Callable, TypeVar
|
||||
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
from reactivex.typing import StartableTarget
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_P = ParamSpec("_P")
|
||||
|
||||
|
||||
def default_thread_factory(target: StartableTarget) -> Thread:
|
||||
return Thread(target=target, daemon=True)
|
||||
|
||||
|
||||
def synchronized(lock: RLock) -> Callable[[Callable[_P, _T]], Callable[_P, _T]]:
|
||||
"""A decorator for synchronizing access to a given function."""
|
||||
|
||||
def wrapper(fn: Callable[_P, _T]) -> Callable[_P, _T]:
|
||||
def inner(*args: _P.args, **kw: _P.kwargs) -> Any:
|
||||
with lock:
|
||||
return fn(*args, **kw)
|
||||
|
||||
return inner
|
||||
|
||||
return wrapper
|
||||
@@ -0,0 +1,4 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
DELTA_ZERO = timedelta(0)
|
||||
UTC_ZERO = datetime.utcfromtimestamp(0)
|
||||
@@ -0,0 +1,36 @@
|
||||
# Rx Exceptions
|
||||
|
||||
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class SequenceContainsNoElementsError(Exception):
|
||||
def __init__(self, msg: Optional[str] = None):
|
||||
super().__init__(msg or "Sequence contains no elements")
|
||||
|
||||
|
||||
class ArgumentOutOfRangeException(ValueError):
|
||||
def __init__(self, msg: Optional[str] = None):
|
||||
super(ArgumentOutOfRangeException, self).__init__(
|
||||
msg or "Argument out of range"
|
||||
)
|
||||
|
||||
|
||||
class DisposedException(Exception):
|
||||
def __init__(self, msg: Optional[str] = None):
|
||||
super().__init__(msg or "Object has been disposed")
|
||||
|
||||
|
||||
class ReEntracyException(Exception):
|
||||
def __init__(self, msg: Optional[str] = None):
|
||||
super().__init__(msg or "Re-entrancy detected")
|
||||
|
||||
|
||||
class CompletedException(Exception):
|
||||
def __init__(self, msg: Optional[str] = None):
|
||||
super().__init__(msg or "Observer completed")
|
||||
|
||||
|
||||
class WouldBlockException(Exception):
|
||||
def __init__(self, msg: Optional[str] = None):
|
||||
super().__init__(msg or "Would block")
|
||||
@@ -0,0 +1,54 @@
|
||||
import heapq
|
||||
from sys import maxsize
|
||||
from typing import Generic, List, Tuple, TypeVar
|
||||
|
||||
_T1 = TypeVar("_T1")
|
||||
|
||||
|
||||
class PriorityQueue(Generic[_T1]):
|
||||
"""Priority queue for scheduling. Note that methods aren't thread-safe."""
|
||||
|
||||
MIN_COUNT = ~maxsize
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.items: List[Tuple[_T1, int]] = []
|
||||
self.count = PriorityQueue.MIN_COUNT # Monotonic increasing for sort stability
|
||||
|
||||
def __len__(self) -> int:
|
||||
"""Returns length of queue"""
|
||||
|
||||
return len(self.items)
|
||||
|
||||
def peek(self) -> _T1:
|
||||
"""Returns first item in queue without removing it"""
|
||||
return self.items[0][0]
|
||||
|
||||
def dequeue(self) -> _T1:
|
||||
"""Returns and removes item with lowest priority from queue"""
|
||||
|
||||
item: _T1 = heapq.heappop(self.items)[0]
|
||||
if not self.items:
|
||||
self.count = PriorityQueue.MIN_COUNT
|
||||
return item
|
||||
|
||||
def enqueue(self, item: _T1) -> None:
|
||||
"""Adds item to queue"""
|
||||
|
||||
heapq.heappush(self.items, (item, self.count))
|
||||
self.count += 1
|
||||
|
||||
def remove(self, item: _T1) -> bool:
|
||||
"""Remove given item from queue"""
|
||||
|
||||
for index, _item in enumerate(self.items):
|
||||
if _item[0] == item:
|
||||
self.items.pop(index)
|
||||
heapq.heapify(self.items)
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def clear(self) -> None:
|
||||
"""Remove all items from the queue."""
|
||||
self.items = []
|
||||
self.count = PriorityQueue.MIN_COUNT
|
||||
@@ -0,0 +1,61 @@
|
||||
from functools import update_wrapper
|
||||
from types import FunctionType
|
||||
from typing import TYPE_CHECKING, Any, Callable, Iterable, Optional, TypeVar, cast
|
||||
|
||||
from typing_extensions import ParamSpec
|
||||
|
||||
from reactivex import abc
|
||||
from reactivex.disposable import CompositeDisposable
|
||||
from reactivex.disposable.refcountdisposable import RefCountDisposable
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from reactivex import Observable
|
||||
|
||||
_T = TypeVar("_T")
|
||||
_P = ParamSpec("_P")
|
||||
|
||||
|
||||
def add_ref(xs: "Observable[_T]", r: RefCountDisposable) -> "Observable[_T]":
|
||||
from reactivex import Observable
|
||||
|
||||
def subscribe(
|
||||
observer: abc.ObserverBase[Any], scheduler: Optional[abc.SchedulerBase] = None
|
||||
) -> abc.DisposableBase:
|
||||
return CompositeDisposable(r.disposable, xs.subscribe(observer))
|
||||
|
||||
return Observable(subscribe)
|
||||
|
||||
|
||||
def infinite() -> Iterable[int]:
|
||||
n = 0
|
||||
while True:
|
||||
yield n
|
||||
n += 1
|
||||
|
||||
|
||||
def alias(name: str, doc: str, fun: Callable[_P, _T]) -> Callable[_P, _T]:
|
||||
# Adapted from
|
||||
# https://stackoverflow.com/questions/13503079/how-to-create-a-copy-of-a-python-function#
|
||||
# See also help(type(lambda: 0))
|
||||
_fun = cast(FunctionType, fun)
|
||||
args = (_fun.__code__, _fun.__globals__)
|
||||
kwargs = {"name": name, "argdefs": _fun.__defaults__, "closure": _fun.__closure__}
|
||||
alias_ = FunctionType(*args, **kwargs) # type: ignore
|
||||
alias_ = update_wrapper(alias_, _fun)
|
||||
alias_.__kwdefaults__ = _fun.__kwdefaults__
|
||||
alias_.__doc__ = doc
|
||||
alias_.__annotations__ = _fun.__annotations__
|
||||
return cast(Callable[_P, _T], alias_)
|
||||
|
||||
|
||||
class NotSet:
|
||||
"""Sentinel value."""
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
return self is other
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "NotSet"
|
||||
|
||||
|
||||
__all__ = ["add_ref", "infinite", "alias", "NotSet"]
|
||||
Reference in New Issue
Block a user