|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
|
|
3
|
|
|
"""Helpers to fit scalar values into sequences. |
|
4
|
|
|
|
|
5
|
|
|
SPDX-FileCopyrightText: Uwe Krien <[email protected]> |
|
6
|
|
|
SPDX-FileCopyrightText: Simon Hilpert |
|
7
|
|
|
SPDX-FileCopyrightText: Cord Kaldemeyer |
|
8
|
|
|
SPDX-FileCopyrightText: henhuy |
|
9
|
|
|
|
|
10
|
|
|
SPDX-License-Identifier: MIT |
|
11
|
|
|
|
|
12
|
|
|
""" |
|
13
|
|
|
|
|
14
|
|
|
from collections import abc |
|
15
|
|
|
from itertools import repeat |
|
16
|
|
|
|
|
17
|
|
|
import numpy as np |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
def sequence(iterable_or_scalar): |
|
21
|
|
|
"""Checks if an object is iterable (except string) or scalar and returns |
|
22
|
|
|
the original sequence if object is an iterable and an 'emulated' |
|
23
|
|
|
sequence object of class _Sequence if object is a scalar or string. |
|
24
|
|
|
|
|
25
|
|
|
Parameters |
|
26
|
|
|
---------- |
|
27
|
|
|
iterable_or_scalar : iterable or None or int or float |
|
28
|
|
|
|
|
29
|
|
|
Examples |
|
30
|
|
|
-------- |
|
31
|
|
|
>>> y = sequence([1,2,3,4,5,6,7,8,9,10,11]) |
|
32
|
|
|
>>> y[0] |
|
33
|
|
|
1 |
|
34
|
|
|
|
|
35
|
|
|
>>> y[10] |
|
36
|
|
|
11 |
|
37
|
|
|
|
|
38
|
|
|
>>> import pandas as pd |
|
39
|
|
|
>>> s1 = sequence(pd.Series([1,5,9])) |
|
40
|
|
|
>>> s1[2] |
|
41
|
|
|
9 |
|
42
|
|
|
|
|
43
|
|
|
>>> x = sequence(10) |
|
44
|
|
|
>>> x[0] |
|
45
|
|
|
10 |
|
46
|
|
|
|
|
47
|
|
|
>>> x[10] |
|
48
|
|
|
10 |
|
49
|
|
|
|
|
50
|
|
|
""" |
|
51
|
|
|
if isinstance(iterable_or_scalar, str): |
|
52
|
|
|
return iterable_or_scalar |
|
53
|
|
|
elif isinstance(iterable_or_scalar, abc.Iterable): |
|
54
|
|
|
return np.array(iterable_or_scalar) |
|
55
|
|
|
else: |
|
56
|
|
|
return _FakeSequence(value=iterable_or_scalar) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
class _FakeSequence: |
|
60
|
|
|
"""Emulates a list whose length is not known in advance. |
|
61
|
|
|
|
|
62
|
|
|
Parameters |
|
63
|
|
|
---------- |
|
64
|
|
|
value : scalar |
|
65
|
|
|
length : integer |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
Examples |
|
69
|
|
|
-------- |
|
70
|
|
|
>>> s = _FakeSequence(value=42, length=5) |
|
71
|
|
|
>>> s |
|
72
|
|
|
[42, 42, 42, 42, 42] |
|
73
|
|
|
>>> s = _FakeSequence(value=42) |
|
74
|
|
|
>>> # undefined lenght, access still works |
|
75
|
|
|
>>> s[1337] |
|
76
|
|
|
42 |
|
77
|
|
|
""" |
|
78
|
|
|
|
|
79
|
|
|
def __init__(self, value, length=None): |
|
80
|
|
|
self._value = value |
|
81
|
|
|
self._length = length |
|
82
|
|
|
|
|
83
|
|
|
@property |
|
84
|
|
|
def size(self): |
|
85
|
|
|
return self._length |
|
86
|
|
|
|
|
87
|
|
|
@size.setter |
|
88
|
|
|
def size(self, value): |
|
89
|
|
|
self._length = value |
|
90
|
|
|
|
|
91
|
|
|
def __getitem__(self, _): |
|
92
|
|
|
return self._value |
|
93
|
|
|
|
|
94
|
|
|
def __repr__(self): |
|
95
|
|
|
if self._length is not None: |
|
96
|
|
|
return str([i for i in self]) |
|
97
|
|
|
else: |
|
98
|
|
|
return f"[{self._value}, {self._value}, ..., {self._value}]" |
|
99
|
|
|
|
|
100
|
|
|
def __len__(self): |
|
101
|
|
|
return self._length |
|
102
|
|
|
|
|
103
|
|
|
def __iter__(self): |
|
104
|
|
|
return repeat(self._value, self._length) |
|
105
|
|
|
|
|
106
|
|
|
def max(self): |
|
107
|
|
|
return self._value |
|
108
|
|
|
|
|
109
|
|
|
def min(self): |
|
110
|
|
|
return self._value |
|
111
|
|
|
|
|
112
|
|
|
def sum(self): |
|
113
|
|
|
if self._length is None: |
|
114
|
|
|
return np.inf |
|
115
|
|
|
else: |
|
116
|
|
|
return self._length * self._value |
|
117
|
|
|
|
|
118
|
|
|
def to_numpy(self, length=None): |
|
119
|
|
|
if length is not None: |
|
120
|
|
|
return np.full(length, self._value) |
|
121
|
|
|
else: |
|
122
|
|
|
return np.full(len(self), self._value) |
|
123
|
|
|
|
|
124
|
|
|
@property |
|
125
|
|
|
def value(self): |
|
126
|
|
|
return self._value |
|
127
|
|
|
|