Skip to content

Commit

Permalink
chore: Fix codebase formatting
Browse files Browse the repository at this point in the history
Signed-off-by: Étienne Boisseau-Sierra <etienne.boisseau.sierra@gmail.com>
  • Loading branch information
EBoisseauSierra committed Oct 7, 2024
1 parent badd06e commit 2f94045
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 73 deletions.
34 changes: 17 additions & 17 deletions gsbparse/account_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import logging
from functools import cached_property
from typing import TextIO, Union
from typing import TextIO

import defusedxml.ElementTree as ET
import defusedxml.ElementTree as Et
import pandas as pd

from gsbparse.account_section import (
GsbSectionAccount,
GsbSectionBudgetary,
GsbSectionCategory,
GsbSectionCurrency,
GsbSectionFinancial_year,
GsbSectionFinancialYear,
GsbSectionParty,
GsbSectionPayment,
GsbSectionReconcile,
Expand All @@ -27,7 +27,7 @@
class AccountFile:
"""Representation of a parsed `.gsb` file."""

def __init__(self, source: Union[str, TextIO]) -> None:
def __init__(self, source: str | TextIO) -> None:
"""Initialize an AccountFile object, given its source (file object or path)."""
self.source = source

Expand All @@ -49,42 +49,42 @@ def sections(self) -> list[dict]:
sections = {}

# Read the .gsb XML content
tree = ET.parse(self.source)
tree = Et.parse(self.source)
root = tree.getroot()

# Instantiate each GsbSection with tags of the relevant type
sections["Account"] = GsbSectionAccount(
[child.attrib for child in root if child.tag == "Account"]
[child.attrib for child in root if child.tag == "Account"],
)
sections["Currency"] = GsbSectionCurrency(
[child.attrib for child in root if child.tag == "Currency"]
[child.attrib for child in root if child.tag == "Currency"],
)
sections["Party"] = GsbSectionParty(
[child.attrib for child in root if child.tag == "Party"]
[child.attrib for child in root if child.tag == "Party"],
)
sections["Category"] = GsbSectionCategory(
[child.attrib for child in root if child.tag == "Category"]
[child.attrib for child in root if child.tag == "Category"],
)
sections["Sub_category"] = GsbSectionSubCategory(
[child.attrib for child in root if child.tag == "Sub_category"]
[child.attrib for child in root if child.tag == "Sub_category"],
)
sections["Budgetary"] = GsbSectionBudgetary(
[child.attrib for child in root if child.tag == "Budgetary"]
[child.attrib for child in root if child.tag == "Budgetary"],
)
sections["Sub_budgetary"] = GsbSectionSubBudgetary(
[child.attrib for child in root if child.tag == "Sub_budgetary"]
[child.attrib for child in root if child.tag == "Sub_budgetary"],
)
sections["Transaction"] = GsbSectionTransaction(
[child.attrib for child in root if child.tag == "Transaction"]
[child.attrib for child in root if child.tag == "Transaction"],
)
sections["Financial_year"] = GsbSectionFinancial_year(
[child.attrib for child in root if child.tag == "Financial_year"]
sections["Financial_year"] = GsbSectionFinancialYear(
[child.attrib for child in root if child.tag == "Financial_year"],
)
sections["Reconcile"] = GsbSectionReconcile(
[child.attrib for child in root if child.tag == "Reconcile"]
[child.attrib for child in root if child.tag == "Reconcile"],
)
sections["Payment"] = GsbSectionPayment(
[child.attrib for child in root if child.tag == "Payment"]
[child.attrib for child in root if child.tag == "Payment"],
)

return sections
Expand Down
94 changes: 50 additions & 44 deletions gsbparse/account_section.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,36 +72,42 @@ def __init__(self, records: list[dict]):
def df(self):
"""Represent the list of records as a pd.DataFrame."""
# Create df
df = pd.DataFrame.from_records(self.records)
dataset = pd.DataFrame.from_records(self.records)

# "Improve" df by casting correct columns dtype
if self._idx_col is not None:
df.set_index(self._idx_col, inplace=True)
dataset = dataset.set_index(self._idx_col)

if self._int_cols:
df[self._int_cols] = df[self._int_cols].apply(
pd.to_numeric, downcast="integer", errors="coerce"
dataset[self._int_cols] = dataset[self._int_cols].apply(
pd.to_numeric,
downcast="integer",
errors="coerce",
)

if self._bool_cols:
# Strings must be cast as integer first so that casting to bool
# doesn't always yield True.
# Cf. https://stackoverflow.com/q/52089711/5433628
df[self._bool_cols] = (
df[self._bool_cols]
dataset[self._bool_cols] = (
dataset[self._bool_cols]
.apply(pd.to_numeric, downcast="integer")
.astype("bool")
)

if self._currency_cols:
df[self._currency_cols] = df[self._currency_cols].apply(pd.to_numeric)
dataset[self._currency_cols] = dataset[self._currency_cols].apply(
pd.to_numeric,
)

if self._date_cols:
df[self._date_cols] = df[self._date_cols].apply(
pd.to_datetime, format="%m/%d/%Y", errors="coerce"
dataset[self._date_cols] = dataset[self._date_cols].apply(
pd.to_datetime,
format="%m/%d/%Y",
errors="coerce",
)

return df
return dataset


class GsbSectionAccount(AccountSection):
Expand Down Expand Up @@ -142,14 +148,14 @@ def _currency_cols(self):
"Minimum_authorised_balance",
]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Account" tags
xml_tags_attributes_values (list(dict)): Values of "Account" tags
attributes.
"""
super(GsbSectionAccount, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionCurrency(AccountSection):
Expand All @@ -173,14 +179,14 @@ def _name_col(self):
def _int_cols(self):
return ["Fl"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Currency" tags
xml_tags_attributes_values (list(dict)): Values of "Currency" tags
attributes.
"""
super(GsbSectionCurrency, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionParty(AccountSection):
Expand All @@ -204,14 +210,14 @@ def _name_col(self):
def _bool_cols(self):
return ["IgnCase", "UseRegex"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Party" tags
xml_tags_attributes_values (list(dict)): Values of "Party" tags
attributes.
"""
super(GsbSectionParty, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionCategory(AccountSection):
Expand All @@ -235,14 +241,14 @@ def _name_col(self):
def _bool_cols(self):
return ["Kd"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Category" tags
xml_tags_attributes_values (list(dict)): Values of "Category" tags
attributes.
"""
super(GsbSectionCategory, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionSubCategory(AccountSection):
Expand All @@ -262,14 +268,14 @@ def _idx_col(self):
def _name_col(self):
return ["Na"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "SubCategory"
xml_tags_attributes_values (list(dict)): Values of "SubCategory"
tags attributes.
"""
super(GsbSectionSubCategory, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionBudgetary(AccountSection):
Expand All @@ -293,14 +299,14 @@ def _name_col(self):
def _bool_cols(self):
return ["Kd"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Budgetary" tags
xml_tags_attributes_values (list(dict)): Values of "Budgetary" tags
attributes.
"""
super(GsbSectionBudgetary, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionSubBudgetary(AccountSection):
Expand All @@ -320,14 +326,14 @@ def _idx_col(self):
def _name_col(self):
return ["Na"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "SubBudgetary"
xml_tags_attributes_values (list(dict)): Values of "SubBudgetary"
tags attributes.
"""
super(GsbSectionSubBudgetary, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionTransaction(AccountSection):
Expand Down Expand Up @@ -359,14 +365,14 @@ def _currency_cols(self):
def _date_cols(self):
return ["Dt", "Dv"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Transaction"
xml_tags_attributes_values (list(dict)): Values of "Transaction"
tags attributes.
"""
super(GsbSectionTransaction, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionPayment(AccountSection):
Expand Down Expand Up @@ -398,17 +404,17 @@ def _bool_cols(self):
"Automatic_number",
]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Payment"
xml_tags_attributes_values (list(dict)): Values of "Payment"
tags attributes.
"""
super(GsbSectionPayment, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionFinancial_year(AccountSection):
class GsbSectionFinancialYear(AccountSection):
"""Represent the <Financial_year …/> tags of a Grisbi file.
Attributes:
Expand All @@ -433,14 +439,14 @@ def _bool_cols(self):
def _date_cols(self):
return ["Bdte", "Edte"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Financial_year"
xml_tags_attributes_values (list(dict)): Values of "Financial_year"
tags attributes.
"""
super(GsbSectionFinancial_year, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)


class GsbSectionReconcile(AccountSection):
Expand Down Expand Up @@ -472,11 +478,11 @@ def _currency_cols(self):
def _date_cols(self):
return ["Idate", "Fdate"]

def __init__(self, XML_tags_attributes_values):
def __init__(self, xml_tags_attributes_values):
"""Build self.df from the list of XML tags attributes values.
Args:
XML_tags_attributes_values (list(dict)): Values of "Reconcile"
xml_tags_attributes_values (list(dict)): Values of "Reconcile"
tags attributes.
"""
super(GsbSectionReconcile, self).__init__(XML_tags_attributes_values)
super().__init__(xml_tags_attributes_values)
Loading

0 comments on commit 2f94045

Please sign in to comment.