I'm Paul Bissex, and e-scribe.com is my consulting business. I build web applications using open source software, especially Django. In the '90s I did graphic design for newspapers and magazines. Then I wrote technology commentary and reviews for Wired, Salon.com, Chicago Tribune, and lots of little places you've never heard of. Feel free to email me.
I'm co-author of "Python Web Development with Django", an excellent guide to my favorite web framework. Published by Addison-Wesley, it is available from Amazon and your favorite technical bookstore as well.
Built using Django, served by Apache and mod_wsgi. The database is SQLite. The operating system is FreeBSD, on a VPS hosted at Johncompanies.com. Comment-spam protection by Akismet. Vintage topo imagery from the Maptech archive. The markup engine is Markdown.
Akismet, bitbucket, del.icio.us, Django, Emacs, FreeBSD, Git, jQuery, LaunchBar, Markdown, Mercurial, OS X, Postfix, Python, Review Board, S3, SQLite, TextMate, Ubuntu Linux
At least 95846 pieces of comment spam killed since January 2008, mostly via Akismet.
I came across this neat Python hack on reddit today, a technique for defining arbitrary infix operators. Not overriding + or >> et al., but creating keyword-style pseudo-operators that... well, the code is probably as clear as any description I could come up with:
class infix(object):
"""
Clever hack (slightly modified) for defining infix operators.
Via http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/384122
Intended for use as a decorator.
>>> @infix
... def removing(str, chars):
... for char in chars:
... str = str.replace(char, '')
... return str
...
>>> print 'Hello, world!' |removing| 'eo'
Hll, wrld!
"""
def __init__(self, function):
self.function = function
def __ror__(self, other):
return infix(lambda x: self.function(other, x))
def __or__(self, other):
return self.function(other)
def __call__(self, value1, value2):
return self.function(value1, value2)
Some people hate this kind of stuff. That's why we call it a hack, to indicate that we don't think it's a great building block for your missile control software. Some of Those People still get their underoos in a bunch regardless. But you have to admit it's damned clever.
This also reminds me of some of the lovely higher-order-function features in Haskell, where you can make a regular (prefix) function into infix by wrapping it in backticks -- 10 `mod` 3 -- and an infix function (operator) into prefix by wrapping it in parentheses -- (+) 2 2.
` `
class infix2(object):
def __init__(self, function):
self.function = function
def __ror__(self, other):
self.value=other
return self
def __or__(self, other):
return self.function(self.value,other)
With this version, you do not instanciate a lambda object for each `__ror__` call. On my PC, it's 45% performance gain !
What about this one http://pypi.python.org/pypi/pipe/1.3 ? It's simplier and have ~30 already implemented infix operators
Bruce, the problem with your solution is that it only has one place to store the left-hand value, so it fails if more than one operation is in flight at a time:
>>> plus = infix2(lambda x,y:x+y)
>>> 1 |plus| 2 |plus| 3
6
>>> 1 |plus| (2 |plus| 3)
7
Thanks for reading! Please note: Your comment will not appear until approved, which may take a few hours or more. Spammers will be torpedoed.
Booktools
2 comments
A different kind of URL shortener
4 comments
The syncbox
2 comments
Branching and merging in real life
8 comments
Summer Spam
1 comment
malpaso
Understanding tuples vs. lists in Python
10 days ago
vj100
Understanding tuples vs. lists in Python
10 days ago
scott
Bicycle Repair Man bundle for TextMate
16 days ago
Jasmine
Trying to send eBay a message?
53 days ago
Smok Cigs
Let's play a game: BASIC vs. Ruby vs. Python vs. PHP
90 days ago
Copyright 2012
by Paul Bissex
and E-Scribe New Media
I totally hate it. :)