Try to make an ID for a parameter in a ParameterSet from its value, if the value type is supported.
(self, val: object)
| 1035 | return None |
| 1036 | |
| 1037 | def _idval_from_value(self, val: object) -> str | None: |
| 1038 | """Try to make an ID for a parameter in a ParameterSet from its value, |
| 1039 | if the value type is supported.""" |
| 1040 | if isinstance(val, str | bytes): |
| 1041 | return _ascii_escaped_by_config(val, self.config) |
| 1042 | elif val is None or isinstance(val, float | int | bool | complex): |
| 1043 | return str(val) |
| 1044 | elif isinstance(val, re.Pattern): |
| 1045 | return ascii_escaped(val.pattern) |
| 1046 | elif val is NOTSET: |
| 1047 | # Fallback to default. Note that NOTSET is an enum.Enum. |
| 1048 | pass |
| 1049 | elif isinstance(val, enum.Enum): |
| 1050 | return str(val) |
| 1051 | elif isinstance(getattr(val, "__name__", None), str): |
| 1052 | # Name of a class, function, module, etc. |
| 1053 | name: str = getattr(val, "__name__") |
| 1054 | return name |
| 1055 | return None |
| 1056 | |
| 1057 | def _idval_from_value_required(self, val: object, idx: int) -> str: |
| 1058 | """Like _idval_from_value(), but fails if the type is not supported.""" |
no test coverage detected