Skip to content

Commit 4b5ff08

Browse files
committed
💄 improve string output of documents
1 parent 007bf85 commit 4b5ff08

File tree

9 files changed

+41
-44
lines changed

9 files changed

+41
-44
lines changed

mindee/__main__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,16 @@ def _parse_args() -> Namespace:
170170
"--no-cut-pdf",
171171
dest="cut_pdf",
172172
action="store_false",
173-
help="Don't cut the PDF",
173+
help="Don't cut document pages",
174174
)
175175
subp.add_argument(
176176
"-p",
177177
"--pdf-pages",
178178
dest="pdf_pages",
179179
type=int,
180180
default=3,
181-
help="Number of PDF pages to cut by, default: 3",
181+
choices=[1, 2, 3],
182+
help="Number of document pages to cut by, default: 3",
182183
)
183184
subp.add_argument(dest="path", help="Full path to the file")
184185

mindee/documents/financial_document.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def __taxes_match_total_incl(self) -> bool:
188188
return False
189189

190190
# Reconstruct total_incl from taxes
191-
total_vat = 0
192-
reconstructed_total = 0
191+
total_vat = 0.0
192+
reconstructed_total = 0.0
193193
for tax in self.taxes:
194194
if tax.rate is not None and tax.rate != 0 and tax.value is not None:
195195
total_vat += tax.value

mindee/documents/invoice.py

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ def __str__(self) -> str:
124124
customer_company_registration = "; ".join(
125125
[str(n.value) for n in self.customer_company_registration]
126126
)
127-
payments = ", ".join([str(p) for p in self.payment_details])
128-
taxes = ", ".join(f"{t}" for t in self.taxes)
127+
payment_details = "\n ".join(
128+
[str(p) for p in self.payment_details]
129+
)
130+
taxes = "\n ".join(f"{t}" for t in self.taxes)
129131
return (
130132
"-----Invoice data-----\n"
131133
f"Filename: {self.filename}\n"
@@ -139,7 +141,7 @@ def __str__(self) -> str:
139141
f"Customer name: {self.customer_name}\n"
140142
f"Customer company registration: {customer_company_registration}\n"
141143
f"Customer address: {self.customer_address}\n"
142-
f"Payment details: {payments}\n"
144+
f"Payment details: {payment_details}\n"
143145
f"Company numbers: {company_numbers}\n"
144146
f"Taxes: {taxes}\n"
145147
f"Total taxes: {self.total_tax}\n"
@@ -174,8 +176,8 @@ def __taxes_match_total_incl(self) -> bool:
174176
return False
175177

176178
# Reconstruct total_incl from taxes
177-
total_vat = 0
178-
reconstructed_total = 0
179+
total_vat = 0.0
180+
reconstructed_total = 0.0
179181
for tax in self.taxes:
180182
if tax.value is None or tax.rate is None or tax.rate == 0:
181183
return False
@@ -211,8 +213,8 @@ def __taxes_match_total_excl(self) -> bool:
211213
return False
212214

213215
# Reconstruct total excl from taxes
214-
total_vat = 0
215-
reconstructed_total = 0
216+
total_vat = 0.0
217+
reconstructed_total = 0.0
216218
for tax in self.taxes:
217219
if tax.value is None or tax.rate is None or tax.rate == 0:
218220
return False
@@ -254,7 +256,7 @@ def __taxes_plus_total_excl_match_total_incl(self) -> bool:
254256
return False
255257

256258
# Reconstruct total_incl
257-
total_vat = 0
259+
total_vat = 0.0
258260
for tax in self.taxes:
259261
if tax.value is not None:
260262
total_vat += tax.value
@@ -288,13 +290,11 @@ def __reconstruct_total_incl_from_taxes_plus_excl(self) -> None:
288290
multiplied by total_excl confidence
289291
"""
290292
# Check total_tax, total excl exist and total incl is not set
291-
if (
293+
if not (
292294
self.total_excl.value is None
293295
or len(self.taxes) == 0
294296
or self.total_incl.value is not None
295297
):
296-
pass
297-
else:
298298
total_incl = {
299299
"value": sum(
300300
[tax.value if tax.value is not None else 0 for tax in self.taxes]
@@ -319,17 +319,15 @@ def __reconstruct_total_excl_from_tcc_and_taxes(self) -> None:
319319
or len(self.taxes) == 0
320320
or self.total_excl.value is not None
321321
):
322-
pass
323-
else:
324-
total_excl = {
325-
"value": self.total_incl.value
326-
- sum(
327-
[tax.value if tax.value is not None else 0 for tax in self.taxes]
328-
),
329-
"confidence": field_array_confidence(self.taxes)
330-
* self.total_incl.confidence,
331-
}
332-
self.total_excl = Amount(total_excl, value_key="value", reconstructed=True)
322+
return
323+
324+
total_excl = {
325+
"value": self.total_incl.value
326+
- sum([tax.value if tax.value is not None else 0 for tax in self.taxes]),
327+
"confidence": field_array_confidence(self.taxes)
328+
* self.total_incl.confidence,
329+
}
330+
self.total_excl = Amount(total_excl, value_key="value", reconstructed=True)
333331

334332
def __reconstruct_total_tax_from_tax_lines(self) -> None:
335333
"""
@@ -362,14 +360,11 @@ def __reconstruct_total_tax_from_incl_and_excl(self) -> None:
362360
or self.total_excl.value is None
363361
or self.total_incl.value is None
364362
):
365-
pass
366-
else:
363+
return
367364

368-
total_tax = {
369-
"value": self.total_incl.value - self.total_excl.value,
370-
"confidence": self.total_incl.confidence * self.total_excl.confidence,
371-
}
372-
if total_tax["value"] >= 0:
373-
self.total_tax = Amount(
374-
total_tax, value_key="value", reconstructed=True
375-
)
365+
total_tax = {
366+
"value": self.total_incl.value - self.total_excl.value,
367+
"confidence": self.total_incl.confidence * self.total_excl.confidence,
368+
}
369+
if total_tax["value"] >= 0:
370+
self.total_tax = Amount(total_tax, value_key="value", reconstructed=True)

mindee/documents/receipt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def __init__(
5454
)
5555

5656
def __str__(self) -> str:
57-
taxes = ", ".join(f"{t}" for t in self.taxes)
57+
taxes = "\n ".join(f"{t}" for t in self.taxes)
5858
return (
5959
"-----Receipt data-----\n"
6060
f"Filename: {self.filename}\n"
@@ -130,8 +130,8 @@ def __taxes_match_total(self) -> bool:
130130
return False
131131

132132
# Reconstruct total_incl from taxes
133-
total_vat = 0
134-
reconstructed_total = 0
133+
total_vat = 0.0
134+
reconstructed_total = 0.0
135135
for tax in self.taxes:
136136
if tax.value is None or tax.rate is None or tax.rate == 0:
137137
return False

mindee/fields/amount.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def __init__(
2727
reconstructed=reconstructed,
2828
page_n=page_n,
2929
)
30-
3130
try:
3231
self.value = round(float(amount_prediction[value_key]), 3)
3332
except (ValueError, TypeError, KeyError):

mindee/fields/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __eq__(self, other: Any) -> bool:
6666
return self.value == other.value
6767

6868
def __str__(self) -> str:
69-
if self.value:
69+
if self.value is not None:
7070
return f"{self.value}"
7171
return ""
7272

mindee/fields/tax.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
class Tax(Field):
7+
value: Optional[float]
78
rate: Optional[float]
89
code: Optional[str]
910

mindee/response.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ def format_response(
5454
logger.debug("Handling API response")
5555

5656
# Create page level objects
57-
for page_prediction in http_response["document"]["inference"]["pages"]:
57+
for api_page in http_response["document"]["inference"]["pages"]:
5858
pages.append(
5959
doc_config.constructor(
60-
api_prediction=page_prediction["prediction"],
60+
api_prediction=api_page["prediction"],
6161
input_file=input_file,
62-
page_n=page_prediction["id"],
62+
page_n=api_page["id"],
6363
document_type=doc_config.document_type,
6464
)
6565
)

tests/documents/test_invoice.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def test_all_na(invoice_object_all_na):
4545
assert invoice_object_all_na.locale.value is None
4646
assert invoice_object_all_na.total_incl.value is None
4747
assert invoice_object_all_na.total_excl.value is None
48+
assert invoice_object_all_na.total_tax.value is None
4849
assert invoice_object_all_na.invoice_date.value is None
4950
assert invoice_object_all_na.invoice_number.value is None
5051
assert invoice_object_all_na.due_date.value is None

0 commit comments

Comments
 (0)