Skip to content

Commit fba2fd0

Browse files
authored
Merge pull request #21 from tushushu/wip-constructors
Construction methods
2 parents 74f2609 + 545d897 commit fba2fd0

File tree

17 files changed

+371
-72
lines changed

17 files changed

+371
-72
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
runs-on: macos-latest
1717
strategy:
1818
matrix:
19-
python-version: ['3.6']
19+
python-version: ['3.7', '3.8', '3.9', '3.10']
2020
steps:
2121
- uses: actions/checkout@v2
2222
- uses: actions/setup-python@v2
@@ -47,7 +47,7 @@ jobs:
4747
runs-on: ubuntu-latest
4848
strategy:
4949
matrix:
50-
python-version: ['3.6']
50+
python-version: ['3.7', '3.8', '3.9', '3.10']
5151
steps:
5252
- uses: actions/checkout@v2
5353
- uses: actions/setup-python@v2

.github/workflows/sphinx.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
name: "Pull Request Docs Check"
1+
name: "doc"
22
on:
3+
- pull_request
34
- release
45

56
jobs:
67
docs:
78
runs-on: macos-latest
89
strategy:
910
matrix:
10-
python-version: ['3.6']
11+
python-version: ['3.8']
1112
steps:
1213
- uses: actions/checkout@v2
1314
- uses: actions/setup-python@v2

README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# ulist
22

3-
![PyPI](https://img.shields.io/pypi/v/ulist)
4-
![License](https://img.shields.io/github/license/tushushu/ulist)
5-
![CI](https://github.com/tushushu/ulist/workflows/CI/badge.svg)
3+
[![PyPI](https://img.shields.io/pypi/v/ulist)](https://pypi.org/project/ulist/)
4+
[![License](https://img.shields.io/github/license/tushushu/ulist)](https://github.com/tushushu/ulist/blob/main/LICENSE)
5+
[![CI](https://github.com/tushushu/ulist/workflows/CI/badge.svg)](https://github.com/tushushu/ulist/actions/workflows/main.yml)
6+
[![doc](https://github.com/tushushu/ulist/workflows/doc/badge.svg)](https://github.com/tushushu/ulist/actions/workflows/sphinx.yml)
67
![PyPI - Format](https://img.shields.io/pypi/format/ulist)
7-
![Code Style](https://img.shields.io/badge/code%20style-flake8-blue)
8+
[![Code Style](https://img.shields.io/badge/code%20style-flake8-blue)](https://github.com/PyCQA/flake8)
89

910

1011
[**Documentation**](https://tushushu.github.io/ulist/) | [**Source Code**](https://github.com/tushushu/ulist)
@@ -15,7 +16,7 @@ Ulist is an ultra fast list data structures written in Rust with Python bindings
1516

1617

1718
### Requirements
18-
Python: 3.6+
19+
Python: 3.7+
1920
OS: Linux or MacOS
2021

2122

@@ -29,7 +30,7 @@ Run `pip install ulist`
2930
```Python
3031
import ulist as ul
3132

32-
arr = ul.from_iter([1.0, 2.0, 3.0, 2.0, 4.0, 5.0], dtype="float")
33+
arr = ul.from_seq([1.0, 2.0, 3.0, 2.0, 4.0, 5.0], dtype="float")
3334
result = arr.unique().mean()
3435
print(result)
3536
```
@@ -39,8 +40,8 @@ print(result)
3940
```Python
4041
import ulist as ul
4142

42-
arr1 = ul.from_iter(range(1, 4), dtype="int")
43-
arr2 = ul.from_iter(range(1, 4), dtype="int")
43+
arr1 = ul.arange(1, 4)
44+
arr2 = ul.arange(1, 4)
4445
result = arr1.mul(arr2).sum()
4546
print(result)
4647
```
@@ -50,7 +51,7 @@ print(result)
5051
```Python
5152
import ulist as ul
5253

53-
arr = ul.from_iter([1, 2, 3, 4, 5], dtype="float")
54+
arr = ul.from_seq([1, 2, 3, 4, 5], dtype="float")
5455
result = arr.sub_scala(arr.mean()).to_list()
5556
print(result)
5657
```
@@ -60,7 +61,7 @@ print(result)
6061
```Python
6162
import ulist as ul
6263

63-
arr = ul.from_iter([1, 2, 3], dtype="float")
64+
arr = ul.from_seq([1, 2, 3], dtype="float")
6465
result = ((arr - arr.mean()) ** 2).mean()
6566
print(result)
6667
```
@@ -72,7 +73,9 @@ print(result)
7273
* `zip` method for `List` and `ListPair` class
7374
* `agg` method for `ListPair`
7475
* `StringList` or `BytesList` class
75-
* More construction methods for `List`, such as `from_str`, `from_iter` `zeros`, `ones`, `empty` and `arrange`
76+
* Support Windows users
77+
* Doctest to check examples in Docstrings
78+
* Inplace and non-inplace mode
7679
* Support missing values
7780
* Airspeed velocity to benchmark
7881
* Further optimizing the benchmark with SIMD

tests/test_base.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,40 @@
1313
"test_method, dtype, nums, expected_value",
1414
[
1515
(
16-
"__str__",
16+
"__repr__",
1717
"bool",
1818
[True, False] * 50,
1919
"UltraFastList([True, False, True, ..., False, True, False])",
2020
),
21-
('__str__', 'bool', [True, False], 'UltraFastList([True, False])'),
22-
('__str__', 'float', [1.0, 2.0], 'UltraFastList([1.0, 2.0])'),
23-
('__str__', 'float', range(0, 100),
21+
('__repr__', 'bool', [True, False], 'UltraFastList([True, False])'),
22+
('__repr__', 'float', [1.0, 2.0], 'UltraFastList([1.0, 2.0])'),
23+
('__repr__', 'float', range(0, 100),
2424
'UltraFastList([0.0, 1.0, 2.0, ..., 97.0, 98.0, 99.0])'),
25-
('__str__', 'int', [1, 2], 'UltraFastList([1, 2])'),
26-
('__str__', 'int', range(0, 100),
25+
('__repr__', 'int', [1, 2], 'UltraFastList([1, 2])'),
26+
('__repr__', 'int', range(0, 100),
2727
'UltraFastList([0, 1, 2, ..., 97, 98, 99])'),
28+
29+
(
30+
"__str__",
31+
"bool",
32+
[True, False] * 50,
33+
"[True, False, True, ..., False, True, False]",
34+
),
35+
('__str__', 'bool', [True, False], '[True, False]'),
36+
('__str__', 'float', [1.0, 2.0], '[1.0, 2.0]'),
37+
('__str__', 'float', range(0, 100),
38+
'[0.0, 1.0, 2.0, ..., 97.0, 98.0, 99.0]'),
39+
('__str__', 'int', [1, 2], '[1, 2]'),
40+
('__str__', 'int', range(0, 100), '[0, 1, 2, ..., 97, 98, 99]'),
41+
2842
('copy', 'bool', [True, False], [True, False]),
2943
('copy', 'float', [1.0, 2.0], [1.0, 2.0]),
3044
('copy', 'int', [1, 2], [1, 2]),
45+
3146
('size', 'bool', [True, False], 2),
3247
('size', 'float', [1.0, 2.0], 2),
3348
('size', 'int', [1, 2], 2),
49+
3450
('to_list', 'bool', [True, False], [True, False]),
3551
('to_list', 'bool', [True, False], [True, False]),
3652
('to_list', 'float', [1.0, 2.0], [1.0, 2.0]),
@@ -43,7 +59,7 @@ def test_methods_no_arg(
4359
nums: LIST_TYPE,
4460
expected_value: NUM_OR_LIST_TYPE,
4561
):
46-
arr = ul.from_iter(nums, dtype)
62+
arr = ul.from_seq(nums, dtype)
4763
result = getattr(arr, test_method)()
4864
check_test_result(dtype, test_method, result, expected_value)
4965

@@ -54,6 +70,7 @@ def test_methods_no_arg(
5470
('__getitem__', 'bool', [True, False, True], True, {'index': 2}),
5571
('__getitem__', 'float', [1.0, 2.0, 3.0], 2.0, {'index': 1}),
5672
('__getitem__', 'int', [1, 2, 3], 1, {'index': 0}),
73+
5774
('get', 'bool', [True, False, True], True, {'index': 2}),
5875
('get', 'float', [1.0, 2.0, 3.0], 2.0, {'index': 1}),
5976
('get', 'int', [1, 2, 3], 1, {'index': 0}),
@@ -66,7 +83,7 @@ def test_methods_with_args(
6683
expected_value: NUM_OR_LIST_TYPE,
6784
kwargs: dict,
6885
):
69-
arr = ul.from_iter(nums, dtype)
86+
arr = ul.from_seq(nums, dtype)
7087
result = getattr(arr, test_method)(**kwargs)
7188
check_test_result(dtype, test_method, result, expected_value)
7289

@@ -79,17 +96,21 @@ def test_methods_with_args(
7996
('__setitem__', 'float', [1.0, 2.0], [
8097
1.0, 3.0], {'index': 1, 'num': 3.0}),
8198
('__setitem__', 'int', [1, 2], [1, 3], {'index': 1, 'num': 3}),
99+
82100
('append', 'bool', [True], [True, False], {'num': False}),
83101
('append', 'float', [1.0], [1.0, 2.0], {'num': 2.0}),
84102
('append', 'int', [1], [1, 2], {'num': 2}),
103+
85104
('pop', 'bool', [True, False], [True], {}),
86105
('pop', 'float', [1.0, 2.0], [1.0], {}),
87106
('pop', 'int', [1, 2], [1], {}),
107+
88108
('replace', 'bool', [True, False, True], [
89109
False, False, False], {'old': True, 'new': False}),
90110
('replace', 'float', [1.0, 0.0, 1.0], [
91111
0.0, 0.0, 0.0], {'old': 1.0, 'new': 0.0}),
92112
('replace', 'int', [1, 0, 1], [0, 0, 0], {'old': 1, 'new': 0}),
113+
93114
('set', 'bool', [True, False], [
94115
True, True], {'index': 1, 'num': True}),
95116
('set', 'float', [1.0, 2.0], [1.0, 3.0], {'index': 1, 'num': 3.0}),
@@ -103,7 +124,7 @@ def test_multable_methods(
103124
expected_value: LIST_TYPE,
104125
kwargs: dict,
105126
):
106-
arr = ul.from_iter(nums, dtype)
127+
arr = ul.from_seq(nums, dtype)
107128
getattr(arr, test_method)(**kwargs)
108129
check_test_result(dtype, test_method, arr, expected_value)
109130

@@ -126,7 +147,7 @@ def test_indexing_operations(
126147
num = kwargs["num"]
127148
# Set
128149
test_method = "set-item"
129-
arr = ul.from_iter(nums, dtype)
150+
arr = ul.from_seq(nums, dtype)
130151
arr[index] = num
131152
check_test_result(dtype, test_method, arr, expected_value)
132153

tests/test_boolean.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
[True, True, True],
2525
True,
2626
),
27+
2728
(
2829
"any",
2930
[False, False, False],
@@ -39,6 +40,7 @@
3940
[True, True, True],
4041
True,
4142
),
43+
4244
(
4345
"not_",
4446
[True, False],
@@ -52,7 +54,7 @@ def test_methods(
5254
expected_value: Union[bool, List[bool]],
5355
):
5456
dtype = "bool"
55-
arr = ul.from_iter(nums, dtype=dtype)
57+
arr = ul.from_seq(nums, dtype=dtype)
5658
result = getattr(arr, test_method)()
5759
check_test_result(dtype, test_method, result, expected_value)
5860

@@ -81,8 +83,8 @@ def test_methods_with_args(
8183
expected_value: List[bool],
8284
):
8385
dtype = "bool"
84-
arr1 = ul.from_iter(nums, dtype=dtype)
85-
arr2 = ul.from_iter(other, dtype=dtype)
86+
arr1 = ul.from_seq(nums, dtype=dtype)
87+
arr2 = ul.from_seq(other, dtype=dtype)
8688
result = getattr(arr1, test_method)(arr2)
8789
check_test_result(dtype, test_method, result, expected_value)
8890

@@ -117,9 +119,9 @@ def test_operators(
117119
expected_value: List[bool],
118120
):
119121
dtype = "bool"
120-
arr1 = ul.from_iter(nums, dtype)
122+
arr1 = ul.from_seq(nums, dtype)
121123
if other is not None:
122-
arr2 = ul.from_iter(other, dtype)
124+
arr2 = ul.from_seq(other, dtype)
123125
result = test_method(arr1, arr2)
124126
else:
125127
result = test_method(arr1)

tests/test_constructor.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Callable, List
2+
3+
import pytest
4+
import ulist as ul
5+
from ulist.utils import check_test_result
6+
7+
8+
@pytest.mark.parametrize(
9+
"test_method, args, kwargs, expected_value",
10+
[
11+
(ul.arange, (3,), {}, [0, 1, 2],),
12+
(ul.arange, (0, 3,), {}, [0, 1, 2],),
13+
(ul.arange, (0, 4, 2,), {}, [0, 2],),
14+
(ul.arange, (0, 5, 2,), {}, [0, 2, 4],),
15+
(ul.arange, (), {"start": 3}, [0, 1, 2],),
16+
(ul.arange, (), {"start": 0, "stop": 3}, [0, 1, 2],),
17+
(ul.arange, (), {"start": 5, "step": 2}, [0, 2, 4],),
18+
(ul.arange, (), {"start": 0, "stop": 5, "step": 2}, [0, 2, 4],),
19+
(ul.arange, (5,), {"step": 2}, [0, 2, 4],),
20+
(ul.arange, (0,), {"stop": 5, "step": 2}, [0, 2, 4],),
21+
22+
(ul.repeat, (0, 3), {}, [0, 0, 0],),
23+
(ul.repeat, (1.0, 3), {}, [1.0, 1.0, 1.0],),
24+
(ul.repeat, (False, 3), {}, [False, False, False],),
25+
26+
(ul.from_seq, (range(3), "int"), {}, [0, 1, 2],),
27+
(ul.from_seq, (range(3), "float"), {}, [0.0, 1.0, 2.0],),
28+
(ul.from_seq, ([False, True], "bool"), {}, [False, True],),
29+
(ul.from_seq, ([0, 1, 2], "int"), {}, [0, 1, 2],),
30+
(ul.from_seq, ((0, 1, 2), "int"), {}, [0, 1, 2],),
31+
32+
(ul.cycle, (range(3), 1, 'int'), {}, [0],),
33+
(ul.cycle, (range(3), 2, 'int'), {}, [0, 1],),
34+
(ul.cycle, (range(3), 3, 'int'), {}, [0, 1, 2],),
35+
(ul.cycle, (range(3), 4, 'int'), {}, [0, 1, 2, 0],),
36+
(ul.cycle, (range(3), 5, 'int'), {}, [0, 1, 2, 0, 1],),
37+
(ul.cycle, (range(3), 6, 'int'), {}, [0, 1, 2, 0, 1, 2],),
38+
(ul.cycle, ([0.0, 1.0], 3, 'float'), {}, [0.0, 1.0, 0.0],),
39+
(ul.cycle, ((True, False), 3, 'bool'), {}, [True, False, True],),
40+
41+
],
42+
)
43+
def test_constructors(
44+
test_method: Callable,
45+
args: tuple,
46+
kwargs: dict,
47+
expected_value: List[bool],
48+
):
49+
result = test_method(*args, **kwargs)
50+
if type(expected_value[0]) == int:
51+
dtype = "int"
52+
elif type(expected_value[0]) == float:
53+
dtype = "float"
54+
elif type(expected_value[0]) == bool:
55+
dtype = "bool"
56+
else:
57+
raise TypeError(f"Unexpected type {type(expected_value[0])}!")
58+
check_test_result(dtype, test_method, result, expected_value)

tests/test_integer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def test_methods(
4242
kwargs: dict,
4343
):
4444
dtype = "int"
45-
arr = ul.from_iter(nums, dtype=dtype)
45+
arr = ul.from_seq(nums, dtype=dtype)
4646
result = getattr(arr, test_method)(**kwargs).to_list()
4747
check_test_result(dtype, test_method, result, expected_value)
4848

@@ -63,9 +63,9 @@ def test_operators(
6363
kwargs: dict,
6464
):
6565
dtype = "int"
66-
arr = ul.from_iter(nums, dtype)
66+
arr = ul.from_seq(nums, dtype)
6767
if isinstance(kwargs["other"], list):
68-
other = ul.from_iter(kwargs["other"], dtype)
68+
other = ul.from_seq(kwargs["other"], dtype)
6969
else:
7070
other = kwargs["other"]
7171
result = test_method(arr, other)

0 commit comments

Comments
 (0)