[docs]deftimedelta_representer(dumper:Dumper,data:timedelta)->Node:"""Represent a timedelta object as the total seconds in a YAML string."""returndumper.represent_str(str(data.total_seconds()))
[docs]deftimedelta_constructor(loader:Loader,node:Node)->timedelta:"""Construct a timedelta object from a YAML scalar representing total seconds."""value:str=loader.construct_scalar(node)returntimedelta(seconds=float(value))
[docs]defcheck_for_invalid_yaml_values(data:Any,path='root'):"""Recursively checks for NaN or binary values in the data."""ifisinstance(data,float)andnp.isnan(data):raiseValueError(f'Invalid NaN value found at {path}')ifisinstance(data,bytes):raiseValueError(f'Binary data (bytes) not allowed at {path}')ifisinstance(data,dict):forkey,valueindata.items():check_for_invalid_yaml_values(value,f'{path}.{key}')elifisinstance(data,(list,tuple)):fori,iteminenumerate(data):check_for_invalid_yaml_values(item,f'{path}[{i}]')