Reference¶
dependenpy
¶
Dependenpy package.
With dependenpy you will be able to analyze the internal dependencies in your Python code, i.e. which module needs which other module. You will then be able to build a dependency matrix and use it for other purposes.
dependenpy.cli
¶
Module that contains the command line application.
Why does this file exist, and why not put this in __main__?
You might be tempted to import things from __main__ later, but that will cause problems: the code will get executed twice:
- When you run python -mdependenpy python will execute
__main__.py
as a script. That means there won’t be anydependenpy.__main__
insys.modules
.- When you import __main__ it will get executed again (as a module) because there’s no
dependenpy.__main__
insys.modules
.Also see (1) from http://click.pocoo.org/5/setuptools/#setuptools-integration
dependenpy.dsm
¶
dependenpy dsm module.
This is the core module of dependenpy. It contains the following classes:
DSM
: to create a DSM-capable object for a list of packages,Package
: which represents a Python package,Module
: which represents a Python module,Dependency
: which represents a dependency between two modules.
-
class
dependenpy.dsm.
DSM
(*packages, build_tree=True, build_dependencies=True, enforce_init=True)[source]¶ Bases:
dependenpy.node.RootNode
,dependenpy.node.NodeMixin
,dependenpy.helpers.PrintMixin
DSM-capable class.
Technically speaking, a DSM instance is not a real DSM but more a tree representing the Python packages structure. However, it has the necessary methods to build a real DSM in the form of a square matrix, a dictionary or a tree-map.
-
__init__
(*packages, build_tree=True, build_dependencies=True, enforce_init=True)[source]¶ Initialization method.
Parameters: - *packages (args) – list of packages to search for.
- build_tree (bool) – auto-build the tree or not.
- build_dependencies (bool) – auto-build the dependencies or not.
- enforce_init (bool) – if True, only treat directories if they contain an
__init__.py
file.
-
isdsm
¶ Inherited from NodeMixin. Always True.
-
-
class
dependenpy.dsm.
Dependency
(source, lineno, target, what=None)[source]¶ Bases:
object
Dependency class.
Represent a dependency from a module to another.
-
__init__
(source, lineno, target, what=None)[source]¶ Initialization method.
Parameters: - source (Module) – source Module.
- lineno (int) – number of line at which import statement occurs.
- target (str/Module/Package) – the target node.
- what (str) – what is imported (optional).
-
__weakref__
¶ list of weak references to the object (if defined)
-
external
¶ Property to tell if the dependency’s target is a valid node.
-
-
class
dependenpy.dsm.
Module
(name, path, dsm=None, package=None)[source]¶ Bases:
dependenpy.node.LeafNode
,dependenpy.node.NodeMixin
,dependenpy.helpers.PrintMixin
Module class.
This class represents a Python module (a Python file).
-
__contains__
(item)[source]¶ Whether given item is contained inside this module.
Parameters: item (Package/Module) – a package or module. Returns: bool – True if self is item or item is self’s package and self if an __init__
module.
-
__init__
(name, path, dsm=None, package=None)[source]¶ Initialization method.
Parameters: - name (str) – name of the module.
- path (str) – path to the module.
- dsm (DSM) – parent DSM.
- package (Package) – parent Package.
-
as_dict
(absolute=False)[source]¶ Return the dependencies as a dictionary.
Returns: dict – dictionary of dependencies.
-
build_dependencies
()[source]¶ Build the dependencies for this module.
Parse the code with ast, find all the import statements, convert them into Dependency objects.
-
cardinal
(to)[source]¶ Return the number of dependencies of this module to the given node.
Parameters: to (Package/Module) – the target node. Returns: int – number of dependencies.
-
get_imports
(ast_body)[source]¶ Return all the import statements given an AST body (AST nodes).
Parameters: ast_body (compiled code’s body) – the body to filter. Returns: list of dict – the import statements.
-
ismodule
¶ Inherited from NodeMixin. Always True.
-
-
class
dependenpy.dsm.
Package
(name, path, dsm=None, package=None, limit_to=None, build_tree=True, build_dependencies=True, enforce_init=True)[source]¶ Bases:
dependenpy.node.RootNode
,dependenpy.node.LeafNode
,dependenpy.node.NodeMixin
,dependenpy.helpers.PrintMixin
Package class.
This class represent Python packages as nodes in a tree.
-
__init__
(name, path, dsm=None, package=None, limit_to=None, build_tree=True, build_dependencies=True, enforce_init=True)[source]¶ Initialization method.
Parameters: - name (str) – name of the package.
- path (str) – path to the package.
- dsm (DSM) – parent DSM.
- package (Package) – parent package.
- limit_to (list of str) – list of string to limit the recursive tree-building to what is specified.
- build_tree (bool) – auto-build the tree or not.
- build_dependencies (bool) – auto-build the dependencies or not.
- enforce_init (bool) – if True, only treat directories if they contain an
__init__.py
file.
-
cardinal
(to)[source]¶ Return the number of dependencies of this package to the given node.
Parameters: to (Package/Module) – target node. Returns: int – number of dependencies.
-
ispackage
¶ Inherited from NodeMixin. Always True.
-
isroot
¶ Property to tell if this node is a root node.
Returns: bool – this package has no parent.
-
issubpackage
¶ Property to tell if this node is a sub-package.
Returns: bool – this package has a parent.
-
dependenpy.finder
¶
dependenpy finder module.
-
class
dependenpy.finder.
Finder
(finders=None)[source]¶ Bases:
object
Main package finder class.
Initialize it with a list of package finder classes (not instances).
-
__init__
(finders=None)[source]¶ Initialization method.
Parameters: finders (list of classes) – list of package finder classes (not instances) in a specific order. Default: [LocalPackageFinder, InstalledPackageFinder].
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
dependenpy.finder.
InstalledPackageFinder
[source]¶ Bases:
dependenpy.finder.PackageFinder
Finder to find installed Python packages using importlib.
-
class
dependenpy.finder.
LocalPackageFinder
[source]¶ Bases:
dependenpy.finder.PackageFinder
Finder to find local packages (directories on the disk).
-
class
dependenpy.finder.
PackageFinder
[source]¶ Bases:
object
Abstract package finder class.
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
dependenpy.finder.
PackageSpec
(name, path, limit_to=None)[source]¶ Bases:
object
Holder for a package specification (given as argument to DSM).
-
__init__
(name, path, limit_to=None)[source]¶ Initialization method.
Parameters: - name (str) – name of the package.
- path (str) – path to the package.
- limit_to (list of str) – limitations.
-
__weakref__
¶ list of weak references to the object (if defined)
-
add
(spec)[source]¶ Add limitations of given spec to self’s.
Parameters: spec (PackageSpec) – another spec.
-
static
combine
(specs)[source]¶ Combine package specifications’ limitations.
Parameters: specs (list of PackageSpec) – the package specifications. Returns: list of PackageSpec – the new, merged list of PackageSpec.
-
ismodule
¶ Property to tell if the package is in fact a module (a file).
-
dependenpy.helpers
¶
dependenpy printer module.
-
class
dependenpy.helpers.
PrintMixin
[source]¶ Bases:
object
Print mixin class.
-
__weakref__
¶ list of weak references to the object (if defined)
-
print
(format='text', output=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, **kwargs)[source]¶ Print the object in a file or on standard output by default.
Parameters: - format (str) – output format (csv, json or text).
- output (file) – descriptor to an opened file (default to standard output).
- **kwargs () – additional arguments.
-
dependenpy.node
¶
dependenpy node module.
-
class
dependenpy.node.
LeafNode
[source]¶ Bases:
object
Shared code between Package and Module.
-
__weakref__
¶ list of weak references to the object (if defined)
-
absolute_name
(depth=0)[source]¶ Return the absolute name of the node.
Concatenate names from root to self within depth.
Parameters: depth (int) – maximum depth to go to. Returns: str – absolute name of the node (until given depth is reached).
-
depth
¶ Property to tell the depth of the node in the tree.
Returns: int – the node’s depth in the tree.
-
root
¶ Property to return the root of this node.
Returns: Package – this node’s root package.
-
-
class
dependenpy.node.
NodeMixin
[source]¶ Bases:
object
Shared code between DSM, Package and Module.
-
__weakref__
¶ list of weak references to the object (if defined)
-
isdsm
¶ Property to check if object is instance of DSM.
-
ismodule
¶ Property to check if object is instance of Module.
-
ispackage
¶ Property to check if object is instance of Package.
-
-
class
dependenpy.node.
RootNode
(build_tree=True)[source]¶ Bases:
object
Shared code between DSM and Package.
-
__contains__
(item)[source]¶ Get result of _contains, cache it and return it.
Parameters: item (Package/Module) – a package or module. Returns: bool – True if self contains item, False otherwise.
-
__getitem__
(item)[source]¶ Return the corresponding Package or Module object.
Parameters: item (str) – name of the package/module, dot-separated. Returns: Package/Module – corresponding object.
-
__init__
(build_tree=True)[source]¶ Initialization method.
Parameters: build_tree (bool) – whether to immediately build the tree or not.
-
__weakref__
¶ list of weak references to the object (if defined)
-
as_dict
()[source]¶ Return the dependencies as a dictionary.
Returns: dict – dictionary of dependencies.
-
as_graph
(depth=0)[source]¶ Create a graph with self as node, cache it, return it.
Parameters: depth (int) – depth of the graph. Returns: Graph – an instance of Graph.
-
as_matrix
(depth=0)[source]¶ Create a matrix with self as node, cache it, return it.
Parameters: depth (int) – depth of the matrix. Returns: Matrix – an instance of Matrix.
-
build_dependencies
()[source]¶ Recursively build the dependencies for sub-modules and sub-packages.
Iterate on node’s modules then packages and call their build_dependencies methods.
-
empty
¶ Whether the node has neither modules nor packages.
Returns: bool – True if empty, False otherwise.
-
get
(item)[source]¶ Get item through
__getitem__
and cache the result.Parameters: item (str) – name of package or module. Returns: Package/Module – the corresponding object.
-
get_target
(target)[source]¶ Get the result of _get_target, cache it and return it.
Parameters: target (str) – target to find. Returns: Package/Module – package containing target or corresponding module.
-
print_graph
(format=None, output=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, depth=0, **kwargs)[source]¶ Print the graph for self’s nodes.
Parameters: - format (str) – output format (csv, json or text).
- output (file) – file descriptor on which to write.
- depth (int) – depth of the graph.
-
print_matrix
(format=None, output=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, depth=0, **kwargs)[source]¶ Print the matrix for self’s nodes.
Parameters: - format (str) – output format (csv, json or text).
- output (file) – file descriptor on which to write.
- depth (int) – depth of the matrix.
-
print_treemap
(format=None, output=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>, **kwargs)[source]¶ Print the matrix for self’s nodes.
Parameters: - format (str) – output format (csv, json or text).
- output (file) – file descriptor on which to write.
-
submodules
¶ Property to return all sub-modules of the node, recursively.
Returns: list of Module – the sub-modules.
-
dependenpy.structures
¶
dependenpy structures module.
-
class
dependenpy.structures.
Edge
(vertex_out, vertex_in, weight=1)[source]¶ Bases:
object
Edge class. Used in Graph class.
-
__init__
(vertex_out, vertex_in, weight=1)[source]¶ Initialization method.
Parameters: - vertex_out (Vertex) – source vertex (edge going out).
- vertex_in (Vertex) – target vertex (edge going in).
- weight (int) – weight of the edge.
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
dependenpy.structures.
Graph
(*nodes, depth=0)[source]¶ Bases:
dependenpy.helpers.PrintMixin
Graph class.
A class to build a graph given a list of nodes. After instantiation, it has two attributes: vertices, the set of nodes, and edges, the set of edges.
-
__init__
(*nodes, depth=0)[source]¶ Initialization method.
An intermediary matrix is built to ease the creation of the graph.
Parameters: - *nodes (list of DSM/Package/Module) – the nodes on which to build the graph.
- depth (int) – the depth of the intermediary matrix. See the documentation for Matrix class.
-
-
class
dependenpy.structures.
Matrix
(*nodes, depth=0)[source]¶ Bases:
dependenpy.helpers.PrintMixin
Matrix class.
A class to build a matrix given a list of nodes. After instantiation, it has two attributes: data, a 2-dimensions array, and keys, the names of the entities in the corresponding order.
-
__init__
(*nodes, depth=0)[source]¶ Initialization method.
Parameters: - *nodes (list of DSM/Package/Module) – the nodes on which to build the matrix.
- depth (int) – the depth of the matrix. This depth is always absolute, meaning that building a matrix with a sub-package “A.B.C” and a depth of 1 will return a matrix of size 1, containing A only. To see the matrix for the sub-modules and sub-packages in C, you will have to give depth=4.
-
total
¶ Return the total number of dependencies within this matrix.
-
-
class
dependenpy.structures.
TreeMap
(*nodes, value=-1, data=None, keys=None)[source]¶ Bases:
dependenpy.helpers.PrintMixin
TreeMap class.
-
class
dependenpy.structures.
Vertex
(name)[source]¶ Bases:
object
Vertex class. Used in Graph class.
-
__weakref__
¶ list of weak references to the object (if defined)
-