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.

Tuesday, September 22, 2015

Installing Python, pip, Django and Bootstrap



Installing Python
Select the latest version of Python from this link. Here you need to make a choice if you want to install Python 2.x or 3.x. Once you decide which Python to use, select the latest version of Python 2 or Python 3.
(If your machine is 64 bit, make sure you download 64 bit version.)

Difference between Python2 and Python 3
From python official website: Python 2.x is legacy, Python 3.x is the present and future of the language. 
You can stick with Python 2.x. Simple rational is that one of the best reason for choosing python is availability of huge variety of packages and tools. Many tools and packages are yet to make transition to Python 3.x. Also Python 3.x doesn’t have backward compatibility so you might face issue with some of the package and some of these packages might not work with python 3.x. You can make transition to 3.x once you have fairly comfortable with python.

Once you download .msi file, install Python in the same manner you install any other application. Python will create a new directory on c drive named C:\Python27
To work seamlessly with python make sure PATH variable is updated correctly.
Go to Control Panel\System and Security\System>  Select Advanced System Setting>Environment Variable. You would see path variable. Click edit. 
Make sure you have following libraries added there. If these are not present add below libraries there.
C:\Python27;C:\Python27\Scripts;

You can also go to this place by right clicking My Computer and select properties> Environment Variable

Installing pip
If you are installing latest version of Python, It already have pip in it. I have Python 2.7.10 and it already had it.

If due to any reason you want to install previous version of Python which doesn’t come with pip pre-installed you can install pip by following below steps

1. Go to https://pip.pypa.io/en/latest/installing.html
2. Download get-pip.py. 
3. Type below command on command prompt
python get-pip.py
Check current version of pip using following command
pip list
If pip that you have installed is of older version you can upgrade it by 'pip install --upgrade pip' command.

Installing virtualenv
Virtualenv is a tool to create isolated Python environments. Why do you need it? Well sometime you need to work with different version of different packages, lets say you are using django 1.8 for current project but you have specific requirements to use Django 1.6 for some other project. How do you manage that ? You create virtual python environment for these and install whatever version you want on that. This way you could work on both the projects from same machine.

Thanks to pip that you have installed before, installing any package will be breeze. Just type below command on command shell and you have virtualenv
pip install virtualenv
For further details about virtualenv check out this link or this link

Below installation are required only for web applications. If your motive is anything other than web development or you are simply trying to learn Python, you don’t need these installed.

Installing Django
Django is one of the most widely used framework for web development. Most of the startups are choosing Django with Python or Ruby with rails. Well, more on that sometime later. To install Django just type below command.
pip install Django
If you want to install any specific version type below command
pip install Django=1.8.0

Installing Boostrap

Boostrap is most widely use and almost standard for front end development. Frankly speaking you don’t have to install it, you just have to download it and save it in required folder. Which folder? The folder from which you would refer CSS files. More on it sometime later.

Installing PostgreSQL

I chose PostgreSQL over MySQL for following reasons:

  1. Development community on general mentioned that PostgreSQL offers some additional features which are not present on MySQL. Whether I need those features or not is different issue altogether but when I need them I don’t want to stare at database migration.
  2. PostgreSQL is support on Openshift as well as heroku. My plan is to host my web app on Openshift which supports both but just in case I want to migrate to heroku, I don’t want to do database migration work.
  3. (This may not be true but my assumption) since MySQL is now with Oracle, I suspect there would certainly keep adding new feature to Oracle and delay these on MySQL…. Well that’s just my guess.

Follow these simple steps:


  1. Download PostgreSQL from this link: http://www.enterprisedb.com/products-services-training/pgdownload#windows
  2. Type this command on command prompt pip install psycopg2
  3. If you are facing issue with installation with pip command check this link  to download and manually install.
  4. Open pgAdmin3.exe from C:\Program Files\PostgreSQL\9.4\bin or whichever folder you have installed it.