Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce complexity Removed unnecessary list conversion: The templates = list(templates) line is removed as it's not needed since we're iterating through the templates directly. Removed result initialization: The result variable initialization is removed as we're now returning directly from the loop if a match is found. Return directly on match: If a template matches, the function now returns the result of template.extract directly within the loop. Simplified OCR fallback: The OCR fallback logic is simplified by directly returning the result if a template is found. Explanation: The original code had some unnecessary steps and nested conditions that increased the complexity. By removing the redundant list conversion and returning directly from the loop when a match is found, we simplify the control flow and reduce the cognitive complexity of the function. The simplified OCR fallback logic also contributes to reducing the complexity by avoiding unnecessary nesting and making the code easier to follow. Added typehint for `Templates`. This fixes mypy error. Changes in ocrmypdf fallback: Renamed template to templates_matched: I renamed the template variable to templates_matched in the assignment to make it clearer that extract_data_fallback_ocrmypdf returns a list of templates. Checked for matched templates: I added a check if templates_matched: to ensure that at least one template matched before trying to access the first element. Assigned the first matched template: If templates_matched is not empty, I assign the first template in the list to the template variable.
- Loading branch information