source.factory module

class module_dependencies.source.factory.Source[source]

Bases: SourceI

Factory class for instances of the SourceI interface. The specific subclass of SourceI is chosen based on the contents of the source input parameter.

Example usage:

>>> from module_dependencies import Source
>>> src = Source(r"from nltk import word_tokenize\nword_tokenize('Hello there!')")
>>> src
<module_dependencies.source.SourceString object at 0x...>
>>> src = Source("my_file.py")
>>> src
<module_dependencies.source.SourceFile object at 0x...>
>>> src = Source("data/real/models")
>>> src
<module_dependencies.source.SourceFolder object at 0x...>
>>> src = Source("ZnJvbSBubHRrIGltcG9ydCB3b3JkX3Rva2VuaXplCndvcmRfdG9rZW5pemUoJ0hlbGxvIHRoZXJlIScp")
>>> src
<module_dependencies.source.SourceBase64 object at 0x...>

The class methods Source.from_... can also be used to initialize specific subclasses of the SourceI interface.

static __new__(cls, source: str) None[source]
Parameters:

source (str) –

Return type:

None

classmethod from_string(source: str) SourceString[source]
Parameters:

source (str) –

Return type:

SourceString

classmethod from_base64(encoded: str) SourceBase64[source]
Parameters:

encoded (str) –

Return type:

SourceBase64

classmethod from_file(filename: str) SourceFile[source]
Parameters:

filename (str) –

Return type:

SourceFile

classmethod from_folder(path: str) SourceFolder[source]
Parameters:

path (str) –

Return type:

SourceFolder

classmethod from_jupyter(source: str) SourceJupyterNotebook[source]
Parameters:

source (str) –

Return type:

SourceJupyterNotebook

dependencies(module: Iterable[str] | str | None = None) List[str]

Return a list of variables, functions and classes originating from module.

Example usage:

>>> src = Source.from_folder("module_dependencies")
>>> print(src.dependencies("os"))
['os.path.isdir', 'os.path.isfile', 'os.path.join']
Parameters:

module (Union[Iterable[str], str], optional) – Module string or list of module strings. For example: 'nltk', 'nltk.parse' or ['nltk.parse', 'nltk.stem']. If module is None, then all uses of imported variables, functions and classes are returned. Defaults to None.

Returns:

List of dot-separated modules, variables, functions and classes.

Return type:

List[str]

dependencies_mapping(module: Iterable[str] | str | None = None) Dict[str, List[str]]

Return a mapping from filenames to a list of variables, functions and classes originating from module that were used in that file.

Only defined for SourceFolder, i.e. instances returned from Source.from_folder().

Example usage:

>>> src = Source("module_dependencies")
>>> pprint(src.dependencies_mapping("ast"))
{'module_dependencies/__init__.py': [],
'module_dependencies/api.py': [],
'module_dependencies/factory.py': [],
'module_dependencies/source.py': ['ast.parse'],
'module_dependencies/tokenize.py': [],
'module_dependencies/visitor.py': ['ast.AST',
                                   'ast.Attribute',
                                   'ast.Import',
                                   'ast.ImportFrom',
                                   'ast.Name',
                                   'ast.NodeVisitor',
                                   'ast.iter_fields']}
Parameters:

module (Union[Iterable[str], str], optional) – Module string or list of module strings. For example: 'nltk', 'nltk.parse' or ['nltk.parse', 'nltk.stem']. If module is None, then all uses of imported variables, functions and classes are returned. Defaults to None.

Returns:

Mapping of filenames to Source.from_file(filename).dependencies(module).

Return type:

Dict[str, List[str]]

imports() List[str]

Return a list of modules that were imported from in source.

Example usage:

>>> from module_dependencies import Source
>>> src = Source("from nltk import word_tokenize\nword_tokenize('Hello there!')")
>>> src.imports()
['nltk']
Returns:

List of modules

Return type:

List[str]

imports_mapping() Dict[str, List[str]]

Return a mapping from filenames to a list of modules that were imported from in source.

Only defined for SourceFolder, i.e. instances returned from Source.from_folder().

Example usage:

>>> src = Source("module_dependencies")
>>> pprint(src.imports_mapping())
{'module_dependencies/__init__.py': ['module_dependencies.factory',
                                     'module_dependencies.source'],
'module_dependencies/api.py': ['typing'],
'module_dependencies/factory.py': ['module_dependencies.api',
                                   'module_dependencies.source',
                                   'os'],
'module_dependencies/source.py': ['__future__',
                                  'ast',
                                  'base64',
                                  'glob',
                                  'module_dependencies.api',
                                  'module_dependencies.tokenize',
                                  'module_dependencies.visitor',
                                  'os',
                                  'typing'],
'module_dependencies/tokenize.py': ['typing'],
'module_dependencies/visitor.py': ['ast',
                                   'module_dependencies.tokenize',
                                   'typing']}
Returns:

Mapping of filenames to Source.from_file(filename).imports().

Return type:

Dict[str, List[str]]