Archive for the ‘python’ tag
setting up a default virtualenv
After using virtualenv for months, I finally got around to putting my main virtualenv into my bash profile. It’s really simple. All I had to do was add the following to ~/.bash_profile:
# python virtualenv
source /Users/ben/virtualenv/pearl/bin/activate
This worked, but there was one thing I wanted to turn off. The activate script that virtualenv creates adds the name of the current virtualenv to the front of the prompt. This is useful when switching between virtualenv directories, but it’s not very useful to me when I’m on the default virtualenv. So I moved my prompt definition to the bottom of the file, so it gets executed after the source command above.
# prompt (moved after virtualenv so it isn't shown)
PS1="\[\e[31;40m\]\@ \[\e[32;40m\]\w \[\e[36;40m\]\$\[\e[0m\] "
By the way, to keep the prompt I use the most often short, I set my own colors. It’s easy for me to tell that it’s my main shell, and since it’s my main shell, I know the username and hostname associated with it.
Graphing retweets with Python and GraphViz
On the microblogging site twitter, a blog post is called an update, or informally, a tweet. When someone copies a tweet and posts it to their twitter feed, it’s called a retweet. Sometimes opinions are retweeted by those who share them, other times it’s information, and other times it’s silly memes. This evening, a local friend of mine on twitter started a meme by saying “tweet.” and asking for a retweet. One person who retweeted it got his message retweeted.

I know I’m not the first person to graph retweets, but being curious as to what this particular graph would look like, I decided to do a graph in Python as a programming exercise. I did it using a GraphViz library. The code 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 code takes a nested list structure (a tree) and produces edges from it, which can be graphed by GraphViz. It uses pydot, a GraphViz library for Python. Here is the resulting image:

Observations:
- Python doesn’t have a built-in list flattening function. This was irritating. Ruby’s is Array#flatten. It would have been so much nicer to have been able to grab the first element of a flattened list rather than write the first_flat function or copy/paste a list-flattening function from the Internet.
- pydot is really simple to use. I liked how it could produce a png file. I was planning just to have it create a dot file and then use GraphViz to create a png, but when I saw the write_png function I decided to just use that.
- GraphViz has reasonable defaults. It produced a nice-looking graph on the first try. I think that’s a big part of why GraphViz is as popular as it is.
I was hoping to have a little more to show for tomorrow’s Python Interest Group meeting, but this will have to do!