• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Nic30 / hwt / 8b945dd2-6cbf-43b3-be54-881b32d4f5e4

10 Jun 2025 09:35PM UTC coverage: 84.734% (-2.6%) from 87.284%
8b945dd2-6cbf-43b3-be54-881b32d4f5e4

push

circleci

Nic30
test: fix call of unittestMain

3415 of 4468 branches covered (76.43%)

Branch coverage included in aggregate %.

11877 of 13579 relevant lines covered (87.47%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.96
/hwt/pyUtils/arrayQuery.py
1
# select = map, groupBy = itertools.groupby
2
from collections import deque
1✔
3
from itertools import zip_longest
1✔
4
from math import inf
1✔
5
from types import GeneratorType
1✔
6

7
from hdlConvertorAst.to.hdlUtils import iter_with_last
1✔
8
from hwt.constants import NOT_SPECIFIED
1✔
9
from typing import Sequence
1✔
10

11

12
class DuplicitValueExc(Exception):
1✔
13
    """
14
    Exception which means that there are multiple items which this query
15
    selected but it should return only one
16
    """
17

18

19
class NoValueExc(Exception):
1✔
20
    """
21
    Exception which means that query did not selected any item
22
    """
23

24

25
def single(iterable, fn):
1✔
26
    """
27
    Get value from iterable where fn(item) and check
28
    if there is not fn(other item)
29

30
    :raise DuplicitValueExc: when there are multiple items satisfying fn()
31
    :raise NoValueExc: when no value satisfying fn(item) found
32
    """
33
    found = False
1✔
34
    ret = None
1✔
35

36
    for i in iterable:
1✔
37
        if fn(i):
1✔
38
            if found:
1!
39
                raise DuplicitValueExc(i)
×
40
            found = True
1✔
41
            ret = i
1✔
42

43
    if not found:
1!
44
        raise NoValueExc()
×
45

46
    return ret
1✔
47

48

49
def arr_any(iterable, fn):
1✔
50
    """
51
    :return: True if fn(item) for any item else False
52
    """
53
    for item in iterable:
1✔
54
        if fn(item):
1✔
55
            return True
1✔
56
    return False
1✔
57

58

59
def arr_all(iterable, fn):
1✔
60
    """
61
    :return: True if fn(item) for all items in interable or iterable
62
        is empty else False
63
    """
64
    for item in iterable:
1✔
65
        if not fn(item):
1✔
66
            return False
1✔
67
    return True
1✔
68

69

70
def take(iterrable, howMay):
1✔
71
    """
72
    :return: generator of first n items from iterrable
73
    """
74
    assert howMay >= 0
1✔
75

76
    if not howMay:
1!
77
        return
×
78

79
    last = howMay - 1
1✔
80
    for i, item in enumerate(iterrable):
1!
81
        yield item
1✔
82
        if i == last:
1✔
83
            return
1✔
84

85

86
def where(iterable, fn):
1✔
87
    """
88
    :return: generator of items from iterable where fn(item)
89
    """
90
    for i in iterable:
1✔
91
        if fn(i):
1✔
92
            yield i
1✔
93

94

95
def groupedby(collection, fn):
1✔
96
    """
97
    same like itertools.groupby
98

99
    :note: This function does not needs initial sorting like itertools.groupby
100

101
    :attention: Order of pairs is not deterministic.
102
    """
103
    d = {}
1✔
104
    for item in collection:
1✔
105
        k = fn(item)
1✔
106
        try:
1✔
107
            arr = d[k]
1✔
108
        except KeyError:
1✔
109
            arr = []
1✔
110
            d[k] = arr
1✔
111
        arr.append(item)
1✔
112

113
    yield from d.items()
1✔
114

115

116
def flatten(iterables, level=inf):
1✔
117
    """
118
    Flatten nested lists, tuples, generators and maps
119

120
    :param level: maximum depth of flattening
121
    """
122
    if level >= 0 and isinstance(iterables, (list, tuple, GeneratorType,
1✔
123
                                             map, zip, set, deque)):
124
        level -= 1
1✔
125
        for i in iterables:
1✔
126
            yield from flatten(i, level=level)
1✔
127
    else:
128
        yield iterables
1✔
129

130

131
def grouper(n: int, iterable: Sequence, padvalue=None):
1✔
132
    """grouper(3, 'abcdefg', 'x') -->
133
       ('a','b','c'), ('d','e','f'), ('g','x','x')
134
    """
135
    return zip_longest(*[iter(iterable)] * n, fillvalue=padvalue)
1✔
136

137

138
def areSetsIntersets(setA: set, setB: set):
1✔
139
    """
140
    Check if intersection of sets is not empty
141
    """
142
    return any(x in setA for x in setB)
1✔
143

144

145
def balanced_reduce(arr: Sequence, opFn):
1✔
146
    while len(arr) > 1:
1✔
147
        nextArr = []
1✔
148
        for a, b in grouper(2, arr, NOT_SPECIFIED):
1✔
149
            if b is NOT_SPECIFIED:
1!
150
                # if number of items was odd we have 1 leftover
151
                nextArr.append(a)
×
152
            else:
153
                nextArr.append(opFn(a, b))
1✔
154
        arr = nextArr
1✔
155

156
    return arr[0]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc