def __init__(self, func, *args, **kwargs):
super(functionable, self).__init__()
self._function = func
self.__name__ = self._function.__name__
try: # sometime cannot get the source
self._source = inspect.getsource(self._function)
except Exception as e:
print("[WARNING] Cannot get source code of function:", func,
"(error:%s)" % str(e))
self._source = None
# try to pickle the function directly
try:
self._sandbox = cPickle.dumps(self._function,
protocol=cPickle.HIGHEST_PROTOCOL)
except Exception:
self._sandbox = _serialize_function_sandbox(func, self._source)
# ====== store argsmap ====== #
argspec = inspect.getargspec(func)
argsmap = OrderedDict([(i, _ArgPlaceHolder_()) for i in argspec.args])
# store defaults
if argspec.defaults is not None:
for name, arg in zip(argspec.args[::-1], argspec.defaults[::-1]):
argsmap[name] = arg
# update positional arguments
for name, arg in zip(argspec.args, args):
argsmap[name] = arg
# update kw arguments
argsmap.update(kwargs)
self._argsmap = argsmap
# ==================== Pickling methods ==================== #
评论列表
文章目录