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

feat: merge holders with storage #1167

Draft
wants to merge 24 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
25 changes: 0 additions & 25 deletions openfisca_core/data_storage/__init__.py

This file was deleted.

63 changes: 0 additions & 63 deletions openfisca_core/data_storage/in_memory_storage.py

This file was deleted.

89 changes: 0 additions & 89 deletions openfisca_core/data_storage/on_disk_storage.py

This file was deleted.

64 changes: 35 additions & 29 deletions openfisca_core/holders/__init__.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
# Transitional imports to ensure non-breaking changes.
# Could be deprecated in the next major release.
#
# How imports are being used today:
#
# >>> from openfisca_core.module import symbol
#
# The previous example provokes cyclic dependency problems
# that prevent us from modularizing the different components
# of the library so to make them easier to test and to maintain.
#
# How could them be used after the next major release:
#
# >>> from openfisca_core import module
# >>> module.symbol()
#
# And for classes:
#
# >>> from openfisca_core.module import Symbol
# >>> Symbol()
#
# See: https://www.python.org/dev/peps/pep-0008/#imports

from .helpers import ( # noqa: F401
set_input_dispatch_by_period,
set_input_divide_by_period,
)
from .holder import Holder # noqa: F401
from .memory_usage import MemoryUsage # noqa: F401
"""Transitional imports to ensure non-breaking changes.

These imports could be deprecated in the next major release.

Currently, imports are used in the following way::

from openfisca_core.module import symbol

This example causes cyclic dependency problems, which prevent us from
modularising the different components of the library and make them easier to
test and maintain.

After the next major release, imports could be used in the following way::

from openfisca_core import module
module.symbol()

And for classes::

from openfisca_core.module import Symbol
Symbol()

.. seealso:: `PEP8#Imports`_ and `OpenFisca's Styleguide`_.

.. _PEP8#Imports:
https://www.python.org/dev/peps/pep-0008/#imports

.. _OpenFisca's Styleguide:
https://github.com/openfisca/openfisca-core/blob/master/STYLEGUIDE.md

"""

from .helpers import set_input_dispatch_by_period, set_input_divide_by_period
from .holder import Holder
from .repos import DiskRepo, MemoryRepo
1 change: 1 addition & 0 deletions openfisca_core/holders/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from ._set_input import set_input_dispatch_by_period, set_input_divide_by_period
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from typing import Any

from openfisca_core.types import Period

import logging

import numpy

from openfisca_core import periods

from ..holder import Holder

log = logging.getLogger(__name__)


def set_input_dispatch_by_period(holder, period, array) -> None:
"""This function can be declared as a ``set_input`` attribute of a variable.
def set_input_dispatch_by_period(holder: Holder, period: Period, array: Any) -> None:
"""
This function can be declared as a ``set_input`` attribute of a variable.

In this case, the variable will accept inputs on larger periods that its definition period, and the value for the larger period will be applied to all its subperiods.

Expand All @@ -19,15 +26,12 @@ def set_input_dispatch_by_period(holder, period, array) -> None:
period_size = period.size
period_unit = period.unit

if holder.variable.definition_period not in (
periods.DateUnit.isoformat + periods.DateUnit.isocalendar
):
msg = "set_input_dispatch_by_period can't be used for eternal variables."
if holder.eternal:
raise ValueError(
msg,
"set_input_dispatch_by_period can't be used for eternal variables."
)

cached_period_unit = holder.variable.definition_period
cached_period_unit = holder.period
after_instant = period.start.offset(period_size, period_unit)

# Cache the input data, skipping the existing cached months
Expand All @@ -43,8 +47,9 @@ def set_input_dispatch_by_period(holder, period, array) -> None:
sub_period = sub_period.offset(1)


def set_input_divide_by_period(holder, period, array) -> None:
"""This function can be declared as a ``set_input`` attribute of a variable.
def set_input_divide_by_period(holder: Holder, period: Period, array: Any) -> None:
"""
This function can be declared as a ``set_input`` attribute of a variable.

In this case, the variable will accept inputs on larger periods that its definition period, and the value for the larger period will be divided between its subperiods.

Expand All @@ -55,15 +60,12 @@ def set_input_divide_by_period(holder, period, array) -> None:
period_size = period.size
period_unit = period.unit

if holder.variable.definition_period not in (
periods.DateUnit.isoformat + periods.DateUnit.isocalendar
):
msg = "set_input_divide_by_period can't be used for eternal variables."
if holder.eternal:
raise ValueError(
msg,
"set_input_divide_by_period can't be used for eternal variables."
)

cached_period_unit = holder.variable.definition_period
cached_period_unit = holder.period
after_instant = period.start.offset(period_size, period_unit)

# Count the number of elementary periods to change, and the difference with what is already known.
Expand All @@ -87,7 +89,8 @@ def set_input_divide_by_period(holder, period, array) -> None:
holder._set(sub_period, divided_array)
sub_period = sub_period.offset(1)
elif not (remaining_array == 0).all():
msg = f"Inconsistent input: variable {holder.variable.name} has already been set for all months contained in period {period}, and value {array} provided for {period} doesn't match the total ({array - remaining_array}). This error may also be thrown if you try to call set_input twice for the same variable and period."
raise ValueError(
msg,
"Inconsistent input: variable {0} has already been set for all months contained in period {1}, and value {2} provided for {1} doesn't match the total ({3}). This error may also be thrown if you try to call set_input twice for the same variable and period.".format(
holder.variable.name, period, array, array - remaining_array
)
)
Loading
Loading