Skip to content

Commit 1b17e1c

Browse files
committed
Complete InMemoryStorage doc
1 parent e26ad10 commit 1b17e1c

File tree

1 file changed

+68
-9
lines changed

1 file changed

+68
-9
lines changed

openfisca_core/data_storage/in_memory_storage.py

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Any, Optional, Sequence
3+
from typing import Optional, Sequence
44

55
import numpy
66

@@ -14,21 +14,41 @@ class InMemoryStorage:
1414
"""Class responsible for storing/retrieving vectors in/from memory.
1515
1616
Attributes:
17-
_arrays: ?
18-
is_eternal: ?
17+
_arrays: A dictionary containing data that has been stored in memory.
18+
is_eternal: A boolean indicating whether the storage is eternal.
1919
2020
Args:
21-
is_eternal: ?
21+
is_eternal: A boolean indicating whether the storage is eternal.
2222
2323
"""
2424

25-
_arrays: Arrays
25+
_arrays: Arrays = Arrays({})
2626

2727
def __init__(self, is_eternal: bool = False) -> None:
28-
self._arrays = Arrays({})
2928
self.is_eternal = is_eternal
3029

31-
def get(self, period: types.Period) -> Any:
30+
def get(self, period: types.Period) -> Optional[numpy.ndarray]:
31+
"""Retrieve the data for the specified period from memory.
32+
33+
Args:
34+
period: The period for which data should be retrieved.
35+
36+
Returns:
37+
The data for the specified period, or None if no data is available.
38+
39+
Examples:
40+
>>> storage = InMemoryStorage()
41+
>>> value = numpy.array([1, 2, 3])
42+
>>> instant = periods.Instant((2017, 1, 1))
43+
>>> period = periods.Period(("year", instant, 1))
44+
45+
>>> storage.put(value, period)
46+
47+
>>> storage.get(period)
48+
array([1, 2, 3])
49+
50+
"""
51+
3252
period = _funcs.parse_period(period, self.is_eternal)
3353
values = self._arrays.get(period)
3454

@@ -37,11 +57,50 @@ def get(self, period: types.Period) -> Any:
3757

3858
return values
3959

40-
def put(self, value: Any, period: types.Period) -> None:
60+
def put(self, value: numpy.ndarray, period: types.Period) -> None:
61+
"""Store the specified data in memory for the specified period.
62+
63+
Args:
64+
value: The data to store
65+
period: The period for which the data should be stored.
66+
67+
Examples:
68+
>>> storage = InMemoryStorage()
69+
>>> value = numpy.array([1, 2, 3])
70+
>>> instant = periods.Instant((2017, 1, 1))
71+
>>> period = periods.Period(("year", instant, 1))
72+
73+
>>> storage.put(value, period)
74+
75+
"""
76+
4177
period = _funcs.parse_period(period, self.is_eternal)
4278
self._arrays = Arrays({period: value, **self._arrays})
4379

4480
def delete(self, period: Optional[types.Period] = None) -> None:
81+
"""Delete the data for the specified period from memory.
82+
83+
Args:
84+
period: The period for which data should be deleted. If not
85+
specified, all data will be deleted.
86+
87+
Examples:
88+
>>> storage = InMemoryStorage()
89+
>>> value = numpy.array([1, 2, 3])
90+
>>> instant = periods.Instant((2017, 1, 1))
91+
>>> period = periods.Period(("year", instant, 1))
92+
93+
>>> storage.put(value, period)
94+
95+
>>> storage.get(period)
96+
array([1, 2, 3])
97+
98+
>>> storage.delete(period)
99+
100+
>>> storage.get(period)
101+
102+
"""
103+
45104
if period is None:
46105
self._arrays = Arrays({})
47106
return None
@@ -58,7 +117,7 @@ def get_known_periods(self) -> Sequence[types.Period]:
58117
"""List of storage's known periods.
59118
60119
Returns:
61-
A list of periods.
120+
A sequence containing the storage's known periods.
62121
63122
Examples:
64123
>>> storage = InMemoryStorage()

0 commit comments

Comments
 (0)