|
4 | 4 | # (see guidata/LICENSE for details) |
5 | 5 |
|
6 | 6 | """ |
7 | | -Conversion utilities |
8 | | --------------------- |
| 7 | +DataSet class conversion/creation functions |
| 8 | +=========================================== |
| 9 | +
|
| 10 | +Update and restore datasets |
| 11 | +--------------------------- |
| 12 | +
|
| 13 | +.. autofunction:: guidata.dataset.update_dataset |
| 14 | +
|
| 15 | +.. autofunction:: guidata.dataset.restore_dataset |
| 16 | +
|
| 17 | +Create dataset classes |
| 18 | +---------------------- |
9 | 19 |
|
10 | 20 | .. autofunction:: guidata.dataset.create_dataset_from_func |
| 21 | +
|
| 22 | +.. autofunction:: guidata.dataset.create_dataset_from_dict |
11 | 23 | """ |
12 | 24 |
|
13 | 25 | from __future__ import annotations |
14 | 26 |
|
15 | 27 | import inspect |
16 | | -from typing import Any |
| 28 | +from typing import TYPE_CHECKING, Any |
17 | 29 |
|
18 | 30 | import guidata.dataset.dataitems as gdi |
19 | 31 | import guidata.dataset.datatypes as gdt |
20 | 32 |
|
| 33 | +if TYPE_CHECKING: # pragma: no cover |
| 34 | + import guidata.dataset.datatypes as gdt |
| 35 | + |
| 36 | +# ============================================================================== |
| 37 | +# Updating, restoring datasets |
| 38 | +# ============================================================================== |
| 39 | + |
| 40 | + |
| 41 | +def update_dataset( |
| 42 | + dest: gdt.DataSet, source: Any | dict[str, Any], visible_only: bool = False |
| 43 | +) -> None: |
| 44 | + """Update `dest` dataset items from `source` dataset. |
| 45 | +
|
| 46 | + Args: |
| 47 | + dest (DataSet): The destination dataset object to update. |
| 48 | + source (Union[Any, Dict[str, Any]]): The source object or dictionary containing |
| 49 | + matching attribute names. |
| 50 | + visible_only (bool): If True, update only visible items. Defaults |
| 51 | + to False. |
| 52 | +
|
| 53 | + For each DataSet item, the function will try to get the attribute |
| 54 | + of the same name from the source. |
| 55 | +
|
| 56 | + If the attribute exists in the source object or the key exists in the dictionary, |
| 57 | + it will be set as the corresponding attribute in the destination dataset. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + None |
| 61 | + """ |
| 62 | + for item in dest._items: |
| 63 | + key = item._name |
| 64 | + if hasattr(source, key): |
| 65 | + try: |
| 66 | + hide = item.get_prop_value("display", source, "hide", False) |
| 67 | + except AttributeError: |
| 68 | + # FIXME: Remove this try...except |
| 69 | + hide = False |
| 70 | + if visible_only and hide: |
| 71 | + continue |
| 72 | + setattr(dest, key, getattr(source, key)) |
| 73 | + elif isinstance(source, dict) and key in source: |
| 74 | + setattr(dest, key, source[key]) |
| 75 | + |
| 76 | + |
| 77 | +def restore_dataset(source: gdt.DataSet, dest: Any | dict[str, Any]) -> None: |
| 78 | + """Restore `dest` dataset items from `source` dataset. |
| 79 | +
|
| 80 | + Args: |
| 81 | + source (DataSet): The source dataset object to restore from. |
| 82 | + dest (Union[Any, Dict[str, Any]]): The destination object or dictionary. |
| 83 | +
|
| 84 | + This function is almost the same as `update_dataset` but requires |
| 85 | + the source to be a DataSet instead of the destination. |
| 86 | +
|
| 87 | + Symmetrically from `update_dataset`, `dest` may also be a dictionary. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + None |
| 91 | + """ |
| 92 | + for item in source._items: |
| 93 | + key = item._name |
| 94 | + value = getattr(source, key) |
| 95 | + if hasattr(dest, key): |
| 96 | + try: |
| 97 | + setattr(dest, key, value) |
| 98 | + except AttributeError: |
| 99 | + # This attribute is a property, skipping this iteration |
| 100 | + continue |
| 101 | + elif isinstance(dest, dict): |
| 102 | + dest[key] = value |
| 103 | + |
| 104 | + |
21 | 105 | # ============================================================================== |
22 | 106 | # Generating a dataset class from a function signature |
23 | 107 | # ============================================================================== |
|
0 commit comments