Posts tagged: FUN

Neat Python hack: infix operators

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.

iPhone, youPhone, weallPhone

Apple and Cisco have reached an agreement on the disputed “iPhone” trademark, and – surprise – Apple gets to keep using it. From the Cisco press release:

Under the agreement, both companies are free to use the “iPhone” trademark on their products throughout the world.

To further differentiate brands, the Apple iPhone will also be known as “the real iPhone”, with the Cisco model being referred to as “Oh, I thought you meant the other one.”

More unexpanded spam macros

I posted one simple example of this a while back, but this one’s much better. (I’ve removed some uninteresting stuff like the actual routing.)

From ...
Received: ...
Date: ...
Received: from 192.168.0.%RND_DIGIT (203-219-%DIGSTAT2-%STATDIG.%RND_FROM_DOMAIN
[203.219.%DIGSTAT2.%STATDIG]) by mail%SINGSTAT.%RND_FROM_DOMAIN (envelope-from
%FROM_EMAIL) (8.13.6/8.13.6) with SMTP id %STATWORD for <%TO_EMAIL>;
%CURRENT_DATE_TIME
Message-Id: <%RND_DIGIT[10].%STATWORD@mail%SINGSTAT.%RND_FROM_DOMAIN>
From: "%FROM_NAME" <@FROM_EMAIL>
To: undisclosed-recipients:;

%TO_CC_DEFAULT_HANDLER
Subject: %SUBJECT
Sender: "%FROM_NAME" <%FROM_EMAIL>
Mime-Version: 1.0
Content-Type: text/html
Date: %CURRENT_DATE_TIME

%MESSAGE_BODY

That Received: line looks like a nice template for a SpamAssassin rule, if you use SpamAssassin.

99-byte Python quicksort

Update: Browsing through my Python Cookbook this evening I discovered entry 5.11, “Showing off quicksort in Three Lines”, which includes some code very much like mine below. The entry does a good job of emphasizing that these bits of code are perhaps to be savored but not to be actually used. It also includes an insanely (impressively?) convoluted version that uses three lambdas in a single line and weighs in at 105 bytes. I thought this might be the best possible in Python 2.4 and earlier, but in fact a simpler version can be constructed using the old short-circuit logic trick, and at 94 bytes it’s even smaller than my original. Here it is: q=lambda s:len(s)and q([x for x in s[1:]if x<s[0]])+[s[0]]+q([x for x in s[1:]if x>=s[0]])or s

Enticed by the lovely Haskell quicksort example, and sullied by the code-crunching ways of Codegolf, I decided to see how small a Python quicksort function I could write. I stopped at 99 bytes.

STFU

E-Scribe is not, strictly speaking, a standards organization. However, I think the time is right to release this draft document on an important Internet standard. The document is presented inline here for convenience; however, the preferred permanent reference is: http://e-scribe.com/stfu

Recent trends in internet-based application development have
fostered the rapid spread of asynchronous, Javascript-based techniques
known by the umbrella term "Ajax" and by related terms such as "AHAH",
"POX", and so on.

This brief paper argues for a plaintext, synchronous alternative style
with some compelling advantages, one of which is (naturally) a catchy
name.

Synchronous Text/plain For User-agents -- STFU -- is a content-
delivery standard offering simple, reliable, high-performance routing
of content to Internet end-users. Implementation is orders of
magnitude faster than for typical Ajax applications, with a
corresponding drop in defect rates.

It's that simple. STFU.

  * well-tested
  * cross-platform
  * highly scalable
  * enterprise-ready
  * supports Unicode!
  * compliant with Section 508 Accessibility Requirements

One prominent adopter of STFU technology is the Internet Engineering
Task Force, which uses it for the dissemination of their most
important documents:

  ietf.org/rfc/rfc-index-latest

STFU also sidesteps technical quandaries related to markup formats,
such as those summarized here:

  hixie.ch/advocacy/xhtml

It's no accident that the author of the above (widely cited) document
chose to deliver his important message via STFU. 

Sometimes the debates over emerging standards and technologies become
wearisome, and pragmatic individuals find themselves wishing for a
magic phrase that might quiet the true believers and allow all
involved to move on to more productive activities. May we recommend:

STFU!

If you are already using STFU, you are welcome to display this PNG badge: