flpit 0.3.2__tar.gz → 0.3.3__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.5
2
2
  Name: flpit
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: Fluent LINQ for Python — flip your iterables
5
5
  Project-URL: Homepage, https://github.com/riemar/flpit
6
6
  Project-URL: Repository, https://github.com/riemar/flpit
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "flpit"
3
- version = "0.3.2"
3
+ version = "0.3.3"
4
4
  description = "Fluent LINQ for Python — flip your iterables"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.12"
@@ -296,6 +296,19 @@ def dotnet_context() -> AbstractContextManager[decimal.Context]:
296
296
  return decimal.localcontext(ctx)
297
297
 
298
298
 
299
+ def _decimal_mean(total: Decimal, count: int) -> Decimal:
300
+ """
301
+ ``total / count`` like .NET ``decimal`` division: rounded to 29 significant digits instead of
302
+ raising ``Inexact`` (the summation context traps ``Inexact`` only to detect decimal overflow).
303
+ """
304
+ ctx = decimal.getcontext().copy()
305
+ ctx.prec = max(ctx.prec, 29)
306
+ ctx.traps[Inexact] = False
307
+ ctx.traps[Overflow] = True
308
+ with decimal.localcontext(ctx):
309
+ return total / Decimal(count)
310
+
311
+
299
312
  class _FactoryIterable[TItem](Iterable[TItem]):
300
313
  """
301
314
  Wraps a generator factory function into a clean Iterable[T].
@@ -929,20 +942,18 @@ class FlpIt[TItem](Iterable[TItem]):
929
942
 
930
943
  Produces everything ``left_join`` does (unmatched outer elements paired with ``None``),
931
944
  followed by every inner element that was never matched by any outer element, paired with
932
- ``None`` as the outer element (``result_selector(None, inner)``), in inner order.
945
+ ``None`` as the outer element (``result_selector(None, inner)``), grouped by key in order of
946
+ first key appearance (like .NET, which walks its inner lookup).
933
947
  Elements with a ``None`` key never match and therefore always appear unmatched.
934
948
  The outer sequence is streamed; the inner sequence is buffered.
935
949
  """
936
950
  _validate_join_args(inner, outer_key_selector, inner_key_selector, result_selector)
937
951
 
938
952
  def _generator() -> Iterator[Any]:
939
- inner_items: list[tuple[Any, TInner]] = []
953
+ # Like .NET (Lookup.Create): None keys are kept, but never match.
940
954
  lookup: dict[Any, list[TInner]] = {}
941
955
  for inner_item in inner:
942
- inner_key = inner_key_selector(inner_item)
943
- inner_items.append((inner_key, inner_item))
944
- if inner_key is not None:
945
- lookup.setdefault(inner_key, []).append(inner_item)
956
+ lookup.setdefault(inner_key_selector(inner_item), []).append(inner_item)
946
957
 
947
958
  matched_keys: set[Any] = set()
948
959
  for outer_item in self:
@@ -961,12 +972,13 @@ class FlpIt[TItem](Iterable[TItem]):
961
972
  else:
962
973
  yield result_selector(outer_item, inner_item)
963
974
 
964
- for inner_key, inner_item in inner_items:
975
+ for inner_key, group in lookup.items():
965
976
  if inner_key is None or inner_key not in matched_keys:
966
- if result_selector is _SENTINEL:
967
- yield JoinTuple(None, inner_item)
968
- else:
969
- yield result_selector(None, inner_item)
977
+ for inner_item in group:
978
+ if result_selector is _SENTINEL:
979
+ yield JoinTuple(None, inner_item)
980
+ else:
981
+ yield result_selector(None, inner_item)
970
982
 
971
983
  return FlpIt(_FactoryIterable(_generator))
972
984
 
@@ -1265,7 +1277,7 @@ class FlpIt[TItem](Iterable[TItem]):
1265
1277
  return None
1266
1278
 
1267
1279
  if isinstance(total, Decimal):
1268
- return total / Decimal(count)
1280
+ return _decimal_mean(total, count)
1269
1281
 
1270
1282
  assert total is not None
1271
1283
  assert isinstance(total, Decimal) or isinstance(total, int) or isinstance(total, float)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes