blob: 705c61975c10df1adec713aaad3d888b16342fb3 (
plain)
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
|
# tabular_data.py
from collections import namedtuple
def headtail(iterable):
"Returns the head and the tail of a non-empty iterator"
it = iter(iterable)
return it.next(), it
def get_table(header_plus_body, ntuple=None):
"Return a sequence of namedtuples in the form header+body"
header, body = headtail(header_plus_body)
ntuple = ntuple or namedtuple('NamedTuple', header)
yield ntuple(*header)
for row in body:
yield ntuple(*row)
def test1():
"Read the fields from the first row"
data = [['title', 'author'], ['Records in Python', 'M. Simionato']]
for nt in get_table(data):
print nt
def test2():
"Use a predefined namedtuple class"
data = [['title', 'author'], ['Records in Python', 'M. Simionato']]
for nt in get_table(data, namedtuple('NamedTuple', 'tit auth')):
print nt
if __name__ == '__main__':
test1()
test2()
|