| Total Complexity | 17 |
| Total Lines | 132 |
| Duplicated Lines | 0 % |
| 1 | def limit(iterator, count): |
||
| 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 |