fluentmap 0.1.0__tar.gz → 0.1.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: fluentmap
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: A drop-in Python map replacement featuring parallel and batch processing.
5
5
  Author: leavers
6
6
  Author-Email: Leavers <leavers930@gmail.com>
@@ -15,6 +15,9 @@ Classifier: Programming Language :: Python :: 3.9
15
15
  Classifier: Programming Language :: Python :: 3.10
16
16
  Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
+ Classifier: Programming Language :: Python :: 3.13
19
+ Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: 3.15
18
21
  Classifier: Topic :: Software Development
19
22
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
23
  Classifier: Topic :: Utilities
@@ -36,9 +39,13 @@ processing.
36
39
  - Use `batch_size`/`chunk_size` to send parameters in batches/chunks.
37
40
  - Use `num_prepare` to prepare data in advance for better performance.
38
41
  - Call `on_return` hook to process the return value of each task.
42
+ - Support the builtin `strict` keyword.
39
43
 
40
44
  ## Installation
41
45
 
46
+ Fluentmap requires Python 3.8 or newer, has no runtime dependencies, and is tested
47
+ on CPython 3.8-3.15, including the free-threaded builds (3.13t-3.15t).
48
+
42
49
  Fluentmap is available on [PyPI](https://pypi.org/project/fluentmap/):
43
50
 
44
51
  ```shell
@@ -13,9 +13,13 @@ processing.
13
13
  - Use `batch_size`/`chunk_size` to send parameters in batches/chunks.
14
14
  - Use `num_prepare` to prepare data in advance for better performance.
15
15
  - Call `on_return` hook to process the return value of each task.
16
+ - Support the builtin `strict` keyword.
16
17
 
17
18
  ## Installation
18
19
 
20
+ Fluentmap requires Python 3.8 or newer, has no runtime dependencies, and is tested
21
+ on CPython 3.8-3.15, including the free-threaded builds (3.13t-3.15t).
22
+
19
23
  Fluentmap is available on [PyPI](https://pypi.org/project/fluentmap/):
20
24
 
21
25
  ```shell
@@ -26,7 +26,7 @@ from typing import (
26
26
  runtime_checkable,
27
27
  )
28
28
 
29
- __version__ = "0.1.0"
29
+ __version__ = "0.1.2"
30
30
  __all__ = ("Arguments", "map")
31
31
 
32
32
 
@@ -61,6 +61,42 @@ class Arguments:
61
61
  return f"{self.__class__.__name__}(args={self.args}, kwargs={self.kwargs})"
62
62
 
63
63
 
64
+ def _zip_args(
65
+ args: Iterable[Any],
66
+ *more_args: Iterable[Any],
67
+ strict: bool = False,
68
+ ) -> Iterator[Tuple[Any, ...]]:
69
+ if not strict:
70
+ yield from zip(args, *more_args)
71
+ return
72
+
73
+ # Same checks and messages as the strict builtin map (zip(strict=True) is only
74
+ # available on Python 3.10+).
75
+ iterators = [iter(args), *(iter(it) for it in more_args)]
76
+ while True:
77
+ values = []
78
+ for i, iterator in enumerate(iterators):
79
+ try:
80
+ values.append(next(iterator))
81
+ except StopIteration:
82
+ if i:
83
+ plural = " " if i == 1 else "s 1-"
84
+ raise ValueError(
85
+ f"map() argument {i + 1} is shorter than argument{plural}{i}"
86
+ ) from None
87
+ for j, other in enumerate(iterators[1:], start=1):
88
+ try:
89
+ next(other)
90
+ except StopIteration:
91
+ continue
92
+ plural = " " if j == 1 else "s 1-"
93
+ raise ValueError(
94
+ f"map() argument {j + 1} is longer than argument{plural}{j}"
95
+ ) from None
96
+ return
97
+ yield tuple(values)
98
+
99
+
64
100
  def _simple_map(
65
101
  fn: Callable[..., T],
66
102
  args: Iterable[Any],
@@ -68,11 +104,12 @@ def _simple_map(
68
104
  batch_size: int = 0,
69
105
  num_prepare: int = 0,
70
106
  on_return: Optional[Callable[[T], R]] = None,
107
+ strict: bool = False,
71
108
  ) -> Iterator[Union[T, R]]:
72
109
  is_batch_args = True if batch_size > 1 else False
73
110
 
74
111
  if more_args:
75
- iterable_args = cast(Iterable[Any], zip(args, *more_args))
112
+ iterable_args = cast(Iterable[Any], _zip_args(args, *more_args, strict=strict))
76
113
  extract_args = True
77
114
  else:
78
115
  iterable_args = args
@@ -109,28 +146,33 @@ def _simple_map(
109
146
  undefined = object()
110
147
  queue: Queue = Queue()
111
148
  exc_event = Event()
149
+ worker_exc: List[BaseException] = []
112
150
 
113
151
  def worker():
114
152
  batch: List[Any] = []
115
- for args in iterable_args:
116
- if exc_event.is_set():
117
- break
118
- if is_batch_args:
119
- batch.append(args)
120
- if len(batch) < batch_size:
121
- continue
122
- queue.put(batch)
123
- batch = []
124
- else:
125
- queue.put(args)
126
- del args
127
- while queue.qsize() >= num_prepare:
153
+ try:
154
+ for args in iterable_args:
128
155
  if exc_event.is_set():
129
156
  break
130
- exc_event.wait(0.05)
131
- if not exc_event.is_set() and batch:
132
- queue.put(batch)
133
- queue.put(undefined)
157
+ if is_batch_args:
158
+ batch.append(args)
159
+ if len(batch) < batch_size:
160
+ continue
161
+ queue.put(batch)
162
+ batch = []
163
+ else:
164
+ queue.put(args)
165
+ del args
166
+ while queue.qsize() >= num_prepare:
167
+ if exc_event.is_set():
168
+ break
169
+ exc_event.wait(0.05)
170
+ if not exc_event.is_set() and batch:
171
+ queue.put(batch)
172
+ except BaseException as exc:
173
+ worker_exc.append(exc)
174
+ finally:
175
+ queue.put(undefined)
134
176
 
135
177
  worker_thread = Thread(target=worker, daemon=True)
136
178
  worker_thread.start()
@@ -140,6 +182,8 @@ def _simple_map(
140
182
  except Empty:
141
183
  continue
142
184
  if data is undefined:
185
+ if worker_exc:
186
+ raise worker_exc[0]
143
187
  break
144
188
  try:
145
189
  yield submit(data)
@@ -194,13 +238,14 @@ def _concurrent_chunk_map(
194
238
  num_prepare: int = 0,
195
239
  sort_by_completion: bool = False,
196
240
  on_return: Optional[Callable[[T], R]] = None,
241
+ strict: bool = False,
197
242
  ) -> Iterator[Union[T, R]]:
198
243
  assert chunk_size > 1
199
244
  is_batch_args = True if batch_size > 1 else False
200
245
 
201
246
  args_iter: Iterator[Any]
202
247
  if more_args:
203
- args_iter = iter(zip(args, *more_args))
248
+ args_iter = iter(_zip_args(args, *more_args, strict=strict))
204
249
 
205
250
  # Whether to deconstruct iterable args in submit, this helps sumbit identify
206
251
  # whether an iterable fn_args is a batch generated by _concurrent_map itself
@@ -293,14 +338,14 @@ def _concurrent_chunk_map(
293
338
  break
294
339
 
295
340
  i, raw_result_chunk = pair
296
- results: List[Tuple[Union[T, BaseException], bool]] = []
341
+ results: List[Tuple[Union[T, R, BaseException], bool]] = []
297
342
  for raw_result, is_exc in raw_result_chunk:
298
343
  if is_exc:
299
344
  results.append((raw_result, True))
300
345
  continue
301
346
  try:
302
- new_result = on_return(raw_result) # type: ignore[arg-type]
303
- results.append((new_result, False)) # type: ignore[arg-type]
347
+ new_result = on_return(cast(T, raw_result))
348
+ results.append((new_result, False))
304
349
  except BaseException as exc:
305
350
  results.append((exc, True))
306
351
  del exc
@@ -423,12 +468,13 @@ def _concurrent_map(
423
468
  num_prepare: int = 0,
424
469
  sort_by_completion: bool = False,
425
470
  on_return: Optional[Callable[[T], R]] = None,
471
+ strict: bool = False,
426
472
  ) -> Iterator[Union[T, R]]:
427
473
  is_batch_args = True if batch_size > 1 else False
428
474
 
429
475
  args_iter: Iterator[Any]
430
476
  if more_args:
431
- args_iter = iter(zip(args, *more_args))
477
+ args_iter = iter(_zip_args(args, *more_args, strict=strict))
432
478
 
433
479
  # Whether to deconstruct iterable args in submit, this helps sumbit identify
434
480
  # whether an iterable fn_args is a batch generated by _concurrent_map itself
@@ -517,7 +563,7 @@ def _concurrent_map(
517
563
 
518
564
  i, raw_result = pair
519
565
  try:
520
- result = on_return(raw_result) # type: ignore[misc]
566
+ result = on_return(raw_result)
521
567
  outqueue.put((i, result, False))
522
568
  except BaseException as exc:
523
569
  outqueue.put((i, exc, True))
@@ -623,8 +669,10 @@ def map(
623
669
  *more_args: Iterable[Any],
624
670
  executor: Optional[SupportsSubmit] = None,
625
671
  batch_size: Optional[int] = None,
672
+ chunk_size: Optional[int] = None,
626
673
  num_prepare: Optional[int] = None,
627
674
  sort_by_completion: bool = False,
675
+ strict: bool = False,
628
676
  ) -> Iterator[T]: ...
629
677
 
630
678
 
@@ -635,9 +683,11 @@ def map(
635
683
  *more_args: Iterable[Any],
636
684
  executor: Optional[SupportsSubmit] = None,
637
685
  batch_size: Optional[int] = None,
686
+ chunk_size: Optional[int] = None,
638
687
  num_prepare: Optional[int] = None,
639
688
  sort_by_completion: bool = False,
640
689
  on_return: Callable[[T], R],
690
+ strict: bool = False,
641
691
  ) -> Iterator[R]: ...
642
692
 
643
693
 
@@ -651,6 +701,7 @@ def map(
651
701
  num_prepare: Optional[int] = None,
652
702
  sort_by_completion: bool = False,
653
703
  on_return: Optional[Callable[[T], R]] = None,
704
+ strict: bool = False,
654
705
  ) -> Iterator[Union[T, R]]:
655
706
  """Drop-in replacement of builtin ``map`` but adds an argument ``executor`` for
656
707
  executing function in an executor.
@@ -718,6 +769,10 @@ def map(
718
769
  It accept each result as its sole argument and its return will be treated as
719
770
  as the final return of each invocation of ``fn``.
720
771
 
772
+ :type strict: bool, optional, default to ``False``
773
+ :param strict: If set to ``True``, raise ``ValueError`` when the iterables have
774
+ different lengths, same as the strict builtin ``map``.
775
+
721
776
  :rtype: Iterator
722
777
  :return: An iterator which yield all results of ``fn``.
723
778
  """
@@ -740,7 +795,8 @@ def map(
740
795
  *more_args,
741
796
  batch_size=batch_size,
742
797
  num_prepare=num_prepare,
743
- on_return=on_return, # type: ignore[arg-type]
798
+ on_return=on_return,
799
+ strict=strict,
744
800
  )
745
801
 
746
802
  if chunk_size is None or chunk_size <= 1:
@@ -752,7 +808,8 @@ def map(
752
808
  batch_size=batch_size,
753
809
  num_prepare=num_prepare,
754
810
  sort_by_completion=sort_by_completion,
755
- on_return=on_return, # type: ignore[arg-type]
811
+ on_return=on_return,
812
+ strict=strict,
756
813
  )
757
814
  return _concurrent_chunk_map(
758
815
  fn,
@@ -763,5 +820,6 @@ def map(
763
820
  chunk_size=chunk_size,
764
821
  num_prepare=num_prepare,
765
822
  sort_by_completion=sort_by_completion,
766
- on_return=on_return, # type: ignore[arg-type]
823
+ on_return=on_return,
824
+ strict=strict,
767
825
  )
@@ -16,6 +16,9 @@ classifiers = [
16
16
  "Programming Language :: Python :: 3.10",
17
17
  "Programming Language :: Python :: 3.11",
18
18
  "Programming Language :: Python :: 3.12",
19
+ "Programming Language :: Python :: 3.13",
20
+ "Programming Language :: Python :: 3.14",
21
+ "Programming Language :: Python :: 3.15",
19
22
  "Topic :: Software Development",
20
23
  "Topic :: Software Development :: Libraries :: Python Modules",
21
24
  "Topic :: Utilities",
@@ -24,7 +27,7 @@ dependencies = []
24
27
  requires-python = ">=3.8"
25
28
  readme = "README.md"
26
29
  dynamic = []
27
- version = "0.1.0"
30
+ version = "0.1.2"
28
31
 
29
32
  [project.license]
30
33
  text = "MIT"
@@ -50,14 +53,6 @@ excludes = [
50
53
  source = "file"
51
54
  path = "fluentmap.py"
52
55
 
53
- [tool.autoflake]
54
- in-place = true
55
- recursive = true
56
- remove-all-unused-imports = true
57
- ignore-init-module-imports = true
58
- remove-duplicate-keys = true
59
- remove-unused-variables = true
60
-
61
56
  [tool.coverage.run]
62
57
  branch = true
63
58
  source = [
@@ -74,21 +69,25 @@ exclude_lines = [
74
69
  [tool.ruff]
75
70
  line-length = 88
76
71
 
77
- [tool.mypy]
78
- check_untyped_defs = true
79
- follow_imports = "error"
80
- ignore_missing_imports = true
81
- show_error_codes = true
82
- warn_unused_configs = true
72
+ [tool.ruff.lint]
73
+ select = [
74
+ "F401",
75
+ "F841",
76
+ "F601",
77
+ ]
78
+
79
+ [tool.ruff.lint.per-file-ignores]
80
+ "__init__.py" = [
81
+ "F401",
82
+ ]
83
83
 
84
84
  [tool.pytest.ini_options]
85
85
  asyncio_mode = "strict"
86
86
 
87
87
  [dependency-groups]
88
88
  dev = [
89
- "autoflake>=2.3.1",
90
89
  "coverage[toml]>=7.6.1",
91
- "mypy>=1.11.1",
90
+ "ty>=0.0.91",
92
91
  "nox>=2024.4.15",
93
92
  "pytest>=8.3.2",
94
93
  "pytest-asyncio>=0.23.8",
@@ -96,5 +95,5 @@ dev = [
96
95
  "pytest-mock>=3.14.0",
97
96
  "pytest-timeout>=2.3.1",
98
97
  "ruff>=0.6.1",
99
- "rtoml>=0.11.0",
98
+ "tomli>=2.0.1",
100
99
  ]
File without changes