add tests

This commit is contained in:
iuvbio 2019-01-27 04:14:12 +01:00
parent 4e282676da
commit 0e89be912e
2 changed files with 45 additions and 9 deletions

View File

@ -12,12 +12,12 @@ def ods_info(doc):
sheet.nrows(), sheet.ncols())) sheet.nrows(), sheet.ncols()))
def load_ods(doc, sheet, header=True, columns=None): def load_ods(doc, sheet, headers=True, columns=None):
# convert the sheet to a pandas.DataFrame # convert the sheet to a pandas.DataFrame
if isinstance(sheet, int): if isinstance(sheet, int):
sheet = doc.sheets[sheet - 1] sheet = doc.sheets[sheet - 1]
elif isinstance(sheet, str): elif isinstance(sheet, str):
sheets = [sheet for sheet in doc.sheets] sheets = [sheet.name for sheet in doc.sheets]
if sheet not in sheets: if sheet not in sheets:
raise ValueError("There is no sheet named {}".format(sheet)) raise ValueError("There is no sheet named {}".format(sheet))
sheet_idx = sheets.index(sheet) sheet_idx = sheets.index(sheet)
@ -26,14 +26,14 @@ def load_ods(doc, sheet, header=True, columns=None):
col_index = {} col_index = {}
for i, row in enumerate(sheet.rows()): for i, row in enumerate(sheet.rows()):
# row is a list of cells # row is a list of cells
if header and i == 0: if headers and i == 0:
# columns as lists in a dictionary # columns as lists in a dictionary
df_dict = {cell.value: [] for cell in row if cell.value} df_dict = {cell.value: [] for cell in row if cell.value}
# create index for the column headers # create index for the column headers
col_index = { col_index = {
j: cell.value for j, cell in enumerate(row) if cell.value} j: cell.value for j, cell in enumerate(row) if cell.value}
continue continue
elif not header and i == 0: elif not headers and i == 0:
columns = columns if columns else ( columns = columns if columns else (
["Column_%s" % j for j in range(len(row))]) ["Column_%s" % j for j in range(len(row))])
# columns as lists in a dictionary # columns as lists in a dictionary
@ -71,7 +71,7 @@ def sanitize_df(df):
return df return df
def read_ods(file_or_path, sheet, header=True, columns=None): def read_ods(file_or_path, sheet, headers=True, columns=None):
""" """
This function reads in the provided ods file and converts it to a This function reads in the provided ods file and converts it to a
dictionary. The dictionary is converted to a DataFrame. Empty rows and dictionary. The dictionary is converted to a DataFrame. Empty rows and
@ -90,5 +90,5 @@ def read_ods(file_or_path, sheet, header=True, columns=None):
the ODS file as a pandas DataFrame the ODS file as a pandas DataFrame
""" """
doc = ezodf.opendoc(file_or_path) doc = ezodf.opendoc(file_or_path)
df = load_ods(doc, sheet, header, columns) df = load_ods(doc, sheet, headers, columns)
return sanitize_df(df) return sanitize_df(df)

View File

@ -1,7 +1,43 @@
import os import os
import sys
from pandas_ods_reader import read_ods from pandas_ods_reader.read_ods import read_ods
root = os.path.dirname(os.path.abspath(read_ods.__file__)) root = os.path.dirname(os.path.abspath(__file__))
test_dir = os.path.join(root, "tests")
def test_header_file():
example = "example_headers.ods"
path = os.path.join(root, example)
print("Test sheet by index")
df = read_ods(path, 1)
print(df)
print("Test sheet by name")
df = read_ods(path, "Sheet1")
print(df)
def test_no_header_file():
example = "example_no_headers.ods"
path = os.path.join(root, example)
print("Test sheet by index and default columns")
df = read_ods(path, 1, headers=False)
print(df)
print("Test sheet by name and default columns")
df = read_ods(path, "Sheet1", headers=False)
print(df)
print("Test sheet by index and specific columns")
columns = ["A", "B", "C", "D", "E"]
df = read_ods(path, 1, headers=False, columns=columns)
print(df)
def main():
test_header_file()
test_no_header_file()
if __name__ == "__main__":
status = main()
sys.exit(status)