Skip to content

Commit

Permalink
Add __post_init_post_parse__ method to Builder (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersy005 authored May 24, 2021
1 parent c8287c2 commit 5e3599d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 6 additions & 2 deletions ecgtools/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Builder:
parsing_func: typing.Callable = None
njobs: int = -1

def __post_init__(self):
def __post_init_post_parse__(self):
self.df = pd.DataFrame()
self.invalid_assets = pd.DataFrame()
self.dirs = None
Expand Down Expand Up @@ -100,11 +100,15 @@ def save(
**kwargs,
):
catalog_file = pathlib.Path(catalog_file)
index = kwargs.pop('index') or False
index = kwargs.pop('index') if 'index' in kwargs else False
self.df.to_csv(catalog_file, index=index, **kwargs)
if not self.invalid_assets.empty:
invalid_assets_report_file = (
catalog_file.parent / f'invalid_assets_{catalog_file.parts[-1]}'
)
self.invalid_assets.to_csv(invalid_assets_report_file, index=False)
print(f'Saved catalog location: {catalog_file}')

def build(self):
self.get_directories().get_filelist().parse().clean_dataframe()
return self
21 changes: 14 additions & 7 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ def test_parse_error():
sample_data_dir / 'cesm',
],
)
def test_parse(root_path):
b = (
Builder(root_path, exclude_patterns=['*/files/*', '*/latest/*'], parsing_func=parsing_func)
.get_directories()
.get_filelist()
.parse()
)
def test_build(root_path):
b = Builder(
root_path, exclude_patterns=['*/files/*', '*/latest/*'], parsing_func=parsing_func
).build()
assert b.entries
assert isinstance(b.entries[0], dict)
assert isinstance(b.df, pd.DataFrame)
Expand All @@ -99,3 +96,13 @@ def test_parse_invalid_assets():

assert not b.invalid_assets.empty
assert set(b.invalid_assets.columns) == set([INVALID_ASSET, TRACEBACK])


def test_save(tmp_path):
catalog_file = tmp_path / 'test_catalog.csv'
b = Builder(sample_data_dir / 'cesm', parsing_func=parsing_func).build()
b.save(catalog_file)

df = pd.read_csv(catalog_file)
assert len(df) == len(b.df)
assert set(df.columns) == set(b.df.columns)
2 changes: 0 additions & 2 deletions tests/test_core.py

This file was deleted.

0 comments on commit 5e3599d

Please sign in to comment.