Skip to content

Commit

Permalink
Add streaming operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Diviloper committed Nov 9, 2022
1 parent d03d209 commit 11942b2
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pymeos/pymeos/main/tbool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pymeos/pymeos/main/tfloat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pymeos/pymeos/main/tint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions pymeos/pymeos/main/tpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions pymeos/pymeos/main/ttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
39 changes: 36 additions & 3 deletions pymeos/pymeos/temporal/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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):
Expand All @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions pymeos/pymeos/temporal/tsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]]],
Expand Down

0 comments on commit 11942b2

Please sign in to comment.