bigarraylist 0.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bigarraylist-0.2.0/LICENSE +21 -0
- bigarraylist-0.2.0/PKG-INFO +158 -0
- bigarraylist-0.2.0/README.md +134 -0
- bigarraylist-0.2.0/bigarraylist/__init__.py +70 -0
- bigarraylist-0.2.0/bigarraylist/_api.py +453 -0
- bigarraylist-0.2.0/bigarraylist/algorithms/__init__.py +22 -0
- bigarraylist-0.2.0/bigarraylist/algorithms/bucket.py +200 -0
- bigarraylist-0.2.0/bigarraylist/algorithms/external_sort.py +200 -0
- bigarraylist-0.2.0/bigarraylist/algorithms/parallel.py +68 -0
- bigarraylist-0.2.0/bigarraylist/algorithms/sample.py +51 -0
- bigarraylist-0.2.0/bigarraylist/config.py +86 -0
- bigarraylist-0.2.0/bigarraylist/core/__init__.py +5 -0
- bigarraylist-0.2.0/bigarraylist/core/base.py +223 -0
- bigarraylist-0.2.0/bigarraylist/exceptions.py +50 -0
- bigarraylist-0.2.0/bigarraylist/operators/__init__.py +14 -0
- bigarraylist-0.2.0/bigarraylist/operators/crud.py +89 -0
- bigarraylist-0.2.0/bigarraylist/operators/search.py +212 -0
- bigarraylist-0.2.0/bigarraylist/operators/sort.py +212 -0
- bigarraylist-0.2.0/bigarraylist/operators/stream.py +341 -0
- bigarraylist-0.2.0/bigarraylist/utils/__init__.py +45 -0
- bigarraylist-0.2.0/bigarraylist/utils/scale.py +139 -0
- bigarraylist-0.2.0/bigarraylist/utils/validation.py +171 -0
- bigarraylist-0.2.0/bigarraylist.egg-info/PKG-INFO +158 -0
- bigarraylist-0.2.0/bigarraylist.egg-info/SOURCES.txt +30 -0
- bigarraylist-0.2.0/bigarraylist.egg-info/dependency_links.txt +1 -0
- bigarraylist-0.2.0/bigarraylist.egg-info/requires.txt +7 -0
- bigarraylist-0.2.0/bigarraylist.egg-info/top_level.txt +1 -0
- bigarraylist-0.2.0/pyproject.toml +109 -0
- bigarraylist-0.2.0/setup.cfg +4 -0
- bigarraylist-0.2.0/tests/test_api.py +665 -0
- bigarraylist-0.2.0/tests/test_core.py +92 -0
- bigarraylist-0.2.0/tests/test_scale.py +91 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PyBigArrayListObject
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bigarraylist
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: 面向大数据量场景的惰性流式列表:惰性迭代 / 并行查找 / 分块排序 / 外部排序 / 蓄水池抽样 / 二级索引
|
|
5
|
+
Author: PyBigArrayListObject
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: big-data,lazy-stream,parallel,external-sort,generator
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
14
|
+
Requires-Python: >=3.10
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Provides-Extra: dev
|
|
18
|
+
Requires-Dist: pytest>=7.4; extra == "dev"
|
|
19
|
+
Requires-Dist: pytest-cov>=4.1; extra == "dev"
|
|
20
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
21
|
+
Requires-Dist: mypy>=1.8; extra == "dev"
|
|
22
|
+
Requires-Dist: pre-commit>=3.6; extra == "dev"
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# BigArrayList
|
|
26
|
+
|
|
27
|
+
> 面向大数据量场景的惰性流式列表:惰性迭代 / 并行查找 / 分块排序 / 低内存外部排序 /
|
|
28
|
+
> 持久化分桶 / 蓄水池抽样 / 二级索引 / 链式算子。
|
|
29
|
+
|
|
30
|
+
零第三方运行时依赖(只用标准库)。核心思想是**惰性流**——数据不一次性物化进内存,
|
|
31
|
+
算子组合、按需产出,从而在内存可控的前提下处理远超内存的数据量。
|
|
32
|
+
|
|
33
|
+
> 版本:0.2.0(重构为四层包结构 + 链式 API + 低内存外部排序 + 持久化分桶;
|
|
34
|
+
> 旧 API 100% 向后兼容)
|
|
35
|
+
|
|
36
|
+
## 安装
|
|
37
|
+
|
|
38
|
+
开发环境(含测试 + 质量工具):
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install -e ".[dev]"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
仅需运行时则零依赖,直接 `import` 即可。
|
|
45
|
+
|
|
46
|
+
## 快速开始
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from bigarraylist import BigArrayList
|
|
50
|
+
|
|
51
|
+
arr = BigArrayList([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 1])
|
|
52
|
+
|
|
53
|
+
# 1) 惰性排序(生成器,按需产出)
|
|
54
|
+
top3 = list(__import__("itertools").islice(arr.sort(), 3)) # [1, 1, 3]
|
|
55
|
+
|
|
56
|
+
# 2) 线性并行查找(返回 (下标, 值),惰性、可早停)
|
|
57
|
+
for idx, val in arr.find(5):
|
|
58
|
+
print(idx, val) # 4 5 / 8 5 / 10 5
|
|
59
|
+
|
|
60
|
+
# 3) 链式 API(新):.lazy() 进入 LazyChain,每个算子返回新 LazyChain
|
|
61
|
+
result = (arr.lazy()
|
|
62
|
+
.filter(lambda x: x > 2)
|
|
63
|
+
.map(lambda x: x * 10)
|
|
64
|
+
.distinct()
|
|
65
|
+
.slice(0, 5)
|
|
66
|
+
.collect()) # [30, 40, 90, 60, 50]
|
|
67
|
+
|
|
68
|
+
# 4) 持久化分桶(新):建桶一次,多次 find 复用,O(n/N) 每次查询
|
|
69
|
+
with arr.build_buckets() as idx:
|
|
70
|
+
for tgt in [3, 5, 9]:
|
|
71
|
+
hits = list(idx.find(tgt))
|
|
72
|
+
print(tgt, hits)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 特性
|
|
76
|
+
|
|
77
|
+
| 能力 | 方法 | 说明 |
|
|
78
|
+
| --- | --- | --- |
|
|
79
|
+
| 惰性迭代 | `__next__` / `cycle` | 标准协议耗尽即停;`.cycle()` 无限循环 |
|
|
80
|
+
| 增删改插 | `append` / `remove` / `replace` / `insert` | 全惰性、不物化 |
|
|
81
|
+
| 线性查找 | `find` | 多进程分块并行,惰性、可早停 |
|
|
82
|
+
| 二分查找 | `binary_search` | 先排序后 bisect,返回排序后下标 |
|
|
83
|
+
| 分桶查找(一次性) | `bucket_find` | 哈希分桶 + 单桶扫描,每次重建桶 |
|
|
84
|
+
| **持久化分桶(新)** | `build_buckets` | 建桶一次返回 `BucketIndex`,多次 `find` 复用 |
|
|
85
|
+
| 蓄水池抽样 | `sample` | O(k) 内存,无需预知总量 |
|
|
86
|
+
| 二级索引 | `build_index` | `dict[key] -> list[下标]`,O(1) 判定 |
|
|
87
|
+
| 统一查找 | `auto_find` | 按数据量自动选线性 / 分桶 |
|
|
88
|
+
| 排序 | `sort` | 三级分流:<10w 直排 / 10w~100w 内存多进程 / ≥100w 外部排序 |
|
|
89
|
+
| 流式算子(裸生成器) | `map`/`filter`/`reduce`/`slice`/`distinct`/`concat`/`chain`/`group_by`/`count_if` | 全惰性、非物化,向后兼容 |
|
|
90
|
+
| **链式算子(新)** | `.lazy()` → `LazyChain` | `.filter().map().distinct().collect()` 链式组合 |
|
|
91
|
+
| **配置对象(新)** | `BigArrayConfig` | 所有阈值可调,外部排序模式 / 桶上限 / 并行阈值 |
|
|
92
|
+
| **自定义异常(新)** | `exceptions` | 继承标准异常,向后兼容;`raise_custom=True` 启用 |
|
|
93
|
+
|
|
94
|
+
## 项目结构
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
PyBigArrayListObject/
|
|
98
|
+
├── bigarraylist/ # 包
|
|
99
|
+
│ ├── __init__.py # 暴露公共 API
|
|
100
|
+
│ ├── _api.py # BigArrayList 用户接口层(参数校验)
|
|
101
|
+
│ ├── config.py # BigArrayConfig(所有阈值可配置)
|
|
102
|
+
│ ├── exceptions.py # 自定义异常层次(继承标准异常)
|
|
103
|
+
│ ├── core/
|
|
104
|
+
│ │ └── base.py # _CoreBase(__slots__+迭代协议+_fresh_iter)
|
|
105
|
+
│ │ # + BaseBigArrayList(组合各 mixin)
|
|
106
|
+
│ ├── operators/ # 算子 mixin 层
|
|
107
|
+
│ │ ├── crud.py # append/remove/replace/insert
|
|
108
|
+
│ │ ├── search.py # find/binary_search/bucket_find/build_buckets
|
|
109
|
+
│ │ ├── sort.py # sort/is_sorted
|
|
110
|
+
│ │ └── stream.py # map/filter/.../count_if + LazyChain
|
|
111
|
+
│ ├── algorithms/ # 算法实现层(可独立调用)
|
|
112
|
+
│ │ ├── parallel.py # 模块级可 pickle 函数(子进程安全)
|
|
113
|
+
│ │ ├── external_sort.py # 低内存外部排序(streaming/block 两种)
|
|
114
|
+
│ │ ├── bucket.py # BucketIndex + build_buckets 持久化分桶
|
|
115
|
+
│ │ └── sample.py # 蓄水池抽样
|
|
116
|
+
│ └── utils/
|
|
117
|
+
│ ├── scale.py # ScaleHelper/choose_chunk_size/...
|
|
118
|
+
│ └── validation.py # 参数校验(标准异常 or 自定义异常切换)
|
|
119
|
+
├── tests/ # pytest 套件(test_core/test_api/test_scale + conftest)
|
|
120
|
+
├── docs/ # 架构说明 / API 参考
|
|
121
|
+
├── examples/ # 使用示例
|
|
122
|
+
├── pyproject.toml # 项目配置 + ruff/mypy/pytest/coverage
|
|
123
|
+
├── .pre-commit-config.yaml
|
|
124
|
+
└── README.md
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## 质量工具
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
pytest # 跑测试(46 个)
|
|
131
|
+
ruff check bigarraylist tests # Lint
|
|
132
|
+
ruff format bigarraylist tests # 格式化
|
|
133
|
+
mypy bigarraylist # 静态类型检查(moderate,见 pyproject 注释)
|
|
134
|
+
pytest --cov=bigarraylist # 覆盖率
|
|
135
|
+
pre-commit run --all-files # 提交前检查
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## 设计要点
|
|
139
|
+
|
|
140
|
+
- **`_fresh_iter()`**:所有算子统一通过它拿一份**独立**的局部迭代器,全程操作局部变量,
|
|
141
|
+
绝不碰 `self.__data`——算子调用零副作用、可重复调用。
|
|
142
|
+
- **三级排序分流**:按数据量选 `sorted()` / 内存多进程分块 / 外部排序,兼顾速度与内存。
|
|
143
|
+
- **低内存外部排序(新)**:默认 streaming 模式——阶段 1 逐条 `pickle.dump` 落盘,
|
|
144
|
+
阶段 2 每个文件用 lazy 迭代器逐条 `pickle.load`,`heapq.merge` 同时驱动。
|
|
145
|
+
内存峰值 = O(块数) 文件句柄 + O(块数) 临时元素(≈ 32),**真正符合"外部排序"语义**,
|
|
146
|
+
能处理远超内存的数据。需要快 3~5 倍但内存放得下时,设
|
|
147
|
+
`BigArrayConfig(external_sort_streaming_merge=False)` 走整块模式。
|
|
148
|
+
- **cooperative multiple inheritance**:`BaseBigArrayList` 多继承 `_CoreBase + CRUDMixin +
|
|
149
|
+
StreamMixin + SortMixin + SearchMixin` 组合各算子;mixin 不声明字段,通过
|
|
150
|
+
`self._fresh_iter()` / `len(self)` / `self._get_config()` 与核心协作,避免 `__slots__`
|
|
151
|
+
多继承冲突与 name mangling 问题。
|
|
152
|
+
- **不支持随机访问 `[]`**:与惰性流设计冲突;要第 N 个用 `itertools.islice` 流式跳过。
|
|
153
|
+
|
|
154
|
+
详见 [docs/architecture.md](docs/architecture.md) 与 [docs/api_reference.md](docs/api_reference.md)。
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
MIT
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# BigArrayList
|
|
2
|
+
|
|
3
|
+
> 面向大数据量场景的惰性流式列表:惰性迭代 / 并行查找 / 分块排序 / 低内存外部排序 /
|
|
4
|
+
> 持久化分桶 / 蓄水池抽样 / 二级索引 / 链式算子。
|
|
5
|
+
|
|
6
|
+
零第三方运行时依赖(只用标准库)。核心思想是**惰性流**——数据不一次性物化进内存,
|
|
7
|
+
算子组合、按需产出,从而在内存可控的前提下处理远超内存的数据量。
|
|
8
|
+
|
|
9
|
+
> 版本:0.2.0(重构为四层包结构 + 链式 API + 低内存外部排序 + 持久化分桶;
|
|
10
|
+
> 旧 API 100% 向后兼容)
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
开发环境(含测试 + 质量工具):
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install -e ".[dev]"
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
仅需运行时则零依赖,直接 `import` 即可。
|
|
21
|
+
|
|
22
|
+
## 快速开始
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from bigarraylist import BigArrayList
|
|
26
|
+
|
|
27
|
+
arr = BigArrayList([3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 1])
|
|
28
|
+
|
|
29
|
+
# 1) 惰性排序(生成器,按需产出)
|
|
30
|
+
top3 = list(__import__("itertools").islice(arr.sort(), 3)) # [1, 1, 3]
|
|
31
|
+
|
|
32
|
+
# 2) 线性并行查找(返回 (下标, 值),惰性、可早停)
|
|
33
|
+
for idx, val in arr.find(5):
|
|
34
|
+
print(idx, val) # 4 5 / 8 5 / 10 5
|
|
35
|
+
|
|
36
|
+
# 3) 链式 API(新):.lazy() 进入 LazyChain,每个算子返回新 LazyChain
|
|
37
|
+
result = (arr.lazy()
|
|
38
|
+
.filter(lambda x: x > 2)
|
|
39
|
+
.map(lambda x: x * 10)
|
|
40
|
+
.distinct()
|
|
41
|
+
.slice(0, 5)
|
|
42
|
+
.collect()) # [30, 40, 90, 60, 50]
|
|
43
|
+
|
|
44
|
+
# 4) 持久化分桶(新):建桶一次,多次 find 复用,O(n/N) 每次查询
|
|
45
|
+
with arr.build_buckets() as idx:
|
|
46
|
+
for tgt in [3, 5, 9]:
|
|
47
|
+
hits = list(idx.find(tgt))
|
|
48
|
+
print(tgt, hits)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## 特性
|
|
52
|
+
|
|
53
|
+
| 能力 | 方法 | 说明 |
|
|
54
|
+
| --- | --- | --- |
|
|
55
|
+
| 惰性迭代 | `__next__` / `cycle` | 标准协议耗尽即停;`.cycle()` 无限循环 |
|
|
56
|
+
| 增删改插 | `append` / `remove` / `replace` / `insert` | 全惰性、不物化 |
|
|
57
|
+
| 线性查找 | `find` | 多进程分块并行,惰性、可早停 |
|
|
58
|
+
| 二分查找 | `binary_search` | 先排序后 bisect,返回排序后下标 |
|
|
59
|
+
| 分桶查找(一次性) | `bucket_find` | 哈希分桶 + 单桶扫描,每次重建桶 |
|
|
60
|
+
| **持久化分桶(新)** | `build_buckets` | 建桶一次返回 `BucketIndex`,多次 `find` 复用 |
|
|
61
|
+
| 蓄水池抽样 | `sample` | O(k) 内存,无需预知总量 |
|
|
62
|
+
| 二级索引 | `build_index` | `dict[key] -> list[下标]`,O(1) 判定 |
|
|
63
|
+
| 统一查找 | `auto_find` | 按数据量自动选线性 / 分桶 |
|
|
64
|
+
| 排序 | `sort` | 三级分流:<10w 直排 / 10w~100w 内存多进程 / ≥100w 外部排序 |
|
|
65
|
+
| 流式算子(裸生成器) | `map`/`filter`/`reduce`/`slice`/`distinct`/`concat`/`chain`/`group_by`/`count_if` | 全惰性、非物化,向后兼容 |
|
|
66
|
+
| **链式算子(新)** | `.lazy()` → `LazyChain` | `.filter().map().distinct().collect()` 链式组合 |
|
|
67
|
+
| **配置对象(新)** | `BigArrayConfig` | 所有阈值可调,外部排序模式 / 桶上限 / 并行阈值 |
|
|
68
|
+
| **自定义异常(新)** | `exceptions` | 继承标准异常,向后兼容;`raise_custom=True` 启用 |
|
|
69
|
+
|
|
70
|
+
## 项目结构
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
PyBigArrayListObject/
|
|
74
|
+
├── bigarraylist/ # 包
|
|
75
|
+
│ ├── __init__.py # 暴露公共 API
|
|
76
|
+
│ ├── _api.py # BigArrayList 用户接口层(参数校验)
|
|
77
|
+
│ ├── config.py # BigArrayConfig(所有阈值可配置)
|
|
78
|
+
│ ├── exceptions.py # 自定义异常层次(继承标准异常)
|
|
79
|
+
│ ├── core/
|
|
80
|
+
│ │ └── base.py # _CoreBase(__slots__+迭代协议+_fresh_iter)
|
|
81
|
+
│ │ # + BaseBigArrayList(组合各 mixin)
|
|
82
|
+
│ ├── operators/ # 算子 mixin 层
|
|
83
|
+
│ │ ├── crud.py # append/remove/replace/insert
|
|
84
|
+
│ │ ├── search.py # find/binary_search/bucket_find/build_buckets
|
|
85
|
+
│ │ ├── sort.py # sort/is_sorted
|
|
86
|
+
│ │ └── stream.py # map/filter/.../count_if + LazyChain
|
|
87
|
+
│ ├── algorithms/ # 算法实现层(可独立调用)
|
|
88
|
+
│ │ ├── parallel.py # 模块级可 pickle 函数(子进程安全)
|
|
89
|
+
│ │ ├── external_sort.py # 低内存外部排序(streaming/block 两种)
|
|
90
|
+
│ │ ├── bucket.py # BucketIndex + build_buckets 持久化分桶
|
|
91
|
+
│ │ └── sample.py # 蓄水池抽样
|
|
92
|
+
│ └── utils/
|
|
93
|
+
│ ├── scale.py # ScaleHelper/choose_chunk_size/...
|
|
94
|
+
│ └── validation.py # 参数校验(标准异常 or 自定义异常切换)
|
|
95
|
+
├── tests/ # pytest 套件(test_core/test_api/test_scale + conftest)
|
|
96
|
+
├── docs/ # 架构说明 / API 参考
|
|
97
|
+
├── examples/ # 使用示例
|
|
98
|
+
├── pyproject.toml # 项目配置 + ruff/mypy/pytest/coverage
|
|
99
|
+
├── .pre-commit-config.yaml
|
|
100
|
+
└── README.md
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 质量工具
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pytest # 跑测试(46 个)
|
|
107
|
+
ruff check bigarraylist tests # Lint
|
|
108
|
+
ruff format bigarraylist tests # 格式化
|
|
109
|
+
mypy bigarraylist # 静态类型检查(moderate,见 pyproject 注释)
|
|
110
|
+
pytest --cov=bigarraylist # 覆盖率
|
|
111
|
+
pre-commit run --all-files # 提交前检查
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## 设计要点
|
|
115
|
+
|
|
116
|
+
- **`_fresh_iter()`**:所有算子统一通过它拿一份**独立**的局部迭代器,全程操作局部变量,
|
|
117
|
+
绝不碰 `self.__data`——算子调用零副作用、可重复调用。
|
|
118
|
+
- **三级排序分流**:按数据量选 `sorted()` / 内存多进程分块 / 外部排序,兼顾速度与内存。
|
|
119
|
+
- **低内存外部排序(新)**:默认 streaming 模式——阶段 1 逐条 `pickle.dump` 落盘,
|
|
120
|
+
阶段 2 每个文件用 lazy 迭代器逐条 `pickle.load`,`heapq.merge` 同时驱动。
|
|
121
|
+
内存峰值 = O(块数) 文件句柄 + O(块数) 临时元素(≈ 32),**真正符合"外部排序"语义**,
|
|
122
|
+
能处理远超内存的数据。需要快 3~5 倍但内存放得下时,设
|
|
123
|
+
`BigArrayConfig(external_sort_streaming_merge=False)` 走整块模式。
|
|
124
|
+
- **cooperative multiple inheritance**:`BaseBigArrayList` 多继承 `_CoreBase + CRUDMixin +
|
|
125
|
+
StreamMixin + SortMixin + SearchMixin` 组合各算子;mixin 不声明字段,通过
|
|
126
|
+
`self._fresh_iter()` / `len(self)` / `self._get_config()` 与核心协作,避免 `__slots__`
|
|
127
|
+
多继承冲突与 name mangling 问题。
|
|
128
|
+
- **不支持随机访问 `[]`**:与惰性流设计冲突;要第 N 个用 `itertools.islice` 流式跳过。
|
|
129
|
+
|
|
130
|
+
详见 [docs/architecture.md](docs/architecture.md) 与 [docs/api_reference.md](docs/api_reference.md)。
|
|
131
|
+
|
|
132
|
+
## License
|
|
133
|
+
|
|
134
|
+
MIT
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"""BigArrayList:面向大数据量场景的惰性流式列表工具包。
|
|
2
|
+
|
|
3
|
+
公共 API:
|
|
4
|
+
BigArrayList —— 用户接口层(增删改插 / 查找 / 排序 / 流式算子 / 链式 API)
|
|
5
|
+
BigArrayConfig —— 阈值配置对象(可调外部排序模式、桶上限等)
|
|
6
|
+
LazyChain —— 链式算子(big.lazy().filter().map().collect())
|
|
7
|
+
BucketIndex —— 持久化分桶索引(build_buckets 返回,多次 find 复用)
|
|
8
|
+
exceptions —— 自定义异常层次(继承标准异常,向后兼容)
|
|
9
|
+
|
|
10
|
+
ScaleHelper / ScaleValue / choose_chunk_size / choose_external_sort_chunk_size /
|
|
11
|
+
chunk_wise_processing —— 向后兼容保留
|
|
12
|
+
|
|
13
|
+
典型用法:
|
|
14
|
+
from bigarraylist import BigArrayList
|
|
15
|
+
arr = BigArrayList([3, 1, 4, 1, 5, 9, 2, 6])
|
|
16
|
+
for x in arr.sort():
|
|
17
|
+
print(x)
|
|
18
|
+
|
|
19
|
+
# 链式 API(新功能)
|
|
20
|
+
result = (arr.lazy()
|
|
21
|
+
.filter(lambda x: x > 2)
|
|
22
|
+
.map(lambda x: x * 10)
|
|
23
|
+
.distinct()
|
|
24
|
+
.collect())
|
|
25
|
+
|
|
26
|
+
# 持久化分桶(多次查询复用)
|
|
27
|
+
with arr.build_buckets() as idx:
|
|
28
|
+
for tgt in targets:
|
|
29
|
+
hits = list(idx.find(tgt))
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
from ._api import BigArrayList
|
|
33
|
+
from .algorithms.bucket import BucketIndex, build_buckets
|
|
34
|
+
from .config import BigArrayConfig
|
|
35
|
+
from .exceptions import (
|
|
36
|
+
BigArrayListError,
|
|
37
|
+
DataSourceError,
|
|
38
|
+
IndexOutOfRangeError,
|
|
39
|
+
InvalidArgumentError,
|
|
40
|
+
)
|
|
41
|
+
from .operators.stream import LazyChain
|
|
42
|
+
from .utils.scale import (
|
|
43
|
+
ScaleHelper,
|
|
44
|
+
ScaleValue,
|
|
45
|
+
choose_chunk_size,
|
|
46
|
+
choose_external_sort_chunk_size,
|
|
47
|
+
chunk_wise_processing,
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
__all__ = [
|
|
51
|
+
"BigArrayConfig",
|
|
52
|
+
# 主类
|
|
53
|
+
"BigArrayList",
|
|
54
|
+
# 异常
|
|
55
|
+
"BigArrayListError",
|
|
56
|
+
"BucketIndex",
|
|
57
|
+
"DataSourceError",
|
|
58
|
+
"IndexOutOfRangeError",
|
|
59
|
+
"InvalidArgumentError",
|
|
60
|
+
"LazyChain",
|
|
61
|
+
# 向后兼容:量级助手
|
|
62
|
+
"ScaleHelper",
|
|
63
|
+
"ScaleValue",
|
|
64
|
+
"build_buckets",
|
|
65
|
+
"choose_chunk_size",
|
|
66
|
+
"choose_external_sort_chunk_size",
|
|
67
|
+
"chunk_wise_processing",
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
__version__ = "0.2.0"
|