Architecture: Adapter design pattern

Adapter Design Pattern allows for two incompatible classes to work together by wrapping an interface around one of the existing classes

I have written the working code based on the Architectural diagram, which can be downloaded from Github.

Working code github link:


4_1 Adapter design pattern - sequence diagram


4_1 Adapter Design Pattern


Python: list comprehensions

# Python's list comprehensions are awesome.

vals = [expression 
        for value in collection 
        if condition]

# This is equivalent to:

vals = []
for value in collection:
    if condition:

# Example:

>>> even_squares = [x * x for x in range(10) if not x % 2]
>>> even_squares
[0, 4, 16, 36, 64]

Python: Dict get with default value to void key not found error

# The get() method on dicts
# and its "default" argument

name_for_userid = {
    382: "Aseem",
    590: "Bob",
    951: "Dilbert",

def greeting(userid):
    return "Hi %s!" % name_for_userid.get(userid, "there")

>>> greeting(382)
"Hi Aseem!"

>>> greeting(333333)
"Hi there!"

Python: emulate switch/case statements by using dict with lambda functions

# Because Python has first-class functions they can
# be used to emulate switch/case statements

def dispatch_if(operator, x, y):
    if operator == 'add':
        return x + y
    elif operator == 'sub':
        return x - y
    elif operator == 'mul':
        return x * y
    elif operator == 'div':
        return x / y
        return None

def dispatch_dict(operator, x, y):
    return {
        'add': lambda: x + y,
        'sub': lambda: x - y,
        'mul': lambda: x * y,
        'div': lambda: x / y,
    }.get(operator, lambda: None)()

>>> dispatch_if('mul', 2, 8)

>>> dispatch_dict('mul', 2, 8)

>>> dispatch_if('unknown', 2, 8)

>>> dispatch_dict('unknown', 2, 8)

Python: Functions are first-class citizens

# Functions are first-class citizens in Python.

# They can be passed as arguments to other functions,
# returned as values from other functions, and
# assigned to variables and stored in data structures.

>>> def myfunc(a, b):
...     return a + b
>>> funcs = [myfunc]
>>> funcs[0]
<function myfunc at 0x107012230>
>>> funcs[0](2, 3)

Testing: An open source load testing tool

There is an easy way to load test simultaneous users in the application. The name of open source tool is called as Locust

Here is sample locust implementation of locust on my github repository (its pure python, no xml etc)

Framework link


Getting Started

Once installed, the first thing you need to do is write your test case, which is just a few lines of simple Python. Mine looks like this.

from locust import HttpLocust, TaskSet, task

json = """
  timestamp: 1339594030236,
  type: test_event,
  fields: [
    { foo: 'FOO' },
    { bar: 'BAR' },
  version: 1

class MyTaskSet(TaskSet):
    def send(l):"/", json)                                                                                                              

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000

I only have one URL to test, so it’s pretty simple. I make a POST request to this URL with some JSON.

To start Locust, simply run the locust tool, passing in the host you want to test.

locust -H http://localhost:37010

It’s not doing anything at the moment. To start the load testing, you need to open up the web interface at (if your’re running Locust locally) and choose the amount of users to simulate, and their hatch rate (i.e. how fast the users will spawn).