-
Notifications
You must be signed in to change notification settings - Fork 1
Simplify Table Loop by encapsulating peek/look behind logic #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b585a5
Simplify Table Loop by encapsulating peek/look behind
hf-kklein 46b1c18
Update src/ebddocx2table/docxtableconverter.py
hf-kklein 62f4d4c
attrs, doch nicht so falsch ;)
hf-kklein d9ff6c3
add attrs to requirements.in
hf-kklein a969234
linti binti
hf-kklein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| """ | ||
| This module converts tables read from the docx file into a format that is easily accessible (but still a table). | ||
| """ | ||
| import dataclasses | ||
| import re | ||
| from enum import Enum | ||
| from itertools import cycle, groupby | ||
| from typing import Generator, List, Literal, Optional, Tuple | ||
|
|
||
| import attrs | ||
| from docx.table import Table, _Cell, _Row # type:ignore[import] | ||
| from ebdtable2graph.models import EbdTable, EbdTableRow, EbdTableSubRow | ||
| from ebdtable2graph.models.ebd_table import _STEP_NUMBER_REGEX, EbdCheckResult, EbdTableMetaData, MultiStepInstruction | ||
|
|
@@ -89,6 +91,38 @@ class _EbdSubRowPosition(Enum): | |
| LOWER = 2 #: the lower sub row | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class _EnhancedDocxTableLine: | ||
| """ | ||
| A structure that primarily contains a single row from a DOCX table but also meta information about previous and | ||
| following elements in the table. It gathers information that are not directly accessible when only looking at one | ||
| single row. | ||
| """ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Die idee bei diesem PR ist, dass wir die Probleme, die bisher in einer großen Loop gelöst werden auf zwei Methoden aufteilen.
Damit
|
||
|
|
||
| row: _Row = attrs.field(validator=attrs.validators.instance_of(_Row)) | ||
| """ | ||
| The row that is currently being processed | ||
| """ | ||
| sub_row_position: _EbdSubRowPosition = attrs.field(validator=attrs.validators.instance_of(_EbdSubRowPosition)) | ||
| """ | ||
| denotes if row is an upper/lower sub row | ||
| """ | ||
| cells: List[_Cell] = attrs.field( | ||
| validator=attrs.validators.deep_iterable( | ||
| member_validator=attrs.validators.instance_of(_Cell), iterable_validator=attrs.validators.instance_of(list) | ||
| ) | ||
| ) | ||
| """ | ||
| the (sanitized) cells of the row | ||
| """ | ||
| multi_step_instruction_text: Optional[str] = attrs.field( | ||
| validator=attrs.validators.optional(attrs.validators.instance_of(str)) | ||
| ) | ||
| """ | ||
| a multistep instruction text that may be applicable to this row (if not None) | ||
| """ | ||
|
|
||
|
|
||
| # pylint: disable=too-few-public-methods, too-many-instance-attributes | ||
| class DocxTableConverter: | ||
| """ | ||
|
|
@@ -135,22 +169,15 @@ def __init__(self, docx_tables: List[Table], ebd_key: str, chapter: str, sub_cha | |
| self._column_index_note = column_index | ||
| self._metadata = EbdTableMetaData(ebd_code=ebd_key, sub_chapter=sub_chapter, chapter=chapter, role=role) | ||
|
|
||
| # I see that there are quite a few local variables, but honestly see no reason to break it down any further. | ||
| # pylint:disable=too-many-locals, too-many-arguments | ||
| def _handle_single_table( | ||
| self, | ||
| table: Table, | ||
| multi_step_instructions: List[MultiStepInstruction], | ||
| row_offset: int, | ||
| rows: List[EbdTableRow], | ||
| sub_rows: List[EbdTableSubRow], | ||
| ) -> None: | ||
| @staticmethod | ||
| def _enhance_list_view(table: Table, row_offset: int) -> List[_EnhancedDocxTableLine]: | ||
| """ | ||
| Handles a single table (out of possible multiple tables for 1 EBD). | ||
| The results are written into rows, sub_rows and multi_step_instructions. Those will be modified. | ||
| Loop over the given table and enhance the table rows with additional information. | ||
| It spares the main loop in _handle_single_table from peeking ahead or looking back. | ||
| """ | ||
| result: List[_EnhancedDocxTableLine] = [] | ||
| upper_lower_iterator = cycle([_EbdSubRowPosition.UPPER, _EbdSubRowPosition.LOWER]) | ||
| use_cases: List[str] = [] | ||
| multi_step_instruction_text: Optional[str] = None | ||
| for table_row, sub_row_position in zip( | ||
| table.rows[row_offset:], | ||
| upper_lower_iterator, | ||
|
|
@@ -163,38 +190,63 @@ def _handle_single_table( | |
| multi_step_instruction_text = row_cells[0].text | ||
| # we store the text in the local variable for now because we don't yet know the next step number | ||
| continue | ||
| if sub_row_position == _EbdSubRowPosition.UPPER: | ||
| use_cases = _get_use_cases(row_cells) | ||
| # clear list every second entry | ||
| sub_rows = [] | ||
| step_number = row_cells[len(use_cases) + self._column_index_step_number].text.strip() | ||
| description = row_cells[len(use_cases) + self._column_index_description].text.strip() | ||
| result.append( | ||
| _EnhancedDocxTableLine( | ||
| row=table_row, | ||
| sub_row_position=sub_row_position, | ||
| multi_step_instruction_text=multi_step_instruction_text, | ||
| cells=row_cells, | ||
| ) | ||
| ) | ||
| multi_step_instruction_text = None | ||
| return result | ||
|
|
||
| # I see that there are quite a few local variables, but honestly see no reason to break it down any further. | ||
| # pylint:disable=too-many-arguments | ||
| def _handle_single_table( | ||
| self, | ||
| table: Table, | ||
| multi_step_instructions: List[MultiStepInstruction], | ||
| row_offset: int, | ||
| rows: List[EbdTableRow], | ||
| sub_rows: List[EbdTableSubRow], | ||
| ) -> None: | ||
| """ | ||
| Handles a single table (out of possible multiple tables for 1 EBD). | ||
| The results are written into rows, sub_rows and multi_step_instructions. Those will be modified. | ||
| """ | ||
| use_cases: List[str] = [] | ||
| for enhanced_table_row in self._enhance_list_view(table=table, row_offset=row_offset): | ||
| if enhanced_table_row.sub_row_position == _EbdSubRowPosition.UPPER: | ||
| use_cases = _get_use_cases(enhanced_table_row.cells) | ||
| sub_rows = [] # clear list every second entry | ||
| step_number = enhanced_table_row.cells[len(use_cases) + self._column_index_step_number].text.strip() | ||
| description = enhanced_table_row.cells[len(use_cases) + self._column_index_description].text.strip() | ||
| boolean_outcome, subsequent_step_number = _read_subsequent_step_cell( | ||
| row_cells[len(use_cases) + self._column_index_check_result] | ||
| enhanced_table_row.cells[len(use_cases) + self._column_index_check_result] | ||
| ) | ||
| sub_row = EbdTableSubRow( | ||
| check_result=EbdCheckResult(subsequent_step_number=subsequent_step_number, result=boolean_outcome), | ||
| result_code=row_cells[len(use_cases) + self._column_index_result_code].text.strip() or None, | ||
| note=row_cells[len(use_cases) + self._column_index_note].text.strip() or None, | ||
| result_code=enhanced_table_row.cells[len(use_cases) + self._column_index_result_code].text.strip() | ||
| or None, | ||
| note=enhanced_table_row.cells[len(use_cases) + self._column_index_note].text.strip() or None, | ||
| ) | ||
| sub_rows.append(sub_row) | ||
| if sub_row_position == _EbdSubRowPosition.LOWER: | ||
| if enhanced_table_row.sub_row_position == _EbdSubRowPosition.LOWER: | ||
| row = EbdTableRow( | ||
| description=description, step_number=step_number, sub_rows=sub_rows, use_cases=use_cases or None | ||
| description=description, | ||
| step_number=step_number, | ||
| sub_rows=sub_rows, | ||
| use_cases=use_cases or None, | ||
| ) | ||
| if "multi_step_instruction_text" in locals(): | ||
| # if the variable with the given name is defined, then we append a multi_step_instruction, once. | ||
| multi_step_instructions.append( | ||
| MultiStepInstruction( | ||
| instruction_text=multi_step_instruction_text, | ||
| # in contrast to the row in which we found the bare multi_step_instruction_text | ||
| # we know the step_number here. This is why the detection of the instruction and the append | ||
| # are not in the same place. | ||
| first_step_number_affected=step_number, | ||
| ) | ||
| ) | ||
| del multi_step_instruction_text # prevent adding the same instructions for all following steps | ||
| rows.append(row) | ||
| if enhanced_table_row.multi_step_instruction_text: | ||
| multi_step_instructions.append( | ||
| MultiStepInstruction( | ||
| first_step_number_affected=step_number, | ||
| instruction_text=enhanced_table_row.multi_step_instruction_text, | ||
| ) | ||
| ) | ||
|
|
||
| def convert_docx_tables_to_ebd_table(self) -> EbdTable: | ||
| """ | ||
|
|
||
You are viewing a condensed version of this merge commit. You can view the full changes here.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.