the original values returned a dataclass as dict thru the call to some specific methods this version allows thru the parameter class_formatter to call a dynamically selected formatting method. Can transmit a filter list to the class_formatter,
( o: DataclassInstance, class_formatter: str | Callable | None = None, # type: ignore[type-arg] # pyright: ignore[reportMissingTypeArgument] filter_list: list[str] = [], )
| 56 | |
| 57 | |
| 58 | def _get_values( |
| 59 | o: DataclassInstance, |
| 60 | class_formatter: str | Callable | None = None, # type: ignore[type-arg] # pyright: ignore[reportMissingTypeArgument] |
| 61 | filter_list: list[str] = [], |
| 62 | ) -> dict[str, Any]: |
| 63 | """ |
| 64 | the original values returned a dataclass as dict thru the call to some specific methods |
| 65 | this version allows thru the parameter class_formatter to call a dynamically selected formatting method. |
| 66 | Can transmit a filter list to the class_formatter, |
| 67 | """ |
| 68 | if class_formatter: |
| 69 | # if invoked per reference it has to be a standard function or a classmethod. |
| 70 | # A method of an instance does not make sense |
| 71 | if callable(class_formatter): |
| 72 | return class_formatter(o, filter_list) |
| 73 | # if is invoked by name we restrict it to a method of the class. No need to mess more |
| 74 | elif hasattr(o, class_formatter) and callable(getattr(o, class_formatter)): |
| 75 | func = getattr(o, class_formatter) |
| 76 | return func(filter_list) |
| 77 | |
| 78 | raise ValueError('Unsupported formatting call') |
| 79 | elif hasattr(o, 'table_data'): |
| 80 | return o.table_data() |
| 81 | elif hasattr(o, 'json'): |
| 82 | return o.json() |
| 83 | elif is_dataclass(o): |
| 84 | return asdict(o) |
| 85 | else: |
| 86 | return o.__dict__ # type: ignore[unreachable] |
| 87 | |
| 88 | |
| 89 | def as_table( |
no test coverage detected