我在尝试将同情表达式转储到YAML时遇到此错误:
TypeError: cannot determine truth value of Relational
我想要转储的是一个包含2个sympy.Symbol
个对象的集合。我不确定为什么同情会尝试将其评估为StrictLessThan
对象。 sympy试图确定真值,或许是为了评估mapping.sort()
函数?
这是追溯:
Traceback (most recent call last):
File "E:/Matt/Documents/ProgrammingProjects/infinitree/abg.py", line 267, in <module>
print yaml.dump(mtree)
File "C:\Python27\lib\site-packages\yaml\__init__.py", line 202, in dump
return dump_all([data], stream, Dumper=Dumper, **kwds)
File "C:\Python27\lib\site-packages\yaml\__init__.py", line 190, in dump_all
dumper.represent(data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 28, in represent
node = self.represent_data(data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 61, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 434, in represent_object
u'tag:yaml.org,2002:python/object:'+function_name, state)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 61, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 434, in represent_object
u'tag:yaml.org,2002:python/object:'+function_name, state)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 223, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 61, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 434, in represent_object
u'tag:yaml.org,2002:python/object:'+function_name, state)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 61, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 434, in represent_object
u'tag:yaml.org,2002:python/object:'+function_name, state)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 223, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 223, in represent_dict
return self.represent_mapping(u'tag:yaml.org,2002:map', data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 61, in represent_data
node = self.yaml_multi_representers[data_type](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 434, in represent_object
u'tag:yaml.org,2002:python/object:'+function_name, state)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 123, in represent_mapping
node_value = self.represent_data(item_value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 57, in represent_data
node = self.yaml_representers[data_types[0]](self, data)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 229, in represent_set
return self.represent_mapping(u'tag:yaml.org,2002:set', value)
File "C:\Python27\lib\site-packages\yaml\representer.py", line 120, in represent_mapping
mapping.sort()
File "C:\Python27\lib\site-packages\sympy\core\relational.py", line 195, in __nonzero__
raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational
答案 0 :(得分:2)
表示器尝试按照与dict类似的方式对集合进行排序,方法是按键值对对项目进行排序,然后转储它们。我不知道sympy
的细节,但是对于排序,需要知道密钥和值的表示(对于一个集合,只使用密钥),并且通常使用字符串表示来完成,我猜,导致评价。因此mapping.sort()
会评估它用作密钥的sympy
个对象,而不是相反(正如您在问题中所指出的那样)
然而,不仅仅是排序问题,sympy
对象在没有其他代表程序的情况下也无法转储:
import sys
import ruamel.yaml
from sympy.abc import x, y
from sympy.core.relational import Relational
data = Relational(y, x+x**2, '==')
ruamel.yaml.round_trip_dump(data, sys.stdout)
还有哪些错误:
ruamel.yaml.representer.RepresenterError: cannot represent an object: Eq(y, x**2 + x)
所以我开始很简单,通过为你使用的对象类型制作代表,并且想要转储,然后那些的集合也应该是可转储的。
我使用了ruamel.yaml库而不是仅支持YAML 1.1规范的过时的PyYAML。对于这个问题,两者都应该采用相同的方式。