-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstr_aware_list.py
More file actions
42 lines (33 loc) · 1.02 KB
/
str_aware_list.py
File metadata and controls
42 lines (33 loc) · 1.02 KB
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
from collections import Iterable
class StrAwareList(Iterable):
def __init__(self, output=None):
if output is None:
output = []
self._output = output
def sep(self):
self += '' if self._output.__class__ == list else '\n'
return self
def __str__(self):
if self._output.__class__ == list:
return '\n'.join(self._output)
else:
return str(self._output)
__repr__ = __str__
def __len__(self):
try:
return getattr(self._output, "__len__")()
except AttributeError:
return 0
def __iter__(self):
for e in self._output:
yield e
def __iadd__(self, other):
self.__add__(other)
return self
def __add__(self, other):
if isinstance(other, Iterable) and not isinstance(other, str) and not isinstance(other, bytes):
self._output.extend(other)
return self
else:
self._output.append(other)
return self