Created
April 24, 2022 10:58
-
-
Save xnuinside/798b3722ffd79bbba8a6ac9d6dd7658a to your computer and use it in GitHub Desktop.
Fixture to save code & load it as python module
This file contains hidden or 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
@pytest.fixture | |
def load_generated_code(): | |
def _inner(code_text: str, module_name: Optional[str] = None) -> ModuleType: | |
"""method saves & returns new generated python module | |
code_text - code to be saved in new module | |
module_name: str - name of the module to use for saving the code | |
""" | |
current_path = os.path.dirname(os.path.abspath(__file__)) | |
package = os.path.dirname(os.path.relpath(__file__)).replace("/", ".") | |
if not module_name: | |
module_name = f"module_{uuid.uuid1()}" | |
with open(os.path.join(current_path, f"{module_name}.py"), "w+") as f: | |
f.write(code_text) | |
module = importlib.import_module(f"{package}.{module_name}") | |
return module | |
return _inner |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment