Created
April 24, 2022 08:39
-
-
Save Renkai/43e7033c81476104b9407dc15e77fa09 to your computer and use it in GitHub Desktop.
python run method from name
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
import base64 | |
import json | |
import sys | |
demo_obj = { | |
'module_name': "spock_spore_engine.tasks.demo_task", | |
'class_name': "DemoTask", | |
'class_args': [], | |
'class_kwargs': { | |
}, | |
'method_name': 'run', | |
'method_args': [], | |
'method_kwargs': { | |
'a': 1, | |
'b': '零', | |
'c': 2.4 | |
} | |
} | |
# demo_obj = { | |
# 'module_name': "array", | |
# 'class_name': "ArrayType", | |
# 'class_args': ['b'], | |
# 'class_kwargs': { | |
# | |
# }, | |
# 'method_name': 'count', | |
# 'method_args': ['b'], | |
# 'method_kwargs': { | |
# } | |
# } | |
demo_str = json.dumps(demo_obj) | |
if __name__ == '__main__': | |
print(demo_str) | |
print(base64.b64encode(demo_str.encode()).decode()) | |
b64_json = sys.argv[1] | |
print("the_json", b64_json) | |
wrapper_args = json.loads(base64.b64decode(b64_json)) | |
# wrapper_args = json.loads(demo_str) | |
module_name = wrapper_args['module_name'] | |
class_name = wrapper_args['class_name'] | |
class_kwargs = wrapper_args['class_kwargs'] | |
class_args = wrapper_args['class_args'] | |
method_name = wrapper_args['method_name'] | |
kwargs = wrapper_args['method_kwargs'] | |
args = wrapper_args['method_args'] | |
import importlib | |
the_module = importlib.import_module(module_name) | |
the_class = getattr(the_module, class_name) | |
instance = the_class(*class_args, **class_kwargs) | |
method_instance = getattr(instance, method_name) | |
method_instance(*args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment