venv added, updated

This commit is contained in:
Norbert
2024-09-13 09:46:28 +02:00
parent 577596d9f3
commit 82af8c809a
4812 changed files with 640223 additions and 2 deletions

View File

@@ -0,0 +1,22 @@
from .disposable import DisposableBase
from .observable import ObservableBase, Subscription
from .observer import ObserverBase, OnCompleted, OnError, OnNext
from .periodicscheduler import PeriodicSchedulerBase
from .scheduler import ScheduledAction, SchedulerBase
from .startable import StartableBase
from .subject import SubjectBase
__all__ = [
"DisposableBase",
"ObserverBase",
"ObservableBase",
"OnCompleted",
"OnError",
"OnNext",
"SchedulerBase",
"PeriodicSchedulerBase",
"SubjectBase",
"Subscription",
"ScheduledAction",
"StartableBase",
]

View File

@@ -0,0 +1,34 @@
from __future__ import annotations
from abc import ABC, abstractmethod
from types import TracebackType
from typing import Optional, Type
class DisposableBase(ABC):
"""Disposable abstract base class."""
__slots__ = ()
@abstractmethod
def dispose(self) -> None:
"""Dispose the object: stop whatever we're doing and release all of the
resources we might be using.
"""
raise NotImplementedError
def __enter__(self) -> DisposableBase:
"""Context management protocol."""
return self
def __exit__(
self,
exctype: Optional[Type[BaseException]],
excinst: Optional[BaseException],
exctb: Optional[TracebackType],
) -> None:
"""Context management protocol."""
self.dispose()
__all__ = ["DisposableBase"]

View File

@@ -0,0 +1,45 @@
from abc import ABC, abstractmethod
from typing import Callable, Generic, Optional, TypeVar, Union
from .disposable import DisposableBase
from .observer import ObserverBase, OnCompleted, OnError, OnNext
from .scheduler import SchedulerBase
_T_out = TypeVar("_T_out", covariant=True)
class ObservableBase(Generic[_T_out], ABC):
"""Observable abstract base class.
Represents a push-style collection."""
__slots__ = ()
@abstractmethod
def subscribe(
self,
on_next: Optional[Union[OnNext[_T_out], ObserverBase[_T_out]]] = None,
on_error: Optional[OnError] = None,
on_completed: Optional[OnCompleted] = None,
*,
scheduler: Optional[SchedulerBase] = None,
) -> DisposableBase:
"""Subscribe an observer to the observable sequence.
Args:
observer: [Optional] The object that is to receive
notifications.
scheduler: [Optional] The default scheduler to use for this
subscription.
Returns:
Disposable object representing an observer's subscription
to the observable sequence.
"""
raise NotImplementedError
Subscription = Callable[[ObserverBase[_T_out], Optional[SchedulerBase]], DisposableBase]
__all__ = ["ObservableBase", "Subscription"]

View File

@@ -0,0 +1,48 @@
from abc import ABC, abstractmethod
from typing import Callable, Generic, TypeVar
_T = TypeVar("_T")
_T_in = TypeVar("_T_in", contravariant=True)
OnNext = Callable[[_T], None]
OnError = Callable[[Exception], None]
OnCompleted = Callable[[], None]
class ObserverBase(Generic[_T_in], ABC):
"""Observer abstract base class
An Observer is the entity that receives all emissions of a
subscribed Observable.
"""
__slots__ = ()
@abstractmethod
def on_next(self, value: _T_in) -> None:
"""Notifies the observer of a new element in the sequence.
Args:
value: The received element.
"""
raise NotImplementedError
@abstractmethod
def on_error(self, error: Exception) -> None:
"""Notifies the observer that an exception has occurred.
Args:
error: The error that has occurred.
"""
raise NotImplementedError
@abstractmethod
def on_completed(self) -> None:
"""Notifies the observer of the end of the sequence."""
raise NotImplementedError
__all__ = ["ObserverBase", "OnNext", "OnError", "OnCompleted"]

View File

@@ -0,0 +1,49 @@
from abc import ABC, abstractmethod
from typing import Callable, Optional, TypeVar, Union
from .disposable import DisposableBase
from .scheduler import RelativeTime, ScheduledAction
_TState = TypeVar("_TState") # Can be anything
ScheduledPeriodicAction = Callable[[Optional[_TState]], Optional[_TState]]
ScheduledSingleOrPeriodicAction = Union[
ScheduledAction[_TState], ScheduledPeriodicAction[_TState]
]
class PeriodicSchedulerBase(ABC):
"""PeriodicScheduler abstract base class."""
__slots__ = ()
@abstractmethod
def schedule_periodic(
self,
period: RelativeTime,
action: ScheduledPeriodicAction[_TState],
state: Optional[_TState] = None,
) -> DisposableBase:
"""Schedules a periodic piece of work.
Args:
period: Period in seconds or timedelta for running the
work periodically.
action: Action to be executed.
state: [Optional] Initial state passed to the action upon
the first iteration.
Returns:
The disposable object used to cancel the scheduled
recurring action (best effort).
"""
return NotImplemented
__all__ = [
"PeriodicSchedulerBase",
"ScheduledPeriodicAction",
"ScheduledSingleOrPeriodicAction",
"RelativeTime",
]

View File

@@ -0,0 +1,152 @@
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
from typing import Callable, Optional, TypeVar, Union
from .disposable import DisposableBase
_TState = TypeVar("_TState") # Can be anything
AbsoluteTime = Union[datetime, float]
RelativeTime = Union[timedelta, float]
AbsoluteOrRelativeTime = Union[datetime, timedelta, float]
ScheduledAction = Callable[
["SchedulerBase", Optional[_TState]],
Optional[DisposableBase],
]
class SchedulerBase(ABC):
"""Scheduler abstract base class."""
__slots__ = ()
@property
@abstractmethod
def now(self) -> datetime:
"""Represents a notion of time for this scheduler. Tasks being
scheduled on a scheduler will adhere to the time denoted by this
property.
Returns:
The scheduler's current time, as a datetime instance.
"""
return NotImplemented
@abstractmethod
def schedule(
self, action: ScheduledAction[_TState], state: Optional[_TState] = None
) -> DisposableBase:
"""Schedules an action to be executed.
Args:
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return NotImplemented
@abstractmethod
def schedule_relative(
self,
duetime: RelativeTime,
action: ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> DisposableBase:
"""Schedules an action to be executed after duetime.
Args:
duetime: Relative time after which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return NotImplemented
@abstractmethod
def schedule_absolute(
self,
duetime: AbsoluteTime,
action: ScheduledAction[_TState],
state: Optional[_TState] = None,
) -> DisposableBase:
"""Schedules an action to be executed at duetime.
Args:
duetime: Absolute time at which to execute the action.
action: Action to be executed.
state: [Optional] state to be given to the action function.
Returns:
The disposable object used to cancel the scheduled action
(best effort).
"""
return NotImplemented
@classmethod
@abstractmethod
def to_seconds(cls, value: AbsoluteOrRelativeTime) -> float:
"""Converts time value to seconds. This method handles both absolute
(datetime) and relative (timedelta) values. If the argument is already
a float, it is simply returned unchanged.
Args:
value: the time value to convert to seconds.
Returns:
The value converted to seconds.
"""
return NotImplemented
@classmethod
@abstractmethod
def to_datetime(cls, value: AbsoluteOrRelativeTime) -> datetime:
"""Converts time value to datetime. This method handles both absolute
(float) and relative (timedelta) values. If the argument is already
a datetime, it is simply returned unchanged.
Args:
value: the time value to convert to datetime.
Returns:
The value converted to datetime.
"""
return NotImplemented
@classmethod
@abstractmethod
def to_timedelta(cls, value: AbsoluteOrRelativeTime) -> timedelta:
"""Converts time value to timedelta. This method handles both absolute
(datetime) and relative (float) values. If the argument is already
a timedelta, it is simply returned unchanged. If the argument is an
absolute time, the result value will be the timedelta since the epoch,
January 1st, 1970, 00:00:00.
Args:
value: the time value to convert to timedelta.
Returns:
The value converted to timedelta.
"""
return NotImplemented
__all__ = [
"SchedulerBase",
"AbsoluteTime",
"RelativeTime",
"AbsoluteOrRelativeTime",
"ScheduledAction",
]

View File

@@ -0,0 +1,14 @@
from abc import ABC, abstractmethod
class StartableBase(ABC):
"""Abstract base class for Thread- and Process-like objects."""
__slots__ = ()
@abstractmethod
def start(self) -> None:
raise NotImplementedError
__all__ = ["StartableBase"]

View File

@@ -0,0 +1,72 @@
from abc import abstractmethod
from typing import Optional, TypeVar, Union
from .disposable import DisposableBase
from .observable import ObservableBase
from .observer import ObserverBase, OnCompleted, OnError, OnNext
from .scheduler import SchedulerBase
_T = TypeVar("_T")
class SubjectBase(ObserverBase[_T], ObservableBase[_T]):
"""Subject abstract base class.
Represents an object that is both an observable sequence as well
as an observer.
"""
__slots__ = ()
@abstractmethod
def subscribe(
self,
on_next: Optional[Union[OnNext[_T], ObserverBase[_T]]] = None,
on_error: Optional[OnError] = None,
on_completed: Optional[OnCompleted] = None,
*,
scheduler: Optional[SchedulerBase] = None,
) -> DisposableBase:
"""Subscribe an observer to the observable sequence.
Args:
observer: [Optional] The object that is to receive
notifications.
scheduler: [Optional] The default scheduler to use for this
subscription.
Returns:
Disposable object representing an observer's subscription
to the observable sequence.
"""
raise NotImplementedError
@abstractmethod
def on_next(self, value: _T) -> None:
"""Notifies the observer of a new element in the sequence.
Args:
value: The received element.
"""
raise NotImplementedError
@abstractmethod
def on_error(self, error: Exception) -> None:
"""Notifies the observer that an exception has occurred.
Args:
error: The error that has occurred.
"""
raise NotImplementedError
@abstractmethod
def on_completed(self) -> None:
"""Notifies the observer of the end of the sequence."""
raise NotImplementedError
__all__ = ["SubjectBase"]