Coverage for liitos/patch.py: 100.00%

12 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-05 17:22:35 +00:00

1"""Apply all pairs in patches to incoming.""" 

2 

3from collections.abc import Iterable 

4 

5from liitos import log 

6 

7 

8def apply(patches: list[tuple[str, str]], incoming: Iterable[str]) -> list[str]: 

9 """Apply all pairs in patches to incoming strings. 

10 

11 Examples: 

12 

13 >>> incoming = ['a', 'b', 'cb', 'd'] 

14 >>> patches = [('b', 'x'), ('c', ''), ('y', 'foo')] 

15 >>> apply(patches, incoming) 

16 ['a', 'x', 'x', 'd'] 

17 """ 

18 outgoing = [line for line in incoming] 

19 

20 log.info(f'applying patches to {len(outgoing)} lines of text') 

21 for this, that in patches: 

22 log.info(f' - trying any ({this}) --> ({that}) ...') 

23 for n, text in enumerate(outgoing): 

24 if this in text: 

25 log.info(f' + found match ({text})') 

26 outgoing[n] = text.replace(this, that) 

27 

28 return outgoing