Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expérimente le cache des paramètres #1311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 27 additions & 3 deletions openfisca_france/france_taxbenefitsystem.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# -*- coding: utf-8 -*-

import os
import pickle

import checksumdir
import appdirs

from openfisca_core.taxbenefitsystems import TaxBenefitSystem

Expand All @@ -11,6 +15,7 @@


COUNTRY_DIR = os.path.dirname(os.path.abspath(__file__))
CACHE_PARAMETER_TREE = True


class FranceTaxBenefitSystem(TaxBenefitSystem):
Expand All @@ -24,9 +29,7 @@ class FranceTaxBenefitSystem(TaxBenefitSystem):
def __init__(self):
TaxBenefitSystem.__init__(self, entities)

param_dir = os.path.join(COUNTRY_DIR, 'parameters')
self.load_parameters(param_dir)

self.load_parameters_from_cache()
self.add_variables_from_directory(os.path.join(COUNTRY_DIR, 'model'))
self.cache_blacklist = conf_cache_blacklist

Expand All @@ -36,6 +39,27 @@ def __init__(self):
"simulation_example": couple,
}

def load_parameters_from_cache(self):
param_dir = os.path.join(COUNTRY_DIR, 'parameters')
if not CACHE_PARAMETER_TREE:
return self.load_parameters(param_dir)
dir_hash = checksumdir.dirhash(param_dir)
cached_dir = appdirs.user_cache_dir(self.get_package_metadata()['name'])
pickle_path = os.path.join(cached_dir, f'{dir_hash}.pickle')

if os.path.isfile(pickle_path):
# print("Found cached parameters")
with open(pickle_path, "rb") as pickle_file:
self.parameters = pickle.load(pickle_file)
else:
# print("No cached parameters")
if not os.path.isdir(cached_dir):
os.mkdir(cached_dir)
self.load_parameters(param_dir)
with open(pickle_path, "wb+") as pickle_file:
pickle.dump(self.parameters, pickle_file)


def prefill_cache(self):
# Compute one "zone APL" variable, to pre-load CSV of "code INSEE commune" to "Zone APL".
from .model.prestations import aides_logement
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
include_package_data = True, # Will read MANIFEST.in
install_requires = [
"OpenFisca-Core >=31.0,<33.0",
"checksumdir == 1.1.6",
"appdirs == 1.4.3",
],
message_extractors = {"openfisca_france": [
("**.py", "python", None),
Expand Down