Animal Farm Review

Animal Farm’s satire makes the novella interesting to read. Written after Bolshevik revolution and no reason why lot go over gaga. Confining to Bolshevik revolution is tunnel view. The core of the novella resembles with lot of today’s politics and ancient kingdom. For a moment forget about the bolshevik revolution. Rebellion outbursts, the leader comes to power and slowly vanquishes non believers in ideas or voices. Slowly becomes benevolent dictator for life, corrupt and power moves from people to few hands. [Read More]

Asyncio and uvloop

Today, I read an article about uvloop. I am aware of libuv and its behind nodejs. What caught me was “In fact, it is at least 2x faster than any other Python asynchronous framework.”. So I decided to give it a try with aiohttp. The test program was simple websocket code which receives a text message, doubles the content and echoes back. Here is the complete snippet with uvloop. I ran naive benchmark using thor and results favoured uvloop. [Read More]

Permissions in Django Admin

Admin dashboard is one of the Django’s useful feature. Admin dashboard allows super users to create, read, update, delete database objects. The super users have full control over the data. Staff user can login into admin dashboard but can’t access data. In few cases, staff users needs restricted access . Super user can access all data from various in built and third party apps. Here is a screenshot of Super user admin interface after login. [Read More]

Testing Django Views

Majority of web frameworks promote MVC/MTV software pattern. The way web applications are designed today aren’t same as 5-6 years back. Back then it was server side templates, HTML, API’s weren’t widespread and mobile apps were becoming popular. Rise of mobile and Single Page Application shifted majority of web development towards API centric development. Testing API is super simple with data in and data out, but testing a django view in classic web application is difficult since HTML is returned. [Read More]

Simple Json Response basic test between Flask and Django

Django and Flask are two well known Python web frameworks. There are lot of benchmarks claim Flask is 2xfaster for simple JSON Response, one such is Techempower. After lookinginto the source, it struckme Django can do better! I will compare Flask and Django for simple json response. The machine used is Macbook pro, Intel Core i5-4258U CPU @ 2.40GHz,with 8 GB Memory on OS X 10.10.3. gunicorn==19.3.0 will be used for serving WSGI application. [Read More]

django print exception to console

Django has very good debug toolbar for debugging SQL. While working with Single Page Application and API exceptions can’t be displayed in browser. Exception is sent to front end. What if the exception can be printed to console ?

Django middleware gets called for every request/response. The small helper class looks like

Add the filename and class name to MIDDLEWARE_CLASSES in settings file like

This is how exceptions looks

Check for custom objects in Python set.

Python set data structure is commonly used for removing duplicate entriesand make lookup faster (O(1)). Any hashable object can be stored in set.For example, list and dict can’t be stored. User defined objects can be stored. Here is how it looks. class Person(object): def __init__(self, name, age): self.name, self.age = name, age In [25]: s = set() In [26]: s.add(Person('kracekumar', 25)) In [27]: s Out[27]: set([<__main__.Person at 0x1033c5e10>]) In [29]: Person('kracekumar', 25) in s Out[29]: False Implement equality check Even though Person object with same value is present but check failed. [Read More]
python  set 

class as decorator

Decorator Decorator is a callable which can modify the function, method, class on runtime. Most ofthe decorators uses closure but it is possible to use class. Closure import functools def cache(f): storage = {} @functools.wraps(f) def inner(n): value = storage.get(n) if value: print("Returning value from cache") return value value = f(n) storage[n] = value return value return inner @cache def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) >>>factorial(20) 2432902008176640000 >>>factorial(20) Returning from cache 2432902008176640000 cache is a function which takes function as an argument and returns a function. [Read More]

Fluent interface in python

Fluent Interface is an implementation of API which improves readability. Example Poem('The Road Not Taken').indent(4).suffix('Robert Frost'). Fluent Interface is similar to method chaining. I was wondering how to implement this in Python.Returning self during method call seemed good idea . class Poem(object): def __init__(self, content): self.content = content def indent(self, spaces=4): self.content = " " * spaces + self.content return self def suffix(self, content): self.content += " - {}".format(content) return self def __str__(self): return self. [Read More]

Python global keyword

Python’s global keyword allows to modify the variable which is out of current scope. In [13]: bar = 1 In [14]: def foo(): ....: global bar ....: bar = 2 ....: In [15]: bar Out[15]: 1 In [16]: foo() In [17]: bar Out[17]: 2 In the above example, bar was declared before foo function. global bar refers to the bar variablewhich is outside the foo scope. After foo invocation bar value was modified inside foo. [Read More]