From 11942b2e7b7568077c89cd4d8969aa6ea8c642cb Mon Sep 17 00:00:00 2001 From: Diviloper Date: Wed, 9 Nov 2022 20:07:50 +0100 Subject: [PATCH] Add streaming operations --- pymeos/pymeos/main/tbool.py | 4 +-- pymeos/pymeos/main/tfloat.py | 4 +-- pymeos/pymeos/main/tint.py | 4 +-- pymeos/pymeos/main/tpoint.py | 12 ++++----- pymeos/pymeos/main/ttext.py | 4 +-- pymeos/pymeos/temporal/temporal.py | 39 ++++++++++++++++++++++++++--- pymeos/pymeos/temporal/tsequence.py | 17 ++++++++++--- 7 files changed, 64 insertions(+), 20 deletions(-) diff --git a/pymeos/pymeos/main/tbool.py b/pymeos/pymeos/main/tbool.py index d485113c..698a41e1 100644 --- a/pymeos/pymeos/main/tbool.py +++ b/pymeos/pymeos/main/tbool.py @@ -193,10 +193,10 @@ class TBoolSeq(TSequence[bool, 'TBool', 'TBoolInst', 'TBoolSeq', 'TBoolSeqSet'], ComponentClass = TBoolInst def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, TBoolInst]]] = None, - lower_inc: bool = True, upper_inc: bool = False, + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, interpolation: TInterpolation = TInterpolation.STEPWISE, normalize: bool = True, _inner=None): super().__init__(string=string, instant_list=instant_list, lower_inc=lower_inc, upper_inc=upper_inc, - interpolation=interpolation, normalize=normalize, _inner=_inner) + expandable=expandable, interpolation=interpolation, normalize=normalize, _inner=_inner) class TBoolSeqSet(TSequenceSet[bool, 'TBool', 'TBoolInst', 'TBoolSeq', 'TBoolSeqSet'], TBool): diff --git a/pymeos/pymeos/main/tfloat.py b/pymeos/pymeos/main/tfloat.py index 5090b825..7c25d019 100644 --- a/pymeos/pymeos/main/tfloat.py +++ b/pymeos/pymeos/main/tfloat.py @@ -345,10 +345,10 @@ class TFloatSeq(TSequence[float, 'TFloat', 'TFloatInst', 'TFloatSeq', 'TFloatSeq ComponentClass = TFloatInst def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, TFloatInst]]] = None, - lower_inc: bool = True, upper_inc: bool = False, + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, interpolation: TInterpolation = TInterpolation.LINEAR, normalize: bool = True, _inner=None): super().__init__(string=string, instant_list=instant_list, lower_inc=lower_inc, upper_inc=upper_inc, - interpolation=interpolation, normalize=normalize, _inner=_inner) + expandable=expandable, interpolation=interpolation, normalize=normalize, _inner=_inner) class TFloatSeqSet(TSequenceSet[float, 'TFloat', 'TFloatInst', 'TFloatSeq', 'TFloatSeqSet'], TFloat): diff --git a/pymeos/pymeos/main/tint.py b/pymeos/pymeos/main/tint.py index 24efa40c..6ef27a0c 100644 --- a/pymeos/pymeos/main/tint.py +++ b/pymeos/pymeos/main/tint.py @@ -330,10 +330,10 @@ class TIntSeq(TSequence[int, 'TInt', 'TIntInst', 'TIntSeq', 'TIntSeqSet'], TInt) ComponentClass = TIntInst def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, TIntInst]]] = None, - lower_inc: bool = True, upper_inc: bool = False, + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, interpolation: TInterpolation = TInterpolation.STEPWISE, normalize: bool = True, _inner=None): super().__init__(string=string, instant_list=instant_list, lower_inc=lower_inc, upper_inc=upper_inc, - interpolation=interpolation, normalize=normalize, _inner=_inner) + expandable=expandable, interpolation=interpolation, normalize=normalize, _inner=_inner) class TIntSeqSet(TSequenceSet[int, 'TInt', 'TIntInst', 'TIntSeq', 'TIntSeqSet'], TInt): diff --git a/pymeos/pymeos/main/tpoint.py b/pymeos/pymeos/main/tpoint.py index 45835e64..d090d7e4 100644 --- a/pymeos/pymeos/main/tpoint.py +++ b/pymeos/pymeos/main/tpoint.py @@ -831,21 +831,21 @@ class TGeomPointSeq(TPointSeq['TGeomPoint', 'TGeomPointInst', 'TGeomPointSeq', ' ComponentClass = TGeomPointInst def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, TGeomPointInst]]] = None, - lower_inc: bool = True, upper_inc: bool = False, interpolation: TInterpolation = TInterpolation.LINEAR, + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, + interpolation: TInterpolation = TInterpolation.LINEAR, normalize: bool = True, _inner=None): super().__init__(string=string, instant_list=instant_list, lower_inc=lower_inc, upper_inc=upper_inc, - interpolation=interpolation, normalize=normalize, _inner=_inner) + expandable=expandable, interpolation=interpolation, normalize=normalize, _inner=_inner) class TGeogPointSeq(TPointSeq['TGeogPoint', 'TGeogPointInst', 'TGeogPointSeq', 'TGeogPointSeqSet'], TGeogPoint): ComponentClass = TGeogPointInst def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, TGeogPointInst]]] = None, - lower_inc: bool = True, upper_inc: bool = False, - interpolation: TInterpolation = TInterpolation.LINEAR, - normalize: bool = True, _inner=None): + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, + interpolation: TInterpolation = TInterpolation.LINEAR, normalize: bool = True, _inner=None): super().__init__(string=string, instant_list=instant_list, lower_inc=lower_inc, upper_inc=upper_inc, - interpolation=interpolation, normalize=normalize, _inner=_inner) + expandable=expandable, interpolation=interpolation, normalize=normalize, _inner=_inner) class TGeomPointSeqSet(TPointSeqSet['TGeomPoint', 'TGeomPointInst', 'TGeomPointSeq', 'TGeomPointSeqSet'], TGeomPoint): diff --git a/pymeos/pymeos/main/ttext.py b/pymeos/pymeos/main/ttext.py index 52897f4d..d2ec6384 100644 --- a/pymeos/pymeos/main/ttext.py +++ b/pymeos/pymeos/main/ttext.py @@ -258,10 +258,10 @@ class TTextSeq(TSequence[str, 'TText', 'TTextInst', 'TTextSeq', 'TTextSeqSet'], ComponentClass = TTextInst def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, TTextInst]]] = None, - lower_inc: bool = True, upper_inc: bool = False, + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, interpolation: TInterpolation = TInterpolation.STEPWISE, normalize: bool = True, _inner=None): super().__init__(string=string, instant_list=instant_list, lower_inc=lower_inc, upper_inc=upper_inc, - interpolation=interpolation, normalize=normalize, _inner=_inner) + expandable=expandable, interpolation=interpolation, normalize=normalize, _inner=_inner) class TTextSeqSet(TSequenceSet[str, 'TText', 'TTextInst', 'TTextSeq', 'TTextSeqSet'], TText): diff --git a/pymeos/pymeos/temporal/temporal.py b/pymeos/pymeos/temporal/temporal.py index f9117251..96bcf994 100644 --- a/pymeos/pymeos/temporal/temporal.py +++ b/pymeos/pymeos/temporal/temporal.py @@ -66,6 +66,10 @@ class Temporal(Generic[TBase, TG, TI, TS, TSS], ABC): _parse_function = None + @property + def _expandable(self) -> bool: + return False + @property def interpolation(self) -> TInterpolation: """ @@ -298,9 +302,11 @@ def to_dataframe(self) -> DataFrame: } return DataFrame(data).set_index(keys='time') - def append(self, instant: TInstant[TBase], expand: bool = False) -> TG: - new_temp = temporal_append_tinstant(self._inner, instant._inner, expand) - return Temporal._factory(new_temp) + def append(self, instant: TInstant[TBase]) -> TG: + new_inner = temporal_append_tinstant(self._inner, instant._inner, self._expandable) + if new_inner == self._inner: + return self + return Temporal._factory(new_inner) def merge(self, other: Union[Temporal[TBase], List[Temporal[TBase]]]) -> TG: if isinstance(other, Temporal): @@ -311,6 +317,33 @@ def merge(self, other: Union[Temporal[TBase], List[Temporal[TBase]]]) -> TG: raise TypeError(f'Operation not supported with type {other.__class__}') return Temporal._factory(new_temp) + def insert(self, other: TG, connect: bool = False) -> TG: + new_inner = temporal_insert(self._inner, other._inner, connect) + if new_inner == self._inner: + return self + return Temporal._factory(new_inner) + + def update(self, other: TG, connect: bool = False) -> TG: + new_inner = temporal_update(self._inner, other._inner, connect) + if new_inner == self._inner: + return self + return Temporal._factory(new_inner) + + def delete(self, other: Time, connect: bool = False) -> TG: + if isinstance(other, datetime): + new_inner = temporal_delete_timestamp(self._inner, datetime_to_timestamptz(other), connect) + elif isinstance(other, TimestampSet): + new_inner = temporal_delete_timestampset(self._inner, other._inner, connect) + elif isinstance(other, Period): + new_inner = temporal_delete_period(self._inner, other._inner, connect) + elif isinstance(other, PeriodSet): + new_inner = temporal_delete_periodset(self._inner, other._inner, connect) + else: + raise TypeError(f'Operation not supported with type {other.__class__}') + if new_inner == self._inner: + return self + return Temporal._factory(new_inner) + # TODO: Move to proper classes (Sequence[Set] with continuous base type) def to_linear(self: Self) -> Self: new_temp = temporal_step_to_linear(self._inner) diff --git a/pymeos/pymeos/temporal/tsequence.py b/pymeos/pymeos/temporal/tsequence.py index cff79836..df972916 100644 --- a/pymeos/pymeos/temporal/tsequence.py +++ b/pymeos/pymeos/temporal/tsequence.py @@ -47,7 +47,7 @@ class TSequence(Temporal[TBase, TG, TI, TS, TSS], ABC): """ def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[Union[str, Any]]] = None, - lower_inc: bool = True, upper_inc: bool = False, + lower_inc: bool = True, upper_inc: bool = False, expandable: Union[bool, int] = False, interpolation: TInterpolation = TInterpolation.LINEAR, normalize: bool = True, _inner=None): assert (_inner is not None) or ((string is not None) != (instant_list is not None)), \ "Either string must be not None or instant_list must be not" @@ -58,8 +58,19 @@ def __init__(self, string: Optional[str] = None, *, instant_list: Optional[List[ else: self._instants = [x._inner if isinstance(x, self.ComponentClass) else self.__class__._parse_function(x) for x in instant_list] - self._inner = tsequence_make(self._instants, len(self._instants), len(self._instants), lower_inc, upper_inc, - interpolation.value, normalize) + count = len(self._instants) + if not expandable: + self._inner = tsequence_make(self._instants, count, lower_inc, upper_inc, + interpolation.value, normalize) + else: + max_size = max(expandable, count) if isinstance(expandable, int) else 2 * count + self._inner = tsequence_make_exp(self._instants, count, max_size, lower_inc, upper_inc, + interpolation.value, normalize) + self._expandable_sequence = bool(expandable) or self._inner.maxcount > self._inner.count + + @property + def _expandable(self) -> bool: + return self._expandable_sequence @classmethod def from_instants(cls: Type[Self], instant_list: Optional[List[Union[str, Any]]],