Category Archives: Python

Pushing IPython Notebooks to WordPress

It turns out that I really, really like writing documents as iPython notebooks – at least shorter documents. I’m also (as you can tell, if you are reading this) into blogging about my technical (and recreational) endavors. Since blog posts are shorter documents … Captain Obvious? … it is a small step to wanting to do my blog posting, or much of it … or any of it with Python code, as an iPython notebook. However, WordPress is primarily a php and database mashup and it doesn’t really work well (natively) with Python or iPython.

This has led several folks (like Jake VdV) to bail ship from WordPress to Pelican) and other folks (like Matt R) to not use notebooks directly in their posts. For myself, I decided to explore another option (b/c I already had web hosting and I didn’t want to spin up additional servers, migrate my blog — again, etc.).

Continue reading

Going All Meta (Part 2) – Some Python-Fu

In a previous post (a long, long time ago), I said I was going to talk about metaclasses (or at least show an abuse of them) in Python. I am going to get to that, but I want to set the stage by talking about another topic that isn’t nearly as black-magic-y: decorators. When I’m teaching or training, people commonly ask about decorators because they have seen them and they are confused by them – mainly because a common type of decorator is a function that takes in, modifies, and returns a different function. Huh. Back to meta-ville.

Simply put, a decorator is a Python function with some special characteristics. That Python function takes a single, lonely input. The decorator can either be a function-decorator or a class-decorator. In the first case, the decorator take a function as its input and produces a (modified) function as its output. In the second case, it takes a class as its input and produces a (modified) class as its output.

The raw notebook: Decorator Fun – Function Timing (raw)

As seen by nbviewer: Decorator Fun – Function Timing (through nbviewer)

Going All Meta (Part 1)

Meta note on a meta post:  this is my first Python code post (I think).  Getting the highlighting was trivial:  in WordPress I installed and activated the SyntaxHighlighter Evolved plugin.  And “go!”  This gives you a square bracket tag (shortcode) [[code language=”python] … [/code]]. And a last meta comment, to display shortcodes in a post, you can use an extra set of brackets to enclose the entire WordPress shortcode start and end block like this: [[[code] … [/code]]]. For those keeping track, I had to use double outer brackets (in addition to the brackets on the code tags) in my WordPress text to get that to show up for you. Also, the “Visual” editor borks this badly.

One of the black magic corners of Python is the use of metaclasses.  Since other folks have written extensively on what they are, I’m going to focus on one use (abuse?) of them.  Here are some reference links on classes, metaclasses, and types in python:

And a quick sample of python code. Actual code will come with the next post.

print "hello world"