Skip to content

Commit 5b08f43

Browse files
committed
Added new functions for v2, and removed deprecated functions.
1 parent 78106ff commit 5b08f43

File tree

10 files changed

+161
-1059
lines changed

10 files changed

+161
-1059
lines changed

domdf_python_tools/iterative.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@
3232
# stdlib
3333
import itertools
3434
import textwrap
35+
from operator import itemgetter
3536
from typing import Any, Callable, Generator, Iterable, Iterator, List, Optional, Sequence, Tuple, Type, Union
3637

3738
# 3rd party
3839
from natsort import natsorted, ns # type: ignore
3940

41+
# this package
42+
from domdf_python_tools.utils import magnitude
43+
4044
__all__ = [
4145
"chunks",
4246
"permutations",
@@ -47,6 +51,8 @@
4751
"make_tree",
4852
"natmin",
4953
"natmax",
54+
"groupfloats",
55+
"ranges_from_iterable",
5056
]
5157

5258

@@ -250,3 +256,69 @@ def natmax(seq: Iterable, key: Optional[Callable] = None, alg: int = ns.DEFAULT)
250256
"""
251257

252258
return natsorted(seq, key=key, alg=alg)[-1]
259+
260+
261+
_group = Tuple[float, ...]
262+
263+
264+
def groupfloats(
265+
iterable: Iterable[float],
266+
step: float = 1,
267+
) -> Iterable[_group]:
268+
"""
269+
Returns an iterator over the discrete ranges of values in ``iterable``.
270+
271+
For example:
272+
273+
.. code-block:: python
274+
275+
>>> list(groupfloats([170.0, 170.05, 170.1, 170.15, 171.05, 171.1, 171.15, 171.2], step=0.05))
276+
[(170.0, 170.05, 170.1, 170.15), (171.05, 171.1, 171.15, 171.2)]
277+
>>> list(groupfloats([1, 2, 3, 4, 5, 7, 8, 9, 10]))
278+
[(1, 2, 3, 4, 5), (7, 8, 9, 10)]
279+
280+
:param iterable:
281+
:param step: The step between values in ``iterable``.
282+
283+
.. seealso::
284+
285+
:func:`~.ranges_from_iterable`, which returns an iterator over the min and max values for each range.
286+
287+
.. versionadded:: 2.0.0
288+
"""
289+
290+
# Based on https://stackoverflow.com/a/4629241
291+
# By user97370
292+
# CC BY-SA 4.0
293+
294+
modifier = 1 / 10**magnitude(step)
295+
296+
a: float
297+
b: Iterable[_group]
298+
299+
def key(pair):
300+
return (pair[1] * modifier) - ((pair[0] * modifier) * step)
301+
302+
for a, b in itertools.groupby(enumerate(iterable), key=key):
303+
yield tuple(map(itemgetter(1), list(b)))
304+
305+
306+
def ranges_from_iterable(iterable: Iterable[float], step: float = 1) -> Iterable[Tuple[float, float]]:
307+
"""
308+
Returns an iterator over the minimum and maximum values for each discrete ranges of values in ``iterable``.
309+
310+
For example:
311+
312+
.. code-block:: python
313+
314+
>>> list(ranges_from_iterable([170.0, 170.05, 170.1, 170.15, 171.05, 171.1, 171.15, 171.2], step=0.05))
315+
[(170.0, 170.15), (171.05, 171.2)]
316+
>>> list(ranges_from_iterable([1, 2, 3, 4, 5, 7, 8, 9, 10]))
317+
[(1, 5), (7, 10)]
318+
319+
:param iterable:
320+
:param step: The step between values in ``iterable``.
321+
"""
322+
323+
for group in groupfloats(iterable, step):
324+
yield group[0], group[-1]

domdf_python_tools/terminal.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
# terminal.py
44
"""
55
Useful functions for terminal-based programs.
6+
7+
.. versionchanged:: 2.0.0
8+
9+
:func:`domdf_python_tools.terminal.get_terminal_size` was removed.
10+
Use :func:`shutil.get_terminal_size` instead.
611
"""
712
#
813
# Copyright © 2014-2020 Dominic Davis-Foster <[email protected]>
@@ -59,13 +64,11 @@
5964
import inspect
6065
import os
6166
import pprint
62-
import shutil
6367
import textwrap
64-
from typing import IO, Tuple
68+
from shutil import get_terminal_size
69+
from typing import IO
6570

6671
# this package
67-
from domdf_python_tools import __version__
68-
from domdf_python_tools.utils import deprecated
6972
from domdf_python_tools.words import CR
7073

7174
__all__ = [
@@ -145,24 +148,6 @@ def overtype(*objects, sep: str = ' ', end: str = '', file: IO = None, flush: bo
145148
print(*objects, sep=sep, end=end, file=file, flush=flush)
146149

147150

148-
@deprecated(
149-
deprecated_in="1.0.0",
150-
removed_in="2.0.0",
151-
current_version=__version__,
152-
details="Use :func:`shutil.get_terminal_size` instead.",
153-
)
154-
def get_terminal_size() -> Tuple[int, int]: # pragma: no cover
155-
"""
156-
Get width and height of console.
157-
158-
Works on Linux, macOS, Windows, and Cygwin.
159-
160-
:return: Screen width and screen height.
161-
"""
162-
163-
return shutil.get_terminal_size((80, 25))
164-
165-
166151
class Echo:
167152
"""
168153
Context manager for echoing variable assignments (in CPython).

0 commit comments

Comments
 (0)