Skip to content

Commit

Permalink
Minor updates to Excel class in data_readers.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Nov 27, 2023
1 parent 7037476 commit 19612b1
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions dcicutils/data_readers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import csv
import openpyxl
from typing import Any, Generator, Iterator, List, Optional, Tuple, Union
from typing import Any, Generator, Iterator, List, Optional, Type, Tuple, Union
from dcicutils.misc_utils import right_trim


Expand All @@ -17,6 +17,8 @@ def __init__(self):
def __iter__(self) -> Iterator:
for row in self.rows:
self.location += 1
if self.is_comment_row(row):
continue
if self.is_terminating_row(row):
break
if len(self.header) < len(row): # Row values beyond what there are headers for are ignored.
Expand All @@ -34,6 +36,9 @@ def _define_header(self, header: List[Optional[Any]]) -> None:
def rows(self) -> Generator[Union[List[Optional[Any]], Tuple[Optional[Any], ...]], None, None]:
yield

def is_comment_row(self, row: Union[List[Optional[Any]], Tuple[Optional[Any]]]) -> bool:
return False

def is_terminating_row(self, row: Union[List[Optional[Any]], Tuple[Optional[Any]]]) -> bool:
return False

Expand Down Expand Up @@ -118,14 +123,18 @@ def open(self) -> None:

class Excel:

def __init__(self, file: str) -> None:
def __init__(self, file: str, reader_class: Optional[Type] = None) -> None:
self._file = file
self._workbook = None
self.sheet_names = None
if isinstance(reader_class, Type) and issubclass(reader_class, ExcelSheetReader):
self._reader_class = reader_class
else:
self._reader_class = ExcelSheetReader
self.open()

def sheet_reader(self, sheet_name: str) -> ExcelSheetReader:
return ExcelSheetReader(sheet_name=sheet_name, workbook=self._workbook)
return self._reader_class(sheet_name=sheet_name, workbook=self._workbook)

def open(self) -> None:
if self._workbook is None:
Expand Down

0 comments on commit 19612b1

Please sign in to comment.