Friday, April 22, 2016

Reading environment variables in GNU Make

# export FOO="HI"

# vi Makefile

all:
      $(info $(FOO))

# make
HI
make: `all' is up to date.



#Make file in single line
all:; $(info $(FOO))


Hello World Makefile

# vi Makefile
all:
    echo Hello World

# make
Hello World

Monday, April 21, 2014

Perl quick reference

Scalar Data

String concatenation

"hello" . "world" -> "helloworld"

----------------------------------------
Numeric and String Comparison Operators
----------------------------------------
Comparison   Numeric   String
--------------------------------------
Equal         ==        eq
Not equal    !=         ne 
Less than     <         lt
Greater than  >         gt
Less than or equal to <= le
Greater than or equal to >= ge

File tests and their meanings
--------------------------------------
File test      Meaning
-------------------------------------
-r             File or directory is readable by this (effective) user or group
-w             File or directory is writable by this (effective) user or group
-x             File or directory is executable by this (effective) user or group
-o             File or directory is owned by this (effective) user
-R             File or directory is readable by this real user or group
-W             File or directory is writable by this real user or group
-X             File or directory is executable by this real user or group
-O             File or directory is owned by this real user
-e             File or directory name exists
-z             File exists and has zero size (always false for directories)
-s             File or directory exists and has nonzero size (the value is the size in bytes)
-f             Entry is a plain file
-d             Entry is a directory
-l             Entry is a symbolic link
-S             Entry is a socket
-p             Entry is a named pipe (a “fifo”)
-b             Entry is a block-special file (like a mountable disk)
-c             Entry is a character-special file (like an I/O device)
-u             File or directory is setuid
-g             File or directory is setgid
-k             File or directory has the sticky bit set
-t             The filehandle is a TTY (as reported by the isatty() system function; filenames can’t be tested by this test)
-T             File looks like a “text” file
-B             File looks like a “binary” file
-M             Modification age (measured in days)
-A             Access age (measured in days)
-C             Inode-modification age (measured in days)

String repetition operator

"fred" x 3  -->   is "fredfredfred"

The chop and chomp Functions

$x = "hello world";
chop($x); # $x is now "hello worl"
$a = "hello world\n";
chomp ($a); # $a is now "hello world"

Reading from standard input

$a = <STDIN>;  # get the text
chomp($a);     # get rid of that pesky newline
or
chomp($a = <STDIN>);

Output with print
print("hello world\n"); # say hello world, followed by newline
print "hello world\n";  # same thing

Arrays

(1,2,3)             # array of three values 1, 2, and 3
() # the empty list (zero elements)

List constructor operator

(1 .. 5)            # same as (1, 2, 3, 4, 5)

"quote word" function

 @a = ("fred","barney","betty","wilma"); # ugh!
 @a = qw(fred barney betty wilma); # better!
 @a = qw(
    fred
    barney
    betty
    wilma
 );                                # same thing

Assignment

@fred = (1,2,3); # The fred array gets a three-element literal
@barney = @fred; # now that is copied to @barney
@huh = 1; # 1 is promoted to the list (1) automatically
@barney = (4,5,@fred,6,7); # @barney becomes 4,5,1,2,3,6,7
@barney = (8,@barney);     # puts 8 in front of @barney
@barney = (@barney,"last");# and a "last" at the end

($a,$b,$c) = (1,2,3);    # give 1 to $a, 2 to $b, 3 to $c
($a,$b) = ($b,$a);       # swap $a and $b
($d,@fred) = ($a,$b,$c); # give $a to $d, and ($b,$c) to @fred

Length of the array

@fred = (4,5,6);   # initialize @fred
$a = @fred;        # $a gets 3, the current length of @fred
$#fred to get the index value of the last element of @fred

Array Element Access

@fred = (7,8,9);
$b = $fred[0];  # give 7 to $b (first element of @fred)
$fred[2]++;                      # increment the third element of @fred

Slice

Accessing a list of elements from the same array is called a slice
@fred[0,1];                 # same as ($fred[0],$fred[1])
@fred[0,1] = @fred[1,0];    # swap the first two elements

If you access an array element beyond the end of the current array, the undef value is returned without warning. 
For example:
@fred = (1,2,3);
$barney = $fred[7]; # $barney is now undef

Assigning a value beyond the end of the current array automatically extends the array
For example:
@fred = (1,2,3);
$fred[3] = "hi"; # @fred is now (1,2,3,"hi")
$fred[6] = "ho"; # @fred is now (1,2,3,"hi",undef,undef,"ho")

The push and pop Functions

push(@mylist,$newvalue);    # like @mylist = (@mylist,$newvalue)
$oldvalue = pop(@mylist);   # removes the last element of @mylist

The shift and unshift Functions

The push and pop functions do things to the "right" side of a list. Similarly, the unshift and shift functions perform the corresponding actions on the "left" side of a list. Here are a few examples:
unshift(@fred,$a);       # like @fred = ($a,@fred);
$x = shift(@fred);       # like ($x,@fred) = @fred;

The reverse Function

@a = (7,8,9);
@b = reverse(@a);    # gives @b the value of (9,8,7)

The sort Function

@y = (1,2,4,8,16,32,64);
@y = sort(@y); # @y gets 1,16,2,32,4,64,8 # Note that sorting numbers does not happen numerically, but by the string values of each number

Control Structures


if ($a < 18) {
    print "So, you're not old enough to vote, eh?\n";
} else {
    print "Old enough!  Cool!  So go vote!\n";    
}

unless ($a < 18) {
    print "Old enough!  Cool!  So go vote!\n";
#Replacing if with unless is in effect saying "If the control expression is false, do...." 

if (some_expression_one) {
    one_true_statement_1; 
} elsif (some_expression_two) {
    two_true_statement_1; 
} else {
    all_false_statement_1;
    all_false_statement_2;
    all_false_statement_3;
}

while ($a > 0) {
    print "At one time, you were $a years old.\n";
    $a--;
}

until (some_expression) {
    statement_1; 
}
#until something is true" rather than "while not this is true."

$stops = 0;
do {
    $stops++;
    print "Next stop? ";
    chomp($location = <STDIN>);
} until $stops > 5 || $location eq 'home';

for ($i = 1; $i <= 10; $i++) {
    print "$i ";
}

@a = (3,5,7,9);
foreach $one (@a) {
    $one *= 3;
}
# @a is now (9,15,21,27)

Hash (key/value pair)

$fred{"aaa"} = "bbb"; # creates key "aaa", value "bbb"
$fred{234.5} = 456.7; # creates key "234.5", value 456.7

print $fred{"aaa"}; # prints "bbb"

@fred_list = %fred;
# @fred_list gets ("aaa","bbb","234.5",456.7)
%barney = %fred;       # a faster way to do the same

%smooth = ("aaa","bbb","234.5",456.7);

%backwards = reverse %normal;
#To construct a hash with keys and values swapped using the reverse operator.
#if %normal has two identical values, those will end up as only a single element in %backwards, so this is best performed only on hashes with unique keys and values

The keys Function

foreach $key (keys (%fred)) { 
    print "at $key we have $fred{$key}\n"; 
}

if (keys(%somehash)) { # if keys() not zero:
    ...; # array is non empty
}

In fact, merely using %somehash in a scalar context will reveal whether the hash is empty or not:

if (%somehash) { # if true, then something's in it 
    # do something with it
}

The values Function

values(%hashname)

The each Function

while (($first,$last) = each(%lastname)) {
    print "The last name of $first is $last\n";
}

The delete Function

%fred = ("aaa","bbb",234.5,34.56); # give %fred two elements
delete $fred{"aaa"};
# %fred is now just one key-value pair

Basinc I/O

Command line arguments.
@ARGV array. Each command-line argument goes into a separate element of the @ARGV array.

printf for Formatted Output
printf "%15s %5d %10.2f\n", $s, $n, $r;
prints $s in a 15-character field, then space, then $n as a decimal integer in a 5-character field, then another space, then $r as a floating-point value with 2 decimal places in a 10-character field, and finally a newline.

Regular Expressions


if (/abc/) {
    print $_;
}

while (<>) {
    if (/ab*c/) {
        print $_;
    }
}

Single-Character Patterns

the dot ".". This matches any single character except newline (\n). For example, the pattern /a./ matches any two-letter sequence that starts with a and is not "a\n".

/[abcde]/ #matches a string containing any one of the first five letters of the lowercase alphabet
/[aeiouAEIOU]/ #matches any of the five vowels in either lower- or uppercase. 
[0-9] #match any single digit
[0-9\-]         # match 0-9, or minus
[a-z0-9]        # match any single lowercase letter or digit
[a-zA-Z0-9_]    # match any single letter, digit, or underscore
[^0-9]        # match any single non-digit
[^aeiouAEIOU] # match any single non-vowel
[^\^]         # match single character except an up-arrow

\d (a digit) [0-9]    \D (digits, not!) [^0-9]

\w (word char) [a-zA-Z0-9_]  \W (words, not!) [^a-zA-Z0-9_]

\s (space char) [ \r\t\n\f]  \S (space, not!) [^ \r\t\n\f]
The \d pattern matches one "digit." The \w pattern matches one "word character",The \s pattern matches one "space" (whitespace)
 \W matches one character that can't be in an identifier, \S matches one character that is not whitespace (including letter, punctuation, control characters, and so on), and \D matches any single nondigit character.

Grouping Patterns

 Multipliers
  The asterisk(*) indicates zero or more of the immediately previous character (or character class).
  The plus sign (+), meaning one or more of the immediately previous character
  The question mark (?), meaning zero or one of the immediately previous character
 For example, the regular expression /fo+ba?r/ matches an f followed by one or more o's followed by a b, followed by an optional a, followed by an r.

Parentheses as memory
/fred(.)barney\1/;
matches a string consisting of fred, followed by any single non-newline character, followed by barney, followed by that same single character. So, it matches fredxbarneyx, but not fredxbarneyy. 

Alternation
a|b|c. This means to match exactly one of the alternatives (a or b or c in this case)

Anchoring Patterns

The \b anchor requires a word boundary at the indicated point for the pattern to match.
/fred\b/;     # matches fred, but not frederick
/\bmo/;       # matches moe and mole, but not Elmo
/\bFred\b/;   # matches Fred but not Frederick or alFred
/\b\+\b/;     # matches "x+y" but not "++" or " + "
/abc\bdef/;   # never matches (impossible for a boundary there)

\B requires that there not be a word boundary at the indicated point.
For example: /\bFred\B/; # matches "Frederick" but not "Fred Flintstone"

The caret (^) matches the beginning of the string. ^a matches an a if, and only if, the a is the first character of the string
The $, anchors the pattern, but to the end of the string. c$ matches a c only if it occurs at the end of the string.

Matching Operator: the =~ Operator

This operator takes a regular expression operator on the right side, and changes the target of the operator to something besides the $_ variable - namely, some value named on the left side of the operator.
It looks like this:
$a = "hello world";
$a =~ /^he/;         # true
$a =~ /(.)\l/;       # also true (matches the double l)
if ($a =~ /(.)\l/) { # true, so yes...
                     # some stuff
}

if (<STDIN> =~ /^[yY]/) {          # does the input begin with a y?

}

Ignoring Case

/somepattern/i
if (<STDIN> =~ /^y/i) { # does the input begin with a y?
    # yes! deal with it
}

Using a Different Delimiter

$path = <STDIN>; # read a pathname (from "find" perhaps?)
if ($path =~ /^\/usr\/etc/) {  # Not comfortable
    # begins with /usr/etc...
}

m@^/usr/etc@      # using @ for a delimiter
m#^/usr/etc#      # using # for a delimiter

Using Variable Interpolation

$what = "bird";
$sentence = "Every good bird does fly.";
if ($sentence =~ /\b$what\b/) {
    print "The sentence contains the word $what!\n";
}

Special Read-Only Variables

After a successful pattern match, the variables $1, $2, $3, and so on are set to the same values as \1, \2, \3, and so on. You can use this to look at a piece of the match in later code.
For example:
$_ = "this is a test";
/(\w+)\W+(\w+)/; # match first two words
                 # $1 is now "this" and $2 is now "is"

$_ = "this is a test";
($first, $second) = /(\w+)\W+(\w+)/; # match first two words
     # $first is now "this" and $second is now "is"

Other predefined read-only variables include $&, which is the part of the string that matched the regular expression; $`, which is the part of the string before the part that matched; and $', which is the part of the string after the part that matched.
For example:
$_ = "this is a sample string";
/sa.*le/; # matches "sample" within the string
          # $` is now "this is a "
          # $& is now "sample"
          # $' is now " string"

Substitutions

s/old-regex/new-string/
$_ = "foot fool buffoon";
s/foo/bar/g; # $_ is now "bart barl bufbarn"

$_ = "hello, world";
$new = "goodbye";
s/hello/$new/; # replaces hello with goodbye

$_ = "this is a test";
s/(\w+)/<$1>/g; # $_ is now "<this> <is> <a> <test>"

The split and join Functions

The split Function

$line = "merlyn::118:10:Randal:/home/merlyn:/usr/bin/perl";
@fields = split(/:/,$line); # split $line, using : as delimiter
# now @fields is ("merlyn","","118","10","Randal","/home/merlyn","/usr/bin/perl")
Note how the empty second field became an empty string. If you don't want this, match all of the colons in one fell swoop:
@fields = split(/:+/, $line);
This matches one or more adjacent colons together, so there is no empty second field

$_ = "some string";
@words = split; # same as @words = split(/\s+/, $_);

$line = "merlyn::118:10:Randal:/home/merlyn:";
($name,$password,$uid,$gid,$gcos,$home,$shell) = split(/:/,$line); # split $line, using : as delimiter
simply gives $shell a null (undef) value if the line isn't long enough or if it contains empty values in the last field. (Extra fields are silently ignored)

The join Function

$bigstring = join($glue,@list);
$outline = join(":", @fields);

Sunday, February 9, 2014

Creating a python distribution and publishing to PyPI

Step 1: Create a folder for your module
mkdir depict
cp depict.py depict

Here depict.py is the python module file, out of which we are creating a distribution

Step 2: Create setup.py file. This file contains distribution metadata.
The setup.py program provides metadata about your module and is used to build, install, and upload your packaged distribution.
cd depict
vi setup.py

from distutils.core import setup

setup (
        name = 'depict',
        version = '1.0.0',
        py_modules = ['depict'],
        author = 'Siddesh BG',
        author_email = 'siddesh.bg@gmail.com',
        url = 'http://sliceoffcodes.blogspot.com',
        description = 'A simple printer of nested lists',
        )

Step 3: Build the distribution
cd depict
python setup.py sdist

Step 4: Install the distribution into the local copy of python.
python setup.py install

Step 5: Register with PyPI. 
URL: https://pypi.python.org/pypi . It's straight forward.

Step 6: Command-line registration with PyPI
Though we have already registered with web, still command-line registration is mandatory to make command-line uploading tools aware of username and password. It needs to be done only once.

python setup.py register
running register
running check
We need to know who you are, so please choose either:
 1. use your existing login,
 2. register as a new user,
 3. have the server generate a new password for you (and email it to you), or
 4. quit
Your selection [default 1]:
1
Username: siddesh_bg
Password:
Registering depict to http://pypi.python.org/pypi
Server response (200): OK

Wow !! this module is successfully uploaded to PyPI. You can search it from PyPI.

Step 7: Re-uploading the module: 
The newly requested changes are applied to module and how to re-upload?
python setup.py sdist upload

How to use this module?
Download the module from https://pypi.python.org/packages/source/d/depict/depict-1.0.2.zip#md5=96e22076b0d67b031bf220b300ef776c
Extract the zip file.
Run :    python setup.py install

Wednesday, January 22, 2014

python3 syntax

IDLE Simple python integrated IDE

  • Tab completion : Type pr + tab will give the choices of function names having "pr"
  • Recall code statements: Alt + p (previous) , Alt + n (next)
  • dir(__builtins__) : to list the python built-in functions
  • help(map) : To get description of 'map' built-in functions
  • PyPI (Python Package Index): Centralized repository for thir-party python modules
  • import sys; sys.path : To get the list of locations where python interpreter searches for modules.
  • type(varname) : It tells whether the variable mentioned in argument is either list, dictionary or any other data structure

Lists

Creating list: 
   movies = ['Toy Story', "UP", "Rio", "Finding Nemo" ]
Accessing list:
   print(movies[1])  --> UP
   print(movies)   --> ['Toy Story', "UP", "Rio", "Finding Nemo" ]
Printing first three elements of list
  print(unique_james[0:3])
Length of list:
    print (len(movies))  --> 4
List methods
    movies.append("Lionking") --> ['Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Lionking']
    movies.pop()   --> 'Lionking'
    movies.extend(["Lionking", "Despicable me"]) 
         --> ['Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Lionking', 'Despicable me']
    movies.remove('Lionking')  --> ['Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Despicable me']
    movies.insert(0,'Lionking')
         -->    ['Lionking', 'Toy Story', 'UP', 'Rio', 'Finding Nemo', 'Despicable me']

isinstance : is a function to check if specific identifier holds the data of specific type
   isinstance(movies,list)  --> True
   isinstance(a,int)  --> True
   isinstance(movies,int) --> False

Lists within lists

movies = [ "UP", 2009, "Pete Docter", 96, ["Ed Asner" , ["Christopher Plummer", 'Jordan Nagai', 'Bob Peterson' ] ] ]
print(movies[4][0])  --> Ed Asner
print(movies[4][1]) --> ['Christopher Plummer', 'Jordan Nagai', 'Bob Peterson']
print(movies[4][1][0]) --> Christopher Plummer

Tuple

It's an unchangeable (immutable) list. 
tup = ( "abc" , "def")
print(tup[0])

Sets

Set is another data structure in python. The data items in set are unordered and duplicates are not allowed.
>>> distances = {12, 44, 53, 44, 32, 101}
>>> distances
{32, 12, 44, 101, 53}
>>> my_list = [ 12, 14, 16, 12, 18, 20 ]
>>> my_list
[12, 14, 16, 12, 18, 20]
>>> filtered_set = set(my_list)
>>> filtered_set
{16, 20, 18, 12, 14}

Set example with list comprehension
   set([sanitize(each_ele) for each_ele in my_list])
In the above example sanitize is a user defined function.

Dictionaries

Creating empty dictionary
>>> dhoni={}
Assigning values to dictionary
>>> dhoni['Name']="Mahendra Singh Dhoni"
>>> dhoni['Occupation']=['Cricketer','Businessmen','actor']
Another way of creating empty dictionary, using dict() factory method
>>> kumble=dict()
Another way of assigning values to dictionary
>>> kumble={'Name':'Anil Kumble','Occupations':['Cricketer','photographer','Engineer']}
Accessing dictionary values
>>> dhoni['Name']
'Mahendra Singh Dhoni'
>>> kumble['Occupations'][-1]
'Engineer'

Example of returning dictionary from a function

def get_runs(runs_scored) :

return ( { 'Name' : runs_scored.pop(0),
  'Team' : runs_scored.pop(0),
  'Runs' : sorted(runs_scored, reverse=True)
  } )

>>> kohli=['Virat Kohli', 'India', 42, 121, 87, 91, 67, 117, 143, 3]
>>> kohli_dict = get_runs(kohli)
>>> print ("Top 3 runs scored by " + kohli_dict['Name'] + str(kohli_dict['Runs'][0:3]))

Top 3 runs scored by Virat Kohli [143, 121, 117]

String operations

line.find(':')
line.split(':',1)  --> Split using delimiter ':' and the 1 suggests to split only based on 1st occurrence of ':'
line.strip()  --> Remove unwanted white-space

Sorting
1) In-place sorting using sort() method: It sorts and replaces the original data. The original ordering is lost
data = [6,3,1,2,4,5]
data.sort()
>>> data
[1, 2, 3, 4, 5, 6]

2) Copied sorting using sorted() method. It returns a sorted copy of original data. Original data is untouched.
data = [6,3,1,2,4,5]
data2 = sorted(data)
>>> data
[6, 3, 1, 2, 4, 5]
>>> data2
[1, 2, 3, 4, 5, 6]

3) Use 'reverse=True' argument to sort in descending order. Ex sorted(my_list,reverse=True)

if else

if isinstance(n,list) :
for o in n:
print ("xx",o)
else:
print ("x", n)

if with Boolean variables
indent=True
if indent:
        print("Yes")
else :
        print("No")

If while comparing values
var=101
if var == 102 :
        print("Correct, value = ",var)
else:
        print("Incorrect, value != 102")

if /else if and string comparision example
if role == 'Man' :
       man.append(line_spoken)
elif role == 'Other Man':

       other.append(line_spoken)


if with not condition
if not line.find(':') == -1:
(role, line_spoken) = line.split(':',1)


If condition to check the contents of list
if ele not in unique_james :

                unique_james.append(ele)

For loop

for m in my_list:
  print(m)

for i in range(5) :
 print(i)   --> 0,1,2,3,4

range() is a built-in function which iterates a fixed number of times (starting from 0).

While loop

count = 0
while count < len(movies)  :
   print(movie[count])
   count = count + 1

Functions

def desc_movies(movies) :
for m in movies:
if ( isinstance(m,list)) :
desc_movies(m)
else :
print(m)

>> desc_movies(movies)

Functions with optional arguments
Provide a default value for an argument and that argument will become optional.

def describe_list(the_list,level=0) :
        for each_item in the_list :
                if (isinstance(each_item, list)) :
                        describe_list(each_item,level+1)
                else :
                        for tab in range(level) :
                                print("\t", end='')
                        print(each_item)

Now this function can be invoked either with argument or without argument.
describe_list(the_list)
describe_list(the_list, 2)

Function with return statement
def sanitize(time_string):
        if '-' in time_string:
                splitter = '-'
        elif ':' in time_string:
                splitter = ':'
        else:
                return(time_string)

        (mins, seccs) = time_string.split(splitter)


        return(mins + '.' + secs)

Modules

Create a python file with the functions:
 vi depict.py
""" This is the depict.py module, and it provides one function called
describe_list() which prints lists that may or may not include nested lists"""

def describe_list(the_list) :
        """ This function takes a positional argument called "the_list", which is any python list (of, possibly nested lists). Each data item in the provided list is (recursively) printed to the screen on its own line """

        for each_item in the_list :
                if (isinstance(each_item, list)) :
                        describe_list(each_item)
                else :
                        print(each_item)

To use this module:
vi test_depcit.py

import depict
cast = ['Palin','Cleese','Idle','Jones','Gilliam','Chapman']
depict.describe_list(cast)

Note: You need to use the package namespace to invoke the function inside the package i.e. depict.describe_list() . Python's main namespace is __main__ and built-in functions namespace is __buildins__ .

Another way of using this module:
vi test_depcit.py

from depict import describe_list
cast = ['Palin','Cleese','Idle','Jones','Gilliam','Chapman']
describe_list(cast)

Note: In this case, no need to prefix module namespace while invoking the function.

To create a distribution out of your module, installation and distribution to PyPI , refer my next blog http://sliceoffcodes.blogspot.in/2014/02/creating-python-distribution.html

Comments

""" This is a 
    multi-line comment """
# This is a single line comment


File/Directory operations

import os
Current directory:  os.getcwd()
Changing directory: os.chdir('../dir2')

Reading from file: 
if os.path.exists('HeadFirstPython/chapter3/sketch.txt') :

        data = open('readme.txt')
        print(data.readline(), end='')   --> The end='' argument will avoid newline's getting printed.

data.seek(0) --> Return to start of the file

Reading file through for loop
for line in data :
print(line , end='')

Another example of reading file.
for line in data :
if not line.find(':') == -1:
(role, line_spoken) = line.split(':',1)
print(role, end='')
print(' said ', line_spoken , end='')

Close the file handler
data.close()


Writing to files
out = open("parser.log","w")
print("This is the parser output",file=out) #Notice 'file=out', it makes print write to the the file pointed by 'out'
out.close()


Handling exceptions

for line in data :
try:
(role, line_spoken) = line.split(':',1)
print(role, end='')
print(' said ', line_spoken , end='')
except:
pass

pass is a python statement, which is equivalent to empty or null statement

try:
        data = open('HeadFirstPython/chapter3/sketch.txt')
        for each_line in data:
                try:
                        (role, line_spoken) = each_line.split(':', 1)
                        print(role, end='')
                        print(' said: ', end='')
                        print(line_spoken, end='')
                except ValueError:
                        pass
                finally:
                       if 'data' in locals():
                           data.close()
except IOError as err:
        print('The data file is missing!' + str(err))

Note: The code in 'finally' section will get execute always, no matter whether exception caught or not. Hence closing of file handles should be put there.
The 'locals' built-in function returns a collection of names defined in the current scope. So we use it to check if the file pointed by 'data' is opened successfully.
The err in 'IOError as err' statement above, points to error message. You need to convert it to string using str(err)

Using 'with' to deal with file operations
If we use 'with' statement, then no need to use 'finally' while handling exceptions. The 'with' statement will automatically close the opened handlers. Check out the example below

try:
with open('parser.log','w') as data:
print('This is the parser output',file=data)
except IOError as err:
print('File error' + str(err))


Data persistance with Pickle

Pickle is used to save and load any python data object. It's like serialization in java. The picked data can be stored in disk file or in database. The pickle.dump() function saves data to disk and the pickle.load() function restores data from disk.

import pickle
#Pickling
with open('mydata.pickle','wb') as pickle_handler:
pickle.dump(['Siddesh',32,25000,'Bangalore'], pickle_handler)

#Unpickling
with open('mydata.pickle','rb') as pickle_restore:
emp = pickle.load(pickle_restore)

Another lengthy example

import pickle
import depict #It's our own module available in PyPI
try:
        with open('man_data.txt','wb') as man_handler, open('other_data.txt','wb') as other_handler:
                pickle.dump(man, man_handler)
                pickle.dump(other, other_handler)
except pickle.PickleError as perr:
        print("File error: " +str(perr))

new_man = []
try:
with open('man_data.txt', 'rb') as man_file:
new_man = pickle.load(man_file)
except IOError as err:
print('File error: ' +str(err))
except pickle.PickleError as perr:
print('Pickling error: '+str(perr))

depict.describe_list(new_man)

List comprehensions

It is designed to reduce the amount of code we need to write when transforming one list to another.
Meter to feet conversion example:
>>> meters = [1, 10, 3]
>>> feet = [ m*3.281 for m in meters]
>>> feet
[3.281, 32.81, 9.843]

Lower case to uppercase conversion
>>> lower = ["I", "don't", "like", "spam"]
>>> upper = [s.upper() for s in lower]
>>> upper
['I', "DON'T", 'LIKE', 'SPAM']

Using our own function while comprehending lists
>>> dirty = ['2-22', '2:22', '2.22']
>>> clean = [AthletesCoach.sanitize(t) for t in dirty]
>>> clean
['2.22', '2.22', '2.22']

Overwriting original list

>>> clean = [ float(s) for s in clean]

>>> clean

[2.22, 2.22, 2.22]

Method Chaining

with open(fname) as fh:
    data=fh.readline()
    times=data.strip().split(',')    
It reads from left to right.

Function chaining

>>> clean = [float(AthletesCoach.sanitize(t)) for t in ['2-22', '3:33', '4.44']]
>>> clean
[2.22, 3.33, 4.44]

It reads from right to left.

Class

- The class keyword used to define a class
- Class methods are defined with 'def' keyword
- Class attributes are like variables that exist within object instances
- The __init__() method can be defined within a class to initialize object instances
- Every method defined in a class must provide self as its first argument
- Every attribute in class must be prefixed with self. in order to associate its data with its instance
- "self" is a method argument that always refers to the current object instance

class NameAnalysis:
def __init__(self, value=0):
self.thing = value
def how_big(self):
return(len(self.thing))

d=NameAnalysis("Siddesh BG")
>>> d.thing
'Siddesh BG'
>>> d.how_big()
10

Full fledged example of class
class Athlete:
        def __init__(self, a_name, a_dob=None,a_times=[]):
                self.name = a_name
                self.dob = a_dob
                self.times = a_times

        def top3(self):
                return(sorted(set([sanitize(t) for t in self.times]))[0:3])

        def add_time(self,time_value):
                self.times.append(time_value)

        def add_times(self,l_times):
                self.times.extend(l_times)


def sanitize(time_string):
        if '-' in time_string:
                splitter = '-'
        elif ':' in time_string:
                splitter = ':'
        else:
                return(time_string)

        (mins, secs) = time_string.split(splitter)
        return(mins + '.' + secs)

def get_coach_data(filename) :
        try:
                with open(filename) as fh:
                        data=fh.readline()
                my_list = data.strip().split(',')
                return(Athlete(my_list.pop(0),my_list.pop(0),my_list))

        except IOException as err:
                print("FIle error", str(err))
                return(None)


james = get_coach_data('james2.txt')
julie = get_coach_data('julie2.txt')
mikey= get_coach_data('mikey2.txt')
sarah = get_coach_data('sarah2.txt')

print(james.name + "'s fastest times are: " + str(james.top3()))
print(julie.name + "'s fastest times are: " + str(julie.top3()))
print(mikey.name + "'s fastest times are: " + str(mikey.top3()))
print(sarah.name + "'s fastest times are: " + str(sarah.top3()))

james.add_times(['2.0.2','2.34','3.43'])
print(james.name + "'s fastest times are: " + str(james.top3()))

vera = Athlete("Vera Vi")
vera.add_time('1.31')
print(vera.top3())
vera.add_times(['2.22','1-21',"2:22"])

print(vera.top3())

Inheritance : Inheriting the built in class 'list'
- Classes can be built from scratch or can inherit from Python's built-in classes or from other custom classes.

class AthleteList(list):
        def __init__(self, a_name, a_dob=None,a_times=[]):
                list.__init__([])
                self.name = a_name
                self.dob = a_dob
                self.extend(a_times)

        def top3(self):
                return(sorted(set([sanitize(t) for t in self]))[0:3])


def sanitize(time_string):
        if '-' in time_string:
                splitter = '-'
        elif ':' in time_string:
                splitter = ':'
        else:
                return(time_string)

        (mins, secs) = time_string.split(splitter)
        return(mins + '.' + secs)

def get_coach_data(filename) :
        try:
                with open(filename) as fh:
                        data=fh.readline()
                my_list = data.strip().split(',')
                return(AthleteList(my_list.pop(0),my_list.pop(0),my_list))

        except IOException as err:
                print("FIle error", str(err))
                return(None)


james = get_coach_data('james2.txt')
julie = get_coach_data('julie2.txt')
mikey= get_coach_data('mikey2.txt')
sarah = get_coach_data('sarah2.txt')

print(james.name + "'s fastest times are: " + str(james.top3()))
print(julie.name + "'s fastest times are: " + str(julie.top3()))
print(mikey.name + "'s fastest times are: " + str(mikey.top3()))
print(sarah.name + "'s fastest times are: " + str(sarah.top3()))

vera = AthleteList("Vera Vi")
vera.append('1.31')
print(vera.top3())
vera.extend(['2.22','1-21',"2:22"])
print(vera.top3())

Wednesday, December 4, 2013

Java BigDecimal class tutorial: How to deal with monetary calculation in Java?

Lets take an example of computing a below mentioned financial operation using java program
 Chocolate cost = 5.52
                Tax = 0.84
 -----------------------------
Expected Total = 6.36

But if you do the above calculations using 'Double/float' primitive data type in Java, the total obtained will be 6.359999999999999.

We need to round of the total value to have a meaningful value in real life. Hence Java provides a class BigDecimal for this purpose.

Below is the simple program, which describes the usage of BigDecimal

----------------- BigDecimalExample.java -------------------------------
package blog.siddesh.basics;

import java.math.BigDecimal;

public class BigDecimalExample {

public static void main(String[] args) {
BigDecimal num = new BigDecimal("5.52");
BigDecimal tax = new BigDecimal("0.84");
BigDecimal total = num.add(tax); 

System.out.println("Total = "+total.toString());

double n = 5.52;
double t = 0.84;
double tot = n + t;
System.out.println("Double total = " + tot);


}

}
---------------------------------------------------------------------------

Thursday, October 31, 2013

FindMonth - A simple Java MVC WebApp using Servlet, JSP, eclipse

In this blog I'll explain with a simple example about creating a Java Web Application using MVC pattern. I'm using a simple java program as model, JSP as view and Servlet as controller. 
This application is developed using eclipse (Juno) IDE,  jdk1.6.0_43 and it is hosted on Apache tomcat 7.0.

What is MVC? 

Here is the definition from Wiki. 
Model–view–controller (MVC) is a software architecture pattern which separates the representation of information from the user's interaction with it.
The model consists of application data, business rules, logic, and functions. 
A view can be any output representation of data, such as a chart or a diagram. Multiple views of the same data are possible, such as a bar chart for management and a tabular view for accountants. The controller mediates input, converting it to commands for the model or view.

FindMonth is my first simple java webapp

How it looks?



Configuration in eclipse

  • Open eclipse -> Select Java EE perspective 
  • File -> New -> Dynamic Web Project
  • Provide ProjectName i.e. FindMonth 
  • Select Target run-time has Apache Tomcat 7.0 (or which ever version installed in your computer)
  • Click Next -> Next -> Select "Generate web.xml deployment descriptor -> Finish

Project layout in eclipse




The files ticked in RED font are the files created by me for this project.

  • NumberToMonth.java acts as Model
  • SelectMonth.java is the Servlet
  • FindMonth.html is the home page
  • result.jsp is the View
  • web.xml is a web descriptor file which links the Homepage to Servlet

Code

Model: NumberToMonth.java

I have created a package blog.sliceoffcodes.model which holds the file NumberToMonth.java
  • Right click on src (under Java Resources) -> New -> Package -> Provide package name
Create a java file
  • Right click on above created package -> New -> Class -> provide name NumberToMonth



package blog.sliceoffcodes.model;

public class NumberToMonth {
int month;
String monthString;

public NumberToMonth(int month) {
super();
this.month = month;
}

public int getMonth() {
return month;
}

public void setMonth(int month) {
this.month = month;
}

public String getMonthString() {
return monthString;
}

private void setMonthString(String monthString) {
this.monthString = monthString;
}

public void FindMonth() {
switch (getMonth()) {
case 1:
setMonthString("January");
break;
case 2:
setMonthString("February");
break;
case 3:
setMonthString("March");
break;
case 4:
setMonthString("April");
break;
case 5:
setMonthString("May");
break;
case 6:
setMonthString("June");
break;
case 7:
setMonthString("July");
break;
case 8:
setMonthString("August");
break;
case 9:
setMonthString("September");
break;
case 10:
setMonthString("October");
break;
case 11:
setMonthString("November");
break;
case 12:
setMonthString("December");
break;
default:
setMonthString("Invalid");
break;
}
}

}



Controller (Servlet): SelectMonth.java

I have created a package blog.sliceoffcodes.web which holds the file SelectMonth.java
  • Right click on src (under Java Resources) -> New -> Package -> Provide package name
Create a java file
  • Right click on above created package -> New -> Servlet -> provide name SelectMonth.java
package blog.sliceoffcodes.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import blog.sliceoffcodes.model.NumberToMonth;

public class SelectMonth extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String m = request.getParameter("inputMonth");
/* response.setContentType("text/html");
PrintWriter out = response.getWriter(); 
out.println("Month Input = " + m); */
NumberToMonth nm = new NumberToMonth(Integer.parseInt(m));
nm.FindMonth();
String result = nm.getMonthString();
//out.println("Month output = " + result);
request.setAttribute("monthExtract", result);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}

}

view (Home page): FindMonth.html

  • Right click on WebContent -> New -> HTML file -> provide name FindMonth.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Find Month</title>
</head>
<body>
<h1 align="center">Find Month</h1>
<form method="POST" action="FetchMonth.do">
Select Month Type: <select name="inputMonth" size=1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select> <br>
<br>
<center>
<input type="Submit">
</center>
</form>
</body>
</html>

view (jsp): result.jsp

  • Right click on WebContent -> New -> JSP file -> provide name result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Result of Month Finder </title>
</head>
<body>
<h1 align="center">Month</h1>
<p>
<%
  String month = (String) request.getAttribute("monthExtract");
  out.print("<b> Month in English: " + month);
  
%>

<br> <br>
<A HREF="FindMonth.html" align="center">HOME</A>
</body>
</html>

web descriptor : web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FindMonth</display-name>
  <welcome-file-list>
    <welcome-file>FindMonth.html</welcome-file>    
  </welcome-file-list>
  
  <servlet>
    <servlet-name>Month</servlet-name>
    <servlet-class>blog.sliceoffcodes.web.SelectMonth</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Month</servlet-name>
    <url-pattern>/FetchMonth.do</url-pattern>
  </servlet-mapping>
  
</web-app>