Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions mindee/documents/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,31 +183,24 @@ def build_from_api_prediction(self, api_prediction: dict, page_n=0):
)

def __str__(self) -> str:
company_numbers = "; ".join([str(n.value) for n in self.company_number])
payments = ", ".join([str(p) for p in self.payment_details])
taxes = ", ".join([f"{t.value} {t.rate}%" for t in self.taxes])
return (
"-----Invoice data-----\n"
"Filename: %s \n"
"Invoice number: %s \n"
"Total amount including taxes: %s \n"
"Total amount excluding taxes: %s \n"
"Invoice date: %s\n"
"Invoice due date: %s\n"
"Supplier name: %s\n"
"Payment details: %s\n"
"Taxes: %s\n"
"Total taxes: %s\n"
f"Filename: {self.filename}\n"
f"Invoice number: {self.invoice_number.value}\n"
f"Total amount including taxes: {self.total_incl.value}\n"
f"Total amount excluding taxes: {self.total_excl.value}\n"
f"Invoice date: {self.invoice_date.value}\n"
f"Invoice due date: {self.due_date.value}\n"
f"Supplier name: {self.supplier.value}\n"
f"Payment details: {payments}\n"
f"Company numbers: {company_numbers}\n"
f"Taxes: {taxes}\n"
f"Total taxes: {self.total_tax.value}\n"
f"Locale: {self.locale}\n"
"----------------------"
% (
self.filename,
self.invoice_number.value,
self.total_incl.value,
self.total_excl.value,
self.invoice_date.value,
self.due_date.value,
self.supplier.value,
", ".join([str(p) for p in self.payment_details]),
", ".join([str(t.value) + " " + str(t.rate) + "%" for t in self.taxes]),
self.total_tax.value,
)
)

@staticmethod
Expand Down
49 changes: 18 additions & 31 deletions mindee/documents/passport.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,40 +150,27 @@ def build_from_api_prediction(self, api_prediction, page_n=0):
self.full_name = Field({"value": None, "confidence": 0.0}, page_n=page_n)

def __str__(self) -> str:
given_names = " ".join(
[
given_name.value if given_name.value is not None else ""
for given_name in self.given_names
]
)
return (
"-----Passport data-----\n"
"Filename: %s \n"
"Full name: %s \n"
"Given names: %s \n"
"Surname: %s\n"
"Country: %s\n"
"ID Number: %s\n"
"Issuance date: %s\n"
"Birth date: %s\n"
"Expiry date: %s\n"
"MRZ 1: %s\n"
"MRZ 2: %s\n"
"MRZ: %s\n"
f"Filename: {self.filename}\n"
f"Full name: {self.full_name.value}\n"
f"Given names: {given_names}\n"
f"Surname: {self.surname.value}\n"
f"Country: {self.country.value}\n"
f"ID Number: {self.id_number.value}\n"
f"Issuance date: {self.issuance_date.value}\n"
f"Birth date: {self.birth_date.value}\n"
f"Expiry date: {self.expiry_date.value}\n"
f"MRZ 1: {self.mrz1.value}\n"
f"MRZ 2: {self.mrz2.value}\n"
f"MRZ: {self.mrz.value}\n"
"----------------------"
% (
self.filename,
self.full_name.value,
" ".join(
[
given_name.value if given_name.value is not None else ""
for given_name in self.given_names
]
),
self.surname.value,
self.country.value,
self.id_number.value,
self.issuance_date.value,
self.birth_date.value,
self.expiry_date.value,
self.mrz1.value,
self.mrz2.value,
self.mrz.value,
)
)

def is_expired(self):
Expand Down
31 changes: 11 additions & 20 deletions mindee/documents/receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,29 +125,20 @@ def get_document_config() -> DocumentConfig:
)

def __str__(self) -> str:
taxes = ", ".join([f"{t.value} {t.rate}%" for t in self.taxes])
return (
"-----Receipt data-----\n"
"Filename: %s\n"
"Total amount including taxes: %s \n"
"Total amount excluding taxes: %s \n"
"Date: %s\n"
"Category: %s\n"
"Time: %s\n"
"Merchant name: %s\n"
"Taxes: %s\n"
"Total taxes: %s\n"
f"Filename: {self.filename}\n"
f"Total amount including taxes: {self.total_incl.value}\n"
f"Total amount excluding taxes: {self.total_excl.value}\n"
f"Date: {self.date.value}\n"
f"Category: {self.category.value}\n"
f"Time: {self.time.value}\n"
f"Merchant name: {self.merchant_name.value}\n"
f"Taxes: {taxes}\n"
f"Total taxes: {self.total_tax.value}\n"
f"Locale: {self.locale}\n"
"----------------------"
% (
self.filename,
self.total_incl.value,
self.total_excl.value,
self.date.value,
self.category.value,
self.time.value,
self.merchant_name.value,
" - ".join([str(t) for t in self.taxes]),
self.total_tax.value,
)
)

def build_from_api_prediction(self, api_prediction, page_n=0):
Expand Down
3 changes: 3 additions & 0 deletions mindee/fields/amount.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from typing import Optional
from mindee.fields.base import Field


class Amount(Field):
value: Optional[float] = None

def __init__(
self, amount_prediction, value_key="amount", reconstructed=False, page_n=None
):
Expand Down
6 changes: 3 additions & 3 deletions mindee/fields/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Any, List
from typing import Optional, Any, List, Dict


class Field:
Expand All @@ -8,8 +8,8 @@ class Field:

def __init__(
self,
abstract_prediction,
value_key="value",
abstract_prediction: Dict[str, Any],
value_key: str = "value",
reconstructed=False,
extra_fields=None,
page_n=None,
Expand Down
41 changes: 22 additions & 19 deletions mindee/fields/locale.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from typing import Optional
from mindee.fields.base import Field


class Locale(Field):
language: Optional[str] = None
country: Optional[str] = None
currency: Optional[str] = None

def __init__(
self, locale_prediction, value_key="value", reconstructed=False, page_n=None
):
Expand All @@ -17,27 +22,25 @@ def __init__(
reconstructed=reconstructed,
page_n=page_n,
)
self.language = self._get_value(locale_prediction, "language")
self.country = self._get_value(locale_prediction, "country")
self.currency = self._get_value(locale_prediction, "currency")

@staticmethod
def _get_value(locale_prediction, key: str):
if (
"language" not in locale_prediction.keys()
key not in locale_prediction.keys()
or locale_prediction["language"] == "N/A"
):
self.language = None
else:
self.language = locale_prediction["language"]

if (
"country" not in locale_prediction.keys()
or locale_prediction["country"] == "N/A"
):
self.country = None
else:
self.country = locale_prediction["country"]
return None
return locale_prediction[key]

if (
"currency" not in locale_prediction.keys()
or locale_prediction["currency"] == "N/A"
):
self.currency = None
else:
self.currency = locale_prediction["currency"]
def __str__(self) -> str:
output_str = f"{self.value}; "
if self.language is not None:
output_str += self.language + "; "
if self.country is not None:
output_str += self.country + "; "
if self.currency is not None:
output_str += self.currency + "; "
return output_str.strip()
18 changes: 7 additions & 11 deletions mindee/fields/payment_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,14 @@ def __init__(
except (KeyError, AssertionError):
self.swift = None

def __str__(self):
payment_str = ""
def __str__(self) -> str:
output_str = ""
if self.account_number is not None:
payment_str += str(self.account_number) + "; "

output_str += str(self.account_number) + "; "
if self.iban is not None:
payment_str += str(self.iban) + "; "

output_str += str(self.iban) + "; "
if self.routing_number is not None:
payment_str += str(self.routing_number) + "; "

output_str += str(self.routing_number) + "; "
if self.swift is not None:
payment_str += str(self.swift) + "; "

return payment_str
output_str += str(self.swift) + "; "
return output_str.strip()
18 changes: 17 additions & 1 deletion tests/documents/test_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,23 @@ def test_constructor(invoice_object):
assert invoice_object.invoice_date.confidence == 0.97
assert invoice_object.invoice_number.value == "0042004801351"
assert invoice_object.invoice_number.confidence == 0.95
assert isinstance(invoice_object.__str__(), str)
assert (
str(invoice_object)
== """-----Invoice data-----
Filename: None
Invoice number: 0042004801351
Total amount including taxes: 587.95
Total amount excluding taxes: 489.97
Invoice date: 2020-02-17
Invoice due date: 2020-02-17
Supplier name: 1
Payment details:
Company numbers: 501124705; FR33501124705
Taxes: 97.98 20.0%
Total taxes: 97.98
Locale: fr; fr; EUR;
----------------------"""
)


def test_all_na(invoice_object_all_na):
Expand Down
16 changes: 15 additions & 1 deletion tests/documents/test_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,21 @@ def test_constructor(receipt_object):
assert receipt_object.date.value == "2016-02-26"
assert receipt_object.total_tax.value == 1.7
assert receipt_object.checklist["taxes_match_total_incl"] is True
assert type(receipt_object.__str__()) == str
assert (
str(receipt_object)
== """-----Receipt data-----
Filename: None
Total amount including taxes: 10.2
Total amount excluding taxes: 8.5
Date: 2016-02-26
Category: food
Time: 15:20
Merchant name: CLACHAN
Taxes: 1.7 20.0%
Total taxes: 1.7
Locale: en-GB; en; GB; GBP;
----------------------"""
)


def test_all_na(receipt_object_all_na):
Expand Down
1 change: 1 addition & 0 deletions tests/fields/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_constructor():
}
date = Date(field_dict)
assert date.value == "2018-04-01"
assert isinstance(date.date_object, datetime.date)


def test_constructor_no_date():
Expand Down
1 change: 1 addition & 0 deletions tests/fields/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def test_constructor():
assert locale.language == "en"
assert locale.country == "uk"
assert locale.currency == "GBP"
assert str(locale) == "en-EN; en; uk; GBP;"


def test_constructor_almost_empty_field():
Expand Down
2 changes: 1 addition & 1 deletion tests/fields/test_payment_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_constructor():
assert payment_detail.iban == "iban"
assert payment_detail.routing_number == "routing_number"
assert payment_detail.swift == "swift"
assert type(str(payment_detail)) == str
assert str(payment_detail) == "account_number; iban; routing_number; swift;"


def test_constructor_all_na():
Expand Down