text
stringlengths
0
828
148,"def register_module_classes(yaml: ruamel.yaml.YAML, modules: Optional[Iterable[Any]] = None) -> ruamel.yaml.YAML:
"""""" Register all classes in the given modules with the YAML object.
This is a simple helper function.
""""""
# Validation
if modules is None:
modules = []
# Extract the classes from the modules
classes_to_register = set()
for module in modules:
module_classes = [member[1] for member in inspect.getmembers(module, inspect.isclass)]
classes_to_register.update(module_classes)
# Register the extracted classes
return register_classes(yaml = yaml, classes = classes_to_register)"
149,"def numpy_to_yaml(representer: Representer, data: np.ndarray) -> Sequence[Any]:
"""""" Write a numpy array to YAML.
It registers the array under the tag ``!numpy_array``.
Use with:
.. code-block:: python
>>> yaml = ruamel.yaml.YAML()
>>> yaml.representer.add_representer(np.ndarray, yaml.numpy_to_yaml)
Note:
We cannot use ``yaml.register_class`` because it won't register the proper type.
(It would register the type of the class, rather than of `numpy.ndarray`). Instead,
we use the above approach to register this method explicitly with the representer.
""""""
return representer.represent_sequence(
""!numpy_array"",
data.tolist()
)"
150,"def numpy_from_yaml(constructor: Constructor, data: ruamel.yaml.nodes.SequenceNode) -> np.ndarray:
"""""" Read an array from YAML to numpy.
It reads arrays registered under the tag ``!numpy_array``.
Use with:
.. code-block:: python
>>> yaml = ruamel.yaml.YAML()
>>> yaml.constructor.add_constructor(""!numpy_array"", yaml.numpy_from_yaml)
Note:
We cannot use ``yaml.register_class`` because it won't register the proper type.
(It would register the type of the class, rather than of `numpy.ndarray`). Instead,
we use the above approach to register this method explicitly with the representer.
""""""
# Construct the contained values so that we properly construct int, float, etc.
# We just leave this to YAML because it already stores this information.
values = [constructor.construct_object(n) for n in data.value]
logger.debug(f""{data}, {values}"")
return np.array(values)"
151,"def enum_to_yaml(cls: Type[T_EnumToYAML], representer: Representer, data: T_EnumToYAML) -> ruamel.yaml.nodes.ScalarNode:
"""""" Encodes YAML representation.
This is a mixin method for writing enum values to YAML. It needs to be added to the enum
as a classmethod. See the module docstring for further information on this approach and how
to implement it.
This method writes whatever is used in the string representation of the YAML value.
Usually, this will be the unique name of the enumeration value. If the name is used,
the corresponding ``EnumFromYAML`` mixin can be used to recreate the value. If the name
isn't used, more care may be necessary, so a ``from_yaml`` method for that particular
enumeration may be necessary.
Note:
This method assumes that the name of the enumeration value should be stored as a scalar node.
Args:
representer: Representation from YAML.
data: Enumeration value to be encoded.
Returns:
Scalar representation of the name of the enumeration value.
""""""
return representer.represent_scalar(
f""!{cls.__name__}"",
f""{str(data)}""
)"
152,"def enum_from_yaml(cls: Type[T_EnumFromYAML], constructor: Constructor, node: ruamel.yaml.nodes.ScalarNode) -> T_EnumFromYAML:
"""""" Decode YAML representation.
This is a mixin method for reading enum values from YAML. It needs to be added to the enum
as a classmethod. See the module docstring for further information on this approach and how
to implement it.
Note:
This method assumes that the name of the enumeration value was stored as a scalar node.
Args:
constructor: Constructor from the YAML object.
node: Scalar node extracted from the YAML being read.
Returns: