Monday, October 7, 2013

Python Scripting on Android

I love to code in python, and was thinking of creating some apps for Android. I figured that there are some ways which we can use to run python scripts on Android.

To run python on Android we need to install the python interpreter on Android.

1.Install sl4a (Scripting Layer for Android) on Android phone from https://code.google.com/p/android-scripting/

2.Install Python for Android from from here .
When you run this Python for android for the first time it download 3 files i.e Interpreter , Scripts , Extra's from internet , extracts and install it.

Then open the SL4A app , there you see lot of sample python scripts to run and see.
you can create your own python script and put in inside sl4a/scripts folder on SD card.

Any python application compatible with python 2.6 can be run on this.

Popular web apps like web2py , flask can also be run using this python for android interpreter.

By default the python for android provide a predefined package called android where you can access android specific features from your python script such as accessing camera , downloading files etc. 

Saturday, June 30, 2012

Parsing Data For Fun , using Google App Engine Python

Hi ,

I just wanted to know about recent movies that are running in theaters near my location ,so i just wanted to create an alert that sends fb messages and sms frequently regarding the latest movie running in the theater near me.
To create this i used Google's App engine to create a python app witch is automatically invoked periodically as a Cron job .

Getting started with app engine using this link:
https://developers.google.com/appengine/docs/python/gettingstartedpython27/

I parsed the movies name by parsing the website  entertainment.oneindia.com and cron will execute the below script to get the movie name.....




import urllib
from google.appengine.api import mail
print 'Content-Type: text/plain'
print ''
try:
    url=urllib.urlopen("http://entertainment.oneindia.in/movie_listings/Bangalore+Lakshmi+Theatre.html");
    content=url.read()
    movie_name=content[content.find('25527878</td>')+13:content.find("<td class = 'm10' >10.00 AM")]
    movie_name=movie_name[movie_name.find("<td class = 'm10' >")+19:movie_name.find("</td>")];
    print "SMS notifier by vengad:Laskshmi theater:",movie_name
    mail.send_mail(sender="Lakshmi theater Information <fantastic.next@gmail.com>",
              to="Admin <vengadanathan@facebook.com>,Way2sms <2way2sms-for-sms-alert@way2sms.com>,friend <my-friend@facebook.com>",
              subject="Alert!!"+movie_name+" running on Lakshmi Theater tavarekere",
              body="""Hi ,
               Recent movie that is running on Lakshmi Theater tavarekere is """+movie_name+"""
               Report Generated by mail-alert App By vengadanathan""")
except:
    print "Sorry I was unable to retrive movie info"


The cron that i have created is


cron:
- description: movie alert
  url: /movie-alert
  schedule: every friday 12:00



so i will be getting movie alerts on every Friday Noon i.e the day new movies get released :)..
I added my friend's email id to the list so he will be also notified..


This app was created to eat time when i was alone in weekend without friends as a time pass...

Saturday, March 10, 2012

Cloud Computing Experiments



I am quite interested towards cloud computing and tried to implement or do some projects in it , the first opportunity  which i got was to setup private cloud in my collage , so i tried using eucalyptus using Ubuntu 10.04 server edition which comes pre-installed as UEC (Ubuntu Enterprise Cloud) with ubuntu installation .

I followed

https://help.ubuntu.com/community/UEC/CDInstall

instructions to install the cloud .

By installing this i learnt a lot about cloud computing the various scenarios , pros and cons of all which i did .

Later i was asked to make use of a IBM Blade Server which is available in our premises effectively ,
for this i didn't chose UEC instead i have used PhpVirtualBox a web based front end for VirtualBox ,since it is a single bulk server and not consist of clusters i did not go for UEC, I then  installed oracle virtual box on the server and installed phpvirtualbox over lampp on that Server machine  .Then i installed Ubuntu Desktop as guest OS on that virtualbox , the instance can be launched anytime using the web interface anywhere withing the local area network covering the premises , the ubuntu instance has several scientific and num crunching  applications installed over it and it has VNC server installed over it , thus the Ubuntu Instance with a lot of application can be accesed via a web browser by any person with proper credentials with our college , so if any engineering faculty or student want to perform some computing task or need to use the computation software installed on the guest OS, he request the admin with the requirement , the admin then using the web based virtualbox interface selects the amount of RAM , No of Processors etc and lauches the instance , and the iIP address of the launched instance will be given to the corresponding faculty or student who have requested it and the faculty uses the IP address to access the instance via a Web Browser using VNC or SSH to the Instance and performs his required tasks  , after his work is over the admin suspends or power offs the running instance .

Then i have now planned of learning Open nebulae .

Sunday, January 15, 2012

Trie using c

Hi ,  I just tried out implementing trie  data structure using c , however my first attempt is not much memory efficient .

here is the code


#include<stdio.h>
#include<stdlib.h>
struct node
{
char data;
int final;
struct node *next[26];
};
struct node *insert(struct node *root,char *p)
{
if(root==NULL && *p!='\0')
{
int i;
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->data = *p;
 
for(i=0;i<26;i++)
{
temp->next[i] = NULL;
 
}
 
root = temp;
if(*(p+1)!='\0')
root->next[*p-97] = insert(root->next[*p-97],++p);
 
}
else if(*p!='\0')
{
if(root->data == *p)
root->next[*p-97] = insert(root->next[*p-97],++p);
else
root->next[*p-97] = insert(root->next[*p-97],p);
}
return root;
}
 
 
void traversal(struct node *root)
{
if(root!=NULL)
{
int i;
printf("%c",root->data);
for(i=0;i<26;i++)
{
traversal(root->next[i]);
 
}
 
}
 
}
  
int main()
{
char *p = "testing";
struct node *root = (struct node *)malloc(sizeof(struct node));
struct node *root1 = (struct node *)malloc(sizeof(struct node));
struct node *root2 = (struct node *)malloc(sizeof(struct node));
root=NULL;
root = insert(root," ");
 
root = insert(root,"apple");
root = insert(root,"ant");
root = insert(root,"body");
traversal(root);
 
} 

Saturday, December 24, 2011

Quick Sort

Hi , this is a little code for quick sort in c ,


#include<stdio.h>
int partition(int *a,int i,int j)
{
int m,t,k;
k=i;
for(m=i+1;m<=j;m++)
{
if(a[m]<=a[k])
{
t = a[k];
a[k] = a[m];
a[m] = t;
k = m;

}
}
return k;
}
void quicksort(int *a,int i,int j)
{
int k;
if(i>=j)
return ;
k = partition(a,i,j);

quicksort(a,i,k-1);
quicksort(a,k+1,j);

}
int main()
{
int i;
int a[]={1,9,4,3,5,6};
quicksort(a,0,5);
for(i=0;i<6;i++)
printf("%d ",a[i]);
return 0;
}

Sunday, December 18, 2011

Printing matrix in a spiral form

Hi all , today i just saw a problem of printing the elements of a matrix in a spiral so spent few time in solving it ..
I did it somehow , here is the solution coded in python.



a=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],[17,18,19,20]]
size = 4
n1=5
m=0
n=size
i1=0
i=0
while (i<size):
    for j in range(m,n):
       print a[i][j] ,
    i=i+1
    m=m+1
    if(i<size):
        for i1 in range(m,n1):
            print a[i1][j],
        n=n-1
        n1=n1-1
        #print "range is "+str(range(j-1,m-2,-1))
        for j in range(n-1,m-2,-1):
            print a[i1][j],
        for i1 in range(n1-1,m-1,-1):
            print a[i1][j],
       

Tuesday, November 29, 2011

Experiment with Python Parallel Computing

I just tried to try out parallel programming with python ,

Before going into the topic lets see why i started choosing python instead of c++ or c which executes faster.
The Reasons are simple , 

1.Python is a dynamic language (Less Code=>facilitates RAD).
2.Extensive libraries i.e SciPy , Matplotlib and even RAD gui tools like PyQt etc .Thus gives us a whole new world of opportunities to develop rich application that has extensive science application and use.
3.  Portability , bindings with majority of other languages likes c++ etc.

So lets start with parallel programming with python ,
When ever we though of parallel programming we think of threads which are light weight that are most commonly parallel computing modules , but there is a slight disadvantage of using threads in python
because,
1.Only one thread is run at a time by the python interpreter i.e (cpython alone) . This is called GIL(Global interpreter lock). This prevents your threads spanning across multicore or multiprocessor architecture so even if your computer has 8 cores , your multi threaded python code runs only on 1 core , so this is a serious disadvantage .

So is there is a alternate solution ?Yes

We should change our implementation from multi threading to multi processing .
There exist a library called multiprocessing in python which helps to accomplish this  ,
it is very easy which allows us to run any python function as a separate process . Thus in case instead of threads separate process will be created , this process creation process is a costly process i.e consumes more memory and time to create a  process rather than thread , so you should be careful when using multiprocessing .

Experiment 1:
Program: Calculation of sum of 1 to 100000000(Seems little big calculation)

Linear approach:
import time
print "In linear"
start = time.time()

k = range(1,100000000)
m = sum(k)
print m

print "it took", time.time() - start, "seconds."

Parallel approach using multiprocessing(1Gb RAM , Core 2 Duo):
from multiprocessing import Process, Queue
import time
def do_sum(q,m,l):
    q.put(sum(range(m,l)))

def main():
    find = 100000000
    list1=[]
    q = Queue()
    k = find / 10
    for i in range(10):
        j = i*k
        p1 = Process(target=do_sum, args=(q,j,j+k))
        p1.start()
        list1.append(p1)
    k=0
    for i in range(10):
        k = k + q.get()
    print k

if __name__=='__main__':
    print "In Parallel"
    start = time.time()
    main()
    print "it took", time.time() - start, "seconds."
BenchMarks:

O/p on Linear Approach:

In linear
4999999950000000
it took 411.590999842 seconds.
O/P on Parallel Approach:

In Parallel
4999999950000000
it took 195.187999964 seconds.
tada! Reduced the execution time by 1/2 using parallel programming .
Note :Dont use multiprocessing for smaller computation , since it may cause excess overload , so think think twice before using multiprocessing.