Monday, 25 June 2012

JAVA - Variable

Variables:

mostly 2 types
1) instance variable
2) class variable


Instance variable :

    Variables of each instance of a class varies . So same variable names of different instances(object) of same class , point to different memory location . So changing of its value by one object doesn't not get reflected in the variable of other object .

Class variables :

    Here  same variable names of different instances(object) of same class , point to same memory location . So a change made in the variable by one object , gets reflected in other objects variable , as these variables point to same memory location .

Next question that comes is how compiler differentiates between instance variables and class variables . Ya , we have to do that . We use the keyword "static" before variable name to denote it as class variables . Others are instance variables .



How to access these variables ?

Instance variable can be accessed by using the objectname(or so called instancename).instancevariable ;
Classvariable can be accessed by using the classname.classvariable ;


Application of the concept "static" to create constants :



The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change.
For example, the following variable declaration defines a constant named PI, whose value is an approximation of pi (the ratio of the circumference of a circle to its diameter):
static final double PI = 3.141592653589793;
Constants defined in this way cannot be reassigned, and it is a compile-time error if your program tries to do so. By convention, the names of constant values are spelled in uppercase letters. If the name is composed of more than one word, the words are separated by an underscore (_).

 
  

Tuesday, 29 May 2012

Iterators


1)   for x in [1,4,5,10]:
...
print x


2) prices = { 'GOOG' : 490.10,
...
'AAPL' : 145.23,
...
'YHOO' : 21.71 }
...
>>> for key in prices:
...
print key


3) s = "Yow!"
>>> for c in s:
...
print c


In all the above for iteration , what is happening ?



items = [1, 4, 5]
>>> it = iter(items)
>>> it.next()
1
>>> it.next()
4
>>> it.next()
5
>>> it.next()
 
this is the underlying steps which are happening .

what does iter(items) do ..... it just returns iterator object ,,, which has inbuilt methods like next , stopiteration , etc ... after printing once , if the list is not empty , next () is exectued , so this process goes on and on , till list is empty , when the stopiteration is executed . 








Thursday, 24 May 2012

Python Utilities : 

We can do a number of things that we do in bash in unix , even in python interpreter .
one thing we have to do in 

import os 

now to see what are all the operations we can do  

EX_CANTCREAT
EX_CONFIG
EX_DATAERR
EX_IOERR
EX_NOHOST
EX_NOINPUT
EX_NOPERM
EX_NOUSER
EX_OK
EX_OSERR
EX_OSFILE
EX_PROTOCOL
EX_SOFTWARE
EX_TEMPFAIL
EX_UNAVAILABLE
EX_USAGE
F_OK
NGROUPS_MAX
O_APPEND
O_ASYNC
O_CREAT
O_DIRECT
O_DIRECTORY
O_DSYNC
O_EXCL
O_LARGEFILE
O_NDELAY
O_NOATIME
O_NOCTTY
O_NOFOLLOW
O_NONBLOCK
O_RDONLY
O_RDWR
O_RSYNC
O_SYNC
O_TRUNC
O_WRONLY
P_NOWAIT
P_NOWAITO
P_WAIT
R_OK
SEEK_CUR
SEEK_END
SEEK_SET
ST_APPEND
ST_MANDLOCK
ST_NOATIME
ST_NODEV
ST_NODIRATIME
ST_NOEXEC
ST_NOSUID
ST_RDONLY
ST_RELATIME
ST_SYNCHRONOUS
ST_WRITE
TMP_MAX
UserDict
WCONTINUED
WCOREDUMP
WEXITSTATUS
WIFCONTINUED
WIFEXITED
WIFSIGNALED
WIFSTOPPED
WNOHANG
WSTOPSIG
WTERMSIG
WUNTRACED
W_OK
X_OK
_Environ
__all__
__builtins__
__doc__
__file__
__name__
__package__
_copy_reg
_execvpe
_exists
_exit
_get_exports_list
_make_stat_result
_make_statvfs_result
_pickle_stat_result
_pickle_statvfs_result
_spawnvef
abort
access
altsep
chdir
chmod
chown
chroot
close
closerange
confstr
confstr_names
ctermid
curdir
defpath
devnull
dup
dup2
environ
errno
error
execl
execle
execlp
execlpe
execv
execve
execvp
execvpe
extsep
fchdir
fchmod
fchown
fdatasync
fdopen
fork
forkpty
fpathconf
fstat
fstatvfs
fsync
ftruncate
getcwd
getcwdu
getegid
getenv
geteuid
getgid
getgroups
getloadavg
getlogin
getpgid
getpgrp
getpid
getppid
getresgid
getresuid
getsid
getuid
initgroups
isatty
kill
killpg
lchown
linesep
link
listdir
lseek
lstat
major
makedev
makedirs
minor
mkdir
mkfifo
mknod
name
nice
open
openpty
pardir
path
pathconf
pathconf_names
pathsep
pipe
popen
popen2
popen3
popen4
putenv
read
readlink
remove
removedirs
rename
renames
rmdir
sep
setegid
seteuid
setgid
setgroups
setpgid
setpgrp
setregid
setresgid
setresuid
setreuid
setsid
setuid
spawnl
spawnle
spawnlp
spawnlpe
spawnv
spawnve
spawnvp
spawnvpe
stat
stat_float_times
stat_result
statvfs
statvfs_result
strerror
symlink
sys
sysconf
sysconf_names
system
tcgetpgrp
tcsetpgrp
tempnam
times
tmpfile
tmpnam
ttyname
umask
uname
unlink
unsetenv
urandom
utime
wait
wait3
wait4
waitpid
walk
write

Now we shall see some examples 

to list contents of dirctory 

os.listdir(directoryname)
list filenames in the directory 

os.path.exists( ) 
to see is the path exists

to do file copy 

import shutil
shutil.copy(source,dest) 

both strings source and dest denote filenames with path 

we cannot do it for copying entire folder content , this can be done by iterating over files inside the folder
Ok these are ways to simulate shell commands in python

Is there a way to run shell commands directly in python and show output 

Ya

import commands 
cmd = "ls -l ."
(status,output) = commands.getstatusoutput(cmd)
print output

this executes ls -l command on directory .  gives the output as a tuple having 2 things ., status (something like exit code, if 0 it means the command executed without error , if command execution had error it exits by sending 1 or even some other error code like 512 ) and the output of the command 


Interesting feature in python :

List comprehension 

if we have a list a = [1, 2 ,3,4]
then you want to get a new list which has the squares of these numbers ..
what we normally do is iterate through the list then get elements and then put it into the new list 

python has a easy way of doing this

b = [num*num      for num in a]

to put additional qualities like removing elements < 2 

b=[num*num     for num in a     if num>2]

Wednesday, 23 May 2012

Python

Dictionary in python :


Dictionary is basically key value binding .
As String is enclosed within " "
As List is enclosed within    [  ]
As tuple is enclosed within  (  )

Here dictionary ,it is enclosed within  {  }

In String , list tuple , also we have this same idea ,,, but there key is always an integer ... so we call it index

So  Stringname or listname or tuplename [1] means , item at index 1

Dictionary gives scope for having having even non integer keys

To declare a Dictionary :   d = { }

>>d['a'] = 'alpha'
>>d['o']='omega'
>>d['g']='gamma'

this is the way to assign value in a dictionary .

Now,  to retrieve a value in String or list or tuple , what will u do ,?  name[index]

similarly here  >>d['a']  displays   'alpha'

The advantage of having key and corresponding value storage is that , it is fast at retrieval using key ,,
this is the advantage of hashtable

But don't know why it is faster in this way .........
 
If you give d['x'] , it flings an error , as the key x is not there in dictionary d .

So , what can be the way to bypass , this even if the key is not there , but still , not flag an error ,,
this can be used in future to check if a key exists in a dictionary ..

Ya, we have a 2 solutions

1) d.get['x]  returns value null if key x is not found and returns the actual corresponding value if the key exists

>>d.get['a']  returns  'alpha'
2) As , we do in lists is ,a =[1,2,3]
to find if 2 is a value inside it ,, we say  2 in a   , returns true if 2 value is there  (note that here we r searching by value , so this time is longer )
Here also same thing

'a' in d  returns true if key 'a' exists , false if it doesn't exist

To see the list of keys at once in a dictionary 

d.keys()

To see the list of values at once in a dictionary

d.values()

To get values ,,, just loop over

for k in d.keys() :
    print d[k]

to have it ordered

for k in sorted(d.keys()):
   print d[k]


To see list of key value pair .

d.items() ...............returns

[('a','alpha') , ('o','omega'),('g','gamma')]

looping over  ? ya , easy
for tuple in d.items() :
    print tuple


File operation in Python : 

f = open(filename , 'r')

the second argument indicates , whether we r opening file for reading or 'w' denotes 'writing .. similary we have 'rU'

f is here the file handler ,,,  by this line we just open the file and give it to f  ,, from f we can read it
now we can iterate each line ,,as soon as it sees a new line in f  ( which is a reflection of file filename which is open ), it stops ,reading ,
like this we iterate over every line

for line in f:
    print line

prints each line but with double line space between

this is because print itself has a new line and the new line in file adds to it ,,
so to eliminate it replace print statement by
print line,

the trailing comma removes this problem as it removes the new line in the print

finally , we have to close the open file ,,as a open file unnecessarily draws memory

f.close()

if u omit this line , when the process ends the file is closed automatical
but in case of softwares which r continuously running , the process also runs , so here if we don't close, it remains open the whole period of time , when software runs ,and that too unnecessarily
what is the advantage of having this concept of reading line by line ? why not read at once

when we read , big files say 90 Gb ,,  reading at once gives problem of locking heavy memory

when we read line by line only a small part of memory is locked for its work ,

f.readlines()

gives o/p as a list of lines

['line1' ,'line2','line3'...]

it even prints it in the same way as shown above

f.read()

reads entire file into a single string

so has same view as we open a file in editor in gui

Thursday, 26 January 2012

POP



POP , otherwise known as Post office protocol , is a protocol used by the email-clients to retrieve emails from a remote server over a TCP/IP connection.

let us try retrieving mail from my gmail account without using any client ,starting from scratch.

gmail uses POP and IMAP , Let us for the time being concentrate on POP.

wikipedia page on POP gives information which reads

"A POP3 server listens on port 110. Encrypted communication for POP3 is either requested after protocol initiation, using the STLS command, if supported, or by POP3S, which connects to the server using secure sockets layer (SSL) on well-known TCP port 995 (e.g. google Gmail)."

so I need to connect using SSL protocol( this is needed because gmail has SSL security layer protection)

The openssl command does this job . By the way i, need to create a client where i can view the mails . This is also done by passing s_client as argument for this openssl command

$openssl s_client

wait ,dont execute this command in command line, the command is not yet over,
we need to connect it to the gmail POP server ( gmail server that uses POP protocol) , so

$openssl s_client -connect pop.gmail.com:995

we can get the port number detail from wikipedia information i quoted above.

so now ,the task that we have completed so far are

1) client has been created

2) client communictaes using SSL ( this is needed because gmail has SSL security layer protection)

3) We have established connection between the client the POP gmail server

thats all, the rest is what we do in the GUI ....

Just give username , password and then read the mail
So the next thing that we need to type is shown in color

user <type your user name here>

And Another Kind Request :
Chase those fellows standing near by , because the password is exposed as you type.

pass <type your user password here>
STAT
LIST
retr <type the mail number of the mail you wanted to read by seeing the list dispalyed>


after reading the mail ,you can quit and signout using command

quit

Thats all I know about POP .

Bye ,

Harish Kayarohanam

Tuesday, 10 January 2012

Apache

Apache in computer technology is defined as a web server software.

Taking it for granted that Apache has already been installed in a ubuntu installed system , the procedure for creating a module and loading it in apache is as follows ,


$ cd /etc/apache2

This is the place were we have the .c version of the newly created modules
now create a module ( here  I have given a name to the module as pracmod3)

$ sudo apxs2 -g -n "pracmod3"

so we have created the module.
now get into the module folder pracmod3 by normal cd command

$ cd pracmod3

Now we have to compile the .c file mod_pracmod3.c

$ sudo apxs2 -c -i mod_pracmod3.c

Now mod_pracmod3.so has been created . This is the dynamic shared object . This is similar to dll files in windows.
Now its our job to load into the apache and this is done by the following sequence of steps

$ cd ..


then open the mod_pracmod3.c and copy that which follows the lines (shown in red here )"Then activate it in Apache's apache2.conf file for instance
for the URL /pracmod3 in as follows: 
#   apache2.conf"
the content to be copied will be as follows

LoadModule pracmod3_module modules/mod_pracmod3.so
<Location /pracmod3>
SetHandler pracmod3


$ sudo vim apache2.conf

now paste the copied content below the line (shown in red here) Include sites-enabled/

now change the path of the file pracmod3.so , to the place where it is there in your system .
In my system it was in 
/usr/lib/apache2/modules

now we have to restart the apache as 

$ sudo apachectl restart

Now , our job is over .
To check whether our module has been loaded properly. check if it displays the content that was in "The sample content handler " portion of mod_pracmod3.c 

to do that type command
$ lynx -mime_header http://localhost/pracmod3 


the result is seen as (shown in red here )
HTTP/1.1 200 OK
Date: Tue, 10 Jan 2012 05:25:55 GMT
Server: Apache/2.2.20 (Ubuntu)
Content-Length: 62
Connection: close
Content-Type: text/html

The sample page from mod_pracmod3.c and it was done by harish




Thank You .









Wednesday, 4 January 2012

Screen Scraping

As soon as I saw the word , in the training agenda sent by my educator, I felt that it is something related to   " Discarding a part of the screen as useless " , as i thought that the word scrap means waste material .
Yes I am in a way correct that scrap means waste .But ...................................................................    then when I started studying about this concept, I found that it is scraping ,,, which comes from the word scrape that means "remove from something " .
Then I understood that this topic deals with "EXTRACTING INFORMATION FROM THE SCREEN ".

I took a website  http://money.livemint.com and tried to extract the eps field information .
I wanted to learn python , so thought of doing this task in python itself .


import urllib
import re
def eps():
    base_url = 'http://money.livemint.com/IID42/F132540/QuickQuote/Company.aspx'
    content = urllib.urlopen(base_url).read()
    me = re.search(r'EPS\s*\(Rs\.\)<.*?><.*?>\s*<.*>\s*\d*\.\d*\s*<.*>', content)
    eps = me.group()
    ma = re.search(r'\d+\.\d+', eps)
    if ma:
                 epse = ma.group()
    else:
                 epse = 'no match available : '
    return epse  
   

This does the screen scraping .....

I wanted to give some color and fragrance for this code so that It makes sense ( the deep seated hidden motive is to make the code in such a way so that the user of our website, may not even get the very thought that the data has been scraped from somewhere )

import urllib
import re


def get_eps():
   
    baseone_url = 'http://money.livemint.com/IID42/'
    basethree_url = '/QuickQuote/Company.aspx'
    symbol = input('Enter the company name and should be one among TCS , INFOSYS ,HCL , WIPRO  : ');
    tcs = "TCS"
    infe = "INFOSYS"
    hcl = "HCL"
    wip = "WIPRO"
    if symbol == tcs :
        code = "F132540"
        content = urllib.urlopen(baseone_url + code + basethree_url).read()
   
        me = re.search(r'EPS\s*\(Rs\.\)<.*?><.*?>\s*<.*>\s*\d*\.\d*\s*<.*>', content)
        eps = me.group()
   
        ma = re.search(r'\d+\.\d+', eps)
        if ma:
                 epse = ma.group()
                 print 'EPS is ' + epse
        else:
                 epse = 'no match available : '
                 return epse
    elif symbol == infe :
        code = "F100209"
        content = urllib.urlopen(baseone_url + code + basethree_url).read()
   
        me = re.search(r'EPS\s*\(Rs\.\)<.*?><.*?>\s*<.*>\s*\d*\.\d*\s*<.*>', content)
        eps = me.group()
   
        ma = re.search(r'\d+\.\d+', eps)
        if ma:
                 epse = ma.group()
                 print 'EPS is ' + epse
        else:
                 epse = 'no match available : '
                 return epse
    elif symbol == wip :
        code = "F107685"
        content = urllib.urlopen(baseone_url + code + basethree_url).read()
   
        me = re.search(r'EPS\s*\(Rs\.\)<.*?><.*?>\s*<.*>\s*\d*\.\d*\s*<.*>', content)
        eps = me.group()
   
        ma = re.search(r'\d+\.\d+', eps)
        if ma:
                 epse = ma.group()
                 print 'EPS is ' + epse
        else:
                 epse = 'no match available : '
                 return epse
    elif symbol == hcl :
        code = "F132281"
        content = urllib.urlopen(baseone_url + code + basethree_url).read()
   
        me = re.search(r'EPS\s*\(Rs\.\)<.*?><.*?>\s*<.*>\s*\d*\.\d*\s*<.*>', content)
        eps = me.group()
   
        ma = re.search(r'\d+\.\d+', eps)
        if ma:
                 epse = ma.group()
                 print 'EPS is ' + epse
        else:
                 epse = 'no match available : '
                 return epse
    else:
        print "Enter a valid company name"
 
 This code gets input from the user and searches the page that belongs to that company and displays the scrapped scraped data .

For your info : Web Scraping from a multitude of sites is known as WEB HARVESTING .

Thank You ,

Meet You in next post ,

Harish Kayarohanam