Browse Source

changed excel row enumeration

pull/1/head
Holger Frey 5 years ago
parent
commit
c6e0468899
  1. 27
      superx_budget/budget.py
  2. 23
      tests/test_budget_parser.py

27
superx_budget/budget.py

@ -14,6 +14,9 @@ EXPECTED_TABLE_HEADERS = [
"Rest", "Rest",
] ]
ExcelRow = namedtuple("ExcelRow", ["row", "data"])
BudgetData = namedtuple( BudgetData = namedtuple(
"BudgetData", "BudgetData",
[ [
@ -29,31 +32,31 @@ BudgetData = namedtuple(
) )
def _check_table_header(row): def _check_table_header(xl_row):
fields = [c.strip() for c in row[:7]] fields = [c.strip() for c in xl_row.data[:7]]
print(fields) print(fields)
print(EXPECTED_TABLE_HEADERS) print(EXPECTED_TABLE_HEADERS)
if fields != EXPECTED_TABLE_HEADERS: if fields != EXPECTED_TABLE_HEADERS:
raise BudgetParserError(f"unexpected headers: '{row}'") raise BudgetParserError(f"unexpected headers: '{xl_row.data}'")
def _skip_empty_lines(rows, start=0): def _skip_empty_lines(rows):
for i, row in enumerate(rows, start=start): for xl_row in rows:
first_cell = row[0] first_cell = xl_row.data[0]
if first_cell is None: if first_cell is None:
continue continue
if isinstance(first_cell, str) and first_cell.strip() == "": if isinstance(first_cell, str) and first_cell.strip() == "":
continue continue
yield i, row yield xl_row
def _parse_data_table(rows, start=2): def _parse_data_table(rows):
for i, data in _skip_empty_lines(rows, start): for xl_row in _skip_empty_lines(rows):
yield BudgetData(i, *data[:7]) yield BudgetData(xl_row.row, *xl_row.data[:7])
def parse_budget_data(xls_sheet): def parse_budget_data(xls_sheet):
""" parses the budget data """ """ parses the budget data """
rows = xls_sheet.values rows = (ExcelRow(i, v) for i, v in enumerate(xls_sheet.values, start=1))
_check_table_header(next(rows)) _check_table_header(next(rows))
return list(_parse_data_table(rows, start=2)) return list(_parse_data_table(rows))

23
tests/test_budget_parser.py

@ -21,19 +21,26 @@ def example_sheet(example_workbook):
def test_check_table_header_raises_error(): def test_check_table_header_raises_error():
from superx_budget.budget import _check_table_header from superx_budget.budget import _check_table_header, ExcelRow
from superx_budget.exceptions import BudgetParserError from superx_budget.exceptions import BudgetParserError
row = ["not", "the", "expected", "row"] row = ExcelRow(None, ["not", "the", "expected", "row"])
with pytest.raises(BudgetParserError): with pytest.raises(BudgetParserError):
_check_table_header(row) _check_table_header(row)
def test_skip_empty_lines(): def test_skip_empty_lines():
from superx_budget.budget import _skip_empty_lines from superx_budget.budget import _skip_empty_lines, ExcelRow
rows = [[""], ["one"], [None], [""], ["two"], [""]] rows = [
ExcelRow(0, [""]),
ExcelRow(1, ["one"]),
ExcelRow(2, [None]),
ExcelRow(3, [""]),
ExcelRow(4, ["two"]),
ExcelRow(5, [""]),
]
result = _skip_empty_lines(rows) result = _skip_empty_lines(rows)
@ -41,12 +48,12 @@ def test_skip_empty_lines():
def test_parse_data_table(): def test_parse_data_table():
from superx_budget.budget import _parse_data_table from superx_budget.budget import _parse_data_table, ExcelRow
rows = [ rows = [
list("ABCDEFG"), ExcelRow(2, list("ABCDEFG")),
[None for i in range(7)], ExcelRow(3, [None for i in range(7)]),
list("tuvwxyzX"), # one item more ExcelRow(4, list("tuvwxyzX")), # one item more
] ]
result = _parse_data_table(rows) result = _parse_data_table(rows)

Loading…
Cancel
Save