Created
December 25, 2013 06:12
-
-
Save nanofi/8120596 to your computer and use it in GitHub Desktop.
The code be enabled to pickle the function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from __future__ import division | |
from __future__ import absolute_import | |
from __future__ import unicode_literals | |
__author__ = "nanofi <nanogenomu@gmail.com>" | |
__version__ = "1.0.0" | |
__date__ = "2013/12/25" | |
__license__ = "MIT License http://opensource.org/licenses/mit-license.php" | |
try: | |
import cPickle as pickle | |
except: | |
import pickle | |
import copy_reg | |
import types | |
def code_unpicker(*args): | |
return types.CodeType(*args) | |
def code_pickler(code): | |
pickled = (code.co_argcount, code.co_nlocals, code.co_stacksize, code.co_flags, code.co_code, code.co_consts, code.co_names, code.co_varnames, code.co_filename, code.co_name, code.co_firstlineno, code.co_lnotab, code.co_freevars, code.co_cellvars) | |
return code_unpicker, pickled | |
copy_reg.pickle(types.CodeType, code_pickler) | |
def function_unpicker(code, gbls, name, defaults, closure): | |
return types.FunctionType(code, gbls, name, defaults, closure) | |
def function_pickler(func): | |
func_globals = {x:func.func_globals[x] for x in func.func_code.co_names} | |
pickled = (func.func_code, func_globals, func.func_name, func.func_defaults, func.func_closure) | |
return function_unpickler, pickled | |
copy_reg.pickle(types.FunctionType, function_pickler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment