Parente's Mindtrove

Python operator Module

December 27, 2010

Today I learned about the Python operator module in the standard library while playing with Blogofile. The module includes functional equivalents of standard Python operators. These are particularly useful for sorting.

import operator
class O:
  def __init__(self, val):
    self.val = val
arr = [O(4), O(5), O(1), O(2)]
# sort by value attribute of the object
arr.sort(key=operator.attrgetter('val'))
print [i.val for i in arr]

Another Read: My Switch to Blogofile »

I saw Mike Pirnat's various posts (see #1 and #2) on Planet Python about switching to Blogofile. I noted with interest its static nature and his deploying it using git to Webfaction, but bookmarked it for later investigation. After all, Wordpress was working well enough relative to the amount of blogging I do. Still, its in-browser editor has always left me pining for something more friendly to posts heavy with code and/or custom markup.