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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
"""Election database builder
Steven Marks
2011-03-21
"""
from __future__ import division
import os
import csv
statenames = {'ACT': 'Australian Capital Territory',
'NSW': 'New South Wales',
'NT': 'Northern Territory',
'QLD': 'Queensland',
'SA': 'South Australia',
'TAS': 'Tasmania',
'VIC': 'Victoria',
'WA': 'Western Australia'}
# Temporary fixed filename
testfile = '/home/steve/Python/psephology/testfile.txt'
class ElectionTable(object):
titlemarker = '[Event:'
def __init__(self):
self.file = None
self.firstrow = None
self.headings = []
self.data = None
self.filename = ""
self.title = ""
self.eventID = ""
def readfile(self):
with open(self.filename, 'r') as datafile:
self.firstrow = datafile.readline()
def getmetadata(self):
fr = self.firstrow
self.title = fr[0:fr.find(self.titlemarker)].strip()
self.eventID = "TBA"
def getdata(filename):
with open(filename, 'r') as datafile:
data = [row for row in datafile]
return data
def CSV_preheader(filename, maxline=16):
"""Calculates the number of introductory rows before the CSV data header.
"""
with open(filename, 'r') as datafile:
line = -1
hasheader = False
while not hasheader:
line += 1
if line > maxline:
raise StandardError("Header not found")
datafile.seek(0)
for row in range(line):
datafile.readline()
hasheader = csv.Sniffer().has_header(datafile.read(8192))
return line
def parsedata(data):
"""
Data should be a list.
"""
pass
class Electorate(object):
def __init__(self, ident=None, name=''):
self.ident = ident
self.name = name.upper()
class PollingPlace(object):
pass
def main():
# load the data from a file
# parse the data
# save the data in a database
pass
if __name__ == "__main__":
import doctest
doctest.testmod()
|