Skip to content

Instantly share code, notes, and snippets.

@prasadtalasila
Created April 10, 2017 15:00
Show Gist options
  • Save prasadtalasila/71893b1eb8dfc425fba169b4c70b1475 to your computer and use it in GitHub Desktop.
Save prasadtalasila/71893b1eb8dfc425fba169b4c70b1475 to your computer and use it in GitHub Desktop.
pycallgraph usage
pythoncallgraph is useful for plotting callgraphs of python code base. The software works by default, but has quirky behavior for some of the options.
To reproduce this tutorial, do the following.
(graphviz already installed)
$pip install pycallgraph
$cat > scrapyTest.py <<EOF
import scrapy
class BlogSpider(scrapy.Spider):
name = 'blogspider'
start_urls = ['https://blog.scrapinghub.com']
def parse(self, response):
for title in response.css('h2.entry-title'):
yield {'title': title.css('a ::text').extract_first()}
next_page = response.css('div.prev-post > a ::attr(href)').extract_first()
if next_page:
yield scrapy.Request(response.urljoin(next_page), callback=self.parse)
EOF
(Ref: Above code fragment has been taken from https://scrapy.org/)
The pycallgraph options I explored are given below.
$pycallgraph graphviz --output-file=pycallgraph.png scrapyTest.py
(plots a huge graph)
$pycallgraph -d 3 graphviz --output-file=pycallgraph.png scrapyTest.py
(gives an error)
plot upto a maximum depth of 3 and exclude all packages that match the regular expressions - Blog*, pkg*, twist*.
$pycallgraph --max-depth=4 -e "Blog*" -e "pkg*" -e "twist*" graphviz --output-file=pycallgraph.png scrapyTest.py
(for some strange reason, pycallgraph is not able to match whole words. In the above run, pycallgraph is excluding a package named pkgutil. But if we give the whole name, pycallgraph is not able to match the name. But with pkg* regex, it is able to match to pkgutil module. Strange indeed.)
See the [sample graph](http://pasteboard.co/2DXyRX2S3.png) generated by pycallgraph for the last command.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment