Skip to content

Commit

Permalink
Use default_factories (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
hf-kklein committed May 20, 2024
1 parent 545f929 commit 5ca7e62
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/kohlrahbi/ahb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def extract_pruefis_from_docx(docx_path: Path) -> Dict[str, str]:
and table_header_starts_with_text_edifact_struktur(item)
and table_header_contains_text_pruefidentifikator(item)
):
# pylint:disable=not-an-iterable
pruefis.update({pruefi: docx_path.name for pruefi in extract_pruefis_from_table(item)})
return pruefis

Expand Down
13 changes: 9 additions & 4 deletions src/kohlrahbi/ahbtable/ahbcondtions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from docx.table import Table as DocxTable
from maus.edifact import EdifactFormat
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, Field

from kohlrahbi.logger import logger

Expand All @@ -16,7 +16,7 @@ class AhbConditions(BaseModel):
Class which contains a dict of conditions for each edifact format
"""

conditions_dict: dict[EdifactFormat, dict[str, str]] = {}
conditions_dict: dict[EdifactFormat, dict[str, str]] = Field(default_factory=dict)

model_config = ConfigDict(arbitrary_types_allowed=True)

Expand Down Expand Up @@ -60,15 +60,19 @@ def include_condition_dict(self, to_add: dict[EdifactFormat, dict[str, str]] | N
return
for edifact_format, edi_cond_dict in to_add.items():
for condition_key, condition_text in edi_cond_dict.items():
if edifact_format in self.conditions_dict:
if edifact_format in self.conditions_dict: # pylint:disable=unsupported-membership-test
if (
# pylint:disable=unsubscriptable-object
condition_key in self.conditions_dict[edifact_format]
and len(condition_text) > len(self.conditions_dict[edifact_format][condition_key])
# pylint:disable=unsubscriptable-object
or condition_key not in self.conditions_dict[edifact_format]
):
self.conditions_dict[edifact_format][condition_key] = condition_text
else:
self.conditions_dict[edifact_format] = {condition_key: condition_text}
self.conditions_dict[edifact_format] = { # pylint:disable=unsupported-assignment-operation
condition_key: condition_text
}

logger.info("Conditions were updated.")

Expand All @@ -78,6 +82,7 @@ def dump_as_json(self, output_directory_path: Path) -> None:
The file will be stored in the directory:
'output_directory_path/<edifact_format>/conditions.json'
"""
# pylint:disable=no-member
for edifact_format, format_cond_dict in self.conditions_dict.items():
condition_json_output_directory_path = output_directory_path / str(edifact_format)
condition_json_output_directory_path.mkdir(parents=True, exist_ok=True)
Expand Down
4 changes: 2 additions & 2 deletions src/kohlrahbi/ahbtable/ahbpackagetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class which contains AHB package condition table
import pandas as pd
from docx.table import Table as DocxTable
from maus.edifact import EdifactFormat
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, Field

from kohlrahbi.ahbtable.ahbcondtions import parse_conditions_from_string
from kohlrahbi.logger import logger
Expand All @@ -23,7 +23,7 @@ class AhbPackageTable(BaseModel):
"""

table: pd.DataFrame = pd.DataFrame()
package_dict: dict[EdifactFormat, dict[str, str]] = {}
package_dict: dict[EdifactFormat, dict[str, str]] = Field(default_factory=dict)
model_config = ConfigDict(arbitrary_types_allowed=True)

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions src/kohlrahbi/ahbtable/ahbtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pandas as pd
from maus.edifact import get_format_of_pruefidentifikator
from more_itertools import peekable
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, Field

from kohlrahbi.ahbtable.ahbsubtable import AhbSubTable
from kohlrahbi.logger import logger
Expand All @@ -32,7 +32,7 @@ class AhbTable(BaseModel):
"""

table: pd.DataFrame
metadata: list[PruefiMetaData] = []
metadata: list[PruefiMetaData] = Field(default_factory=list)

model_config = ConfigDict(arbitrary_types_allowed=True)

Expand Down
2 changes: 1 addition & 1 deletion src/kohlrahbi/read_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def process_table(
"""Processes tables to find and build the AHB table."""
if is_item_table_with_pruefidentifikatoren(item):
seed = Seed.from_table(docx_table=item)

# pylint:disable=unsupported-membership-test
if pruefi in seed.pruefidentifikatoren and not searched_pruefi_is_found:
log_found_pruefi(pruefi)
ahb_sub_table = AhbSubTable.from_table_with_header(docx_table=item)
Expand Down
12 changes: 6 additions & 6 deletions src/kohlrahbi/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

from docx.table import Table
from pydantic import BaseModel
from pydantic import BaseModel, Field

from kohlrahbi.enums import RowType
from kohlrahbi.table_header import PruefiMetaData, TableHeader, get_tabstop_positions
Expand All @@ -15,13 +15,13 @@ class Seed(BaseModel):
helper class to store all values to extract the AHB and the final AHB as dataframe
"""

pruefidentifikatoren: list[str] = []
column_headers: list[str] = []
pruefidentifikatoren: list[str] = Field(default_factory=list)
column_headers: list[str] = Field(default_factory=list)
edifact_struktur_left_indent_position: int = 0
middle_cell_left_indent_position: int = 0
tabstop_positions: list[int] = []
last_two_row_types: list[RowType] = []
metadata: list[PruefiMetaData] = []
tabstop_positions: list[int] = Field(default_factory=list)
last_two_row_types: list[RowType] = Field(default_factory=list)
metadata: list[PruefiMetaData] = Field(default_factory=list)

# why this classmethod?
# to decouple the data structure of Elixir from the input data
Expand Down
5 changes: 3 additions & 2 deletions src/kohlrahbi/table_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from docx.table import _Cell
from docx.text.paragraph import Paragraph
from more_itertools import first, last
from pydantic import BaseModel
from pydantic import BaseModel, Field


class HeaderSection(StrEnum):
Expand Down Expand Up @@ -93,7 +93,7 @@ class TableHeader(BaseModel):
It contains the information about the Prüfidentifikatoren.
"""

pruefi_meta_data: List[PruefiMetaData] = []
pruefi_meta_data: List[PruefiMetaData] = Field(default_factory=list)

@classmethod
def from_header_cell(cls, row_cell: _Cell) -> "TableHeader":
Expand Down Expand Up @@ -188,4 +188,5 @@ def get_pruefidentifikatoren(self) -> List[str]:
The order of the Prüfidentifikatoren is the same as in the docx table headers.
So there should be no duplicates.
"""
# pylint:disable=not-an-iterable
return [pruefi.pruefidentifikator for pruefi in self.pruefi_meta_data]

0 comments on commit 5ca7e62

Please sign in to comment.