Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ instance/

# Sphinx documentation
docs/_build/
doc/_build/
doc_build/

# PyBuilder
Expand Down
Empty file added scripts/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions scripts/parse_md_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from collections.abc import Sequence
from pathlib import Path

import pandas as pd

def parse_file(file_path: Path) -> pd.DataFrame:
""" Parses a markdown file, containing a shift-table, into a pandas dataframe.
Args:
file_path (Path): Path to the markdown file.
Returns:
pd.DataFrame: DataFrame containing the shift table.
"""
header, data = get_table_parts(file_path.read_text().split("\n"))
df = pd.DataFrame(
columns=parse_line(header[0]),
data=[parse_line(line) for line in data],
)
return df


def parse_line(line: str):
"""Convert a single line of a table into a list of parts.
Args:
line (str): Line of the table.
Returns:
List[str]: List of the table row entries.
"""
return [part.strip() for part in line.split("|")][1:-1]


def get_table_parts(content: Sequence[str]):
""" Splits a markdown table into header and data. """
header = []
data = []
header_finished = False

for line in content:
line = line.strip()

if not line.startswith("|"):
if not data:
continue
else:
break

if ":---" in line:
header_finished = True
continue

if not header_finished:
header.append(line)
else:
data.append(line)

return header, data


Loading