Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a dependency graph in python

Tags:

I was wondering if python has some built-in library (or any library on the net..) That will create for for me a graph of dependencies ? I have a file formatted like that

A::Requires         = "" B::Requires     = A C::Requires     = B H::Requires     = A  AA::Requires         = "" BB::Requires         = AA C::Requires     = B  CC::Requires    = BB 

Ideally I would like to have something like a tree like that:

A  +-B    +-C  +-H  AA  +-BB    +-CC 

So basically A lib where I will provide a tuple (A,B) or (A,H) and it will build the tree for me? If such a lib doesn't exist, what would be the easier way to accomplish something like that?

Thank you

like image 200
Johny19 Avatar asked Jan 09 '13 16:01

Johny19


People also ask

What is a dependency graph Python?

Modulegraph2 is a library for creating and introspecting the dependency graph between Python modules. The graph is created using static analisys from source and byte code. The dependency graph contains information about packages, modules, extensions and their dependencies.

What is dependency graph with example?

A dependency graph is a data structure formed by a directed graph that describes the dependency of an entity in the system on the other entities of the same system. The underlying structure of a dependency graph is a directed graph where each node points to the node on which it depends.

What is dagger in Python?

Dagger is written in Python to make it portable and extensible. It's graph evaluation engine is non-recursive, so it can handle very deep dependency paths. A benchmark tool (see below) is available to test and visualize complex graphs, and demonstrates using 1 million files.


2 Answers

Assuming your input from above is given as a string in raw:

import networkx as nx import re  regex = re.compile(r'^([A-Z]+)::Requires\s+=\s([A-Z"]+)$')  G = nx.DiGraph() roots = set() for l in raw.splitlines():     if len(l):         target, prereq = regex.match(l).groups()         if prereq == '""':             roots.add(target)         else:             G.add_edge(prereq, target) 

Now print the tree(s):

for s in roots:     print s     spacer = {s: 0}     for prereq, target in nx.dfs_edges(G, s):         spacer[target] = spacer[prereq] + 2         print '{spacer}+-{t}'.format(                                      spacer=' ' * spacer[prereq],                                      t=target)     print '' 

this prints:

A +-H +-B   +-C  AA +-BB   +-CC 

This requires that all roots are presented through root::Requires = "" in order for them to be identified as such.

like image 193
tzelleke Avatar answered Oct 13 '22 00:10

tzelleke


Try with one of the several ones:

  • graph-tool
  • networkx
  • igraph

graph-tool is very difficult to install (it needs a lot of memory for compilation, I think it was around 5GB of RAM and around 12 hours of compilation).

networkx is pretty decent.

igraph quote from their page: igraph is a free software package for creating and manipulating undirected and directed graphs. It includes implementations for classic graph theory problems like minimum spanning trees and network flow, and also implements algorithms for some recent network analysis methods, like community structure search.

I've been using all of them. It really depends on what exactly do you need. If you need them for something as simple as dependencies, then it really is not important which one you are going to use, though, I would recomend you to avoud graph-tool if you need it for something shorter and lighter.

like image 25
Belphegor Avatar answered Oct 13 '22 00:10

Belphegor