Problema:
Você está tentando fazer yaml.safe_dump() de um objeto que é (ou contém) uma instância namedtuple como:
yaml_namedtuple_fix.py
import yaml
import collections
MyTuple = collections.namedtuple("MyTuple", ["a", "b", "c"])
mytuple = MyTuple(1,2,3)
yaml.safe_dump(mytuple)que resulta na seguinte exceção:
yaml_namedtuple_error.txt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/uli/.local/lib/python3.10/site-packages/yaml/__init__.py", line 269, in safe_dump
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
File "/home/uli/.local/lib/python3.10/site-packages/yaml/__init__.py", line 241, in dump_all
dumper.represent(data)
File "/home/uli/.local/lib/python3.10/site-packages/yaml/representer.py", line 27, in represent
node = self.represent_data(data)
File "/home/uli/.local/lib/python3.10/site-packages/yaml/representer.py", line 58, in represent_data
node = self.yaml_representers[None](self, data)
File "/home/uli/.local/lib/python3.10/site-packages/yaml/representer.py", line 231, in represent_undefined
raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', MyTuple(a=1, b=2, c=3))Solução
Você precisa adicionar um representer personalizado para converter implicitamente seu namedtuple para um dict.
Antes de executar yaml.safe_dump(), adicione as seguintes linhas:
represent_namedtuple_fix.py
import collections
def represent_namedtuple(dumper, data):
return dumper.represent_dict(data._asdict())
yaml.SafeDumper.add_representer(MyTuple, represent_namedtuple)Você precisa fazer add_representer() para cada namedtuple que você usa!
Agora, a chamada yaml.safe_dump() deve funcionar perfeitamente:
yaml_safe_dump_fixed.py
yaml.safe_dump(mytuple) # Returns 'a: 1\nb: 2\nc: 3\n'
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow