Refactor `_readfile` to have a seperate `read_td_file` method
The lines https://gitlab.com/octopus-code/postopus/-/blob/main/src/postopus/files/pandas_text.py#L118 in _readfile method could be refactored to moves the code for extracting metadata from the file into a separate method called read_td_file. The method takes no parameters and returns a tuple containing the metadata dictionary, the column headers list, and the units list.
def read_td_file(self):
with open(self.filepath, "r") as file:
# skip "HEADER" line
for i in range(2):
_ = file.readline()
# After HEADER and until # Iter, everything is metadata
metadata_lines = 0
metadata_dict = {}
for line in file:
if line.startswith("# Iter"):
break
metadata_lines += 1
# split key and values if two spaces or more, ignore breaklines
# and eq. signs
split_line = re.split(
r"\s{2,}", line[2:].replace("\n", "").replace("=", "")
)
metadata_dict[split_line[0]] = split_line[1:]
# last two lines are column headers and units
col_line = line
unit_line = file.readline()
# complicated splitting needed because some column titles have a
# space inside
column_headers = [
s.strip() for s in col_line[1:-1].split(" ") if len(s) > 1
]
# unit dict
units = [s.strip() for s in unit_line[1:-1].split(" ") if len(s) > 1]
return metadata_dict, column_headers, units