source.source module¶
- class module_dependencies.source.source.SourceString[source]¶
 Bases:
SourceIImplementation of
SourceIinterface. Reads a string which represents Python source code, and parses it for dependencies and import statements.- __init__(source: str) None[source]¶
 Read a string with Python source code.
Example usage:
>>> from module_dependencies import Source >>> src = Source("from nltk import word_tokenize\nword_tokenize('Hello there!')") >>> src.dependencies() ['nltk.word_tokenize'] >>> src.imports() ['nltk']
- Parameters:
 source (src) – String with Python source code.
- Return type:
 None
- imports() List[str][source]¶
 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]
- dependencies(module: Iterable[str] | str | None = None) Dict[str, List[str]][source]¶
 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']. Ifmoduleis 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
modulethat were used in that file.Only defined for
SourceFolder, i.e. instances returned fromSource.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']. Ifmoduleis 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_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 fromSource.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]]
- class module_dependencies.source.source.SourceBase64[source]¶
 Bases:
SourceStringReads a base64 encoded string which represents Python source code, and parses it for dependencies and import statements.
- __init__(encoded: str) None[source]¶
 Read a string with base64 encoded Python source code.
Example usage:
>>> from module_dependencies import Source >>> src = Source("ZnJvbSBubHRrIGltcG9ydCB3b3JkX3Rva2VuaXplCndvcmRfdG9rZW5pemUoJ0hlbGxvIHRoZXJlIScp") >>> src.dependencies() ['nltk.word_tokenize'] >>> src.imports() ['nltk']
- Parameters:
 encoded (src) – String with base64 encoded Python source code.
- Return type:
 None
- dependencies(module: Iterable[str] | str | None = None) Dict[str, 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']. Ifmoduleis 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
modulethat were used in that file.Only defined for
SourceFolder, i.e. instances returned fromSource.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']. Ifmoduleis 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 fromSource.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]]
- class module_dependencies.source.source.SourceFile[source]¶
 Bases:
SourceStringReads a path to a Python file, and parses it for dependencies and import statements.
- __init__(filename: str) None[source]¶
 Read a file path to a Python file.
Example usage:
>>> from module_dependencies import Source >>> src = Source("my_file.py") >>> src.dependencies() ['nltk.word_tokenize'] >>> src.imports() ['nltk']
- Parameters:
 filename (src) – File path to a Python file.
- Return type:
 None
- dependencies(module: Iterable[str] | str | None = None) Dict[str, 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']. Ifmoduleis 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
modulethat were used in that file.Only defined for
SourceFolder, i.e. instances returned fromSource.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']. Ifmoduleis 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 fromSource.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]]
- class module_dependencies.source.source.SourceJupyterNotebook[source]¶
 Bases:
SourceStringReads a string which represents a Jupyter Notebook, and parses it for dependencies and import statements.
- __init__(jupyter_source: str) None[source]¶
 Read a Jupyter Notebook.
Note
Cells that contain un-compilable code (e.g.
print 'hello'from Python 2) will be discarded. Cells that do compile will be kept to gather the dependencies and imports from.Example usage:
>>> from module_dependencies import Source >>> src = Source.from_jupyter(r''' ... { ... "cells": [ ... { ... "cell_type": "code", ... "execution_count": null, ... "metadata": {}, ... "outputs": [], ... "source": [ ... "from nltk import word_tokenize
- “,
 … “word_tokenize(‘Hello there!’)” … ] … } … ], … “metadata”: { … “language_info”: { … “name”: “python” … }, … “orig_nbformat”: 4 … }, … “nbformat”: 4, … “nbformat_minor”: 2 … } … ‘’’) >>> src.dependencies() [‘nltk.word_tokenize’] >>> src.imports() [‘nltk’]
- param str jupyter_source:
 A Jupyter Notebook as a string, to be parsed with json.loads.
- raises SyntaxError:
 Raised whenever the string is not a Jupyter Notebook with major versions 2, 3 or 4.
- Parameters:
 jupyter_source (str) –
- Return type:
 None
- dependencies(module: Iterable[str] | str | None = None) Dict[str, 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']. Ifmoduleis 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
modulethat were used in that file.Only defined for
SourceFolder, i.e. instances returned fromSource.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']. Ifmoduleis 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 fromSource.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]]
- class module_dependencies.source.source.SourceFolder[source]¶
 Bases:
SourceIReads a path to a folder, and parses each Python file for dependencies and import statements.
- __init__(path: str) None[source]¶
 Read a path to a folder containing Python file. The Python files will be looked for recursively, and the folder may contain non-Python files.
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:
 path (str) – String path to a folder containing Python code.
- Return type:
 None
- imports_mapping() Dict[str, List[str]][source]¶
 Return a mapping from filenames to a list of modules that were imported from in
source.Only defined for
SourceFolder, i.e. instances returned fromSource.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]]
- imports() List[str][source]¶
 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]
- dependencies_mapping(module: Iterable[str] | str | None = None) Dict[str, List[str]][source]¶
 Return a mapping from filenames to a list of variables, functions and classes originating from
modulethat were used in that file.Only defined for
SourceFolder, i.e. instances returned fromSource.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']. Ifmoduleis 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]]
- dependencies(module: Iterable[str] | str | None = None) List[str][source]¶
 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']. Ifmoduleis 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]