Skip to content

Commit da44fd3

Browse files
committed
♻️ simplify document object
1 parent e52aeb6 commit da44fd3

File tree

9 files changed

+31
-20
lines changed

9 files changed

+31
-20
lines changed

mindee/documents/base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def serialize_for_json(obj: Any) -> Any:
1919
return vars(obj)
2020

2121

22+
def clean_out_string(out_string: str) -> str:
23+
"""Clean up the string representation."""
24+
regexp = re.compile(r" \n")
25+
return regexp.sub("\n", out_string)
26+
27+
2228
class Document:
2329
type: str
2430
"""Document type"""
@@ -83,11 +89,5 @@ def all_checks(self) -> bool:
8389
"""Return status of all checks."""
8490
return all(self.checklist)
8591

86-
@staticmethod
87-
def clean_out_string(out_string: str) -> str:
88-
"""Clean up the string representation."""
89-
regexp = re.compile(r" \n")
90-
return regexp.sub("\n", out_string)
91-
9292

9393
TypeDocument = TypeVar("TypeDocument", bound=Document)

mindee/documents/custom/custom_v1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict, Optional, TypeVar
22

3-
from mindee.documents.base import Document, TypeApiPrediction
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.fields.api_builder import ClassificationField, ListField
55

66

@@ -60,7 +60,7 @@ def __str__(self) -> str:
6060
for field_name, field_info in self.fields.items():
6161
custom_doc_str += f"{field_name}: {field_info}\n"
6262
custom_doc_str += "----------------------"
63-
return self.clean_out_string(custom_doc_str)
63+
return clean_out_string(custom_doc_str)
6464

6565
def _checklist(self) -> None:
6666
pass

mindee/documents/financial/financial_v1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, TypeVar
22

3-
from mindee.documents.base import Document, TypeApiPrediction
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.documents.invoice.invoice_v3 import InvoiceV3
55
from mindee.documents.receipt.receipt_v3 import ReceiptV3
66
from mindee.endpoints import Endpoint
@@ -125,7 +125,7 @@ def _build_from_api_prediction(
125125
self.customer_address = TextField({"value": None, "confidence": 0.0})
126126

127127
def __str__(self) -> str:
128-
return self.clean_out_string(
128+
return clean_out_string(
129129
"-----Financial Document data-----\n"
130130
f"Filename: {self.filename or ''}\n"
131131
f"Invoice number: {self.invoice_number.value}\n"

mindee/documents/invoice/invoice_v3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, TypeVar
22

3-
from mindee.documents.base import Document, TypeApiPrediction
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.fields.amount import AmountField
55
from mindee.fields.base import field_array_confidence
66
from mindee.fields.company_registration import CompanyRegistrationField
@@ -126,7 +126,7 @@ def __str__(self) -> str:
126126
[str(p) for p in self.payment_details]
127127
)
128128
taxes = "\n ".join(f"{t}" for t in self.taxes)
129-
return self.clean_out_string(
129+
return clean_out_string(
130130
"-----Invoice data-----\n"
131131
f"Filename: {self.filename or ''}\n"
132132
f"Invoice number: {self.invoice_number}\n"

mindee/documents/passport/passport_v1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from datetime import datetime
22
from typing import List, Optional, TypeVar
33

4-
from mindee.documents.base import Document, TypeApiPrediction
4+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
55
from mindee.fields.base import field_array_confidence
66
from mindee.fields.date import DateField
77
from mindee.fields.text import TextField
@@ -92,7 +92,7 @@ def __str__(self) -> str:
9292
for given_name in self.given_names
9393
]
9494
)
95-
return self.clean_out_string(
95+
return clean_out_string(
9696
"-----Passport data-----\n"
9797
f"Filename: {self.filename or ''}\n"
9898
f"Full name: {self.full_name}\n"

mindee/documents/receipt/receipt_v3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, TypeVar
22

3-
from mindee.documents.base import Document, TypeApiPrediction
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.fields.amount import AmountField
55
from mindee.fields.base import field_array_confidence, field_array_sum
66
from mindee.fields.date import DateField
@@ -56,7 +56,7 @@ def __init__(
5656

5757
def __str__(self) -> str:
5858
taxes = "\n ".join(f"{t}" for t in self.taxes)
59-
return self.clean_out_string(
59+
return clean_out_string(
6060
"-----Receipt data-----\n"
6161
f"Filename: {self.filename or ''}\n"
6262
f"Total amount including taxes: {self.total_incl}\n"

mindee/documents/receipt/receipt_v4.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, TypeVar
22

3-
from mindee.documents.base import Document, TypeApiPrediction
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.fields.amount import AmountField
55
from mindee.fields.date import DateField
66
from mindee.fields.locale import LocaleField
@@ -93,7 +93,7 @@ def _build_from_api_prediction(
9393

9494
def __str__(self) -> str:
9595
taxes = "\n ".join(f"{t}" for t in self.taxes)
96-
return self.clean_out_string(
96+
return clean_out_string(
9797
"----- Receipt V4 -----\n"
9898
f"Filename: {self.filename or ''}\n"
9999
f"Total amount: {self.total_amount}\n"

mindee/documents/us/bank_check/bank_check_v1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import List, Optional, TypeVar
22

3-
from mindee.documents.base import Document, TypeApiPrediction
3+
from mindee.documents.base import Document, TypeApiPrediction, clean_out_string
44
from mindee.fields.amount import AmountField
55
from mindee.fields.date import DateField
66
from mindee.fields.orientation import OrientationField
@@ -82,7 +82,7 @@ def __str__(self) -> str:
8282
payees = ", ".join(
8383
[payee.value if payee.value is not None else "" for payee in self.payees]
8484
)
85-
return (
85+
return clean_out_string(
8686
"----- US Bank Check -----\n"
8787
f"Filename: {self.filename or ''}".rstrip() + "\n"
8888
f"Routing number: {self.routing_number}\n"

mindee/response.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ class PredictResponse(Generic[TypeDocument]):
1717
"""Raw HTTP response JSON"""
1818
document_type: str
1919
"""Document type"""
20+
input_path: Optional[str] = None
21+
"""Path of the input file"""
22+
input_filename: Optional[str] = None
23+
"""Name of the input file"""
24+
input_mimetype: Optional[str] = None
25+
"""MIME type of the input file"""
2026
document: Optional[TypeDocument]
2127
"""An instance of the ``Document`` class, according to the type given."""
2228
pages: List[TypeDocument]
@@ -42,6 +48,11 @@ def __init__(
4248
self.document_type = doc_config.document_type
4349
self.pages = []
4450

51+
if input_source:
52+
self.input_path = input_source.filepath
53+
self.input_filename = input_source.filename
54+
self.input_mimetype = input_source.file_mimetype
55+
4556
if not response_ok:
4657
self.document = None
4758
else:

0 commit comments

Comments
 (0)