Python : Converting unicode logs of dict into valid json format

While debugging, programmer goes through logs to grab request payload. The issue is payload is wrapped with u for unicode, ‘ and True and False and None which are python standards however fails to pass json validation.

Hence to use them, we need to convert the unicoded python dict into valid json to do crul call, for the same here is the simple trick :

{u’a’: u’aValue’, u’b’: u’bValue’, u’c’: u’cValue’} is a dictionary which you are calling as unicode json. Now, in your language if you want a regular json from this then just do something like this:

x={u'a': u'aValue', u'b': u'bValue', u'c': u'cValue'}
print y

The output will be {“a”: “aValue”, “c”: “cValue”, “b”: “bValue”}

This will remove all u, convert False into false, True into true, None into null and convert ‘ into ”   🙂

MongoDb : Case in-sensative search using pymongo

As we all know Json is case sensitive and so is MongoDB, hence searching same name which got save in different case might not return any result. The solution is to do case in-sensative search.

Below is the example of pymongo

db.users.find_one({'name': re.compile(username, re.IGNORECASE)})

Python : List of Python Tutorials

If you are looking for a single list of python tutorial, then you landed on a very good blog post.

Learn Python online – A curated list of courses on Python

Here is the link :

This will allow you to try our different python courses available online.

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’]

Python : What is None in python ;-)

Python’s None is Null of of Java, php, javascript of other programing language.

The concept of a null keyword is that it gives a variable a neutral, or “null” behaviour.

Python’s null Equivalent: None

The equivalent of the null keyword in Python is None. It was designed this way for two reasons:

  • Many would argue that the word “null” is somewhat esoteric. It’s not exactly the most friendliest word to programming novices. Also, “None” refers exactly to the intended functionality – it is nothing, and has no behaviour.
  • In most object-oriented languages, the naming of objects tend to use camel-case syntax. eg. ThisIsMyObject. As you’ll see soon, Python’s None type is an object, and behaves as one.

Checking if a Variable is None

There are two ways to check if a variable is None. One way can be performed by using the is keyword. Another is using the == syntax. Both comparison methods are different, and you’ll see why later:

null_variable is None
not_null_variable is not None
null_variable is None
not_null_variable is not None

Python : How to set a global variable in a function

x = 1 # make a global module variable

def f():
      print x # try to print the global
      for j in range(100):
           if q > 3:

Any variable assigned in a function is local to that function, unless it is specifically declared global. Since a value is bound to x as the last statement of the function body, the compiler assumes that x is local. Consequently the “print x” statement attempts to print an uninitialized local variable and will trigger aUnboundLocalError (or in earlier Python versions, a NameError).

The solution is to insert an explicit global declaration at the start of the function:

def f():
      global x
      print x # try to print the global
      for j in range(100):
           if q > 3:

In this case, all references to x are interpreted as references to the x from the module namespace.

Note that the global declarations must be placed at the beginning of the function, and that it affects all uses of the variable inside the function.

Python : SQLAlchemy ORM Library for python

SQLAlchemy  is a Python Library created by Mike Bayer to provide a high-level, Pythonic (idiomatically Python) interface to relational databases such as Oracle, DB2, MySQL, PostgreSQL, and SQLite. SQLAlchemy attempts to be unobtrusive to your Python code, allowing you to map plain old Python objects (POPOs) to database tables without substantially changing your existing Python code. SQLAlchemy includes a database server-independent SQL expression language and an object-relational mapper (ORM) that lets you use SQL to persist your application objects automatically.

Doc link :

NOTE : It is similar to what hibernate framework does in java.

SQLAlchemy  provides the following functionality:

  1. It maps relational databases into objects
  2. It manages an applications database connections
  3. It can create/alter a database layout if it is allowed to

The most powerful feature of SQLAlchemy is the first point: given a table description, it maps the data in tables into classes of objects, where each instance of an object is a row in the table, and can be worked with like a class or structure in code.

Example: Given a table called “Users”, with a FirstName and LastName column. Once the columns are described in the Python code, to add a row to the users table might look like this:

joebruin = User()
joebruin.FirstName = “Joe”
joebruin.LastName = “Bruin”