forked from AllenDowney/ThinkStats2
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtable.py
More file actions
86 lines (67 loc) · 2.27 KB
/
Copy pathtable.py
File metadata and controls
86 lines (67 loc) · 2.27 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
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
84
85
"""This file contains code for use with "Think Stats",
by Allen B. Downey, available from greenteapress.com
Copyright 2010 Allen B. Downey
License: GNU GPLv3 http://www.gnu.org/licenses/gpl.html
"""
import gzip
import os
class Record(object):
"""Represents a record."""
class Table(object):
"""Represents a table as a list of objects"""
def __init__(self):
self.records = []
def __len__(self):
return len(self.records)
def ReadFile(self, data_dir, filename, constructor, n=None):
"""Reads a compressed data file builds one object per record.
Args:
data_dir: string directory name
filename: string name of the file to read
constructor: what kind of object to create
"""
filename = os.path.join(data_dir, filename)
if filename.endswith('gz'):
fp = gzip.open(filename)
else:
fp = open(filename)
for i, line in enumerate(fp):
if i == n:
break
record = self.MakeRecord(line, constructor)
self.AddRecord(record)
fp.close()
def MakeRecord(self, line, constructor):
"""Scans a line and returns an object with the appropriate fields.
Args:
line: string line from a data file
constructor: callable that makes an object for the record.
Returns:
Record with appropriate fields.
"""
obj = constructor()
for (field, start, end, cast) in self.GetFields():
try:
s = line[start-1:end]
val = cast(s)
except ValueError:
#print line
#print field, start, end, s
val = 'NA'
setattr(obj, field, val)
return obj
def AddRecord(self, record):
"""Adds a record to this table.
Args:
record: an object of one of the record types.
"""
self.records.append(record)
def ExtendRecords(self, records):
"""Adds records to this table.
Args:
records: a sequence of record object
"""
self.records.extend(records)
def Recode(self):
"""Child classes can override this to recode values."""
pass