I'm getting this error when trying to dump a sympy expression to YAML:
我试图将一个sympy表达式转储给YAML时收到此错误:
TypeError: cannot determine truth value of Relational
What I am trying to dump is a set with 2 sympy.Symbol
objects. I'm not sure why sympy tries to evaluate it as a StrictLessThan
object. sympy is trying to determine the truth value, perhaps to evaluate the mapping.sort()
function?
我想要转储的是一个带有2个sympy.Symbol对象的集合。我不确定为什么sympy会尝试将其评估为StrictLessThan对象。 sympy试图确定真值,或许是为了评估mapping.sort()函数?
Here is the traceback:
这是追溯:
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
1 个解决方案
#1
2
The representer tries to sort the set, which is dumped in a similar way as a dict, by sorting its items by key-value pairs and then dumping those. I don't know the details of sympy
, but for the sorting a representation of the key and value need be knows (for a set only the key is used) and that is often done using the string representation, which, I guess, causes the evaluation. So the mapping.sort()
evaluates the sympy
objects it uses as keys, not the other way around (as you indicated in your question might have been the case)
代表尝试按照与dict相似的方式对集合进行排序,方法是按键值对对项目进行排序,然后转储它们。我不知道sympy的详细信息,但是对于排序,需要知道键和值的表示(对于一个只使用键的集合)并且通常使用字符串表示来完成,我猜,这会导致评价。所以mapping.sort()会评估它用作键的sympy对象,而不是相反(正如你在问题中所指出的那样)
However it is not just the sorting that is the problem, sympy
objects cannot be dumped without additional representer routines:
然而,不仅仅是排序是问题,没有额外的代表程序例程,不能转储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)
which errors as well:
哪些错误:
ruamel.yaml.representer.RepresenterError: cannot represent an object: Eq(y, x**2 + x)
So I would start simple, by making representers for your the object types you use and want to dump and then collections of those should be dumpable as well.
所以我开始简单,通过为你使用的对象类型制作代表,并希望转储,然后那些的集合也应该是可转储的。
I used my ruamel.yaml library instead of the outdated PyYAML that only supports the YAML 1.1 specifications. Both should behave in the same way with regards to this issue.
我使用了ruamel.yaml库而不是仅支持YAML 1.1规范的过时的PyYAML。两者在这个问题上的行为方式应该相同。
#1
2
The representer tries to sort the set, which is dumped in a similar way as a dict, by sorting its items by key-value pairs and then dumping those. I don't know the details of sympy
, but for the sorting a representation of the key and value need be knows (for a set only the key is used) and that is often done using the string representation, which, I guess, causes the evaluation. So the mapping.sort()
evaluates the sympy
objects it uses as keys, not the other way around (as you indicated in your question might have been the case)
代表尝试按照与dict相似的方式对集合进行排序,方法是按键值对对项目进行排序,然后转储它们。我不知道sympy的详细信息,但是对于排序,需要知道键和值的表示(对于一个只使用键的集合)并且通常使用字符串表示来完成,我猜,这会导致评价。所以mapping.sort()会评估它用作键的sympy对象,而不是相反(正如你在问题中所指出的那样)
However it is not just the sorting that is the problem, sympy
objects cannot be dumped without additional representer routines:
然而,不仅仅是排序是问题,没有额外的代表程序例程,不能转储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)
which errors as well:
哪些错误:
ruamel.yaml.representer.RepresenterError: cannot represent an object: Eq(y, x**2 + x)
So I would start simple, by making representers for your the object types you use and want to dump and then collections of those should be dumpable as well.
所以我开始简单,通过为你使用的对象类型制作代表,并希望转储,然后那些的集合也应该是可转储的。
I used my ruamel.yaml library instead of the outdated PyYAML that only supports the YAML 1.1 specifications. Both should behave in the same way with regards to this issue.
我使用了ruamel.yaml库而不是仅支持YAML 1.1规范的过时的PyYAML。两者在这个问题上的行为方式应该相同。