I have a module: generate_ruml
that has a global variable, foo
. When I run the script python3 generate_ruml.py
, it works.
However, when I call the function of generate_ruml
(function name is: gruml()
) from another module: gruml_cli
using python3 gruml_cli.py
, the global variable is not accessed and is considered as None
while using Trace .
How do I make the line global foo
work?
Below is the code for generate_ruml
:
foo = None
class GRUML:
def generate_sequential_function_calls(self):
"""generate sequential function calls
for tracing source code and plotting sequence diagram.
"""
# generating sequence diagram for a use-case
_ = GenerateSequenceDiagram(
self.driver_path, self.driver_name, self.source_code_path[0])
spec = importlib.util.spec_from_file_location(
self.driver_name, self.driver_path)
global foo
foo = self.foo
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
tracer = Trace(countfuncs=1, countcallers=1, timing=1)
tracer.run('foo.{}()'.format(self.driver_function))
def gruml(source_code_path, **kwargs):
"""driver function of GRUML.
"""
gruml = GRUML()
print('Generating RUML for source code at: {}'.format(source_code_path))
gruml.get_source_code_path_and_modules(source_code_path)
gruml.get_driver_path_and_driver_name(
kwargs.get('use_case', None),
kwargs.get('driver_name', None),
kwargs.get('driver_path', None),
kwargs.get('driver_function', None),
)
gruml.generate_dependency_data()
if gruml.use_case:
gruml.generate_sequential_function_calls()
Below is the code for gruml_cli
:
from generate_ruml import gruml
def call_generate_ruml(source_code_link, **kwargs):
"""call gruml function, assume that if use_case is given, all other related
arguments will be present, so checking for them can be skipped in here.
Arguments:
source_code_link {[type]} -- [description]
"""
source_code_dir = download_source_code(source_code_link)
gruml(
source_code_dir,
**kwargs
)
call_generate_ruml(source_code_link, use_case=use_case, driver_name=driver_name, driver_path=driver_path, driver_function=driver_function)
This is the error I receive when I call the function from gruml_cli
:
Traceback (most recent call last):
File "gruml-cli.py", line 64, in <module>
driver_name=driver_name, driver_path=driver_path, driver_function=driver_function)
File "gruml-cli.py", line 36, in call_generate_ruml
**kwargs
File "/Users/aviralsrivastava/dev/generate_uml/generate_ruml.py", line 249, in gruml
gruml.generate_sequential_function_calls()
File "/Users/aviralsrivastava/dev/generate_uml/generate_ruml.py", line 212, in generate_sequential_function_calls
tracer.run('foo.{}()'.format(self.driver_function))
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/trace.py", line 441, in run
self.runctx(cmd, dict, dict)
File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/trace.py", line 450, in runctx
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
NameError: name 'foo' is not defined
I also tried to insert the script generate_ruml.py
in the sys
path but in vain.
It looks like you need to import the global variable as well. Like:
from generate_ruml import gruml, foo
You can either import the variable as
from generate_ruml import gruml, foo
Or you can reference the variable as, throughout your code.
generate_ruml.foo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.