Compare commits

...

10 Commits

Author SHA1 Message Date
openeuler-ci-bot
d73f010b37
!31 Add prepend recipe
From: @zhang-liang-pengkun 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-12-28 09:44:08 +00:00
zhang-liang-pengkun
66430cef45 Add prepend recipe
Signed-off-by: zhang-liang-pengkun <zhangliangpengkun@xfusion.com>
2023-12-28 16:35:58 +08:00
openeuler-ci-bot
fb3c71ef77
!24 Add map_reduce function
From: @zhang-liang-pengkun 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-12-25 08:38:51 +00:00
zhang-liang-pengkun
8352412e6d Add map_reduce function
Signed-off-by: zhang-liang-pengkun <zhangliangpengkun@xfusion.com>
2023-12-22 17:25:51 +08:00
openeuler-ci-bot
58e881c935
!21 fix changelog error
From: @zhang-liang-pengkun 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-12-01 06:34:08 +00:00
zhang-liang-pengkun
4bd592bb5a fix version error
Signed-off-by: zhang-liang-pengkun <zhangliangpengkun@xfusion.com>
2023-11-30 21:23:23 +08:00
openeuler-ci-bot
bc1c5c3bbc
!18 more.py: document ilen
From: @zhang-liang-pengkun 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-11-24 07:44:05 +00:00
zhang-liang-pengkun
b4b843ff79 more.py: document ilen
Signed-off-by: zhang-liang-pengkun <zhangliangpengkun@xfusion.com>
2023-11-24 12:21:37 +08:00
openeuler-ci-bot
66d9da2364
!14 fix for python 3.7 deprecation warning about importing certain types from collections vs collections.abc
From: @zhang-liang-pengkun 
Reviewed-by: @Charlie_li 
Signed-off-by: @Charlie_li
2023-11-16 10:54:59 +00:00
zhang-liang-pengkun
5b7b2367f0 fix for python 3.7 deprecation warning about importing certain types from collections vs collections.abc
Signed-off-by: zhang-liang-pengkun <zhangliangpengkun@xfusion.com>
2023-11-16 17:10:21 +08:00
5 changed files with 328 additions and 1 deletions

View File

@ -0,0 +1,36 @@
From 30a861bc5a4f53a9ba73923c9048a3632a0f9d18 Mon Sep 17 00:00:00 2001
From: Irmen de Jong <irmen@razorvine.net>
Date: Tue, 3 Apr 2018 18:15:39 +0200
Subject: [PATCH] fix for python 3.7 deprecation warning about importing
certain types from collections vs collections.abc
---
more_itertools/more.py | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/more_itertools/more.py b/more_itertools/more.py
index c4b08ae..4dc9f15 100644
--- a/more_itertools/more.py
+++ b/more_itertools/more.py
@@ -1,6 +1,6 @@
from __future__ import print_function
-from collections import Counter, defaultdict, deque, Sequence
+from collections import Counter, defaultdict, deque
from functools import partial, wraps
from heapq import merge
from itertools import (
@@ -17,6 +17,10 @@ from itertools import (
)
from operator import itemgetter, lt, gt, sub
from sys import maxsize, version_info
+if version_info < (3, 3):
+ from collections import Sequence
+else:
+ from collections.abc import Sequence
from six import binary_type, string_types, text_type
from six.moves import filter, map, range, zip, zip_longest
--
2.39.0.windows.2

View File

@ -0,0 +1,29 @@
From fc39d84358199cd0fc7a33e9179244a4987e36c7 Mon Sep 17 00:00:00 2001
From: Ben Mintz <bmintz@protonmail.com>
Date: Mon, 7 May 2018 03:29:41 -0500
Subject: [PATCH] more.py: document ilen
I found the implementation of ilen to be confusing, so I documented it.
---
more_itertools/more.py | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/more_itertools/more.py b/more_itertools/more.py
index 6de5b03..a5fd960 100644
--- a/more_itertools/more.py
+++ b/more_itertools/more.py
@@ -409,7 +409,11 @@ def ilen(iterable):
This consumes the iterable, so handle with care.
"""
+ # maxlen=1 only stores the last item in the deque
d = deque(enumerate(iterable, 1), maxlen=1)
+ # since we started enumerate at 1,
+ # the first item of the last pair will be the length of the iterable
+ # (assuming there were items)
return d[0][0] if d else 0
--
2.39.0.windows.2

View File

@ -0,0 +1,166 @@
From edfb4704efe9bdb2dc6022fb4b665c62a1525a2c Mon Sep 17 00:00:00 2001
From: Bo Bayles <bbayles@gmail.com>
Date: Tue, 20 Feb 2018 06:04:55 -0600
Subject: [PATCH] Add map_reduce function (#196)
* Add map_reduce function
* Note return type
* keyfunc won't be None
* Correct typo in docstring
---
docs/api.rst | 1 +
more_itertools/more.py | 68 +++++++++++++++++++++++++++++++
more_itertools/tests/test_more.py | 35 +++++++++++++++-
3 files changed, 103 insertions(+), 1 deletion(-)
diff --git a/docs/api.rst b/docs/api.rst
index b1c0632..914e5bc 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -129,6 +129,7 @@ These tools return summarized or aggregated data from an iterable.
.. autofunction:: consecutive_groups(iterable, ordering=lambda x: x)
.. autofunction:: exactly_n(iterable, n, predicate=bool)
.. autoclass:: run_length
+.. autofunction:: map_reduce
----
diff --git a/more_itertools/more.py b/more_itertools/more.py
index 21a1497..c4b08ae 100644
--- a/more_itertools/more.py
+++ b/more_itertools/more.py
@@ -51,6 +51,7 @@ __all__ = [
'locate',
'lstrip',
'make_decorator',
+ 'map_reduce',
'numeric_range',
'one',
'padded',
@@ -1983,3 +1984,70 @@ def make_decorator(wrapping_func, result_index=0):
return outer_wrapper
return decorator
+
+
+def map_reduce(iterable, keyfunc, valuefunc=None, reducefunc=None):
+ """Return a dictionary that maps the items in *iterable* to categories
+ defined by *keyfunc*, transforms them with *valuefunc*, and
+ then summarizes them by category with *reducefunc*.
+
+ *valuefunc* defaults to the identity function if it is unspecified.
+ If *reducefunc* is unspecified, no summarization takes place:
+
+ >>> keyfunc = lambda x: x.upper()
+ >>> result = map_reduce('abbccc', keyfunc)
+ >>> sorted(result.items())
+ [('A', ['a']), ('B', ['b', 'b']), ('C', ['c', 'c', 'c'])]
+
+ Specifying *valuefunc* transforms the categorized items:
+
+ >>> keyfunc = lambda x: x.upper()
+ >>> valuefunc = lambda x: 1
+ >>> result = map_reduce('abbccc', keyfunc, valuefunc)
+ >>> sorted(result.items())
+ [('A', [1]), ('B', [1, 1]), ('C', [1, 1, 1])]
+
+ Specifying *reducefunc* summarizes the categorized items:
+
+ >>> keyfunc = lambda x: x.upper()
+ >>> valuefunc = lambda x: 1
+ >>> reducefunc = sum
+ >>> result = map_reduce('abbccc', keyfunc, valuefunc, reducefunc)
+ >>> sorted(result.items())
+ [('A', 1), ('B', 2), ('C', 3)]
+
+ You may want to filter the input iterable before applying the map/reduce
+ proecdure:
+
+ >>> all_items = range(30)
+ >>> items = [x for x in all_items if 10 <= x <= 20] # Filter
+ >>> keyfunc = lambda x: x % 2 # Evens map to 0; odds to 1
+ >>> categories = map_reduce(items, keyfunc=keyfunc)
+ >>> sorted(categories.items())
+ [(0, [10, 12, 14, 16, 18, 20]), (1, [11, 13, 15, 17, 19])]
+ >>> summaries = map_reduce(items, keyfunc=keyfunc, reducefunc=sum)
+ >>> sorted(summaries.items())
+ [(0, 90), (1, 75)]
+
+ Note that all items in the iterable are gathered into a list before the
+ summarization step, which may require significant storage.
+
+ The returned object is a :obj:`collections.defaultdict` with the
+ ``default_factory`` set to ``None``, such that it behaves like a normal
+ dictionary.
+
+ """
+ valuefunc = (lambda x: x) if (valuefunc is None) else valuefunc
+
+ ret = defaultdict(list)
+ for item in iterable:
+ key = keyfunc(item)
+ value = valuefunc(item)
+ ret[key].append(value)
+
+ if reducefunc is not None:
+ for key, value_list in ret.items():
+ ret[key] = reducefunc(value_list)
+
+ ret.default_factory = None
+ return ret
diff --git a/more_itertools/tests/test_more.py b/more_itertools/tests/test_more.py
index d8ada9c..0ac4abd 100644
--- a/more_itertools/tests/test_more.py
+++ b/more_itertools/tests/test_more.py
@@ -15,7 +15,7 @@ from itertools import (
product,
repeat,
)
-from operator import add, itemgetter
+from operator import add, mul, itemgetter
from unittest import TestCase
from six.moves import filter, map, range, zip
@@ -1792,3 +1792,36 @@ class MakeDecoratorTests(TestCase):
it.seek(0)
self.assertEqual(list(it), ['0', '1', '2', '3', '4'])
+
+
+class MapReduceTests(TestCase):
+ def test_default(self):
+ iterable = (str(x) for x in range(5))
+ keyfunc = lambda x: int(x) // 2
+ actual = sorted(mi.map_reduce(iterable, keyfunc).items())
+ expected = [(0, ['0', '1']), (1, ['2', '3']), (2, ['4'])]
+ self.assertEqual(actual, expected)
+
+ def test_valuefunc(self):
+ iterable = (str(x) for x in range(5))
+ keyfunc = lambda x: int(x) // 2
+ valuefunc = int
+ actual = sorted(mi.map_reduce(iterable, keyfunc, valuefunc).items())
+ expected = [(0, [0, 1]), (1, [2, 3]), (2, [4])]
+ self.assertEqual(actual, expected)
+
+ def test_reducefunc(self):
+ iterable = (str(x) for x in range(5))
+ keyfunc = lambda x: int(x) // 2
+ valuefunc = int
+ reducefunc = lambda value_list: reduce(mul, value_list, 1)
+ actual = sorted(
+ mi.map_reduce(iterable, keyfunc, valuefunc, reducefunc).items()
+ )
+ expected = [(0, 0), (1, 6), (2, 4)]
+ self.assertEqual(actual, expected)
+
+ def test_ret(self):
+ d = mi.map_reduce([1, 0, 2, 0, 1, 0], bool)
+ self.assertEqual(d, {False: [0, 0, 0], True: [1, 2, 1]})
+ self.assertRaises(KeyError, lambda: d[None].append(1))
--
2.39.0.windows.2

View File

@ -0,0 +1,80 @@
From a17e5e4f8d9d9b8e1932bda10a1ab5cf59290deb Mon Sep 17 00:00:00 2001
From: Bo Bayles <bbayles@gmail.com>
Date: Mon, 14 May 2018 21:20:08 -0500
Subject: [PATCH] Add prepend recipe
---
docs/api.rst | 1 +
more_itertools/recipes.py | 15 +++++++++++++++
more_itertools/tests/test_recipes.py | 16 ++++++++++++++++
3 files changed, 32 insertions(+)
diff --git a/docs/api.rst b/docs/api.rst
index 914e5bc..49459f3 100644
--- a/docs/api.rst
+++ b/docs/api.rst
@@ -110,6 +110,7 @@ These tools combine multiple iterables.
.. autofunction:: dotproduct
.. autofunction:: flatten
.. autofunction:: roundrobin
+.. autofunction:: prepend
Summarizing
diff --git a/more_itertools/recipes.py b/more_itertools/recipes.py
index ec5e756..3a7706c 100644
--- a/more_itertools/recipes.py
+++ b/more_itertools/recipes.py
@@ -33,6 +33,7 @@ __all__ = [
'pairwise',
'partition',
'powerset',
+ 'prepend',
'quantify',
'random_combination_with_replacement',
'random_combination',
@@ -548,3 +549,17 @@ def nth_combination(iterable, r, index):
result.append(pool[-1 - n])
return tuple(result)
+
+
+def prepend(value, iterator):
+ """Yield *value*, followed by the elements in *iterator*.
+
+ >>> value = '0'
+ >>> iterator = ['1', '2', '3']
+ >>> list(prepend(value, iterator))
+ ['0', '1', '2', '3']
+
+ To prepend multiple values, see :func:`itertools.chain`.
+
+ """
+ return chain([value], iterator)
diff --git a/more_itertools/tests/test_recipes.py b/more_itertools/tests/test_recipes.py
index f6d7680..81721fd 100644
--- a/more_itertools/tests/test_recipes.py
+++ b/more_itertools/tests/test_recipes.py
@@ -589,3 +589,19 @@ class NthCombinationTests(TestCase):
actual = mi.nth_combination(range(180), 4, 2000000)
expected = (2, 12, 35, 126)
self.assertEqual(actual, expected)
+
+
+class PrependTests(TestCase):
+ def test_basic(self):
+ value = 'a'
+ iterator = iter('bcdefg')
+ actual = list(mi.prepend(value, iterator))
+ expected = list('abcdefg')
+ self.assertEqual(actual, expected)
+
+ def test_multiple(self):
+ value = 'ab'
+ iterator = iter('cdefg')
+ actual = tuple(mi.prepend(value, iterator))
+ expected = ('ab',) + tuple('cdefg')
+ self.assertEqual(actual, expected)
--
2.39.0.windows.2

View File

@ -1,12 +1,16 @@
Name: python-more-itertools
Version: 4.1.0
Release: 5
Release: 9
Summary: An opensource python library wrapping around itertools
License: MIT
URL: https://github.com/erikrose/more-itertools
Source0: https://pypi.io/packages/source/m/more-itertools/more-itertools-%{version}.tar.gz
BuildArch: noarch
Patch0000: python37.patch
Patch0001: 0001-fix-for-python-3.7-deprecation-warning-about-importi.patch
Patch0002: 0002-more.py-document-ilen.patch
Patch0003: 0003-Add-map_reduce-function-196.patch
Patch0004: 0004-Add-prepend-recipe.patch
%description
This is a python library for efficient use of itertools utility, which also \
@ -61,5 +65,17 @@ See https://pythonhosted.org/more-itertools/index.html for more information.
%{python3_sitelib}/more_itertools-%{version}-py%{python3_version}.egg-info
%changelog
* Thu Dec 28 2023 zhangliangpengkun<zhangliangpengkun@xfusion.com> - 4.1.0-9
- Add prepend recipe
* Fri Dec 22 2023 zhangliangpengkun<zhangliangpengkun@xfusion.com> - 4.1.0-8
- Add map_reduce function (#196)
* Thu Nov 30 2023 zhangliangpengkun<zhangliangpengkun@xfusion.com> - 4.1.0-7
- more.py: document ilen
* Thu Nov 16 2023 zhangliangpengkun<zhangliangpengkun@xfusion.com> - 4.1.0-6
- fix for python 3.7 deprecation warning about importing certain types from collections vs collections.abc
* Fri Nov 15 2019 sunguoshuai <sunguoshuai@huawei.com> - 4.1.0-5
- Package init