def test006_cells_more_that_max_fields(self): inp = ("testmail10testmail.com:testname0:testsurname0\n" "[email protected]:δοκιμαστικόόνομα:δοκιμαστικόεπίθετο\n" "[email protected]:όνομα3:επίθετο3\n") reader = CSVReader(inp, 1, 2) with pytest.raises(CSVCellError): next(reader)
def test003_readAllData(self): inp = ("testmail10testmail.com:testname0:testsurname0\n" "[email protected]:δοκιμαστικόόνομα:δοκιμαστικόεπίθετο\n" "[email protected]:όνομα3:επίθετο3\n") lines = 0 for nl in inp: if nl == '\n': lines += 1 reader = CSVReader(inp) assert len(list(reader)) == lines
def parse_csv(self, csv_file_path): data = [] with open(csv_file_path, 'rb') as f: reader = CSVReader(f, min_fields=2, max_fields=10) for row in reader: keys = ('inst', 'nr_voters', 'nr_voters_voted', 'start', 'end', 'uuid', 'election_name', 'admin', 'official') new_row = {} for index, key in enumerate(keys): new_row[key] = row[index] new_row['nr_polls'] = '-' data.append(new_row) self.csvData += data
def test011_invalid_arguments_min_fields_more(self): with pytest.raises(ValueError): CSVReader('', 10, 1)
def test010_invalid_arguments_fields_are_zero(self): with pytest.raises(ValueError): CSVReader('', 0, 0)
def test001_nodata(self): reader = CSVReader('') assert ValueError
def test002_data(self): inp = ("testmail10testmail.com:testname0:testsurname0\n" "[email protected]:δοκιμαστικόόνομα:δοκιμαστικόεπίθετο\n" "[email protected]:όνομα3:επίθετο3\n") reader = CSVReader(inp) assert len(list(reader)) > 0
def elections_from_csv(sort_keys=['polls', 'voters']): db = { 'turnout': -1, 'elections_count': 0, 'polls_count': 0, 'voters_count': 0, 'elections_held': {}, 'institutions_sorted': [], 'institutions': defaultdict(lambda: dict(name=None, elections=0, polls=0, voters=0, voted=0)) } csv_path = getattr(settings, 'ZEUS_ELECTION_STATS_CSV_PATH', None) if not csv_path: return db csvdata = '' with open(csv_path) as fd: csvdata = fd.read() csv = CSVReader(csvdata, max_fields=10, min_fields=5) insts = [] for row in csv: if not row[0]: continue insts.append([row[0], row[-1]]) setup_institutions(insts) csv = CSVReader(csvdata, max_fields=10, min_fields=5) election = {} for row in csv: if not row[0]: continue election['institution'] = get_institution(row[0]) try: int(row[1]) except ValueError: continue election['voters'] = int(row[1]) election['voted'] = int(row[2]) election['turnout'] = float(election['voters']) / float(election['voted']) election['start_at'] = row[3] election['end_at'] = row[4] election['id'] = row[5] election['name'] = row[6] election['user'] = row[7] election['index'] = election['institution'] + election['end_at'] + election['start_at'] INSTITUTIONS.append(election['institution']) if db['turnout'] == -1: db['turnout'] = election['turnout'] db['turnout'] = (db['turnout'] + election['turnout']) / 2 inst = db['institutions'][election['institution']] if election['index'] not in ELECTIONS: inst['elections'] += 1 inst['polls'] += 1 inst['voters'] += election['voters'] inst['name'] = election['institution'] db['polls_count'] += 1 db['voters_count'] += election['voters'] ELECTIONS.append(election['index']) db['elections_count'] = len(set(ELECTIONS)) db['institutions_count'] = len(set(INSTITUTIONS)) db['institutions'] = dict(db['institutions']) def cmp(a, b): result = 0 for key in sort_keys: val1 = a[key] valb = b[key] if val1 == valb: continue result = -1 if val1 > valb else 1 break return result db['institutions_sorted'] = sorted(db['institutions'].values(), cmp=cmp) return db
def test001_nodata(self): reader = CSVReader('') self.failUnless(ValueError)
def test005_cells_less_than_min_fields(self): inp = ("testmail10testmail.com:testname0:testsurname0\n" "[email protected]:δοκιμαστικόόνομα:δοκιμαστικόεπίθετο\n" "[email protected]:όνομα3:επίθετο3\n") reader = CSVReader(inp, 4) self.assertRaises(CSVCellError, reader.next)