From 32c89cc3a281878202f1803298da78a65c8f3584 Mon Sep 17 00:00:00 2001 From: Denis Merigoux Date: Wed, 21 Sep 2022 18:39:23 +0200 Subject: [PATCH] Fix Python backend for housing benefits --- french_law/python/main.py | 2 +- french_law/python/src/api.py | 1 + runtimes/python/catala/src/catala/runtime.py | 28 ++++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/french_law/python/main.py b/french_law/python/main.py index aca3c020..c63c88bf 100755 --- a/french_law/python/main.py +++ b/french_law/python/main.py @@ -150,5 +150,5 @@ if __name__ == '__main__': "".ljust(indentation), colored("Decision taken:", "green"), colored("{}".format(log_event.payload), "magenta"))) # type: ignore else: print("Action '{}' not recognized!".format(action)) - call_aides_logement() + print(call_aides_logement()) exit(-1) diff --git a/french_law/python/src/api.py b/french_law/python/src/api.py index dc8c1850..faac3eae 100644 --- a/french_law/python/src/api.py +++ b/french_law/python/src/api.py @@ -361,3 +361,4 @@ def aides_logement( ressources_menage_prises_en_compte_in=money_of_units_int( ressources_menage_prises_en_compte), )) + return money_to_float(out.aide_finale_out) diff --git a/runtimes/python/catala/src/catala/runtime.py b/runtimes/python/catala/src/catala/runtime.py index 79ed588d..1cae0d91 100644 --- a/runtimes/python/catala/src/catala/runtime.py +++ b/runtimes/python/catala/src/catala/runtime.py @@ -9,7 +9,7 @@ # This file should be in sync with compiler/runtime.{ml, mli} ! -from gmpy2 import log2, mpz, mpq, mpfr, t_divmod, f_div, sign # type: ignore +from gmpy2 import log2, mpz, mpq, mpfr, t_divmod, qdiv, f_div, sign # type: ignore import datetime import calendar import dateutil.relativedelta @@ -410,11 +410,18 @@ def money_to_cents(m: Money) -> Integer: def money_round(m: Money) -> Money: - res, remainder = t_divmod(m, 100) + res, remainder = t_divmod(m.value.value, 100) if remainder < 50: - return res * 100 + return Money(Integer(res * 100)) else: - return (res + sign(res)) * 100 + return Money(Integer((res + sign(res)) * 100)) + + +def money_of_decimal(d: Decimal) -> Money: + """ + Warning: rounds to the nearest cent + """ + return Money(Integer(mpz(d.value))) # -------- @@ -443,13 +450,18 @@ def decimal_to_string(precision: int, i: Decimal) -> str: def decimal_round(q: Decimal) -> Decimal: - # Implements the workaround by - # https://gmplib.org/list-archives/gmp-discuss/2009-May/003767.html *) - return f_div(2*q.numerator + q.denominator, 2*q.denominator) # type:ignore + """ + Implements the workaround by + https://gmplib.org/list-archives/gmp-discuss/2009-May/003767.html + """ + return Decimal( + mpq(f_div(2*q.value.numerator + q.value.denominator, + 2*q.value.denominator), 1) # type:ignore + ) def decimal_of_money(m: Money) -> Decimal: - return Decimal(f_div(mpq(m.value), mpq(100))) + return Decimal(mpq(qdiv(m.value.value, 100))) # -------- # Integers