Pickle functools wrapper error: can't pickle functools.KeyWrapper objects
Pickle functools wrapper error: can't pickle functools.KeyWrapper objects
我正在尝试从 functools 中使用 cmp_to_key() 对 SortedListWithKey 进行腌制,以将比较函数转换为键函数。
但是,cmp_to_key() 似乎使我的对象不可拾取,并且出现以下错误:TypeError: can't pickle functools.KeyWrapper objects
我该如何解决?这是重现错误的代码示例:
import pickle
from functools import cmp_to_key
from sortedcontainers import SortedListWithKey
def order_fun(a, b):
if abs(a[0]-b[0]) < 1e-8:
return 0
elif a[0]-b[0] > 0:
return 1
else:
return -1
pickle.loads(pickle.dumps(SortedListWithKey([[1,2], [3,4]], key=cmp_to_key(order_fun))))
谢谢!
注意:在不使用 cmp_to_key() 函数的情况下酸洗工作正常,但我需要它,因为我的函数不是关键函数。
问题似乎是cmp_tp_key is now written in C, and the class it returns is neither pickleable nor subclassable. However, the original pure python version is still maintained in the source, and is very simple. When this is used with your example, it works correctly. Of course, the obvious downside is that the pure python version is slower - but the difference is not huge。
这是您示例的工作版本:
import pickle
from sortedcontainers import SortedListWithKey
def order_fun(a, b):
if abs(a[0]-b[0]) < 1e-8:
return 0
elif a[0]-b[0] > 0:
return 1
else:
return -1
class KeyFunc(object):
__slots__ = ['obj']
def __init__(self, obj):
self.obj = obj
def __lt__(self, other):
return order_fun(self.obj, other.obj) < 0
def __gt__(self, other):
return order_fun(self.obj, other.obj) > 0
def __eq__(self, other):
return order_fun(self.obj, other.obj) == 0
def __le__(self, other):
return order_fun(self.obj, other.obj) <= 0
def __ge__(self, other):
return order_fun(self.obj, other.obj) >= 0
__hash__ = None
sl = SortedListWithKey([[1,2], [3,4]], key=KeyFunc)
print(sl)
print(pickle.loads(pickle.dumps(sl)))
输出:
SortedListWithKey([[1, 2], [3, 4]], key=<class '__main__.KeyFunc'>)
SortedListWithKey([[1, 2], [3, 4]], key=<class '__main__.KeyFunc'>)
我正在尝试从 functools 中使用 cmp_to_key() 对 SortedListWithKey 进行腌制,以将比较函数转换为键函数。 但是,cmp_to_key() 似乎使我的对象不可拾取,并且出现以下错误:TypeError: can't pickle functools.KeyWrapper objects
我该如何解决?这是重现错误的代码示例:
import pickle
from functools import cmp_to_key
from sortedcontainers import SortedListWithKey
def order_fun(a, b):
if abs(a[0]-b[0]) < 1e-8:
return 0
elif a[0]-b[0] > 0:
return 1
else:
return -1
pickle.loads(pickle.dumps(SortedListWithKey([[1,2], [3,4]], key=cmp_to_key(order_fun))))
谢谢!
注意:在不使用 cmp_to_key() 函数的情况下酸洗工作正常,但我需要它,因为我的函数不是关键函数。
问题似乎是cmp_tp_key is now written in C, and the class it returns is neither pickleable nor subclassable. However, the original pure python version is still maintained in the source, and is very simple. When this is used with your example, it works correctly. Of course, the obvious downside is that the pure python version is slower - but the difference is not huge。
这是您示例的工作版本:
import pickle
from sortedcontainers import SortedListWithKey
def order_fun(a, b):
if abs(a[0]-b[0]) < 1e-8:
return 0
elif a[0]-b[0] > 0:
return 1
else:
return -1
class KeyFunc(object):
__slots__ = ['obj']
def __init__(self, obj):
self.obj = obj
def __lt__(self, other):
return order_fun(self.obj, other.obj) < 0
def __gt__(self, other):
return order_fun(self.obj, other.obj) > 0
def __eq__(self, other):
return order_fun(self.obj, other.obj) == 0
def __le__(self, other):
return order_fun(self.obj, other.obj) <= 0
def __ge__(self, other):
return order_fun(self.obj, other.obj) >= 0
__hash__ = None
sl = SortedListWithKey([[1,2], [3,4]], key=KeyFunc)
print(sl)
print(pickle.loads(pickle.dumps(sl)))
输出:
SortedListWithKey([[1, 2], [3, 4]], key=<class '__main__.KeyFunc'>)
SortedListWithKey([[1, 2], [3, 4]], key=<class '__main__.KeyFunc'>)