File: utils.py

package info (click to toggle)
python-mt-940 4.30.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,272 kB
  • sloc: python: 1,746; makefile: 201
file content (43 lines) | stat: -rw-r--r-- 693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import enum


def coalesce(*args):
    '''
    Return the first non-None argument

    >>> coalesce()

    >>> coalesce(0, 1)
    0
    >>> coalesce(None, 0)
    0
    '''

    for arg in args:
        if arg is not None:
            return arg


class Strip(enum.IntEnum):
    NONE = 0
    LEFT = 1
    RIGHT = 2
    BOTH = 3


def join_lines(string, strip=Strip.BOTH):
    '''
    Join strings together and strip whitespace in between if needed
    '''
    lines = []

    for line in string.splitlines():
        if strip & Strip.RIGHT:
            line = line.rstrip()

        if strip & Strip.LEFT:
            line = line.lstrip()

        lines.append(line)

    return ''.join(lines)