About

Have you ever dreamed with automagically plotting method calls in your code by just running it? Wouldn’t be that really cool for tracing your code?

Here you are a damn simple tool for that!

Requirements

Usage

Just decorate your methods, functions or classes:

import autouml
import autouml.log
import numpy

autouml.set_options(show_arguments=True, use_instance_ids=True)

# We also want to decorate all methods in numpy module
autouml.sequence_dia(numpy.random)


@autouml.sequence_dia
class Dice(object):
    '''
    Just a random point in segment [0,radius]
    '''
    radius = None

    def __str__(self):
        return 'Dice(%s)' % self.radius

    def __init__(self, radius=1):
        self.radius = radius

    def roll(self):
        return numpy.random.random() * self.radius

    def rpoint(self):
        return Point(self.roll(), self.roll())


@autouml.sequence_dia
class Circle(object):
    '''
    A circle centered in (0,0) with radius r
    '''
    radius = None

    def __init__(self, radius=1):
        self.radius = radius
        self.__inside = 0.
        self.__total = 0.
        self.dice = Dice(self.radius)

    def __str__(self):
        return 'Circle(%s)' % self.radius

    def is_inside(self, point):
        return point.x**2 + point.y**2 < self.radius**2

    def shot(self, point):
        self.__total += 1
        if self.is_inside(point):
            self.__inside += 1

    def rshot(self, n=1):
        for i in range(n):
            self.shot(self.dice.rpoint())

    @property
    def pi(self):
        return 4 * self.__inside / self.__total


@autouml.sequence_dia
class Point(object):
    x = None
    y = None

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __str__(self):
        return 'Point(%s, %s)' % (self.x, self.y)

c = Circle(1)
c.rshot(3)
print c.pi

And run your code. A log file will be generated with the plantuml syntax.

__main__->o 140489816401032: __init__(self, 1)
box " Circle" 
  participant 140489816401032
end box

140489816401032->o 140489816468296: __init__(self, 1)
box " Dice" 
  participant 140489816468296
end box

__main__->140489816401032: rshot(self, 3)
140489816401032->140489816468296: rpoint(self)
140489816468296->140489816468296: roll(self)
140489816468296->__main__: random_sample()
140489816468296->140489816468296: roll(self)
140489816468296->__main__: random_sample()
140489816468296->o 140489816507528: __init__(self, 0.328646075817, 0.801006041382)
box " Point" 
  participant 140489816507528
end box

140489816401032->140489816401032: shot(self, Point(0.328646075817, 0.801006041382))
140489816401032->140489816401032: is_inside(self, Point(0.328646075817, 0.801006041382))
140489816401032->140489816468296: rpoint(self)
140489816468296->140489816468296: roll(self)
140489816468296->__main__: random_sample()
140489816468296->140489816468296: roll(self)
140489816468296->__main__: random_sample()
140489816468296->o 140489816524056: __init__(self, 0.150825468413, 0.335550416174)
box " Point" 
  participant 140489816507528
  participant 140489816524056
end box

140489816401032->140489816401032: shot(self, Point(0.150825468413, 0.335550416174))
140489816401032->140489816401032: is_inside(self, Point(0.150825468413, 0.335550416174))
140489816401032->140489816468296: rpoint(self)
140489816468296->140489816468296: roll(self)
140489816468296->__main__: random_sample()
140489816468296->140489816468296: roll(self)
140489816468296->__main__: random_sample()
140489816468296->o 140489816540512: __init__(self, 0.491640395508, 0.231049778945)
box " Point" 
  participant 140489816507528
  participant 140489816524056
  participant 140489816540512
end box

140489816401032->140489816401032: shot(self, Point(0.491640395508, 0.231049778945))
140489816401032->140489816401032: is_inside(self, Point(0.491640395508, 0.231049778945))
@enduml

And also an image with the drawn diagram once the execution ends.

_images/example.autouml.png