|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# iterative.py |
| 4 | +""" |
| 5 | +Functions for iteration, looping etc. |
| 6 | +
|
| 7 | +.. versionadded:: 1.4.0 |
| 8 | +""" |
| 9 | +# |
| 10 | +# Copyright © 2018-2020 Dominic Davis-Foster <[email protected]> |
| 11 | +# |
| 12 | +# This program is free software; you can redistribute it and/or modify |
| 13 | +# it under the terms of the GNU Lesser General Public License as published by |
| 14 | +# the Free Software Foundation; either version 3 of the License, or |
| 15 | +# (at your option) any later version. |
| 16 | +# |
| 17 | +# This program is distributed in the hope that it will be useful, |
| 18 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | +# GNU Lesser General Public License for more details. |
| 21 | +# |
| 22 | +# You should have received a copy of the GNU Lesser General Public License |
| 23 | +# along with this program; if not, write to the Free Software |
| 24 | +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 25 | +# MA 02110-1301, USA. |
| 26 | +# |
| 27 | +# chunks from https://stackoverflow.com/a/312464/3092681 |
| 28 | +# Copyright © 2008 Ned Batchelder |
| 29 | +# Licensed under CC-BY-SA |
| 30 | +# |
| 31 | + |
| 32 | +# stdlib |
| 33 | +import itertools |
| 34 | +import textwrap |
| 35 | +from typing import Any, Generator, Iterable, Iterator, List, Sequence, Tuple, Type, Union |
| 36 | + |
| 37 | +__all__ = [ |
| 38 | + "chunks", |
| 39 | + "permutations", |
| 40 | + "split_len", |
| 41 | + "Len", |
| 42 | + "double_chain", |
| 43 | + "flatten", |
| 44 | + "make_tree", |
| 45 | + ] |
| 46 | + |
| 47 | + |
| 48 | +def chunks(l: Sequence[Any], n: int) -> Generator[Any, None, None]: |
| 49 | + """ |
| 50 | + Yield successive n-sized chunks from l. |
| 51 | +
|
| 52 | + :param l: The objects to yield chunks from |
| 53 | + :param n: The size of the chunks |
| 54 | +
|
| 55 | + .. versionchanged:: 1.4.0 Moved from :mod:`domdf_python_tools.utils` |
| 56 | + """ |
| 57 | + |
| 58 | + for i in range(0, len(l), n): |
| 59 | + yield l[i:i + n] |
| 60 | + |
| 61 | + |
| 62 | +def permutations(data: Iterable[Any], n: int = 2) -> List[Tuple[Any, ...]]: |
| 63 | + """ |
| 64 | + Return permutations containing ``n`` items from ``data`` without any reverse duplicates. |
| 65 | +
|
| 66 | + If ``n`` is equal to or greater than the length of the data an empty list of returned. |
| 67 | +
|
| 68 | + :param data: |
| 69 | + :param n: |
| 70 | +
|
| 71 | + .. versionchanged:: 1.4.0 Moved from :mod:`domdf_python_tools.utils` |
| 72 | + """ |
| 73 | + |
| 74 | + if n == 0: |
| 75 | + raise ValueError("'n' cannot be 0") |
| 76 | + |
| 77 | + perms = [] |
| 78 | + for i in itertools.permutations(data, n): |
| 79 | + if i[::-1] not in perms: |
| 80 | + perms.append(i) |
| 81 | + |
| 82 | + return perms |
| 83 | + |
| 84 | + |
| 85 | +def split_len(string: str, n: int) -> List[str]: |
| 86 | + """ |
| 87 | + Split a string every ``n`` characters. |
| 88 | +
|
| 89 | + :param string: The string to split |
| 90 | + :param n: The number of characters to split after |
| 91 | +
|
| 92 | + :return: The split string |
| 93 | +
|
| 94 | + .. versionchanged:: 1.4.0 Moved from :mod:`domdf_python_tools.utils` |
| 95 | + """ |
| 96 | + |
| 97 | + return [string[i:i + n] for i in range(0, len(string), n)] |
| 98 | + |
| 99 | + |
| 100 | +def Len(obj: Any, start: int = 0, step: int = 1) -> range: |
| 101 | + """ |
| 102 | + Shorthand for ``range(len(obj))``. |
| 103 | +
|
| 104 | + Returns an object that produces a sequence of integers from start (inclusive) |
| 105 | + to len(obj) (exclusive) by step. |
| 106 | +
|
| 107 | + :param obj: The object to iterate over the length of. |
| 108 | + :param start: The start value of the range. |
| 109 | + :param step: The step of the range. |
| 110 | +
|
| 111 | + .. versionadded:: 0.4.7 |
| 112 | +
|
| 113 | + .. versionchanged:: 1.4.0 Moved from :mod:`domdf_python_tools.utils` |
| 114 | + """ |
| 115 | + |
| 116 | + return range(start, len(obj), step) |
| 117 | + |
| 118 | + |
| 119 | +def double_chain(iterable: Iterable[Iterable]): |
| 120 | + """ |
| 121 | + Flatten a list of lists of lists into a single list. |
| 122 | +
|
| 123 | + Literally just: |
| 124 | +
|
| 125 | + .. code-block:: python |
| 126 | +
|
| 127 | + chain.from_iterable(chain.from_iterable(iterable)) |
| 128 | +
|
| 129 | + Converts |
| 130 | +
|
| 131 | + .. code-block:: python |
| 132 | +
|
| 133 | + [[(1, 2), (3, 4)], [(5, 6), (7, 8)]] |
| 134 | +
|
| 135 | + to |
| 136 | +
|
| 137 | + .. code-block:: python |
| 138 | +
|
| 139 | + [1, 2, 3, 4, 5, 6, 7, 8] |
| 140 | +
|
| 141 | +
|
| 142 | + :param iterable: The iterable to chain. |
| 143 | +
|
| 144 | + :rtype: |
| 145 | +
|
| 146 | + .. versionadded:: 0.4.7 |
| 147 | +
|
| 148 | + .. versionchanged:: 1.4.0 Moved from :mod:`domdf_python_tools.utils` |
| 149 | + """ |
| 150 | + |
| 151 | + yield from itertools.chain.from_iterable(itertools.chain.from_iterable(iterable)) |
| 152 | + |
| 153 | + |
| 154 | +def flatten(iterable: Iterable, primitives: Tuple[Type] = (str, int, float)): |
| 155 | + """ |
| 156 | + Flattens a mixed list of primitive types and iterables of those types into a single list, |
| 157 | + regardless of nesting. |
| 158 | +
|
| 159 | + :param iterable: |
| 160 | + :param primitives: The primitive types to allow. |
| 161 | +
|
| 162 | + .. versionadded:: 1.4.0 |
| 163 | + """ # noqa: D400 |
| 164 | + |
| 165 | + for item in iterable: |
| 166 | + if isinstance(item, primitives): |
| 167 | + yield item |
| 168 | + elif isinstance(item, Iterable): |
| 169 | + yield from flatten(item) |
| 170 | + else: |
| 171 | + raise NotImplementedError |
| 172 | + |
| 173 | + |
| 174 | +Branch = Union[List[str], List["Branch"]] |
| 175 | + |
| 176 | + |
| 177 | +def make_tree(tree: Branch) -> Iterator[str]: |
| 178 | + """ |
| 179 | + Returns the string representation of a mixed list of strings and lists of strings, |
| 180 | + similar to :manpage:`tree(1)`. |
| 181 | +
|
| 182 | + :param tree: |
| 183 | +
|
| 184 | + .. versionadded:: 1.4.0 |
| 185 | + """ # noqa: D400 |
| 186 | + |
| 187 | + last_string = 0 |
| 188 | + for idx, entry in enumerate(tree): |
| 189 | + if isinstance(entry, str): |
| 190 | + last_string = idx |
| 191 | + |
| 192 | + for idx, entry in enumerate(tree[:-1]): |
| 193 | + if isinstance(entry, str): |
| 194 | + if idx > last_string: |
| 195 | + yield f"│ {entry}" |
| 196 | + elif idx == last_string: |
| 197 | + yield f"└── {entry}" |
| 198 | + else: |
| 199 | + yield f"├── {entry}" |
| 200 | + |
| 201 | + elif isinstance(entry, Iterable): |
| 202 | + for line in make_tree(entry): |
| 203 | + if idx - 1 == last_string: |
| 204 | + yield textwrap.indent(line, "└── ") |
| 205 | + else: |
| 206 | + yield textwrap.indent(line, "│ ") |
| 207 | + |
| 208 | + if tree: |
| 209 | + if isinstance(tree[-1], str): |
| 210 | + yield f"└── {tree[-1]}" |
| 211 | + elif isinstance(tree[-1], Iterable): |
| 212 | + for line in make_tree(tree[-1]): |
| 213 | + yield textwrap.indent(line, " ") |
0 commit comments