Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/unblob/iter_utils.py: 46%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

13 statements  

1import itertools 

2from collections.abc import Iterable, Iterator 

3from typing import TypeVar 

4 

5T = TypeVar("T") 

6 

7 

8def pairwise(iterable: Iterable[T]) -> Iterator[tuple[T, T]]: 

9 # Copied from Python 3.10 

10 # pairwise('ABCDEFG') --> AB BC CD DE EF FG 

11 a, b = itertools.tee(iterable) 

12 next(b, None) 

13 return zip(a, b) 

14 

15 

16def get_intervals(values: list[int]) -> list[int]: 

17 """Get all the intervals between numbers. 

18 

19 It's similar to numpy.diff function. 

20 

21 Example: 

22 ------- 

23 >>> get_intervals([1, 4, 5, 6, 10]) 

24 [3, 1, 1, 4] 

25 

26 """ 

27 all_diffs = [] 

28 for value, next_value in pairwise(values): 

29 all_diffs.append(next_value - value) 

30 return all_diffs