[docs]classUnstructurer:def__init__(self,lookup_handlers:Mapping[Any,Callable[["Unstructurer",Any,Any],Any]]={},sequential_handlers:Iterable[SequentialUnstructureHandler]=[],):self._lookup_handlers=dict(lookup_handlers)self._sequential_handlers=list(sequential_handlers)defunstructure_as(self,unstructure_as:Any,val:Any)->Any:stack=GeneratorStack((self,unstructure_as),val)lookup_order=get_lookup_order(unstructure_as)fortpinlookup_order:handler=self._lookup_handlers.get(tp,None)ifstack.push(handler):returnstack.result()# Check all sequential handlers in order and see if there is one that applies# TODO (#10): should `applies()` raise an exception which we could collect# and attach to the error below, to provide more context on why no handlers were found?forsequential_handlerinself._sequential_handlers:ifsequential_handler.applies(unstructure_as,val):ifstack.push(sequential_handler):returnstack.result()breakifstack.is_empty():raiseUnstructuringError(f"No handlers registered to unstructure as {unstructure_as}")raiseUnstructuringError(f"Could not find a non-generator handler to unstructure as {unstructure_as}")