Monday, October 5, 2015

Creating and Parsing JSON

What is JSON

Before dealing with creating and parsing JSON, let’s understand what JSON is. JSON stands for JavaScript Object Notation.

Main Features of JSON:
  •          It is a lightweight data-interchange format.
  •          It is easy for humans to read and write.
  •          It is easy for machines to parse and generate.
  •          JSON is a text format that is completely language independent


Sample JSON element:
  
['{"key": 0}', '{"key": 1}', '{"key": 2}', '{"key": 3}', '{"key": 4}']

JSON is built on two structures:
  1. A collection of name/value pairs (Object)
  2. An ordered list of values


An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by: (colon) and the name/value pairs are separated by, (comma).

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by, (comma).

Note you can put array inside objects and objects inside array as well. By using this combination you can pass very complex data easily.

Creating JSON Message in Python

Please note: Before you start creating a python program which would create JSON, you need to have predefined JSON format that you would expect.

I need data in following format:
[              {
"square": 1,
"key": 1
}
{ ….
}
]

Sample Program:
import json

data = {}
array = []
for i in range(0,5):
      data['square'] = i*i
      data['key'] = i
      json_data = json.dumps(data)
      array.append(json_data)

print json_data
print array

Parsing JSON Message in Python

Similar to creation of JSON, while parsing also you need to know the JSON structure. This will enable you to properly design your program that will parse the JSON
Sample program:
import json
array = ['{"square": 0, "key": 0}', '{"square": 1, "key": 1}', '{"square": 4, "key": 2}', '{"square": 9, "key": 3}', '{"square": 16, "key": 4}']
print array
print "*****************now parsing the data"
for i in range(0,5):
      parsed_json =     json.loads(array[i])
      square      =     parsed_json['square']
      key         =     parsed_json['key']
      print "squar is  :%s" % square
      print "key is    :%s" % key

Friday, October 2, 2015

PostrgreSQL with Django

Installation:
Follow this link for PostgreSQL installation instructions:
http://www.techtrekking.com/2015/09/installing-python-select-latest-version.html

Getting started 
1. Open pgAdmin3.exe from C:\Program Files\PostgreSQL\9.4\bin or whichever folder you have installed it.
2. Right click on PostgreSQL 9.4 (Localhost:5432) and click Connect
3. Now go to models.py and add one model. Sample Model.py as belowfrom 

django.db import models
class Searchterms(models.Model):    
name = models.CharField(max_length=150)
def __unicode__(self):  #For Python 2, use __str__ on Python 3        
return self.name

4. Update settings.py as below:
DATABASES = {    
'default': {        
'ENGINE': 'django.db.backends.postgresql_psycopg2',        
'NAME': 'postgres',        
'USER': 'postgres',        
'PASSWORD': 'password_that_you_set_while_installing_PostgreSQL',
'HOST': 'localhost',       
'PORT': '5432',    }
}
5. Now go to command prompt and run below commands
C:\Python27\literating>python manage.py makemigrations
C:\Python27\literating>python manage.py migrate

Operations to perform:  Synchronize unmigrated apps: staticfiles, messages  
Apply all migrations: admin, contenttypes, search, auth, sessionsSynchronizing apps without migrations:  
Creating tables...    
Running deferred SQL...  
Installing custom SQL...
Running migrations:  
Rendering model states... DONE  
Applying contenttypes.0001_initial... OK  
Applying auth.0001_initial... OK  
Applying admin.0001_initial... OK  
Applying contenttypes.0002_remove_content_type_name... OK  
Applying auth.0002_alter_permission_name_max_length... OK  
Applying auth.0003_alter_user_email_max_length... OK  
Applying auth.0004_alter_user_username_opts... OK  
Applying auth.0005_alter_user_last_login_null... OK  
Applying auth.0006_require_contenttypes_0002... OK  
Applying search.0001_initial... OK  
Applying search.0002_auto_20150911_2246... OK  
Applying sessions.0001_initial... OK

6. If you run migrate command without making any changes you would see below result on command prompt:
C:\Python27\literating>python manage.py 
migrateOperations to perform:  
Synchronize unmigrated apps: staticfiles, messages  
Apply all migrations: admin, contenttypes, search, auth, sessions
Synchronizing apps without migrations:  
Creating tables...    
Running deferred SQL...  
Installing custom SQL...
Running migrations:  
No migrations to apply.

7. Further details about how to create database can be found here: http://www.postgresqltutorial.com/postgresql-create-database/ 


Accessing PostgreSQL data base through command line.
Open windows command prompt. If below commands gives you an error try opening it with administrative rights.
C:\Python27\literating>psql --username=postgres
Password for user postgres:
psql (9.4.4)
WARNING: Console code page (437) differs from Windows code page (1252)         8-bit characters might not work correctly. See psql reference         page "Notes for Windows users" for details.Type "help" for help.

postgres=# \list
List of databases   
   Name    |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
 mydb      | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres  | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres  +
           |          |          |                            |                            | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres+

           |          |          |                            |                            | postgres=CTc/postgres

(4 rows)

postgres=# \connect mydb

WARNING: Console code page (437) differs from Windows code page (1252)         8-bit characters might not work correctly. See psql reference         page "Notes for Windows users" for details.You are now connected to database "mydb" as user "postgres".
postgres=# \dt                   
List of relations Schema |            Name            | Type  |  Owner
--------+----------------------------+-------+---------- 
public | auth_group                 | table | postgres 
public | auth_group_permissions     | table | postgres 
public | auth_permission            | table | postgres 
public | auth_user                  | table | postgres 
public | auth_user_groups           | table | postgres 
public | auth_user_user_permissions | table | postgres 
public | django_admin_log           | table | postgres 
public | django_content_type        | table | postgres 
public | django_migrations          | table | postgres 
public | django_session             | table | postgres 
public | search_searchterms         | table | postgres
(11 rows) 

Running Queries on command prompt:

mydb=# SELECT datname FROM search_searchterms;

Getting list of databases 
mydb=# SELECT datname FROM pg_database
mydb-# WHERE datistemplate = false; 
datname
---------- 
postgres 
mydb
(2 rows)

Alternatively type \? To get full list of commands.Hit Ctr+c to come out of PostreSQL database.