vimtk.pyinspect module

TODO: use the closer to ensure these functions are synced with xdoctest

vimtk.pyinspect.check_module_installed(modname)[source]

Check if a python module is installed without attempting to import it. Note, that if modname indicates a child module, the parent module is always loaded.

Parameters:

modname (str) – module name

Returns:

found

Return type:

bool

References

http://stackoverflow.com/questions/14050281/module-exists-without-importing

Example

>>> import sys
>>> import ubelt as ub
>>> modname = ub.argval('--modname', default='this')
>>> is_installed = check_module_installed(modname)
>>> is_imported = modname in sys.modules
>>> print('module(%r).is_installed = %r' % (modname, is_installed))
>>> print('module(%r).is_imported = %r' % (modname, is_imported))
>>> assert 'this' not in sys.modules, 'module(this) should not have ever been imported'
vimtk.pyinspect.in_pythonpath(modname)[source]
vimtk.pyinspect.parse_import_names(sourcecode, top_level=True, fpath=None, branch=False)[source]

Finds all function names in a file without importing it

Parameters:

sourcecode (str)

Returns:

func_names

Return type:

list

References

https://stackoverflow.com/questions/20445733/how-to-tell-which-modules-have-been-imported-in-some-source-code

Example

>>> from vimtk import pyinspect
>>> import pathlib
>>> fpath = pathlib.Path(pyinspect.__file__.replace('.pyc', '.py'))
>>> sourcecode = fpath.read_text()
>>> func_names = parse_import_names(sourcecode)
>>> result = (f'func_names = {func_names}')
>>> print(result)
vimtk.pyinspect._node_is_main_if(node)[source]
vimtk.pyinspect.parse_function_names(sourcecode, top_level=True, ignore_condition=1)[source]

Finds all function names in a file without importing it

Parameters:

sourcecode (str)

Returns:

func_names

Return type:

list

Example

>>> from vimtk import pyinspect
>>> import ubelt as ub
>>> fpath = pyinspect.__file__.replace('.pyc', '.py')
>>> sourcecode = ub.readfrom(fpath)
>>> func_names = parse_function_names(sourcecode)
>>> result = (f'func_names = {func_names}')
>>> print(result)