This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra_value.py
46 lines (38 loc) · 1.54 KB
/
extra_value.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
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from ..registry import check
from ..error import Error
# Module API
@check('extra-value')
class ExtraValue(object):
def __init__(self, **options):
self._num_columns = None
def check_row(self, cells):
errors = []
# Check that all rows have the same number of columns
if self._num_columns is None:
self._num_columns = len(cells)
elif len(cells) > self._num_columns:
extra_cells = cells[self._num_columns:]
for cell in extra_cells:
error = Error('extra-value', cell)
errors.append(error)
cells.remove(cell)
return errors
# Check that if any cell has a header, all cells MUST have headers as
# well. This deals with the case of data files without headers.
headers = [cell.get('header') for cell in cells]
has_header = any(headers)
# should report extra value when cell does not have header
# and also has value in the cell
cells_without_header = filter(lambda cell: cell.get('header') is None
and cell.get('value'), cells)
if has_header and cells_without_header:
for cell in cells_without_header:
error = Error('extra-value', cell)
errors.append(error)
cells.remove(cell)
return errors