Skip to content

Commit

Permalink
Miscellaneous bug fixes (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-5546 committed Jan 5, 2024
1 parent 12085a2 commit 7dcbfd9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/xlbudget/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ def _check_input(
raise ValueError(f"Must specify 'year' argument when {format=}")

# validate input file type in more detail
if input_format.seperator == "\t" and not input.endswith(".tsv"):
if input.endswith(".csv") and not input_format.seperator == ",":
raise ValueError(f"Input file should be CSV for {format=}")

if input.endswith(".tsv") and not input_format.seperator == "\t":
raise ValueError(f"Input file should be TSV for {format=}")

def run(self) -> None:
Expand Down
40 changes: 20 additions & 20 deletions src/xlbudget/inputformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io
import sys
from argparse import Action
from datetime import datetime
from logging import getLogger
from typing import Callable, Dict, List, NamedTuple, Optional

Expand Down Expand Up @@ -33,7 +34,7 @@ class InputFormat(NamedTuple):
names: List[str]
usecols: List[int]
ignores: List[str]
pre_processing: Callable = lambda input, _: input
pre_processing: Callable = lambda input, year: input
post_processing: Callable = lambda df: df
seperator: str = ","

Expand Down Expand Up @@ -65,32 +66,30 @@ def bmo_cc_adobe_pre_processing(_input: Optional[str], year: str) -> io.StringIO

rows = []
i = 0
is_header = True
while i < len(lines):
elements = lines[i : i + 4]
elems = lines[i : i + 4]

# add year to dates
elements[0] += f" {year}"
elements[1] += f" {year}"
# reformat dates and add year
if not is_header:
elems[0] = year + datetime.strptime(elems[0], "%b. %d").strftime("-%m-%d")
elems[1] = year + datetime.strptime(elems[1], "%b. %d").strftime("-%m-%d")

# add negative sign to amounts that are not credited (CR on next line)
if i + 4 < len(lines) and lines[i + 4] == "CR":
i += 5
else:
# check if amount is a float (header will not be float)
is_float = True
try:
float(elements[-1])
except ValueError:
is_float = False

if is_float:
elements[-1] = "-" + elements[-1]
if not is_header:
elems[-1] = "-" + elems[-1]

i += 4

row = ",".join(elements) + "\n"
row = "\t".join(elems) + "\n"
rows.append(row)

if is_header:
is_header = False

new_input = "".join(rows)
return io.StringIO(new_input)

Expand Down Expand Up @@ -142,7 +141,7 @@ def bmo_cc_web_post_processing(df: pd.DataFrame) -> pd.DataFrame:
"Description",
],
usecols=[2, 4, 3],
ignores=[r"^\[CW\] TF.*(?:285|593|625)$"],
ignores=[r"^\[CW\] TF.*(?:285|493|593|625)$"],
)

BMO_ACCT_WEB = InputFormat(
Expand All @@ -155,7 +154,7 @@ def bmo_cc_web_post_processing(df: pd.DataFrame) -> pd.DataFrame:
"Balance",
],
usecols=[0, 1, 2, 3],
ignores=[r"^TF.*(?:285|593|625)$"],
ignores=[r"^TF.*(?:285|493|593|625)$"],
post_processing=bmo_acct_web_post_processing,
seperator="\t",
)
Expand All @@ -171,7 +170,7 @@ def bmo_cc_web_post_processing(df: pd.DataFrame) -> pd.DataFrame:
"Description",
],
usecols=[2, 5, 4],
ignores=[r"^TRSF FROM.*285"],
ignores=[r"^TRSF FROM.*(?:285|493|593)$"],
)

BMO_CC_WEB = InputFormat(
Expand All @@ -182,7 +181,7 @@ def bmo_cc_web_post_processing(df: pd.DataFrame) -> pd.DataFrame:
"Money in/out",
],
usecols=[0, 1, 2],
ignores=[r"^TRSF FROM.*285"],
ignores=[r"^TRSF FROM.*(?:285|493|593)$"],
post_processing=bmo_cc_web_post_processing,
seperator="\t",
)
Expand All @@ -196,8 +195,9 @@ def bmo_cc_web_post_processing(df: pd.DataFrame) -> pd.DataFrame:
"Amount",
],
usecols=[0, 2, 3],
ignores=[r"^TRSF FROM.*285"],
ignores=[r"^TRSF FROM.*(?:285|493|593)$"],
pre_processing=bmo_cc_adobe_pre_processing,
seperator="\t",
)


Expand Down

0 comments on commit 7dcbfd9

Please sign in to comment.