Failed Conditions
Pull Request — master (#1152)
by Lasse
03:36
created

coalib.parsing.StringProcessing.iglob()   F

Complexity

Conditions 12

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 12
dl 0
loc 39
rs 2.7856

How to fix   Complexity   

Complexity

Complex classes like coalib.parsing.StringProcessing.iglob() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
def limit(iterator, count):
2
    """
3
    A filter that removes all elements behind the set limit.
4
5
    :param iterator: The iterator to be filtered.
6
    :param count:    The iterator limit. All elements at positions bigger than
7
                     this limit are trimmed off. Exclusion: 0 or numbers below
8
                     does not limit at all, means the passed iterator is
9
                     completely yielded.
10
    """
11
    if count <= 0:  # Performance branch
12
        for elem in iterator:
13
            yield elem
14
    else:
15
        for elem in iterator:
16
            yield elem
17
            count -= 1
18
            if count == 0:
19
                break
20
21
22
def trim_empty_matches(iterator, groups=(0,)):
23
    """
24
    A filter that removes empty match strings. It can only operate on iterators
25
    whose elements are of type MatchObject.
26
27
    :param iterator: The iterator to be filtered.
28
    :param groups:   An iteratable defining the groups to check for blankness.
29
                     Only results are not yielded if all groups of the match
30
                     are blank.
31
                     You can not only pass numbers but also strings, if your
32
                     MatchObject contains named groups.
33
    """
34
    for elem in iterator:
35
        if any(len(elem.group(group)) > 0 for group in groups):
36
            yield elem
37