Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-migas committed Aug 13, 2024
1 parent 7174954 commit 53b6550
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/koyo/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,12 +422,12 @@ def timed_iterator(
func(f"[{i}/{n_tasks}] {_text} {format_human_time_s(execution_time)} [{avg}{rem}{tot}]")


def parse_arg(arg: str, key: str):
def parse_arg(arg: str, key: str) -> tuple[str, ty.Any]:
"""Parse argument."""
try:
if key:
arg = arg.split(key)[1]
name, value = arg.split("=")
name, value = arg.split("=", maxsplit=1)
# try parsing value - it will fail if string value was provided
try:
value = literal_eval(value)
Expand Down
42 changes: 42 additions & 0 deletions src/koyo/dataframe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Dataframe utilities"""

from __future__ import annotations
from pathlib import Path
import typing as ty
from koyo.typing import PathLike

if ty.TYPE_CHECKING:
import pandas as pd


def read_csv_with_comments(path: PathLike) -> pd.DataFrame:
"""Read CSV with comments."""
import pandas as pd

path = Path(path)
try:
df = pd.read_csv(path)
first_col = df.iloc[:, 0]
# check whether any of the values start with # (comment)
for value in first_col:
if value.startswith("# "):
df = df[~first_col.str.contains("# ")]
df.reset_index(drop=True, inplace=True)
df.columns = df.iloc[0]
df.drop(df.index[0], inplace=True)
break
except pd.errors.ParserError:
from io import StringIO

data = path.read_text().split("\n")
start_index, end_index = 0, 0
for row in data:
if row.startswith("#"):
start_index += 1
continue
elif not row:
end_index += 1
df = pd.read_csv(StringIO("\n".join(data[start_index:-end_index])), sep=",")
except Exception:
raise pd.errors.ParserError(f"Failed to parse grid '{path}'.")
return df
1 change: 1 addition & 0 deletions tests/test_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
("n=5", ("n", 5)),
("n=5.0", ("n", 5.0)),
("n=[10,20,30]", ("n", [10, 20, 30])),
("n=value=with=equal=sign", ("n", "value=with=equal=sign")),
],
)
def test_parse_arg(arg, expected):
Expand Down

0 comments on commit 53b6550

Please sign in to comment.