I am trying to pickle a SortedListWithKey for which I am using cmp_to_key() from functools to convert a comparison function to a key function. However, the cmp_to_key() seems to make my object unpickable and I get the following error: TypeError: can't pickle functools.KeyWrapper objects
How can I fix it? This is a code sample to reproduce the error:
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))))
Thanks!
Note: The pickling works fine without using the cmp_to_key() function, but I need it since my function is not a key function.