Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Latest commit

 

History

History
108 lines (76 loc) · 1.95 KB

README.md

File metadata and controls

108 lines (76 loc) · 1.95 KB

Snarl

Build Status codecov

Snarl is a tool for creating execution graphs of Python programs.

Installation: pip install git+https://github.com/JoshKarpel/snarl.git. You'll also need to make sure that the dot executable (https://www.graphviz.org/download/) is available on your system and visible in your path.

Here's an example:

from snarl import Snarl

def a():
    b()
    recurse(2)

def b():
    for _ in range(10):
        c()
    recurse(3)

    f = Foo()
    f.method(5)

def c():
    def inner():
        pass

    inner()

def recurse(n):
    if n == 0:
        return
    recurse(n - 1)

class Foo:
    def method(self, x):
        pass

    @classmethod
    def clsmethod(cls):
        return cls()

with Snarl() as snarl:
    a()
    recurse(5)

    Foo.clsmethod()

dot = snarl.dot(format = 'png')
dot.render(view = True)

examples/context_mananger.gv.png

The region that we collected information on was defined by the with block. We can use the snarled decorator to attach to a function instead:

from snarl import snarled

@snarled
def a():
    b()
    recurse(2)

def b():
    for _ in range(10):
        c()
    recurse(3)

    f = Foo()
    f.method(5)

def c():
    def inner():
        pass

    inner()

def recurse(n):
    if n == 0:
        return
    recurse(n - 1)

class Foo:
    def method(self, x):
        pass

    @classmethod
    def clsmethod(cls):
        return cls()

a()
recurse(5)

Foo.clsmethod()

dot = a.snarl.dot(name = 'decorator', format = 'png')
dot.render(view = True)

examples/decorator.gv.png

Now we don't see the recurse(5) or Foo.clsmethod() calls because the "root" of the execution graph is the function a.