| 1 |
""" |
| 2 |
Module defining the `ImportExport` class that enables getting state variable |
| 3 |
data in and out of groups in various formats (see `Group.get_states` and |
| 4 |
`Group.set_states`). |
| 5 |
""" |
| 6 |
|
| 7 |
import abc |
| 8 |
from abc import abstractmethod |
| 9 |
|
| 10 |
|
| 11 |
class ImportExport(metaclass=abc.ABCMeta): |
| 12 |
""" |
| 13 |
Class for registering new import/export methods (via static methods). Also |
| 14 |
the base class that should be extended for such methods |
| 15 |
(`ImportExport.export_data`, `ImportExport.import_data`, and |
| 16 |
`ImportExport.name` have to be overwritten). |
| 17 |
|
| 18 |
See Also |
| 19 |
-------- |
| 20 |
VariableOwner.get_states, VariableOwner.set_states |
| 21 |
|
| 22 |
""" |
| 23 |
|
| 24 |
#: A dictionary mapping import/export methods names to `ImportExport` objects |
| 25 |
methods = dict() |
| 26 |
|
| 27 |
@staticmethod |
| 28 |
def register(importerexporter): |
| 29 |
""" |
| 30 |
Register a import/export method. Registered methods can be referred to |
| 31 |
via their name. |
| 32 |
|
| 33 |
Parameters |
| 34 |
---------- |
| 35 |
importerexporter : `ImportExport` |
| 36 |
The importerexporter object, e.g. an `DictImportExport`. |
| 37 |
""" |
| 38 |
if not isinstance(importerexporter, ImportExport): |
| 39 |
t = str(type(importerexporter)) |
| 40 |
error_msg = ( |
| 41 |
f"Given importerexporter of type {t} does not seem to " |
| 42 |
"be a valid importerexporter." |
| 43 |
) |
| 44 |
raise ValueError(error_msg) |
| 45 |
name = importerexporter.name |
| 46 |
if name in ImportExport.methods: |
| 47 |
raise ValueError( |
| 48 |
f"An import/export methods with the name '{name}'" |
| 49 |
"has already been registered" |
| 50 |
) |
| 51 |
ImportExport.methods[name] = importerexporter |
| 52 |
|
| 53 |
@staticmethod |
| 54 |
@abstractmethod |
| 55 |
def export_data(group, variables): |
| 56 |
""" |
| 57 |
Asbtract static export data method with two obligatory parameters. |
| 58 |
It should return a copy of the current state variable values. The |
| 59 |
returned arrays are copies of the actual arrays that store the state |
| 60 |
variable values, therefore changing the values in the returned |
| 61 |
dictionary will not affect the state variables. |
| 62 |
|
| 63 |
Parameters |
| 64 |
---------- |
| 65 |
group : `Group` |
| 66 |
Group object. |
| 67 |
variables : list of str |
| 68 |
The names of the variables to extract. |
| 69 |
""" |
| 70 |
raise NotImplementedError() |
| 71 |
|
| 72 |
@staticmethod |
| 73 |
@abstractmethod |
| 74 |
def import_data(group, data): |
| 75 |
""" |
| 76 |
Import and set state variables. |
| 77 |
|
| 78 |
Parameters |
| 79 |
---------- |
| 80 |
group : `Group` |
| 81 |
Group object. |
| 82 |
data : dict_like |
| 83 |
Data to import with variable names. |
| 84 |
""" |
| 85 |
raise NotImplementedError() |
| 86 |
|
| 87 |
@property |
| 88 |
@abc.abstractmethod |
| 89 |
def name(self): |
| 90 |
""" |
| 91 |
Abstract property giving a method name. |
| 92 |
""" |
| 93 |
pass |