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.
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.