File: CatIterator.py

package info (click to toggle)
python-stringtemplate3 3.1-4
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 1,392 kB
  • sloc: python: 8,819; makefile: 20
file content (48 lines) | stat: -rw-r--r-- 1,123 bytes parent folder | download | duplicates (2)
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
44
45
46
47
48
from six import StringIO



## Given a list of lists, return the combined elements one by one.
#
class CatList(object):

    def __init__(self, lists):
	## List of lists to cat together 
	#
        self._lists = lists

    def __len__(self):
        k = 0
        for list_ in self._lists:
            k += len(list_)
        return k

    def lists(self):
        for list_ in self._lists:
            for item in list_:
                yield item

    def __iter__(self):
        for list_ in self._lists:
            for item in list_:
                yield item

        
    ## The result of asking for the string of a CatList is the list of
    #  items and so this is just the cat'd list of both items.  This
    #  is destructive in that the iterator cursors have moved to the end
    #  after printing.
    def __str__(self):
        buf = StringIO()
        #buf.write('[')
        k = len(self)
        for item in self.lists():
            buf.write(str(item))
            k -= 1
            #if k:
            #    buf.write(', ')
        #buf.write(']')

        return buf.getvalue()

    __repr__ = __str__