Rest : What is Idempotent request ?

From a RESTful service standpoint, for an operation (or service call) to be idempotent, clients can make that same call repeatedly while producing the same result. In other words, making multiple identical requests has the same effect as making a single request. Note that while idempotent operations produce the same result on the server (no side effects), the response itself may not be the same (e.g. a resource’s state may change between requests).

The PUT and DELETE methods are defined to be idempotent. However, there is a caveat on DELETE. The problem with DELETE, which if successful would normally return a 200 (OK) or 204 (No Content), will often return a 404 (Not Found) on subsequent calls, unless the service is configured to “mark” resources for deletion without actually deleting them. However, when the service actually deletes the resource, the next call will not find the resource to delete it and return a 404. However, the state on the server is the same after each DELETE call, but the response is different.

GET, HEAD, OPTIONS and TRACE methods are defined as safe, meaning they are only intended for retrieving data.

Python : How to covert python dictionary into json and why we need that


You want to read or write data encoded as JSON (JavaScript Object Notation).


The json module provides an easy way to encode and decode data in JSON. The two main functions are json.dumps() and json.loads(), mirroring the interface used in other serialization libraries, such as pickle. Here is how you turn a Python data structure into JSON:

import json

data = {
   'name' : 'ACME',
   'shares' : 100,
   'price' : 542.23

json_str = json.dumps(data)

Here is how you turn a JSON-encoded string back into a Python data structure:

data = json.loads(json_str)

If you are working with files instead of strings, you can alternatively usejson.dump() and json.load() to encode and decode JSON data. For example:

# Writing JSON data
with open('data.json', 'w') as f:
     json.dump(data, f)

# Reading data back
with open('data.json', 'r') as f:
     data = json.load(f)

Dictionary and json are not same, so when dealing with web application in python we need to convert the python dictonay into json and visa vera. json.dumps and json.loads are used for the same.

# json dumps takes dict as input and return the json object as string.

# json loads takes a json object / string and returns the dict.


The sample console output , while trying to get value out on dumped dict :

>> import json
>>> a = {‘foo’: 3}
>>> json.dumps(a)
‘{“foo”: 3}’
>>> obj = json.dumps(a)
>>> print obj
{“foo”: 3}

>>> isinstance(obj,str)
>>> a[‘foo’]
>>> obj[‘foo’]
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
TypeError: string indices must be integers, not str
>>> dict = json.loads(obj)
>>> dict[‘foo’]

How to install maven on Mac ?

Use this simple command : $ brew install maven

Below is sample command output

➜ aws-java-sample git:(master) brew install maven
==> Downloading
==> Best Mirror
######################################################################## 100.0%
🍺 /usr/local/Cellar/maven/3.3.9: 94 files, 9.6M, built in 51 seconds
➜ aws-java-sample git:(master)

To validate if it is installed successfully : $  mvn -version 

Below is sample command output

➜ aws-java-sample git:(master) mvn -version
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T10:41:47-06:00)
Maven home: /usr/local/Cellar/maven/3.3.9/libexec
Java version: 1.8.0_60, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: “mac os x”, version: “10.10.5”, arch: “x86_64”, family: “mac”
➜ aws-java-sample git:(master)


MongoDB : How to simulate joins or sub query in no SQL MongoDB

No sql MongoDb by its very nature did not support joins and promotes to embed documents, however with new version 3.2 of mongoDb they have an alternative of joins which can be used in aggregation.

For a better performing design you can keep all related data in one big document (embedded) but for some security reasons if you have to keep it separate, then normalization is good idea.

Mongodb does not allow for sub queries or joins, however that can be simulated for example you employee table and instead of embedding salary details in employee document you kept in salary collection and you want to fetch,

SQL query : select salary from salary where employee_id = (select employee_id from employee where employee name like ‘premaseem’)

# Inserted 2 records in 2 different collections for join
> db.employee.insert({eid:1,name:”premaseem”})
WriteResult({ “nInserted” : 1 })
> db.salary.insert({ eid:1, salary:6000 })
WriteResult({ “nInserted” : 1 })

# Validated data in 2 tables
> db.salary.find({ eid:1})
{ “_id” : ObjectId(“56da1a5b2253b2199c53025b”), “eid” : 1, “salary” : 6000 }
> db.salary.find({ eid: db.employee.find({eid:1}) })
> db.employee.find({name : “prem” })
{ “_id” : ObjectId(“56da19d42253b2199c53025a”), “eid” : 1, “name” : “prem” }

#simulated join to get salary for employee premaseem
> db.employee.find({name : “premaseem” }).map(function(d){
var obj = db.salary.findOne({eid : d.eid });
return obj.salary;
} )
Output : 6000



Here is the python script to try out same

__author__ = 'premaseem'

from pymongo import Connection
c = Connection()
db = c.test


obj1 = {"eid":1,"name":"premaseem"}
obj2 = {"eid":2,"name":"sony"}
obj3 = {"eid":3,"name":"meera"}
bulk_employee_insert = [obj1,obj2,obj3]

# insert salary

objs1 = {"eid":1,"salary":1000}
objs2 = {"eid":2,"salary":8000}
objs3 = {"eid":3,"salary":25}
bulk_salary_insert = [objs1,objs2,objs3]


print str(db.employee.count()) + str("total employee")
print str(db.employeeSalary.count()) + str("total salary")

def find_employee() :
    emp_obj = db.employee.find_one({"eid":1})
    print emp_obj

def find_employee_with_joined_salary(eid) :
    emp_obj = db.employee.find_one({"eid":eid})
    emp_sal_obj = db.employeeSalary.find_one({"eid":eid})
    emp_obj["salary"] = emp_sal_obj["salary"]
    print emp_obj


MongoDB 3.2 has come up with $lookup which helps to join in aggregation. For reference follow links



Reference : MongoDB and the Shocking Case of the Missing JOIN ($lookup)

MongoDB doc :



What is 401(k) retirement saving account in USA

A 401(k) or 401k is a company retirement savings account that offers tax and savings benefits.

401k plans offer many benefits, but there are restrictions also.

  • Any business, whether a C Corporation, S Corporation, partnership, sole proprietorship, self-employed can establish Plan.
  • The company sets the eligibility requirements, within certain guidelines, at the time the plan is established.
  • Employer can restrict individuals with less than one year service, union members, non US citizens, part-time workers, etc., from being eligible for the plan.
  • Contributions to plan can come from voluntary employee salary reduction, from employer, or both.
  • Each individual employee can defer in 2015 up to $18,000 or 100% of compensation, whichever is less.
  • Participants age 50 and over can make additional “catch-up” contributions of $6,000 in 2015.
  • Employees are immediately 100% vested with their own salary reduction tax deferred contributions.
  • Employee withdrawals before age 59 1/2 may be subject to 10% penalty.
  • Employees who retire any time during the calendar year in which they turn 55, or later, are not subject to the 10% penalty.
  • Employers can establish a vesting schedule, within certain guidelines, for the contribution the company makes to the 401k.
  • Employers are not required nor obligated to make any contribution to the 401k, although employer may have some obligation to contribute if plan is deemed top heavy.
  • Turnkey and Internet based plans are available.
  • Excellent range of investment options available for the plan sponsor to offer within the plan.
  • The investment choices in most plans range from 8 to 25 options. The average plan has about 19.
  • 401k plans may permit “self-directed investment accounts” and company stock purchase within the plan.
  • Employee contributions to the plan are not subject to federal income taxes until a distribution from the plan is made. Any investment gains and earnings also enjoy tax deferral until distribution.
  • This type of plan can permit loans and hardship withdrawals, but is not required to do so.
  • Participants can start, stop contribution during course of year, as determined by the company.
  • The employer can receive certain tax benefits for contributions.
  • Plans are subject to top heavy and discrimination testing.
  • Typically the amount the owners and highly compensated individuals can contribute to a 401k is a function of the contributions of the other employers.
  • 401k plans can be subject to IRS Form 5500 filings.
  • Generally, the vendor selected by the plan sponsor does all accounting, participant reporting, testing, and files 5500 reports with the IRS.

Cloud : The transition to an as-a-service model

These days there is a tread of companies moving away from a product-oriented to a service-oriented business. Every thing is exposed as Service oriented component which is essential a rest api which can be consumed easily without much dependency or coupling.