serialization - reconstruct python method with kwargs with marshal and types? -
i using marshal module serialize python methods, , reconstruct them using types module (is there easy way pickle python function (or otherwise serialize code)?). having trouble getting work optional kwargs. e.g.
import marshal import types def test_func(x, y, kw='asdf'): return x, y, kw serialized_code_string = marshal.dumps(test_func.func_code) # in real code there bunch of file i/o here instead unserialized_code_string = marshal.load(code_string) new_func = types.functiontype(code, globals(), "deserialized_function")
when run new_func(1,2) error new_func() takes 3 arguments (2 given), though kwarg optional. think issue arises in reconstructing function func_code, not sure how fix this. if there isn't way, interested in alternative way reconstruct function keeps kwarg functionality.
after more searching discovered dill package, allows pickle more things cpickle (the reason using marshal). when directly reconstruct serialized function (not using func_code), avoid keyword issue having.
Comments
Post a Comment