forked from AllenDowney/ThinkBayes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
columns.py
84 lines (61 loc) · 1.78 KB
/
columns.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""This file contains code related to "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2012 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import csv
def read_csv(filename, constructor):
"""Reads a CSV file, returns the header line and a list of objects.
filename: string filename
"""
fp = open(filename)
reader = csv.reader(fp)
header = reader.next()
names = [s.lower() for s in header]
objs = [make_object(t, names, constructor) for t in reader]
fp.close()
return objs
def write_csv(filename, header, data):
"""Writes a CSV file
filename: string filename
header: list of strings
data: list of rows
"""
fp = open(filename, 'w')
writer = csv.writer(fp)
writer.writerow(header)
for t in data:
writer.writerow(t)
fp.close()
def print_cols(cols):
"""Prints the index and first two elements for each column.
cols: list of columns
"""
for i, col in enumerate(cols):
print i, col[0], col[1]
def make_col_dict(cols, names):
"""Selects columns from a dataset and returns a map from name to column.
cols: list of columns
names: list of names
"""
col_dict = {}
for name, col in zip(names, cols):
col_dict[name] = col
return col_dict
def make_object(row, names, constructor):
"""Turns a row of values into an object.
row: row of values
names: list of attribute names
constructor: function that makes the objects
Returns: new object
"""
obj = constructor()
for name, val in zip(names, row):
func = constructor.convert.get(name, int)
try:
val = func(val)
except:
pass
setattr(obj, name, val)
obj.clean()
return obj