Lambda lambda lambda

It can help ya help ya help ya.

It’s Python’s lambda function1! Pesky urllib2 not allowing you to construct a DELETE request? Environment Overlords not letting you install a sane http module like Requests2? Don’t like what a module you have no control over is doing for you?

Lambda the damn thing!

For example urllib2 has a method called .get_method(). It returns the type of HTML request being made. In their infinite wisdom, the writers of urllib2 thought there were only two useful types of request anyone would ever want to make using their module, GET and PUT. The method looks at the URL and if it sees post data it returns “POST”. Otherwise it returns “GET”.

But it’s a modern Web we live in and we want to do modern things like make calls against a RESTful api provided by a vendor. Sometimes that vendor uses the kind of request being sent to determine the kind of action to take. Makes sense, no? But urllib2 spits on us and makes us write bad checks. What shall we do?

In this case we just hack the bejesus out of the .get_method() method and force it to return “DELETE” every time.


import urllib2
req = urllib2.Request( "http://www.example.com" )
req.get_method = lambda: "DELETE"

Now we can run DELETE requests against the RESTful API all damn day long. And then, later, when we want to do a “GET” all we have to do is lambda the damn thing again and make it return “GET”. Yay for us!

Yay for lambdas!

This is my rose for the day.

1http://www.secnetix.de/olli/Python/lambda_functions.hawk
2http://docs.python-requests.org/en/latest/

Refactoring problems

Refactoring without unit tests is a lot like playing that game where you splay your fingers out on a table and then stab the spaces between the fingers as fast as you can with a really sharp knife in your other hand.

Writing unit tests for one-off scripts isn’t really worth the effort.

Refactoring one-off scripts pretty much sucks, regardless.

Self Worth

Programming is kind of an objective thing. Code works or it doesn’t. This can haunt people who, to any extent, tie their sense of self worth to their current job performance. Starting some time Tuesday afternoon my personal stock plummeted as the IR Index climbed to five tickets, all of which I thought I’d solved already. Yet this afternoon I am happy happy happy. One issue was redundant code (I fixed one of two blocks of identical code) and the other was what we’ll charitably call “not my fault.” Now my ticket queue is on its way to zero.

And some people say programming is boring.

Java Mathematic Expression Parser

JMEP is exactly what I’ve been beating my head over for the past few weeks. Seriously way better than what I was writing and, from the testing I’ve done, way more robust. If you’ve been looking for an open-source java parser for mathematical expressions, your search is over.

Never was interested in building a parser, I’m way more interested in building a cross-platform GUI fantasy baseball draft manager. Using JMEP cut my own codebase by several dozen classes and simplified the whole stats interface aspect significantly.

Dynamic Languages Strike Back

Steveys Blog Rants: Dynamic Languages Strike Back is a great article. Or at least it reads like a great article. To be honest, I don’t have the chops to voice a truly knowledgeable opinion but I figure if I keep reading this stuff it will come to me eventually.

It all relates to a paradigm I and my first mentor skirmished over a bit. He was old school and in to sucking every bit of processing out of a single command as possible. I, being much less experienced, would write code that was a bit more human readable but also less direct, less elegant. To be sure, I greatly admire elegant code and strive for it myself but I also recognize that processors have a little overhead when it comes to dealing with most tasks so why not use it?

Bringing it all around then, the essay deals with how dynamic languages are becoming competitive with static typed languages when considering the application as a whole. Improvements in hardware, compilers, and runtime engines plus smaller code bases and quicker prototyping make dynamic languages attractive.

Of course, he’s pushing for Lisp while I’m pretty happy with Python.