dtlibs.dev

This module contains some development tools, incuding a graphical profiler and a timer.

Members

class dtlibs.dev.profile([filename=None])

Profile a function, to observer its performance.

This provides a decorator interface to cProfile. It does not add any functionality, but rather makes it easier to quickly profile an existing function during testing without needing a large amount of boilerplate.

The simplest way of using this is:

@profile()
def long_spam_song(spam):
    for s in spam:
        sing_spam_song(s)

This is similar to calling:

def long_spam_song(spam):
    def run(spam):
        for s in spam:
            sing_spam_song(s)
    cProfile.run('run()', sort='calls')

A more complex usage is:

prof = profile('long_spam_song.profile')
prof.sort_stats('time').print_callers(10)
@prof
def long_spam_song(spam):
    for s in spam:
        sing_spam_song(s)

The argument to profile is the name of the file to write profile information to (see the cProfile documentation for more detail). The additional methods called on this are the same as those provided by pstats.Stats. If no additional methods are used, .sort_stats('calls').print_stats(10) is applied. Note that if any method is used, one of the print methods must also be used to display the results.

profile can also be used as a context manager:

with profile(‘spam_song’):
sing_spam_song()
class dtlibs.dev.Theme

A Colour theme to use in graphs.

Parameters:
  • bgcolor – Global background colour (default is white)
  • mincolor – Minimum node colour (default is black)
  • maxcolor – Maximum node colour (default is white)
  • fontname – Fontname for text (default is ‘Arial’)
  • minfontsize – Minimum font size (default is 10)
  • maxfontsize – Maximum font size (default is 10)
  • minpenwidth – Minimum pen width for links (default is 0.5)
  • maxpenwidth – Maximum pen with for links (default is 4.0)
  • gamma – Gamma correction (default is 2.2)
  • skew – Skew the colour curve (Default is 1.0)

Colours are specified as RGB tuples.

The following themes are predefined.

dtlibs.dev.temp_theme

A colour theme ranging from red to blue.

dtlibs.dev.pink_theme

A pink colour theme.

dtlibs.dev.gray_theme

A gray colour theme

dtlibs.dev.mono_theme

A black and white colour theme

graph(filename, [fmt='pdf', nodethres=0.5, edgethres=0.1,
theme=`temp_theme`, strip=False, wrap=False])

This is similar to profile, but shows a graph instead.

Parameters:
  • filename – The name of the file (including extension) to write.
  • fmt – The file format as used by dot’s ‘-T’ parameter
  • nodethres – Eliminate nodes below this threshold fraction
  • edgethres – Eliminate edges below this threshold fraction
  • theme – Colour theme (see Theme)
  • strip – Strip mangling from C++ function names
  • wrap – Wrap function names

This requires <graphviz http://www.graphviz.org/>_ and <gprof2dot http://code.google.com/p/jrfonseca/wiki/Gprof2Dot>_.