Skip to content

Commit

Permalink
Stop returning defaultdict metadata
Browse files Browse the repository at this point in the history
...and some type hints to make mypy happier.

Closes #46
  • Loading branch information
michaelosthege committed Aug 4, 2023
1 parent 801014a commit 167a139
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions bletl/parsing/blpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import warnings
import xml.etree.ElementTree
from typing import Optional, Union
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union

import numpy
import pandas
Expand All @@ -20,6 +20,7 @@
BLDParser,
FilterTimeSeries,
FluidicsSource,
IncompatibleFileError,
InvalidLotNumberError,
)

Expand Down Expand Up @@ -128,26 +129,29 @@ def _parse_datalines(datalines) -> pandas.DataFrame:
return dfraw


def parse_metadata_data(fp):
def parse_metadata_data(fp) -> Tuple[Dict[str, Any], pandas.DataFrame]:
with open(fp, "r", encoding="utf-8") as f:
lines = f.readlines()

metadata = collections.defaultdict(dict)
datalines = collections.defaultdict(list)
metadata: DefaultDict[str, Any] = collections.defaultdict(dict)
section = None
data_start = None
data_start: Optional[int] = None

for l, line in enumerate(lines):
if line.startswith("="):
# any section header encountered
section = line.strip().strip("=").strip()
if not data_start and section == "data":
data_start = l + 1
elif section is None:
raise IncompatibleFileError("No metadata section header before first setting.")
elif line.startswith("["):
# register the value
key, value = line.split("]")
key = key.strip("[")
metadata[section][key] = value.strip()
if data_start is None:
raise IncompatibleFileError("Section header 'data' not found.")

# standardize the metadata keys
metadata["date_start"] = datetime.datetime.strptime(
Expand Down Expand Up @@ -195,7 +199,7 @@ def parse_metadata_data(fp):
f"{fp} contains defects in lines {defect_lines}. Be extra skeptical about the parsed results."
)

return metadata, dfraw[list(dfraw.columns)[:-1]]
return dict(metadata), dfraw[list(dfraw.columns)[:-1]]


def standardize(df):
Expand Down

0 comments on commit 167a139

Please sign in to comment.