Skip to content

Latest commit

 

History

History
210 lines (153 loc) · 9.08 KB

usage.rst

File metadata and controls

210 lines (153 loc) · 9.08 KB

Usage

Grimp provides an API in the form of an ImportGraph that represents all the internal imports within a top-level Python package. This object has various methods that make it easy to find out information about that package's structure and interdependencies.

Terminology

The terminology around Python packages and modules can be a little confusing. Here are the definitions we use, taken in part from the official Python docs:

  • Module: A file containing Python definitions and statements. This includes ordinary .py files and __init__.py files.
  • Package: A special kind of module that namespaces other modules using dotted module names. For example, the module name A.B designates a submodule named B in a package named A. Packages take the form of __init__.py files in a container directory. Packages may contain other packages. A package is also a module.
  • Top Level Package: A package in the root namespace - in other words, one that is not a subpackage. For example, A is a top level package, but A.B is not.
  • Graph: A graph in the mathematical sense of a collection of items with relationships between them. Grimp's ImportGraph is a directed graph of all the internal imports contained in a particular top level package.
  • Direct Import: An import from one module to another.
  • Import Path: A chain of imports between two modules, possibly via other modules. For example, if mypackage.foo imports mypackage.bar, which in turn imports mypackage.baz, then there is an import path between mypackage.foo and mypackage.baz.

Building the graph

import grimp

graph = grimp.build_graph('name_)

Methods for analysing the module tree

Methods for analysing direct imports

Methods for analysing import paths

Methods for manipulating the graph

Add a direct import between two modules to the graph. If the modules are not already present, they will be added to the graph.

param str importer

The name of the module that is importing the other module.

param str imported

The name of the module being imported.

param int line_number

The line number of the import statement in the module.

param str line_contents

The line that contains the import statement.

return

None