RC week 0000

The long awaited Recurse Center debut day, 26th Sep 2016 kick started with a welcome note by Nicholas Bergson-Shilcock and David Albert ; decorated by other events and activities to get to know the batchmates; the culture of RC and ended with closing note by Sonali Sridhar. At the end of the day, I had decided to build a BitTorrent client as a first project. I was at the crossroad to choose Python or Rust or Go for the project. [Read More]

HTTP Exception as control flow

As per Wikipedia , Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the flow of program execution. In Python errors like SyntaxError, ZeroDivisionError are exceptions.Exception paves the way to alter the normal execution path. While working with API, a web request goes through the following process,authentication, authorization, input validation, business logic and finally, the response is given out. [Read More]

State machine in DB model

A state machine is an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. While using the database, individual records should be in allowed states. The database or application stores rules for the states. There are many ways to design the database schema to achieve this. [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]

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]