Как оказалось, мы можем использовать кастомный Unpickler
из модуля pickle
для выполнения sh this!
Полный скрипт ниже:
from cloudpickle import dump
import pickle
def save():
from uvpn.maze.uvpn_e2e import UVPN
agent = UVPN(graph_n=100, buffer_n=100, latent_dim=10,
neighbor_r=1, neighbor_r_min=0)
data = dict(title="agent object",
count=0,
nested=dict(value=list(range(100))),
agent=agent)
with open("agent.pkl", 'wb') as f:
dump(data, f)
print("done.")
def factory(module, name):
class Lie:
def __init__(self, *args, **kwargs):
print(args, kwargs)
self.__d = dict()
def __setitem__(self, key, value):
self.__d[key] = value
def __getitem__(self, item):
return self.__d[item]
def __call__(self, *args, **kwargs):
print(args, kwargs)
def __repr__(self):
return f"<class '{module}.{name}'>"
return Lie
def read():
from termcolor import cprint
class Whatever(pickle.Unpickler):
def find_class(self, module, name):
try:
return super().find_class(module, name)
except:
return factory(module, name)
with open("agent.pkl", 'rb') as f:
agent = Whatever(f).load()
print(agent)
cprint("success!", "green")
if __name__ == '__main__':
# save()
read()