Skip to content

Commit

Permalink
test(storage): fix data_storage doctest (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko authored Oct 4, 2024
2 parents 9cd7d81 + 8aba546 commit 6a8c7d7
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

### 42.0.5 [#1261](https://github.com/openfisca/openfisca-core/pull/1261)

#### Technical changes

- Fix doctests of `data_storage` module

### 42.0.4 [#1257](https://github.com/openfisca/openfisca-core/pull/1257)

#### Technical changes
Expand Down
93 changes: 92 additions & 1 deletion openfisca_core/data_storage/in_memory_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@


class InMemoryStorage:
"""Low-level class responsible for storing and retrieving calculated vectors in memory."""
"""Storing and retrieving calculated vectors in memory."""

def __init__(self, is_eternal=False) -> None:
self._arrays = {}
self.is_eternal = is_eternal

def get(self, period):
"""
Examples:
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put(value, period)
>>> storage.get(period)
array([1, 2, 3])
"""

if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(period)
Expand All @@ -22,13 +40,59 @@ def get(self, period):
return values

def put(self, value, period) -> None:
"""
Examples:
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> value = numpy.array([1, "2", "salary"])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put(value, period)
>>> storage.get(period)
array(['1', '2', 'salary'], dtype='<U21')
"""

if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(period)

self._arrays[period] = value

def delete(self, period=None) -> None:
"""
Examples:
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put(value, period)
>>> storage.get(period)
array([1, 2, 3])
>>> storage.delete(period)
>>> storage.get(period)
>>> storage.put(value, period)
>>> storage.delete()
>>> storage.get(period)
"""

if period is None:
self._arrays = {}
return
Expand All @@ -44,9 +108,36 @@ def delete(self, period=None) -> None:
}

def get_known_periods(self):
"""
Examples:
>>> from openfisca_core import data_storage, periods
>>> storage = data_storage.InMemoryStorage()
>>> storage.get_known_periods()
dict_keys([])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> storage.put([], period)
>>> storage.get_known_periods()
dict_keys([Period(('year', Instant((2017, 1, 1)), 1))])
"""

return self._arrays.keys()

def get_memory_usage(self):
"""
Examples:
>>> from openfisca_core import data_storage
>>> storage = data_storage.InMemoryStorage()
>>> storage.get_memory_usage()
{'nb_arrays': 0, 'total_nb_bytes': 0, 'cell_size': nan}
"""

if not self._arrays:
return {
"nb_arrays": 0,
Expand Down
155 changes: 154 additions & 1 deletion openfisca_core/data_storage/on_disk_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


class OnDiskStorage:
"""Low-level class responsible for storing and retrieving calculated vectors on disk."""
"""Storing and retrieving calculated vectors on disk."""

def __init__(
self, storage_dir, is_eternal=False, preserve_storage_dir=False
Expand All @@ -21,12 +21,59 @@ def __init__(
self.storage_dir = storage_dir

def _decode_file(self, file):
"""
Examples
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, indexed_enums, periods
>>> class Housing(indexed_enums.Enum):
... OWNER = "Owner"
... TENANT = "Tenant"
... FREE_LODGER = "Free lodger"
... HOMELESS = "Homeless"
>>> array = numpy.array([1])
>>> value = indexed_enums.EnumArray(array, Housing)
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage._decode_file(storage._files[period])
EnumArray([<Housing.TENANT: 'Tenant'>])
"""

enum = self._enums.get(file)
if enum is not None:
return EnumArray(numpy.load(file), enum)
return numpy.load(file)

def get(self, period):
"""
Examples:
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.get(period)
array([1, 2, 3])
"""

if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(period)
Expand All @@ -37,6 +84,26 @@ def get(self, period):
return self._decode_file(values)

def put(self, value, period) -> None:
"""
Examples:
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, "2", "salary"])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.get(period)
array(['1', '2', 'salary'], dtype='<U21')
"""

if self.is_eternal:
period = periods.period(DateUnit.ETERNITY)
period = periods.period(period)
Expand All @@ -50,6 +117,38 @@ def put(self, value, period) -> None:
self._files[period] = path

def delete(self, period=None) -> None:
"""
Examples:
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.get(period)
array([1, 2, 3])
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.delete(period)
... storage.get(period)
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put(value, period)
... storage.delete()
... storage.get(period)
"""

if period is None:
self._files = {}
return
Expand All @@ -66,9 +165,63 @@ def delete(self, period=None) -> None:
}

def get_known_periods(self):
"""
Examples:
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.get_known_periods()
dict_keys([])
>>> with tempfile.TemporaryDirectory() as directory:
... storage = data_storage.OnDiskStorage(directory)
... storage.put([], period)
... storage.get_known_periods()
dict_keys([Period(('year', Instant((2017, 1, 1)), 1))])
"""

return self._files.keys()

def restore(self) -> None:
"""
Examples:
>>> import tempfile
>>> import numpy
>>> from openfisca_core import data_storage, periods
>>> value = numpy.array([1, 2, 3])
>>> instant = periods.Instant((2017, 1, 1))
>>> period = periods.Period(("year", instant, 1))
>>> directory = tempfile.TemporaryDirectory()
>>> storage1 = data_storage.OnDiskStorage(directory.name)
>>> storage1.put(value, period)
>>> storage1._files
{Period(('year', Instant((2017, 1, 1)), 1)): '.../2017.npy'}
>>> storage2 = data_storage.OnDiskStorage(directory.name)
>>> storage2._files
{}
>>> storage2.restore()
>>> storage2._files
{Period((<DateUnit.YEAR: 'year'>, Instant((2017, 1, 1.../2017.npy'}
>>> directory.cleanup()
"""

self._files = files = {}
# Restore self._files from content of storage_dir.
for filename in os.listdir(self.storage_dir):
Expand Down
1 change: 1 addition & 0 deletions openfisca_tasks/test_code.mk
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test-core: $(shell git ls-files "*test_*.py")
@$(call print_help,$@:)
@python -m pytest --capture=no --xdoctest --xdoctest-verbose=0 \
openfisca_core/commons \
openfisca_core/data_storage \
openfisca_core/entities \
openfisca_core/holders \
openfisca_core/periods \
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

setup(
name="OpenFisca-Core",
version="42.0.4",
version="42.0.5",
author="OpenFisca Team",
author_email="[email protected]",
classifiers=[
Expand Down

0 comments on commit 6a8c7d7

Please sign in to comment.