Monday, 12 June 2017

Network Programming


  • Network applications are everywhere. 
  • Any time you browse the Web, send an email message, or pop up an X window, you are using a network application.
  • Python provides two levels of access to network services.
  •  At a low level, you can access the basic socket support in the underlying operating system,which allows you to implement clients and servers for both connection-oriented and connection-less protocols.
  • Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.
  • understanding on most famous concept in Networking - Socket Programming.
What is Socket ?
  • Sockets are the endpoints of a bidirectional communications channel. 
  • Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.
  • Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. 
Client:


import socket               
# Import socket module

s = socket.socket( )         
# Create a socket object
host = socket.gethostname( ) 
# Get local machine name
port = 12345                
# Reserve a port for your service
s.connect((host, port))
print s.recv(1024)
s.close  

Client example2:

 import socket
clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(('localhost', 8089))
clientsocket.send('hello')

Client example3:

import socket
import sys

# Create a TCP/IP socket
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 9999)
print >>sys.stderr, 'connecting to %s port %s' % server_address
sock.connect(server_address)
try:
    
    # Send data
    message = 'This is the message.  It will be repeated.'
    print >>sys.stderr, 'sending "%s"' % message
    sock.sendall(message)

    # Look for the response
    amount_received = 0
    amount_expected = len(message)
    
    while amount_received < amount_expected:
        data = sock.recv(16)
        amount_received += len(data)
        print >>sys.stderr, 'received "%s"' % data

finally:
    print >>sys.stderr, 'closing socket'
    sock.close( )

Server :

import socket               
# Import socket module

s = socket.socket( )         
# Create a socket object
host = socket.gethostname( ) 
# Get local machine name
port = 12345                
# Reserve a port for your service.
s.bind((host, port))        
# Bind to the port

s.listen(5)                 
# Now wait for client connection.
while True:
   c, addr = s.accept()     
# Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                
# Close the connection

Server example2:

import socket

serversocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', 8089))
serversocket.listen(5) 
# become a server socket, maximum 5 connections

while True:
    connection, address = serversocket.accept()
    buf = connection.recv(64)
    if len(buf) > 0:
        print buf
        break

Server Example3:

import socket
import sys

# Create a TCP/IP socket
sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = ('localhost', 9999)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)
# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()
    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall(data)
            else:
                print >>sys.stderr, 'no more data from', client_address
                break
            
    finally:
        # Clean up the connection
        connection.close( )


Connecting to server:

example:

import socket
ip = socket.gethostbyname('www.google.com')
print ip

example:

import socket 
import sys 

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print "Socket successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)

# default port for socket
port = 80

try:
    host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:
    # this means could not resolve the host
    print "there was an error resolving the host"
    sys.exit()

# connecting to the server
s.connect((host_ip,port))

print "the socket has successfully connected to google \
on port == %s" %(host_ip)

Server:

# first of all import the socket library
import socket              

# next create a socket object
s = socket.socket()        
print "Socket successfully created"

# reserve a port on your computer in our
# case it is 12345 but it can be anything
port = 12345              

# Next bind to the port
# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests
# coming from other computers on the network
s.bind(('', port))      
print "socket binded to %s" %(port)

# put the socket into listening mode
s.listen(5)    
print "socket is listening"          

# a forever loop until we interrupt it or
# an error occurs
while True:
   # Establish connection with client.
   c, addr = s.accept()    
   print 'Got connection from', addr

   # send a thank you message to the client.
   c.send('Thank you for connecting')
   # Close the connection with the client
   c.close()              

Client:

# Import socket module
import socket              

# Create a socket object
s = socket.socket()        

# Define the port on which you want to connect
port = 12345              

# connect to the server on local computer
s.connect(('127.0.0.1', port))

# receive data from the server
print s.recv(1024)
# close the connection
s.close()                     
         

Saturday, 10 June 2017

Hard copy and Shallow copy and Deep copy

>>> import copy

>>> par_list=[1,2,3]

>>> c1_list=par_list

>>> c2_list=copy.copy(par_list)

>>> c3_list=copy.deepcopy(par_list)

>>> par_list==c1_list
True

>>> par_list is c1_list
True

>>> par_list == c2_list
True

>>> par_list is c2_list
False

>>> par_list ==c3_list
True

>>> par_list is c3_list
False

>>> par_list
[1, 2, 3]

>>> par_list[1]='siva'

>>> par_list
[1, 'siva', 3]

>>> c1_list
[1, 'siva', 3]

>>> c2_list
[1, 2, 3]

>>> c3_list
[1, 2, 3]

>>> c1_list[1]=3+4j

>>> c1_list
[1, (3+4j), 3]

>>> par_list
[1, (3+4j), 3]

>>> c2_list
[1, 2, 3]

>>> c3_list
[1, 2, 3]

>>> c2_list[1]=2.3

>>> c2_list
[1, 2.3, 3]

>>> par_list
[1, (3+4j), 3]

>>> c1_list
[1, (3+4j), 3]

>>> c3_list
[1, 2, 3]

>>> c3_list[1]='krishna'

>>> c3_list
[1, 'krishna', 3]

>>> par_list
[1, (3+4j), 3]

>>> c1_list
[1, (3+4j), 3]

>>> c2_list
[1, 2.3, 3]

#multi dimensional list

>>> p_list=[1,2,3,[4,5,[6,7]]]

>>> ch1_list=p_list

>>> ch1_list
[1, 2, 3, [4, 5, [6, 7]]]

>>> ch2_list=copy.copy(p_list)

>>> ch2_list
[1, 2, 3, [4, 5, [6, 7]]]

>>> ch3_list=copy.deepcopy(p_list)

>>> ch3_list
[1, 2, 3, [4, 5, [6, 7]]]

>>> p_list==ch1_list==ch2_list==ch3_list
True

>>> p_list is ch1_list,p_list is ch2_list,p_list is ch3_list
(True, False, False)


>>> p_list[3][2][1]
7

>>> p_list[3][2][1]='siva'

>>> p_list
[1, 2, 3, [4, 5, [6, 'siva']]]

>>> ch1_list
[1, 2, 3, [4, 5, [6, 'siva']]]

>>> ch2_list
[1, 2, 3, [4, 5, [6, 'siva']]]

>>> ch3_list
[1, 2, 3, [4, 5, [6, 7]]]
>>> 

Thursday, 8 June 2017

Python2.x v/s Python3.x


  • print statement vs print( ) function
  • raw_input( ) => input( )
  • xrange( ) vs range( )
  • Integer division

Tuesday, 6 June 2017

Data Analysis and Reporting

Data Analysis:

install pandas module

pip install pandas

after installing importing the pandas module

import pandas

Help on pandas:

import pandas
help(pandas.DataFrame)



Example:

from pandas import DataFrame, read_csv
import pandas as pd
import matplotlib.pyplot as plt
names = ['Bob','Jessica','Mary','John','Mel']
births = [968, 155, 77, 578, 973]
BabyDataSet = list(zip(names,births))
print(BabyDataSet)
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
print(df)
df.to_csv('births1880.csv',index=False,header=False)
df = pd.read_csv('births1880.csv')
print(df)
df = pd.read_csv('births1880.csv', header=None)
print(df)
df = pd.read_csv('births1880.csv', names=['Names','Births'])
print(df)
Sorted = df.sort_values(['Births'], ascending=False)
print(Sorted.head(1))
print(df['Births'].max())
print(df['Names'])

print(df['Births'])

Example2:

import pandas as pd
from numpy import random
import matplotlib.pyplot as plt
import os
import sys #only needed to determine Python version number
import matplotlib #only needed to determine Matplotlib version number
names = ['Bob','Jessica','Mary','John','Mel']
random.seed(500)
random_names = [names[random.randint(low=0,high=len(names))] for i in range(1000)]
# Print first 10 records
print(random_names[:10])
births = [random.randint(low=0,high=1000) for i in range(1000)]
print(births[:10])
BabyDataSet = list(zip(random_names,births))
print(BabyDataSet[:10])
df = pd.DataFrame(data = BabyDataSet, columns=['Names', 'Births'])
print(df[:10])
df.to_csv('births1880.txt',index=False,header=False)
df = pd.read_csv('births1880.txt')
print(df.info())
df = pd.read_csv('births1880.txt', header=None)
print(df.info())
print(df.tail())
df = pd.read_csv('births1880.txt', names=['Names','Births'])
print(df.head(5))
print(df['Names'].unique())
for x in df['Names'].unique():
    print(x)
print(df['Names'].describe())
name = df.groupby('Names')

# Apply the sum function to the groupby object
df = name.sum()
print(df)
Sorted = df.sort_values(['Births'], ascending=False)

print(Sorted.head(1))

Example3:

import pandas as pd
# Our small data set
d = {'one':[1,1,1,1,1],
     'two':[2,2,2,2,2],
     'letter':['a','a','b','b','c']}
# Create dataframe
df = pd.DataFrame(d)
print(df)
one = df.groupby('letter')
# Apply sum function
print(one.sum())
letterone = df.groupby(['letter','one']).sum()

print(letterone)

Example4:

import pandas as pd
import sys
# Create a dataframe with dates as your index
States = ['NY', 'NY', 'NY', 'NY', 'FL', 'FL', 'GA', 'GA', 'FL', 'FL'] 
data = [1.0, 2, 3, 4, 5, 6, 7, 8, 9, 10]
idx = pd.date_range('1/1/2012', periods=10, freq='MS')
df1 = pd.DataFrame(data, index=idx, columns=['Revenue'])
df1['State'] = States

# Create a second dataframe
data2 = [10.0, 10.0, 9, 9, 8, 8, 7, 7, 6, 6]
idx2 = pd.date_range('1/1/2013', periods=10, freq='MS')
df2 = pd.DataFrame(data2, index=idx2, columns=['Revenue'])
df2['State'] = States
# Combine dataframes
df = pd.concat([df1,df2])
print(df)
newdf = df.copy()

newdf['x-Mean'] = abs(newdf['Revenue'] - newdf['Revenue'].mean())
newdf['1.96*std'] = 1.96*newdf['Revenue'].std()  
newdf['Outlier'] = abs(newdf['Revenue'] - newdf['Revenue'].mean()) > 1.96*newdf['Revenue'].std()
print(newdf)



Data science/data reporting:

import matplotlib.pyplot as plt
plt.plot([1,2,3],[5,7,4])
plt.show( )

Example2:

import matplotlib.pyplot as plt

x = [1,2,3]
y = [5,7,4]

x2 = [1,2,3]
y2 = [10,14,12]

plt.plot(x, y, label='First Line')
plt.plot(x2, y2, label='Second Line')
plt.xlabel('Plot Number')
plt.ylabel('Important var')
plt.title('Interesting Graph\n Check it out')
plt.legend( )
plt.show( )



Example3:

import matplotlib.pyplot as plt
plt.bar([1,3,5,7,9],[5,2,7,8,2], label="Example one")

plt.bar([2,4,6,8,10],[8,6,2,5,6], label="Example two", color='g')
plt.legend()
plt.xlabel('bar number')
plt.ylabel('bar height')

plt.title('Epic Graph\n Another Line! Whoa')

plt.show( )

Example4:

import matplotlib.pyplot as plt

population_ages = [22,55,62,45,21,22,34,42,42,4,99,102,110,120,121,122,130,111,115,112,80,75,65,54,44,43,42,48]

bins = [0,10,20,30,40,50,60,70,80,90,100,110,120,130]

plt.hist(population_ages, bins, histtype='bar', rwidth=0.8)

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend( )
plt.show( )

Example5:

import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7,8]
y = [5,2,4,2,1,4,5,2]

plt.scatter(x,y, label='skitscat', color='k', s=25, marker="o")

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend( )
plt.show( )

Example6:

import matplotlib.pyplot as plt

days = [1,2,3,4,5]

sleeping = [7,8,6,11,7]
eating =   [2,3,4,3,2]
working =  [7,8,7,2,2]
playing =  [8,5,7,8,13]
plt.stackplot(days, sleeping,eating,working,playing, colors=['m','c','r','k'])

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')

plt.show( )

example:

import matplotlib.pyplot as plt

days = [1,2,3,4,5]

sleeping = [7,8,6,11,7]
eating =   [2,3,4,3,2]
working =  [7,8,7,2,2]
playing =  [8,5,7,8,13]


plt.plot([],[],color='m', label='Sleeping', linewidth=5)
plt.plot([],[],color='c', label='Eating', linewidth=5)
plt.plot([],[],color='r', label='Working', linewidth=5)
plt.plot([],[],color='k', label='Playing', linewidth=5)

plt.stackplot(days, sleeping,eating,working,playing, colors=['m','c','r','k'])

plt.xlabel('x')
plt.ylabel('y')
plt.title('Interesting Graph\nCheck it out')
plt.legend( )
plt.show( )

example8:

import matplotlib.pyplot as plt

slices = [7,2,2,13]
activities = ['sleeping','eating','working','playing']
cols = ['c','m','r','b']

plt.pie(slices,
        labels=activities,
        colors=cols,
        startangle=90,
        shadow= True,
        explode=(0,0.1,0,0),
        autopct='%1.1f%%')

plt.title('Interesting Graph\n Check it out')
plt.show( )




sorting techniques

Bubble sort:


def bubbleSort(alist):
    for passnum in range(len(alist)-1,0,-1):
        for i in range(passnum):
            if alist[i]>alist[i+1]:
                temp = alist[i]
                alist[i] = alist[i+1]
                alist[i+1] = temp

alist = [54,26,93,17,77,31,44,55,20]
bubbleSort(alist)
print(alist)

insertion sort:

def insertionSort(alist):
   for index in range(1,len(alist)):

     currentvalue = alist[index]
     position = index

     while position>0 and alist[position-1]>currentvalue:
         alist[position]=alist[position-1]
         position = position-1

     alist[position]=currentvalue

alist = [54,26,93,17,77,31,44,55,20]
insertionSort(alist)
print(alist)


merge sort:

def mergeSort(alist):
    print("Splitting ",alist)
    if len(alist)>1:
        mid = len(alist)//2
        lefthalf = alist[:mid]
        righthalf = alist[mid:]

        mergeSort(lefthalf)
        mergeSort(righthalf)

        i=0
        j=0
        k=0
        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:
                alist[k]=lefthalf[i]
                i=i+1
            else:
                alist[k]=righthalf[j]
                j=j+1
            k=k+1

        while i < len(lefthalf):
            alist[k]=lefthalf[i]
            i=i+1
            k=k+1

        while j < len(righthalf):
            alist[k]=righthalf[j]
            j=j+1
            k=k+1
    print("Merging ",alist)

alist = [54,26,93,17,77,31,44,55,20]
mergeSort(alist)
print(alist)


Quick sort:

def quickSort(alist):
   quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
   if first<last:

       splitpoint = partition(alist,first,last)

       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
   pivotvalue = alist[first]

   leftmark = first+1
   rightmark = last

   done = False
   while not done:

       while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1

       while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
           rightmark = rightmark -1

       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp

   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp


   return rightmark

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)


selection sort:

def selectionSort(alist):
   for fillslot in range(len(alist)-1,0,-1):
       positionOfMax=0
       for location in range(1,fillslot+1):
           if alist[location]>alist[positionOfMax]:
               positionOfMax = location

       temp = alist[fillslot]
       alist[fillslot] = alist[positionOfMax]
       alist[positionOfMax] = temp

alist = [54,26,93,17,77,31,44,55,20]
selectionSort(alist)
print(alist)


Shell sort:

def shellSort(alist):
    sublistcount = len(alist)//2
    while sublistcount > 0:

      for startposition in range(sublistcount):
        gapInsertionSort(alist,startposition,sublistcount)

      print("After increments of size",sublistcount,
                                   "The list is",alist)

      sublistcount = sublistcount // 2

def gapInsertionSort(alist,start,gap):
    for i in range(start+gap,len(alist),gap):

        currentvalue = alist[i]
        position = i

        while position>=gap and alist[position-gap]>currentvalue:
            alist[position]=alist[position-gap]
            position = position-gap

        alist[position]=currentvalue

alist = [54,26,93,17,77,31,44,55,20]
shellSort(alist)
print(alist)