Skip to content

Commit 5ed63a5

Browse files
authored
Merge pull request #148 from tushushu/wip-i64-f64
Support int64 and f64 types
2 parents 878ee29 + f9a70d9 commit 5ed63a5

24 files changed

+1202
-216
lines changed

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
author = 'tushushu'
2323

2424
# The full version, including alpha/beta/rc tags
25-
release = '0.8.0'
25+
release = '0.9.0'
2626

2727

2828
# -- General configuration ---------------------------------------------------

tests/test_base.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
import operator as op
44
import pytest
55
import ulist as ul
6-
from ulist.utils import check_test_result
6+
from ulist.utils import check_test_result, compare_dtypes, expand_dtypes
77

88
ELEM_TYPE = Union[float, int, bool, str]
99
LIST_TYPE = Union[List[float], List[int], List[bool], List[str]]
1010
COUNTER = Union[Dict[int, int], Dict[bool, int]]
1111
RESULT = Union[ELEM_TYPE, LIST_TYPE, COUNTER]
1212

1313

14+
@expand_dtypes
1415
@pytest.mark.parametrize(
1516
"test_method, dtype, nums, expected_value",
1617
[
@@ -125,6 +126,7 @@ def test_methods_no_arg(
125126
check_test_result(dtype, test_method, result, expected_value)
126127

127128

129+
@expand_dtypes
128130
@pytest.mark.parametrize(
129131
"test_method, dtype, nums, expected_value, kwargs",
130132
[
@@ -150,21 +152,6 @@ def test_methods_no_arg(
150152
("apply", "string", ['foo', 'bar'], [
151153
True, False], {"fn": lambda x: x != 'bar'},),
152154
153-
('astype', 'bool', [True, False], [1, 0], {'dtype': 'int'}),
154-
('astype', 'bool', [True, False], [1.0, 0.0], {'dtype': 'float'}),
155-
('astype', 'bool', [True, False], [True, False], {'dtype': 'bool'}),
156-
157-
('astype', 'float', [1.0, 2.0, 3.0], [1, 2, 3], {'dtype': 'int'}),
158-
('astype', 'float', [1.0, 2.0, 3.0], [
159-
1.0, 2.0, 3.0], {'dtype': 'float'}),
160-
('astype', 'float', [-2.0, -1.0, 0.0, 1.0, 2.0],
161-
[True, True, False, True, True], {'dtype': 'bool'}),
162-
163-
('astype', 'int', [1, 2, 3], [1, 2, 3], {'dtype': 'int'}),
164-
('astype', 'int', [1, 2, 3], [1.0, 2.0, 3.0], {'dtype': 'float'}),
165-
('astype', 'int', [-2, -1, 0, 1, 2],
166-
[True, True, False, True, True], {'dtype': 'bool'}),
167-
168155
("equal_scala", 'bool', [True, False], [False, True], {"elem": False}),
169156
("equal_scala", 'float', [1.0, 2.0, 3.0],
170157
[False, True, False], {"elem": 2.0}),
@@ -335,6 +322,7 @@ def test_methods_with_args(
335322
check_test_result(dtype, test_method, result, expected_value)
336323

337324

325+
@expand_dtypes
338326
@pytest.mark.parametrize(
339327
"test_method, dtype, nums, expected_value, kwargs",
340328
[
@@ -376,6 +364,7 @@ def test_multable_methods(
376364
check_test_result(dtype, test_method, arr, expected_value)
377365

378366

367+
@expand_dtypes
379368
@pytest.mark.parametrize(
380369
"dtype, nums, expected_value, kwargs",
381370
[
@@ -407,6 +396,7 @@ def test_indexing_operations(
407396
check_test_result(dtype, test_method, result, expected_value)
408397

409398

399+
@expand_dtypes
410400
@pytest.mark.parametrize(
411401
"dtype, nums, expected_value, expected_dtype",
412402
[
@@ -451,7 +441,7 @@ def test_astype(
451441
result = arr.astype(expected_dtype)
452442
test_method = f"astype {expected_dtype}"
453443
check_test_result(dtype, test_method, result, expected_value)
454-
assert result.dtype == expected_dtype
444+
assert compare_dtypes(result.dtype, expected_dtype)
455445
assert id(result) != id(arr)
456446

457447
# Cast back to origin type
@@ -461,6 +451,7 @@ def test_astype(
461451
check_test_result(expected_dtype, test_method, arr1, arr.to_list())
462452

463453

454+
@expand_dtypes
464455
@pytest.mark.parametrize(
465456
"test_method, dtype, nums, expected_value, kwargs",
466457
[

tests/test_constructors.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"test_method, args, kwargs, expected_value",
1010
[
1111
(ul.arange, (3,), {}, [0, 1, 2],),
12+
(ul.arange, (3,), {"dtype": "int"}, [0, 1, 2],),
13+
(ul.arange, (3,), {"dtype": "int32"}, [0, 1, 2],),
14+
(ul.arange, (3,), {"dtype": "int64"}, [0, 1, 2],),
15+
1216
(ul.arange, (0, 3,), {}, [0, 1, 2],),
1317
(ul.arange, (0, 4, 2,), {}, [0, 2],),
1418
(ul.arange, (0, 5, 2,), {}, [0, 2, 4],),
@@ -26,6 +30,8 @@
2630
2731
(ul.from_seq, (range(3), "float"), {}, [0.0, 1.0, 2.0],),
2832
(ul.from_seq, (range(3), "int"), {}, [0, 1, 2],),
33+
(ul.from_seq, (range(3), "int32"), {}, [0, 1, 2],),
34+
(ul.from_seq, (range(3), "int64"), {}, [0, 1, 2],),
2935
3036
(ul.from_seq, ([False, True], "bool"), {}, [False, True],),
3137
(ul.from_seq, ([0.0, 1.0, 2.0], "float"), {}, [0.0, 1.0, 2.0],),
@@ -38,6 +44,8 @@
3844
(ul.from_seq, (('foo', 'bar'), "string"), {}, ['foo', 'bar'],),
3945
4046
(ul.cycle, (range(3), 1, 'float'), {}, [0.0],),
47+
(ul.cycle, (range(3), 1, 'int32'), {}, [0],),
48+
(ul.cycle, (range(3), 1, 'int64'), {}, [0],),
4149
(ul.cycle, (range(3), 1, 'int'), {}, [0],),
4250
(ul.cycle, (range(3), 2, 'int'), {}, [0, 1],),
4351
(ul.cycle, (range(3), 3, 'int'), {}, [0, 1, 2],),

tests/test_control_flow.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import pytest
44
import ulist as ul
55
import operator as op
6-
from ulist.utils import check_test_result
6+
from ulist.utils import check_test_result, expand_dtypes
77

88
LIST_TYPE = Union[List[float], List[int], List[bool], List[str]]
99

1010

11+
@expand_dtypes
1112
@pytest.mark.parametrize(
1213
"dtype, nums, kwargs, expected_value",
1314
[
@@ -200,6 +201,7 @@ def test_select(
200201
check_test_result(result_dtype, "ul.select", result, expected_value)
201202

202203

204+
@expand_dtypes
203205
@pytest.mark.parametrize(
204206
"dtype, nums, kwargs, expected_value",
205207
[

tests/test_numerical.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
import pytest
55
import ulist as ul
6-
from ulist.utils import check_test_result
6+
from ulist.utils import check_test_result, expand_dtypes
77

88
NUM_TYPE = Union[float, int]
99
LIST_TYPE = Union[List[float], List[int]]
1010

1111

12+
@expand_dtypes
1213
@pytest.mark.parametrize(
1314
"test_method, dtype, nums, expected_value",
1415
[
@@ -44,6 +45,7 @@ def test_statistics_methods(
4445
check_test_result(dtype, test_method, result, expected_value)
4546

4647

48+
@expand_dtypes
4749
@pytest.mark.parametrize(
4850
"test_method, dtype, nums, expected_value, kwargs",
4951
[
@@ -130,6 +132,7 @@ def test_arithmetic_methods(
130132
check_test_result(dtype, test_method, result, expected_value)
131133

132134

135+
@expand_dtypes
133136
@pytest.mark.parametrize(
134137
"test_method, dtype, nums, expected_value, kwargs",
135138
[

ulist/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ulist"
3-
version = "0.8.0"
3+
version = "0.9.0"
44
authors = ["tushushu"]
55
edition = "2018"
66

ulist/python/ulist/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from .core import UltraFastList # noqa:F401
44
from .ulist import IndexList # noqa:F401
55

6-
__version__ = "0.8.0"
6+
__version__ = "0.9.0"

ulist/python/ulist/constructor.py

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
from typing import Optional, Sequence
22

33
from .core import UltraFastList
4-
from .ulist import BooleanList, FloatList, IntegerList, StringList
5-
from .ulist import arange as _arange
64
from .typedef import ELEM
7-
8-
9-
def arange(start: int, stop: Optional[int] = None, step: int = 1
10-
) -> UltraFastList:
5+
from .ulist import (
6+
BooleanList,
7+
FloatList32,
8+
FloatList64,
9+
IntegerList32,
10+
IntegerList64,
11+
StringList,
12+
arange32,
13+
arange64
14+
)
15+
16+
17+
def arange(
18+
start: int,
19+
stop: Optional[int] = None,
20+
step: int = 1,
21+
dtype: str = 'int',
22+
) -> UltraFastList:
1123
"""Return evenly spaced values within a given interval, which is similar
1224
to the Python built-in range function, but returns an ulist rather than
1325
a list.
@@ -21,6 +33,8 @@ def arange(start: int, stop: Optional[int] = None, step: int = 1
2133
Defaults to None.
2234
step (int, optional):
2335
Spacing between values. Defaults to 1.
36+
dtype (str, optional):
37+
The type of the output ulist. 'int', 'int32', 'int64'.
2438
2539
Returns:
2640
UltraFastList: A ulist object.
@@ -43,7 +57,13 @@ def arange(start: int, stop: Optional[int] = None, step: int = 1
4357
if stop is None:
4458
stop = start
4559
start = 0
46-
return UltraFastList(_arange(start=start, stop=stop, step=step))
60+
if dtype == "int" or dtype == "int64":
61+
return UltraFastList(arange64(start=start, stop=stop, step=step))
62+
elif dtype == "int32":
63+
return UltraFastList(arange32(start=start, stop=stop, step=step))
64+
else:
65+
raise ValueError(
66+
"Parameter dtype should be 'int', 'int32' or 'int64'!")
4767

4868

4969
def cycle(obj: Sequence, size: int, dtype: str) -> UltraFastList:
@@ -55,11 +75,13 @@ def cycle(obj: Sequence, size: int, dtype: str) -> UltraFastList:
5575
size (int):
5676
size (int): Size of the new ulist.
5777
dtype (str):
58-
The type of the output ulist. 'int', 'float', 'bool' or 'string'.
78+
The type of the output ulist. 'int', 'int32', 'int64',
79+
'float', 'float32', 'float64', 'bool' or 'string'.
5980
6081
Raises:
6182
ValueError:
62-
Parameter dtype should be 'int', 'float', 'bool' or 'string'!
83+
Parameter dtype should be 'int', 'int32', 'int64',
84+
'float', 'float32', 'float64', 'bool' or 'string'!
6385
6486
Returns:
6587
UltraFastList: A ulist object.
@@ -83,17 +105,23 @@ def cycle(obj: Sequence, size: int, dtype: str) -> UltraFastList:
83105
>>> arr4
84106
UltraFastList(['foo', 'foo', 'foo'])
85107
"""
86-
if dtype == "int":
87-
result = UltraFastList(IntegerList.cycle(obj, size))
88-
elif dtype == "float":
89-
result = UltraFastList(FloatList.cycle(obj, size))
108+
if dtype == "int" or dtype == "int64":
109+
result = UltraFastList(IntegerList64.cycle(obj, size))
110+
elif dtype == "int32":
111+
result = UltraFastList(IntegerList32.cycle(obj, size))
112+
elif dtype == "float" or dtype == "float64":
113+
result = UltraFastList(FloatList64.cycle(obj, size))
114+
elif dtype == "float32":
115+
result = UltraFastList(FloatList32.cycle(obj, size))
90116
elif dtype == "bool":
91117
result = UltraFastList(BooleanList.cycle(obj, size))
92118
elif dtype == "string":
93119
result = UltraFastList(StringList.cycle(obj, size))
94120
else:
95121
raise ValueError(
96-
"Parameter dtype should be 'int', 'float', 'bool' or 'string'!")
122+
"Parameter dtype should be 'int', 'int32', 'int64', " +
123+
"'float', 'float32', 'float64', 'bool' or 'string'!"
124+
)
97125
return result
98126

99127

@@ -104,11 +132,13 @@ def from_seq(obj: Sequence, dtype: str) -> UltraFastList:
104132
obj (Sequence):
105133
Sequence object such as list, tuple and range.
106134
dtype (str):
107-
The type of the output ulist. 'int', 'float', 'bool' or 'string'.
135+
The type of the output ulist. 'int', 'int32', 'int64',
136+
'float', 'float32', 'float64', 'bool' or 'string'.
108137
109138
Raises:
110139
ValueError:
111-
Parameter dtype should be 'int', 'float', 'bool' or 'string'!
140+
Parameter dtype should be 'int', 'int32', 'int64',
141+
'float', 'float32', 'float64', 'bool' or 'string'!
112142
113143
Returns:
114144
UltraFastList: A ulist object.
@@ -132,17 +162,23 @@ def from_seq(obj: Sequence, dtype: str) -> UltraFastList:
132162
>>> arr4
133163
UltraFastList(['foo', 'bar', 'baz'])
134164
"""
135-
if dtype == "int":
136-
result = UltraFastList(IntegerList(obj))
137-
elif dtype == "float":
138-
result = UltraFastList(FloatList(obj))
165+
if dtype == "int" or dtype == "int64":
166+
result = UltraFastList(IntegerList64(obj))
167+
elif dtype == "int32":
168+
result = UltraFastList(IntegerList32(obj))
169+
elif dtype == "float" or dtype == "float64":
170+
result = UltraFastList(FloatList64(obj))
171+
elif dtype == "float32":
172+
result = UltraFastList(FloatList32(obj))
139173
elif dtype == "bool":
140174
result = UltraFastList(BooleanList(obj))
141175
elif dtype == "string":
142176
result = UltraFastList(StringList(obj))
143177
else:
144178
raise ValueError(
145-
"Parameter dtype should be 'int', 'float', 'bool' or 'string'!")
179+
"Parameter dtype should be 'int', 'int32', 'int64', " +
180+
"'float', 'float32', 'float64', 'bool' or 'string'!"
181+
)
146182
return result
147183

148184

@@ -181,9 +217,9 @@ def repeat(elem: ELEM, size: int) -> UltraFastList:
181217
if isinstance(elem, bool):
182218
return UltraFastList(BooleanList.repeat(elem, size))
183219
elif isinstance(elem, float):
184-
return UltraFastList(FloatList.repeat(elem, size))
220+
return UltraFastList(FloatList64.repeat(elem, size))
185221
elif isinstance(elem, int):
186-
return UltraFastList(IntegerList.repeat(elem, size))
222+
return UltraFastList(IntegerList64.repeat(elem, size))
187223
elif isinstance(elem, str):
188224
return UltraFastList(StringList.repeat(elem, size))
189225
else:

0 commit comments

Comments
 (0)