29

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

Johny19
  • 5,364
  • 14
  • 61
  • 99

4 Answers4

26

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.

tzelleke
  • 15,023
  • 5
  • 33
  • 49
  • to find roots, see also [Finding all the roots inside a DiGraph](https://stackoverflow.com/questions/62468287/finding-all-the-roots-inside-a-digraph-networkx) – milahu Jun 07 '22 at 09:28
20

Try with one of the several ones:

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.

jemand771
  • 457
  • 1
  • 6
  • 17
Belphegor
  • 4,456
  • 11
  • 34
  • 59
  • Thank you. I think that networkx will be more than enough! I really just need to do like Graph.add_edge(A,B) etc.. and networkx will create for me the graph. One question though. I have been playing a bit with it. But How Can I get the list of depencies ? After creating the graph, i would ask networkx to give me {A:{B:{C:{}}, H:{}}} – Johny19 Jan 09 '13 at 17:53
  • 2
    IMHO igraph is _not_ made for biological research. It is made for any kind of networks. – Gabor Csardi Jan 09 '13 at 20:24
  • Thanks @GaborCsardi for the critics. I edited my post. +1 on your comment for the correction! – Belphegor Jan 14 '13 at 17:05
  • @Johny19 I am not entirely sure, I've been using a lot of graph libraries lately and things got mixed up in my head. Search through the documentation and find some examples and learn from them, it shouldn't be hard for you. – Belphegor Jan 14 '13 at 17:07
3

Graphviz is great for building documentation of dependencies in an automated manner.

There's a useful Python library too called pygraphviz

I use this to build up a dependency map then output in both text form and as a visual that automatically exports to PDF.

0

I found and am trying https://github.com/thebjorn/pydeps

It is easy to install and easy to use. I am currently learning how to customize the resulting diagram.

Installation

pip install pydeps

Use

pydeps <path to python app>

Heads up

You need to have Graphviz installed in your OS and in your path (you hsould be able to run dot in your terminal without issues).

Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47