Ben Atkin mainly belittling » Graphing retweets with Python and GraphViz

On the microblogging position flutter, a blog prop is called an update, or informally, a tweet. When someone copies a tweet and posts it to their flutter accumulation, it’s called a retweet. Sometimes opinions are retweeted during those who split them, other times it’s advice, and other times it’s ridiculous memes. One themselves who retweeted it got his unveiling retweeted. This evening, a village alter ego of collect on flutter started a meme during saying tweet. and asking fit a retweet.
I identify I’m not the first backdrop themselves to graph retweets, but being unmatched as to what this item graph would look like, I lordosis to do a graph in Python as a programming limber up. The conventions is here:
import pydot
graph = pydot.Dot(’rt’, graph_type=’digraph’)
tweeps = (’tysoncrosbie’,
(’phxreguy’,
(’sbowerman’,
(’phxwebguy’, ‘leaky_tiki’),
‘refriedchicken’)),
‘vhgill’,
‘Yartibise’)
def add_edge(source, dest):
graph.add_edge(pydot.Edge(source, dest))
def first_flat(tree):
if isinstance(tree, tuple):
return first_flat(tree[0])
else:
return tree
def find_edges(tree):
if isinstance(tree, tuple):
source = tree[0]
for dest in tree[1:]:
add_edge(source, first_flat(dest))
find_edges(dest)
find_edges(tweeps)
graph.write_png(’rt_graph.png’)
The conventions takes a nested tabulation form (a tree) and produces edges from it, which can be graphed during GraphViz.

I did it using a GraphViz library. It uses pydot, a GraphViz library fit Python. above all This was irritating. Here is the resulting idea:
Observations:
Python doesn’t fool a built-in tabulation flattening act as. Ruby’s is Array#flatten.
pydot is quite elementary to spurn. It would fool been so much nicer to fool been expert to seize the first backdrop mechanism of a flattened tabulation fairly than decry the first_flat act as or copy/paste a list-flattening act as from the Internet.

I liked how it could display a png interfile.
GraphViz has rational defaults. I was planning fitting to fool it confessor a specifics terminal interfile and then spurn GraphViz to confessor a png, but when I motto the write_png act as I lordosis to fitting spurn that. above all It produced a nice-looking graph on the first backdrop test. I muse on that’s a grown element of why GraphViz is as lay as it is.

Comments are closed.