summaryrefslogtreecommitdiffstats
path: root/venv/lib/python3.9/site-packages/toolz/recipes.py
diff options
context:
space:
mode:
authornoptuno <repollo.marrero@gmail.com>2023-04-28 02:29:30 +0200
committernoptuno <repollo.marrero@gmail.com>2023-04-28 02:29:30 +0200
commit355dee533bb34a571b9367820a63cccb668cf866 (patch)
tree838af886b4fec07320aeb10f0d1e74ba79e79b5c /venv/lib/python3.9/site-packages/toolz/recipes.py
parentadded pyproject.toml file (diff)
downloadgpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.gz
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.bz2
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.lz
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.xz
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.tar.zst
gpt4free-355dee533bb34a571b9367820a63cccb668cf866.zip
Diffstat (limited to 'venv/lib/python3.9/site-packages/toolz/recipes.py')
-rw-r--r--venv/lib/python3.9/site-packages/toolz/recipes.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/venv/lib/python3.9/site-packages/toolz/recipes.py b/venv/lib/python3.9/site-packages/toolz/recipes.py
new file mode 100644
index 00000000..89de88db
--- /dev/null
+++ b/venv/lib/python3.9/site-packages/toolz/recipes.py
@@ -0,0 +1,46 @@
+import itertools
+from .itertoolz import frequencies, pluck, getter
+
+
+__all__ = ('countby', 'partitionby')
+
+
+def countby(key, seq):
+ """ Count elements of a collection by a key function
+
+ >>> countby(len, ['cat', 'mouse', 'dog'])
+ {3: 2, 5: 1}
+
+ >>> def iseven(x): return x % 2 == 0
+ >>> countby(iseven, [1, 2, 3]) # doctest:+SKIP
+ {True: 1, False: 2}
+
+ See Also:
+ groupby
+ """
+ if not callable(key):
+ key = getter(key)
+ return frequencies(map(key, seq))
+
+
+def partitionby(func, seq):
+ """ Partition a sequence according to a function
+
+ Partition `s` into a sequence of lists such that, when traversing
+ `s`, every time the output of `func` changes a new list is started
+ and that and subsequent items are collected into that list.
+
+ >>> is_space = lambda c: c == " "
+ >>> list(partitionby(is_space, "I have space"))
+ [('I',), (' ',), ('h', 'a', 'v', 'e'), (' ',), ('s', 'p', 'a', 'c', 'e')]
+
+ >>> is_large = lambda x: x > 10
+ >>> list(partitionby(is_large, [1, 2, 1, 99, 88, 33, 99, -1, 5]))
+ [(1, 2, 1), (99, 88, 33, 99), (-1, 5)]
+
+ See also:
+ partition
+ groupby
+ itertools.groupby
+ """
+ return map(tuple, pluck(1, itertools.groupby(seq, key=func)))