Archive for the ‘Python’ Category

Introducing Cushion, a very thin python wrapper around CouchDB’s Document API. Its interface closely mimics REST and it was created primarily as an exercise to learn CouchDB’s API. Cushion is not an object mapper, it doesn’t enforce ‘OOP’, and if you found it difficult to get started with CouchDB using other libraries then you might be interested in Cushion.

To retrieve a document using Cushion:

To create a document with a document id using Cushion:

To install Cushion: easy_install cushion. If you’re interested, the source code and more documentation is available on GitHub: git://github.com/sheysrebellion/cushion.git

Relax, enjoy.

Share and Enjoy:
  • del.icio.us
  • Reddit
  • Facebook
  • Identi.ca
  • TwitThis

Long if-else statements and tall case statements are scary. As the number of lines and conditional branches grow, the more difficult it becomes to fit the program’s logic inside your head and the more likely you are to introduce bugs into the code.

We rely on polymorphism and design patterns to help reduce complexity; often those solutions are well suited for static programming languages like Java, but feel ceremonious in languages like Python and Javascript. If you’re programming in a dynamic programming language that supports dictionaries out of the box, then you may find the Dispatch Table “pattern” a useful way to simply your code.

The pattern uses a dictionary where the keys represent possible actions, and the value of each key contains a callable that does the actual work. Compared to if-else statements, dispatch tables require fewer lines of code, and we know fewer lines of code equals fewer bugs.

Here’s a mostly real world example that creates handlers for webhooks. You can imagine how long the if-else statement, that is used to decide which handler to instantiate, can become, especially when the consuming code has to respond to multiple keys.

Neat.

Share and Enjoy:
  • del.icio.us
  • Reddit
  • Facebook
  • Identi.ca
  • TwitThis

In a recent project, I’ve experimented with “partial()” to simplify object creation. Partially applying a function, similar to currying, is a technique in which a new function is synthesized by pre-filling the arguments to an existing function. Python’s implementation of partial is available in two separate modules: functools and functional. It’s best understood with an example, albeit contrived:

Instead of defining a factory class and a creation method on that class, I define creation function which accepts the dependencies as parameters, partially apply them and use the new function as my “creation function.” I prefer the second example, I enjoyed writing it, but both examples perform the same function with essentially the same number of lines and ultimately my preference is as basic as preferring a functional style of programming over object oriented.

So I’m wondering, how do other developers use currying or partial application in their projects?

Share and Enjoy:
  • del.icio.us
  • Reddit
  • Facebook
  • Identi.ca
  • TwitThis

Search