catala/french_law/python/main.py

89 lines
3.6 KiB
Python
Raw Normal View History

#!python3
2021-06-26 18:09:47 +03:00
from datetime import date
from src.allocations_familiales import PriseEnCharge_Code, Collectivite_Code
from src.api import allocations_familiales, Enfant
2021-06-26 19:04:36 +03:00
from src.catala import LogEvent, LogEventCode, reset_log, retrieve_log
2021-06-25 01:56:08 +03:00
import timeit
2021-06-26 18:09:47 +03:00
import argparse
2021-06-26 19:13:13 +03:00
from typing import List, Any
2021-06-26 19:04:36 +03:00
from termcolor import colored
2021-06-26 18:09:47 +03:00
def call_allocations_familiales() -> float:
return allocations_familiales(
date_courante=date(2020, 4, 20),
enfants=[
Enfant(id=0, remuneration_mensuelle=0,
date_de_naissance=date(2003, 2, 2),
prise_en_charge=PriseEnCharge_Code.EffectiveEtPermanente,
a_deja_ouvert_droit_aux_allocations_familiales=True),
Enfant(id=1, remuneration_mensuelle=300,
date_de_naissance=date(2013, 9, 30),
prise_en_charge=PriseEnCharge_Code.GardeAlterneePartageAllocations,
a_deja_ouvert_droit_aux_allocations_familiales=True)
],
ressources_menage=30000,
residence=Collectivite_Code.Metropole,
personne_charge_effective_permanente_est_parent=True,
personne_charge_effective_permanente_remplit_titre_I=True,
2021-11-07 02:53:14 +03:00
avait_enfant_a_charge_avant_1er_janvier_2012=False,
2021-06-26 18:09:47 +03:00
)
2021-06-26 18:09:47 +03:00
def benchmark_iteration():
money_given = call_allocations_familiales()
assert (money_given == 99.37)
2021-06-25 01:56:08 +03:00
2021-06-26 19:04:36 +03:00
def run_with_log() -> List[LogEvent]:
money_given = call_allocations_familiales()
assert (money_given == 99.37)
log = retrieve_log()
reset_log()
return log
2021-06-26 19:13:13 +03:00
def print_value(v: Any) -> str:
if isinstance(v, list):
return "[" + ",".join([str(x) for x in v]) + "]"
else:
return str(v)
if __name__ == '__main__':
2021-06-26 18:09:47 +03:00
parser = argparse.ArgumentParser(
description='French law library in Python')
parser.add_argument('action', metavar='ACTION', type=str, nargs=1,
help="'bench' or 'show_log'")
args = parser.parse_args()
action = args.action[0]
if action == "bench":
iterations = 10000
print("Iterating {} iterations of the family benefits computation. Total time (s):".format(
iterations))
print(timeit.timeit(benchmark_iteration, number=iterations))
2021-06-26 19:04:36 +03:00
elif action == "show_log":
log = run_with_log()
indentation = 0
for log_event in log:
if log_event.code == LogEventCode.BeginCall:
print("{}{} {}".format(
"".ljust(indentation), colored("Begin call:", "yellow"), colored(" >> ".join(log_event.payload), "magenta"))) # type: ignore
indentation += 2
elif log_event.code == LogEventCode.EndCall:
indentation -= 2
print("{}{} {}".format(
"".ljust(indentation), colored("End call:", "yellow"), colored(" >> ".join(log_event.payload), "magenta"))) # type: ignore
elif log_event.code == LogEventCode.VariableDefinition:
headings, value = log_event.payload # type: ignore
print("{}{} {} {} {}".format(
2021-06-26 19:13:13 +03:00
"".ljust(indentation), colored("Variable definition:", "blue"), colored(" >> ".join(headings), "magenta"), colored(":=", "blue"), colored(print_value(value), "green"))) # type: ignore
2021-06-26 19:04:36 +03:00
elif log_event.code == LogEventCode.DecisionTaken:
print("{}{} {}".format(
"".ljust(indentation), colored("Decision taken:", "green"), colored("{}".format(log_event.payload), "magenta"))) # type: ignore
2021-06-26 18:09:47 +03:00
else:
print("Action '{}' not recognized!".format(action))
exit(-1)