def pickle_dumps_without_main_refs(obj):
"""
Yeah this is horrible, but it allows you to pickle an object in the main module so that it can be reloaded in another
module.
:param obj:
:return:
"""
currently_run_file = sys.argv[0]
module_path = file_path_to_absolute_module(currently_run_file)
try:
pickle_str = pickle.dumps(obj, protocol=0)
except:
print("Using Dill")
# TODO: @petered There is something very fishy going on here that I don't understand.
import dill
pickle_str = dill.dumps(obj, protocol=0)
pickle_str = pickle_str.replace('__main__', module_path) # Hack!
return pickle_str
评论列表
文章目录