28 Dec 2014

A generic method for python nested dictionary

In recent project i was working to integrate the third party library. In response to call i have a json object which holds the data of all the trips. It was a list of dictionaries, each dictionary holding the data about a trip and it was a nested dictionary.

Now situation was at different part of the code has to look out the trip dictionary for given key(s). So i wrote a generic method which will take the key and results the value of that key.  

Following is a simplification of scenario as i described above. 

Here is a python nested dictionary,
dic = {"a": 1, {"b": {"c": 2}}.

Now have generic method which operates on this dictionary, and this method can be called from different places of the program passing key of dictionary.
def get_value(dic, key):
      # return value of key

In gerenal one can get the value from dictionary like:
dic['a']   ---> 1

If i had to fetch the value of 2 from dic , it is quite simple.
dic['b']['c']  ---> 2

Since it is a nested dictionary, how get_value function is to return 2 ?
Here the reduce function comes to help:
reduce(dic.get, ['b', 'c'], dic) 

What it does is,
dic.get('b')  ---> {'c': 2},     #now dic is updated to {'c': 2}
dic.get('c')  ----> 2 

Using reduce helped me to wrote the generic method which can be operated on any python dictionary.

Thanks falsetru helped me to understand this.



22 Dec 2014

My introduction of REST

Colleague: "hey Naveen,  we have to write the API back-end for mobile developers".
Me: "OK,  i can write the API calls".
Colleague: "Make it RESTful".
Me: "RESTful .. ha ....  mmm".

Although i have worked with other REST APIs but never wrote myself.  So it was a good time to understand RESTful concepts before writing code.

REST means Representational state transfer.

History of REST
REST was developed during 1994-1995 as a part of web concepts. HTTP protocol 1.0 and HTTP1.1 lays the conceptual foundation of REST although it has significantly improved a lot over time.
It was initially referred as a "HTTP object model".

Understanding REST
REST system in which a developer has a whole system at his parcel and on applies the constraints. Implementation details of the system does not matters while developing REST interface of the system.
I would like elaborate on the specific meaning of the "Representational state transfer".
RESTful system works as a state machine. A user progress through the application by  choosing the link or passing some parameters from one state to another. User is presented with the state of the application for each action.

REST architectural constraints
Here is the very brief overview of these constraints
Client-Server : client-server model is being used.
Stateless: each request from client to server contains enough information to process itself and does not uses any stored context of server.
Cache: if response is catchable , then client can use that response for equivalent request.
Uniform Interface: identification of resources, manipulation of resource
through representations, self-descriptive messages, and hypermedia as the engine of application state.
Layered system: architecture of the system can be composed in a hierarchical style. There is complete isolation between the hierarchies.
Code on demand: server can transfer the piece of code to client. e.g. javascript code to be excuted by client.


Lot to be shared on REST, keep reading.