Sunday, December 29, 2013

Deploy Django app in heroku

Here is how a Django app deploy to heroku.Django app should be success in your local host.Then read carefully,it is something confusing.But leave it,i will help you..


Your application should be like this..

project
-------project
--------------__init.py
--------------setings.py
--------------urls.py
--------------wsgi.py
-------manage.py


This setup will get using commands

django-admin.py startproject project

then,

cd project
django-admin.py startapp paint  #here i am adding a paint app

Care that we are in the main project folder,

Then Whole project should be like this..


project
-------project
--------------__init.py
--------------setings.py
--------------urls.py
--------------wsgi.py
-------paint
--------------__init__.py
--------------models.py
--------------tests.py
--------------views.py
-------manage.py
-------requirements.txt
-------Procfile
-------venv


Don't be upset,we should cover how these gets...

In settings.py we have to add some codes..

import urlparse
import dj_database_url
DATABASES['default'] =  dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['*']
# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)


Add this line also

here = lambda * x: os.path.join(os.path.abspath(os.path.dirname(__file__)), *x)
PROJECT_ROOT = here("..")
root = lambda * x: os.path.join(os.path.abspath(PROJECT_ROOT), *x)
TEMPLATE_DIRS = (
    root('templates'),
)

Delete already existing template directory in your settings.py.But dont edit already existing database

your wsgi.py shoulbe

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())

Then run command

Heroku login     #to enter username and password
heroku create websitename   #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev
heroku config:set HEROKU=1
heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using
virtualenv venv --distribute   #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt

requirements.txt should be like this

Django==1.6
argparse==1.2.1
distribute==0.6.34
dj-database-url==0.2.2
dj-static==0.0.5
gunicorn==18.0
psycopg2==2.5.1
static==0.4
wsgiref==0.1.2

then,

pip install -r requirements.txt
git init

Create a Procfile using vi Procfile ('P' is capital) and add following line in it

web: gunicorn project.wsgi

Also add a .gitignore file using vi .gitignore and add following lines in it

venv
*.pyc
*.py~

Then,

git add .
git commit -m "hosting"
git push heroku master 

After launching it,

heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using

heroku config | grep HEROKU_POSTGRESQL

heroku run python manage.py syncdb

Then,'

heroku ps:scale web=1
heroku restart --app websitename

heroku open

Then you can see it is working......!!!!!!

heroku logs #for further info

If u have any doubts just clone my simple paint app using,

heroku git:clone -a latheefdjangopaint

and verify the codes and structure of Django app

Deploy Flask app in Heroku

In a flask app,database let us consider postgresql

Now we have to deploy Flask app in heroku which is success in local host.First,

In our app,database connection is  like this

con = psycopg2.connect(database='firstdb') 
cur = con.cursor()

Instead of this,use....

urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
con=psycopg2.connect(database=url.path[1:],user=url.username,password=url.password,host=url.hostname,port=url.port)

also import some modules

import os
import psycopg2
import urlparse

wherever database to connect,we should connect like above in each function..

Next in routes.py or the routing python program.our app includes

app.run(debug=True)

Instead of this...

if __name__ == "__main__":
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)


Then also add

app.config.from_object(__name__)

in the below of app = Flask(__name__)..so it should be like this..

app = Flask(__name__)
app.config.from_object(__name__)


Then,

Create account on heroku.com
open terminal and enter

Heroku login     #to enter username and password
cd appfloder      #appfolder includes route.py,templates etc...
heroku create websitename   #should like www.websitename.herokuapp.com
heroku addons:add heroku-postgresql:dev   #creating database
heroku config:set HEROKU=1
virtualenv venv --distribute   #virtual environment for flask
source venv/bin/activate
pip freeze > requirements.txt

requirements.txt should be like this

Flask==0.10.1
Jinja2==2.7.1
MarkupSafe==0.18
Werkzeug==0.9.4
argparse==1.2.1
distribute==0.6.34
gunicorn==18.0
itsdangerous==0.23
psycopg2==2.5.1
wsgiref==0.1.2

then,

pip install -r requirements.txt
git init

Create a Procfile using vi Procfile ('P' is capital) and add following line in it

web: python routeprogam.py #here route program is yours

Also add a .gitignore file using vi .gitignore and add following 3 lines in it.This is for avoiding the virtual env files to deploy.

venv
*.pyc
*.py~

Then,


git add .
git commit -m "hosting"
git push heroku master 

After launching it,

heroku pg:promote HEROKU_POSTGRESQL_PINK   #PINK is here only your's can be find using

heroku config | grep HEROKU_POSTGRESQL

Then,'

heroku ps:scale web=1
heroku restart --app websitename

heroku open

Then you can see it is working......!!!!!!

heroku logs #for further info

If u have any doubts just clone my simple microblog app using,

heroku git:clone -a latheefmicroblog

and verify the codes and structure of Flask app

Saturday, December 28, 2013

Unit testing in Python

Unit testing may be define as testing each units of a program.Unit testing in python has a separate testing python program for original program.

Let us explain with an example:
i want to create a program which accepts a number and check whether odd or even.

In python unit testing testing,we must assume the original program to predict requirements,inputs,outputs,errors etc..

Let us assume original program is oddeven.py and test program is testoddeven.py.

First we must know the requirements,inputs,outputs,errors..
Input may be int,string,float etc..
So errors may arise when input is string,float,zero.

So lets start our testoddeven.py

import oddeven #our original program
import unittest #python inbuilt module

class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))

Here some input and expected output is defined in a class

import oddeven #our original program
import unittest #python inbuilt module

class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)



Here checking whether the output equals to output of our oddeven.py Now we have to design errors or assume errors.Here possible errors are
zero,float value,string.Now i am defining expected errors.

import oddeven
import unittest

class KnownValues(unittest.TestCase):
knownValues=((1,'odd'),(2,'even'),(11,'odd'))
 
           def testoddeven(self):
for i,j in self.knownValues:
result=oddeven.testoe(i)
self.assertEqual(j,result)
class OddEvenBadInput(unittest.TestCase):
def testZero(self):
self.assertRaises(oddeven.ZeroError, oddeven.testoe,0)
def testNonInteger(self):
self.assertRaises(oddeven.NonIntegerError, oddeven.testoe,6.5)
def testString(self):
self.assertRaises(oddeven.StringError, oddeven.testoe,"abc")

if __name__ == "__main__":
unittest.main() 


Then start coding oddeven.py Then define the errors in original program sa below.

class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass


Then define each line and test each time using python testoddeven.py Rewrite the program until 'ok' gets...




class ZeroError(Exception):pass
class NonIntegerError(Exception):pass
class StringError(Exception):pass

def testoe(n):
if type(n)==int:
if n==0:raise ZeroError,"zero..."
elif n%2==0:
return "even"
else:
return "odd"
elif type(n)==str:
raise StringError,"string...!!!!"
else: raise NonIntegerError,"Non int...."

unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. Unit tests are created by programmers or occasionally by white box testers during the development process.One can view a unit as the smallest testable part of an application.Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended.

To view the codes.Click here

Wednesday, December 25, 2013

Python flask microblog application using postgresql

   
As in the previous example,there used sqlite3.Here database is postgresql

    Blog app act as a small scale blogspot account which provides options such as posting commenting etc.Blog app uses postgresql database to store the post and comments. psycopg2 module provided by python is used for it.Posting can be done only by the admin. Admin has to log in to post to avoid fake users.

documentation on flask


These kind of things takes a lot of work to code in normal networking in python.  Flask makes everything easier.Also flask helps the debugging much easier. The blog app is my first work in flask and it didn't give me much headache.
Let me show you some basic items in flask.

@app.route('url')
def func_name():
.....

Here first line tells us 'Where to go'.For example @app.route('/') means home page, @app.route('/post') means post link(ie home/post/).

Under that we define the function to tell the app 'how to behave' when these links are provided. We can render an html page, read posts from forum, save data etc.

In short if have a 'not bad' knowledge in flask then web based apps can be easily made. Flask is a great web framework provided by python where rookies can have a shot too.

Postgresql setup:

    sudo apt-get install libpq-dev python-dev
    sudo apt-get install postgresql
    sudo -u postgres createuser --superuser $USER
    sudo -u postgres psql 
    sudo -u postgres createdb $USER

Now you can create database in terminal using command psql

    create database databasename;
    \c databasename;   #to connect database
    create table posts(id integer,title TEXT,text INT);
    \d   #show tables
    \d tablename #to show corresponding table
    then use select query and more...

Connection:

To connect postgresql,we must install psycopg2

    sudo apt-get install python-psycopg2
 
In a program connection done through this lines.for example:

import psycopg2
con = psycopg2.connect(database='firstdb')
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS paintstore1")
cur.execute("CREATE TABLE paintstore1(id serial,title text,imagedata text)")
con.commit()
con.close()

To view the codes: Click here
To view the site i deployed to heroku,click here

Monday, December 16, 2013

Paint app using Django and sqlite3 as databae

Django is a web framework designed to help us build complex web applications simply and quickly. It’s written in the Python programming language.Django takes it name from the early jazz guitarist Django Reinhardt, a gypsy savant who managed to play dazzling and electrifying runs on his instrument even though two of the fingers on his left hand were paralyzed in an accident when he was young.Thus, it’s a fitting name for the framework: Django can do some very complex things with less code and a simpler execution than you’d expect. It doesn’t take a heavy hand to build with Django. The framework does the repetitive work for you, allowing you to get a working website up quickly and easily.

Django setup:

After install django,make a directory to store our project.

Open the terminal and enter the following commands:

mkdir djando
cd django
django-admin.py startproject mysite

this produces:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

This is the general structure of django.Here we didnt add any apps.

First we have to setup the database.Open settings.py and do as following.Here i am using sqlite3 database.


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': 'test.db',
    }
}

Then enter the command:

python manage.py syncdb   #Creates database and corresponding tables for django

Now its time to add our paint application.so

python manage.py startapp paint

this produces following directory like

├── manage.py
├── paint
│   ├── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
└── mysite
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Here we are...

Here model view controller(MVC) is the routing procedure or model template view.
In models.py we have to setup database model
In views.py we have to setup routing functions or rendering functions
In urls.py corresponding urls are determined

Then we have to add this app in settings.py

INSTALLED_APPS = (
#    'django.contrib.admin',
#    'django.contrib.auth',
#    'django.contrib.contenttypes',
#    'django.contrib.sessions',
#   'django.contrib.messages',
    'django.contrib.staticfiles',
    'paint',
)


In models.py

from django.db import models

class Pic(models.Model):
        name=models.CharField(max_length=100)
        data=models.CharField(max_length=10000000000)

Then we have to create template directory and static directory inside.
So this will like

├── manage.py
├── paint
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   ├── wsgi.py
├── templates
└── static

Then htmls routing functions are done.To view the codes click here

To view the site i deployed on heroku.Click here

Paint app using Flask and postgresql

Paint application using postgresql i defined earlier is implementing in Flask.Here database is postgresql.

The paint app that was created using HTML5 canvas and javascript is modified with a save option. When each object is drawn all the details about that object is saved as a JSON object. Then it is saved to the postgreSql database.

For example when a Rectangle object is drawn the details such as width, height starting points etc are saved as a JSON object.Then it is added to a list


After everything is drawn the list will contain all the info of drawn objects as a list in the order of appearences.

Then it is saved to the database.

Example: Saving object info using JSON object.

Here on each drawing function JSONDATA.push is used.for example:

jsondata.push({"Type":"pencil", "X0":sX,"Y0":sY,"X1":mX,"Y1":mY,"width":c.lineWidth,"color":c.strokeStyle})data=JSON.stringify(jsondata)

Then this 'data' is posted to python program and insert to database using sql query.

At the time of reloading a selected picture these details are retrieved from database and redrawn in the canvas.
The following code shows how to redraw a rectangle in the canvas. The stringified data is parsed before it is drawn on canvas.

l=JSON.parse(data);

Then check the type in list 'l' and redraw.For example:

if(l[i]['Type']=='line'){
context.beginPath()
x0=l[i]['X0']
y0=l[i]['Y0']
x1=l[i]['X1']
y1=l[i]['Y1']
context.moveTo(x0,y0)
context.lineTo(x1,y1)
context.lineWidth=l[i]['width'];
context.strokeStyle=l[i]['color'];
context.stroke()
context.closePath()
}
context.beginPath()
x0=l[i]['X0']
y0=l[i]['Y0']
x1=l[i]['X1']
y1=l[i]['Y1']
context.moveTo(x0,y0)
context.lineTo(x1,y1)
context.lineWidth=l[i]['width'];
context.strokeStyle=l[i]['color'];
context.stroke()
context.closePath()
}

jsondata.push({"Type":"pencil", "X0":sX,"Y0":sY,"X1":mX,"Y1":mY,"width":c.lineWidth,"color":c.strokeStyle})data=JSON.stringify(jsondata)

By using flask the whole paint app became efficient ,interactive and useful

To view this code : Click here

To view the paint app which i deployed in heroku click here

Friday, November 1, 2013

Python-Flask microblog Application Using Sqlite3

  Here i am defining how to create a Python-Flask application using database Sqlite3.First we should know what is Flask?What is the use of Flask?
   Flask is a framework in python for Web development.It has specific structure.

Folders should be like this structure.

Flaskapp /
                / templates /        # contains html files
                / statics /             # contains js,images,css
                / static / css
                / .py files            # main files for application routing and database management

Flask can be install using:
       sudo pip install Flask
   
     In a python-Flask application, a routing python program is needed which determines the url routing.We can define sql query inside this or outside as a single python file.

     To start the routing program,let  route.py. First an instance of Flask is to be created inside route.py
app=Flask(__name__)
Then defines the routing functions.Here i am showing how does this works:

Let me define how the html and python program interacts:

we want to do a python function or change url when a button in html press.This done by

html
<a href='/post'>button name</a>
Here button name will see as a link,but it can modify as button using css

route.py

@app.route('/post')
def func():
      -----------
      -----------
Here it is...when you click the link that function works.

Accessing Database:

         We can add separate python file for creating database.

let datadb.py

To create sqlite3 database 
   
import sqlite3 as lite
import sys
con=lite.connect("blog.db")
with con:
        cur=con.cursor()
        cur.execute("DROP TABLE IF EXISTS posts")
        cur.execute("CREATE TABLE posts(id integer primary key autoincrement,title TEXT,text INT)")

   Here created blog.db (database name),created  table posts
I think this much knowledge is needed for accessing database.you can refer the codes through the link i am added in last.


To view program code: Click here


Monday, September 30, 2013

Simple Paint Application using HTML5 Canvas


         Paint application is used to draw curves,pictures,import images,etc..Here is a simple Paint App i designed for beginners.This done using HTML and javaScript.Canvas(drawing area) created using Html tags.That is Picturising the window is done by Html tags and the functions done using javaScript.


         JavaScript insert elements from documents using:
                  document.getElementById(id)

   This App includes mouse events.For eg:

   My Paint App View:


     Canvas and buttons are designrd using HTML where the functions are done with JavaSsript.

MouseEvents:


 function events(){b.onmousedown=md;b.onmouseup=mu;b.onmousemove=mv;}  
 function md(e){img=c.getImageData(0,0,b.width,b.height);sX=e.x;sY=e.y;pulse="on";}  
 function mu(e1){eX=e1.x;eY=e1.y;pulse="off";}  
 function mv(e2){mX=e2.x;mY=e2.y;if (pulse=="on"){c.putImageData(img,0,0);draw();}}  

LineTool:


 c.beginPath();c.moveTo(sX,sY);c.lineTo(mX,mY);c.stroke();c.closePath();  

RectangleTool:


 c.strokeRect(sX,sY,mX-sX,mY-sY);c.stroke();if(f==1){c.fillRect(sX,sY,mX-sX,mY-sY);  

CircleTool:


 c.beginPath();c.arc(Math.abs(mX+sX)/2,Math.abs(mY+sY)/2,Math.sqrt(Math.pow(mX-sX,2)+Math.pow(mY-sY,2))/2, 0, Math.PI*2);  
 c.stroke();if(f==1){c.fill();}c.closePath();  

Penciltool:

 c.moveTo(sX,sY);c.lineTo(mX,mY);c.stroke();sX=mX;sY=mY;  

In this way i set function fill() ,Stroke() etc..
For full program: Click here

Thursday, September 26, 2013

Lisp Interpreter in Python and JavaScript

    LISP is a family of computer programming languages with a long history, fully parenthesized Polish prefix notation.Lisp is specified in 1958.Lisp is the second-oldest high-level programming language in widespread use today; only Fortran is older (by one year). Like Fortran,The name LISP derives from "LISt Processing". Linked lists are one of Lisp language's major data structures, and Lisp source code is itself made up of lists.   LISP expressions are composed of forms. The most common LISP form is function application. LISP represents a function call f(x) as (f x). For example, cos(0) is written as (cos 0).LISP expressions are case-insensitive. It makes no difference whether we type (cos 0) or (COS 0).Some functions, like "+" and "*", could take an arbitrary number of arguments. In our example, "*" took three arguments. It could as well take 2 arguments, as in "(* 2 3)", or 4 arguments, as in "(* 2 3 4 5)".Numeric values like 4 and 6 are called self-evaluating forms: they evaluate to themselves.

Defining a function in LISP : 


 (define (add a b)
  (+ a b))  
(add 3 4)  #4
  Lisp interpreter in Python : LISP.py .(for more details click here)
  Lisp interpreter in JavaScript : LISP.js

Monday, September 23, 2013

Huffman Compression

         A data compression technique which varies the length of the encoded symbol in proportion to its information content,that is the more often a symbol or token is used, the shorter the binary string used to represent it in the compressed stream.Huffman codes can be properly decoded because they obey the prefix property, which means that no code can be a prefix of another code, and so the complete set of codes can be represented as a binary tree, known as a Huffman tree.
        The least length codes correspond to the most often occurring symbols allowing to perform compression. For example, the sequence “aaaaaaaa” contains only one symbol ‘a' that corresponds to the Huffman code with size 1 bit; after encoding the output sequence will have size 1 byte - thus compression ratio will be 8.Because generally a character uses 8 bits.Huffman encoding is based on the principle that letters that appear more frequently should have a smaller code than the ones that are used less often. So, in the English language, vowels would be used more than the letter 'z', and would get shorter codes.

Huffman program  in python Huffman.py
Huffman program in javaScript Huffman.js


For example: Huffman Tree for String "malayalam"

                                                  

Generating Huffman Tree is also simple.We can just check in Huffman Tree Generation

       So Generally The idea behind Huffman coding is to find a way to compress the storage of data using variable length codes. Our standard model of storing data uses fixed length codes. For example, each character in a text file is stored using 8 bits. There are certain advantages to this system. When reading a file, we know to ALWAYS read 8 bits at a time to read a single character. But as you might imagine, this coding scheme is inefficient. The reason for this is that some characters are more frequently used than other characters. Let's say that the character 'e' is used 10 times more frequently than the character 'q'. It would then be advantageous for us to use a 7 bit code for e and a 9 bit code for q instead because that could shorten our overall message length.
Huffman coding finds the optimal way to take advantage of varying character frequencies in a particular file. On average, using Huffman coding on standard files can shrink them anywhere from 10% to 30% depending to the character distribution.

Wednesday, September 4, 2013

Word count programs in Python



text file a.txt:     
      India is my country and all Indians are my brothers and sisters.I love my country and I am proud of its rich and varied heritage.I shall always strive to be worthy of it.I shall give my parents, teachers and all elders respect and treat everyone with courtesy.To my country and my people, I pledge my devotion. In their well-being and prosperity alone lies my happiness.
 >>>from collections import Counter  
 >>>import re  
 >>>import os  
 >>>list_word=re.findall("\w+",open('a.txt').read().lower())  
 >>>for i in Counter(list_word):
 . . .  print i,'-',Counter(list_word)[ i ]  

#
and - 8
all - 2
everyone - 1
love - 1
pledge - 1
give - 1
being - 1
is - 1
india - 1
it - 1
alone - 1
are - 1
in - 1
respect - 1
its - 1
happiness - 1
prosperity - 1
varied - 1
their - 1
indians - 1
to - 2
parents - 1
treat - 1
rich - 1
heritage - 1
be - 1
brothers - 1
elders - 1
shall - 2
am - 1
of - 2
courtesy - 1
worthy - 1
sisters - 1
with - 1
people - 1
strive - 1
lies - 1
i - 5
country - 3
proud - 1
well - 1
teachers - 1
always - 1
my - 8
devotion - 1

another program for word count:
 >>>d={}  
 >>> list_word=re.findall("\w+",open('a.txt').read().lower())  
 >>>for i in list_word:  
 . . .   d[i]=d.get(i,0)+1  
 >>>d  
# dict of word as above

another program for word count:
 >>> list_word=re.findall("\w+",open('a.txt').read().lower())  
 >>> for i in list(set(list_word)):  
 . . .  print i,list_word.count(i)  


Python programming language

Python programming language is more powerful and is often comparable to Perl,Ruby or Java.
Readability of syntax is a distinguishing factor of Python.Python is available for all major operating systems: Windows, Linux/Unix, OS/2, Mac.Python is friendly and easy to learn.

          Comparing to Java,Python programs are 3-5 times smaller than java prog's.Python is dynamic typed language.Python programmers don't need to waste time in declaring variable types as in java.

          List is one of the Data Structure used in Python.Main feature is LIST COMPREHENSION.
Python supports a concept called "list comprehensions". It can be used to construct lists in a very natural, easy way, like a mathematician is used to do.


for eg:

            >>> a=[1,2,3,4]  
            # want to square each item in list a.  
            >>> b=[]  
            >>> for i in a:  
                 b.append(i*i)  
            >>> b  
                [1,4,9,16]  
using List Comprehension: 
            >>> b=[i*i for i in a]  
            >>> b  
                [1,4,9,16]