| 1 |
import enum |
| 2 |
|
| 3 |
|
| 4 |
def 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 |
|
| 21 |
class Strip(enum.IntEnum): |
| 22 |
NONE = 0 |
| 23 |
LEFT = 1 |
| 24 |
RIGHT = 2 |
| 25 |
BOTH = 3 |
| 26 |
|
| 27 |
|
| 28 |
def 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) |