-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFragment.py
37 lines (27 loc) · 1.18 KB
/
Fragment.py
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
from Text import Text
import itertools
#TODO: Not generalized; should be in html?
def _iter(a):
if isinstance(a, str):
return [Text(a)]
try:
#Must be iterable for itertools.chain to work
iter(a)
return a
except:
return [a]
class Fragment(list):
"""Fragment: a collection on nodes.
Use this when you don't have a parent element"""
def __init__(self, argList = [], *args, **kwargs):
#TypeError: list() takes at most 1 argument (2 given)
fragItems = itertools.ifilter(None, #Filter out empty
itertools.chain(_iter(argList), itertools.chain.from_iterable( #Support two syntaxes
itertools.imap(_iter,args))))
super(Fragment, self).__init__(fragItems)
def __str__(self):
return '\n'.join(map(str, iter(self)))
def __repr___(self):
return ',\n'.join(map(repr, self))
def add(self, argList=[], *args):
self.extend(Fragment(argList, *args))