|
| 1 | +from typing import Optional, TypeVar |
| 2 | + |
| 3 | +from mindee.documents.base import Document, TypeApiPrediction, clean_out_string |
| 4 | +from mindee.fields.amount import AmountField |
| 5 | + |
| 6 | +from .petrol_receipt_v1_fuel import PetrolReceiptV1Fuel |
| 7 | +from .petrol_receipt_v1_total import PetrolReceiptV1Total |
| 8 | + |
| 9 | + |
| 10 | +class PetrolReceiptV1(Document): |
| 11 | + """Petrol Receipt v1 prediction results.""" |
| 12 | + |
| 13 | + fuel: PetrolReceiptV1Fuel |
| 14 | + """The fuel type.""" |
| 15 | + price: AmountField |
| 16 | + """The price per unit of fuel.""" |
| 17 | + total: PetrolReceiptV1Total |
| 18 | + """The total amount paid.""" |
| 19 | + volume: AmountField |
| 20 | + """The volume of fuel purchased.""" |
| 21 | + |
| 22 | + def __init__( |
| 23 | + self, |
| 24 | + api_prediction=None, |
| 25 | + input_source=None, |
| 26 | + page_n: Optional[int] = None, |
| 27 | + ): |
| 28 | + """ |
| 29 | + Petrol Receipt v1 prediction results. |
| 30 | +
|
| 31 | + :param api_prediction: Raw prediction from HTTP response |
| 32 | + :param input_source: Input object |
| 33 | + :param page_n: Page number for multi pages pdf input |
| 34 | + """ |
| 35 | + super().__init__( |
| 36 | + input_source=input_source, |
| 37 | + document_type="petrol_receipt", |
| 38 | + api_prediction=api_prediction, |
| 39 | + page_n=page_n, |
| 40 | + ) |
| 41 | + self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n) |
| 42 | + |
| 43 | + def _build_from_api_prediction( |
| 44 | + self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None |
| 45 | + ) -> None: |
| 46 | + """ |
| 47 | + Build the object from the prediction API JSON. |
| 48 | +
|
| 49 | + :param api_prediction: Raw prediction from HTTP response |
| 50 | + :param page_n: Page number |
| 51 | + """ |
| 52 | + self.fuel = PetrolReceiptV1Fuel( |
| 53 | + api_prediction["fuel"], |
| 54 | + page_id=page_n, |
| 55 | + ) |
| 56 | + self.price = AmountField( |
| 57 | + api_prediction["price"], |
| 58 | + page_id=page_n, |
| 59 | + ) |
| 60 | + self.total = PetrolReceiptV1Total( |
| 61 | + api_prediction["total"], |
| 62 | + page_id=page_n, |
| 63 | + ) |
| 64 | + self.volume = AmountField( |
| 65 | + api_prediction["volume"], |
| 66 | + page_id=page_n, |
| 67 | + ) |
| 68 | + |
| 69 | + def __str__(self) -> str: |
| 70 | + return clean_out_string( |
| 71 | + "FR Petrol Receipt V1 Prediction\n" |
| 72 | + "===============================\n" |
| 73 | + f":Filename: {self.filename or ''}\n" |
| 74 | + f":Fuel Type:\n{self.fuel.to_field_list()}\n" |
| 75 | + f":Price per Unit: {self.price}\n" |
| 76 | + f":Volume: {self.volume}\n" |
| 77 | + f":Total Amount:\n{self.total.to_field_list()}\n" |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +TypePetrolReceiptV1 = TypeVar( |
| 82 | + "TypePetrolReceiptV1", |
| 83 | + bound=PetrolReceiptV1, |
| 84 | +) |
0 commit comments