Python code extractor
Written in pure Python
and has no dependencies other than the base libraries.
From source code:
pip install .
From PyPI
:
pip install code-extractor
import code_extractor
from code_extractor import extract_code, load_code
import code_extractor.pickle
or as a drop-in for pickle
:
import code_extractor.pickle as pickle
Given the following:
class Class:
def __init__(self):
self.test = 42
def function():
return 42
Use
>>> import code_extractor
>>> extracted_class = code_extractor.extract_code(Class)
>>> extracted_function = code_extractor.extract_code(function)
>>> reconstructed_class = code_extractor.load_code(extracted_class)
>>> instance = reconstructed_class()
>>> instance.test
42
>>> reconstructed_function = code_extractor.load_code(extracted_function)
>>> reconstructed_function()
42
>>> import code_extractor
>>> code_extractor.dump(...)
>>> code_extractor.dumps(...)
>>> code_extractor.load(...)
>>> code_extractor.loads(...)
Or
>>> import code_extractor.pickle as pickle
>>> pickle.dump(...)
>>> pickle.dumps(...)
>>> pickle.load(...)
>>> pickle.loads(...)