Coverage for mt940/utils.py: 0%

19 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2025-03-01 10:17 +0000

1import enum 

2 

3 

4def coalesce(*args): 

5 ''' 

6 Return the first non-None argument 

7 

8 >>> coalesce() 

9 

10 >>> coalesce(0, 1) 

11 0 

12 >>> coalesce(None, 0) 

13 0 

14 ''' 

15 

16 for arg in args: 

17 if arg is not None: 

18 return arg 

19 

20 

21class Strip(enum.IntEnum): 

22 NONE = 0 

23 LEFT = 1 

24 RIGHT = 2 

25 BOTH = 3 

26 

27 

28def join_lines(string, strip=Strip.BOTH): 

29 ''' 

30 Join strings together and strip whitespace in between if needed 

31 ''' 

32 lines = [] 

33 

34 for line in string.splitlines(): 

35 if strip & Strip.RIGHT: 

36 line = line.rstrip() 

37 

38 if strip & Strip.LEFT: 

39 line = line.lstrip() 

40 

41 lines.append(line) 

42 

43 return ''.join(lines)