schrodinger.models.yaml_utils module¶
Utility functions for converting jsonable objects to/from YAML format.
In addition to simply serializing/deserializing objects, these utility functions support the concept of “versioning” objects. See the adapter decorator in schrodinger.models.json for more information on versioning.
Example usage (CompoundParam
inherits JsonableClassMixin
):
>>> from schrodinger.models import yaml_utils
>>> from schrodinger.models.parameters import CompoundParam
>>>
>>> class Contact(CompoundParam):
... first_name: str
... last_name: str
... age: int
>>>
>>> contact = Contact(first_name='John', last_name='Doe', age=30)
>>> yaml_str = yaml_utils.obj_to_yaml_str(contact)
>>> imported_contact = yaml_utils.yaml_str_to_obj(yaml_str, Contact)
- schrodinger.models.yaml_utils.obj_to_yaml_str(obj: JsonableClassMixin, exclude_private: bool = False) str ¶
Convert a CompoundParam object to a YAML string. The YAML string will encode version information so that it can still be used with future versions of the CompoundParam class.
- Parameters:
obj – the object to convert
exclude_private – whether fields with names starting with ‘_’ should be excluded from the yaml string
- schrodinger.models.yaml_utils.yaml_str_to_obj(yaml_str: str, cls: type) JsonableClassMixin ¶
Convert a YAML string to a CompoundParam object of the type specified by cls. If the YAML string contains version information, version adapter methods in the CompoundParam class will be used so older YAML strings can be converted to newer CompoundParam objects.
- Parameters:
yaml_str – the YAML string to convert
cls – the class of the object to create
- schrodinger.models.yaml_utils.obj_to_yaml_file(obj: JsonableClassMixin, file_path: str, exclude_private: bool = False)¶
Write a CompoundParam object to the YAML file specified by file_path. For more information, see param_to_yaml_str.
- schrodinger.models.yaml_utils.yaml_file_to_obj(file_path: str, cls: type[JsonableClassMixin]) JsonableClassMixin ¶
Read a CompoundParam object from the YAML file specified by file_path. For more information, see yaml_str_to_param.