Monday 6 August 2012

Magnificent Mary boxes in to olympic glory

Magnificient boxes  herself into olympic glory in Quaterfinals


The quarterfinals match was scheduled and the red corner  was donned by the maraeau rahali . A Tunisian , 5 ft 6 in. tall ranking 17th in the overall ratings . She was having her olympic run and determined to secure the medal for her country which had just one till now.

In the blue corner was Mary Kom . She uses MC Chigmante as her boxing name . Yes a boxer from India with 11 years of experience and a proud mother of two.

After celebrating her children's birthday yesterday , this mom was now up to face this Tunisian . Mary Kom is older and considering her age she might have her last chance at Olympics . Whereas , rahali is just 24 and has a lot ahead of her.

The match  has 4 rounds of 3 min each and has points for the clean punches. Hitting at back of head is not allowed.

Match starts !!

Round 1

Mary started with a clenching Rahali . Rahali tried to jab a bit .Mary strategy was to fighting on the inside as she knew Rahali has the height advantage . So Mary com was  leaping in and Rahali was pulling her down. A few punches at the back of the head and the round turned out tough and Mary barely got a left jab . She punched Rahali at the back of the head - but it was illegal and got warned by umpire

Round 2
A rerun of round 1 with both the boxers so focused on trying to punch each other , both slipped individually.

Round 3

The match took an interesting turn and Mary busted out into startling chain of left and right jabs . Before Rahali could get her balance she had received a bout of punches. Mary was showing her mettle in this round with 2 bursts of sudden punches. A outcome was a 6-1 points tally.

Round 4
The last round it was already just 3 minutes away from a medal . Only daunting task was to keep the points gained with her , rahali tried a desperate comeback but Mary was consistently a step ahead. Her technique to effectively evade her and her Indian apologizing way was a real treat to eyes .

In all and all she put on a great show and won the bout with 15-6 , a nine point win . Mary Com , no matter which colored medal you get , I guess you are changing your name from magnificent Mary to Super Mom. Kudos !!



Monday 11 June 2012

Using Python and Beautiful Soup

           Python is very versatile and has a lot of cool tools . In this post i am going to show you how to scrape a website . In lots of cases we usually scrape website for a lot of reason . In my case i am going to scrape yahoo finance site for options . I am going to use Beautiful Soup  and urllib for that . BeautifulSoup is quite versatile . Although, it was once thought to be not maintained, the fellows at BeautifulSoup have done an excellent job. I found it easy to use quite good in terms of speed too.  You can use it from here . The latest version is stable . Also i have referred quite a bit from pythoncentral too.            

Here i am going to scrape the Options for Google as in figure below:

google options

We must first set up the page as below:

Loading ....
            Here the important thing to remember is that , the property of html tags are changed by yahoo so when it does , be sure to change the propery of the html tags in my case change attrs in the given code below . So we are going to use soup lets do some magic.
     
Loading ....
Use firebug or something to find the html tags and their corresponding properties . As the property keeps on changing so i am going to show how it is done step wise with the results from each step.
Firstly take a look at the site of yahoo finance here
            <
     
Loading ....
      Finally we can go off with the values for the same . 
     
Loading ....
             In the entire excercise we have obtained all the values needed . For my case i would like to now enter import it in excel and use IronSpread to calculate the cell values . I guess that will be in some other post . I will soon tell you how to use python with excel and bug off all the complicated vba scripts .Also write the macros . it will be pretty interesting stuff, if you want to take a look i suggest take a look at IronSpread.




Friday 8 June 2012

Python namespaces


          Namespaces are mapping from names to objects . They are a collection of names in a module . Here in a single module no two names of methods are to be similar for obvious reasons. The other thing one has to remember is there is no connection between the names in different namespaces. Say two different modules may define same function "maximize" without confusion , so the user must prefix the module.

        Any name following the dot is an attribute so guass.solve here guass is a module and solve is a function name . The method is called directly and then can be used in the similar manner.    In order to remove the methods (on in this case - attribute) , we can use
del module.attribute

       Namespaces are created at different times and have different life spans. They are some that are called when a module is imported into the terminal . Also there are global variables within a namespace , local variables within a namespace .

In the below code some clarification can be achieved.
Loading ....

   The namespaces are important before going into the details . As because it forms the zen of python .
For details about zen of python . type into your python interpreter
import this

So , we will be continuing with python classes and metaclasses very soon.

my math to code methodology

 
        To code - I guess it is a pretty awkward thing to do , to make or implement an algorithm is a sane thing to do. Coding in itself is not language oriented , yet it influences the thinking pattern . I have used FORTRAN , MATLAB , Ruby and Python intensely . Only thing i was about to make out was before you look into code look at the mathematics, develop algorithms and chart out a flowchart.

      The great codes are well structured in nature and have room to grow. The language itself presents the ease of implementing the algorithms with ease or without ease. A normal software professional approach is pragmatic and patched one . An engineer's approach is planned , mathematical and detailed. Precision needs a detail. It is always possible to improve upon code with the power of the programming language but it is not possible to improve algorithm as such.

     A few of the points i gained over the years are presented below:

1. Limiting oneself is  a key to create a better mathematical representation of the problem .

2. Lots of languages have a lot of modules built , to learn each of them is a mess - Before you actually code present the flowchart and psuedo code . Then eliminate the parts of it by intense search over the web.

3.  A great code is implementation oriented , not logic oriented . People have written better logic , but gets used by someone with better implementation.

4. The mathematics is very tricky thing to do. The way to convert maths into an algorithm that could be understood by computers is more important. The recommended steps to do that are :

  • Do not understand the method before solving the problem . Solve the example by looking at the methodology used. Why? - Because when you look at something incomprehensible to you means it is incomprehensible to do for computer too.
  • Once a problem is solved , see what parts of the solution follow logic without assumptions . Now understand the method and solve the problem .*( I am hoping the concepts are clear before even starting all this excercise.)
  • Solve the same problem again . See the mathematical flow of things , when you get that clear . You can jump to writing the flowchart.
  • Now write the psuedo code and each step is analysed with respect to language . 
  • Now jump start your coding !
5. Now when code , remember it is an iterative process . The same things are to be implemented . It is very well established that while finding the newer procedures or tricks you may try to change the algorithm and this is bad. 










Monday 4 June 2012

Import FORTRAN subroutine into Python

          We always want that our FORTRAN subroutines to work in python. Since i could not find any of that anywhere on net i thought i would give it a try . 

           So before starting lets check out things we have we should have a gcc compiler , gfortran   and f2py installed in our ubuntu linux systems. I would suggest you to check the system by typing f2py in the terminal. Usually it comes installed with the python. Also ubuntu system have python installed by default these days . 

         The next thing you can do is to write a subroutine in FORTRAN and this is advisable as we are going to convert it into FORTRAN object . 
         So Lets write a  subroutine in FORTRAN. It is as follows: 
Loading ....

        Then let us build it in c and then import this c module into FORTRAN using this shell command 
Loading ....

Here the the command builds (see -c flag, execute f2py without arguments to see the explanation of command line options) an extension module circle1.so (see -m flag) to the current directory. Now, in Python the FORTRAN subroutine circle1 is accessible via circle1.circle1

If incase an error like this comes :
Loading ....

Then just install python 2.7-dev

sudo apt-get install python2.7-dev

Build again and then no error should come.
 
In the command lets call the python interpreter to check whether we can import FORTRAN subroutine circle1 : 
Loading ....






Saturday 2 June 2012

scilab tutorial using script

      Hey , i have gone through a lot of bogs on modelling and simulation and found out various software regarding the same.

      I have learnt about matlab/simulink which is the most widely used by scientists and engineers and hence i will show you how to use scilab/scicos  a matlab/simulink alternative . In these series of blog posts i will teaching usage of scilab/scicos in a hands on approach . 

      So, in the first post i would like to tell you regarding getting scilab , mine is version 5.3.3 got it from here.  A few things we need to know before we begin . 
1. Scilab is a scripting language interpreted by the scilab compiler .
2. Scilab reads a vast inbuilt functions and also can read hardcoded c or fortran code . 
3. We can develop our own toolboxes , customize them to suit our specific use . Since this can certainly make it more advantageous. 
4. Also we have a matlab to scilab interpreter so do not worry :)

In this first post i will tell you regarding scripting ; It has .sci and .sce file extensions . .sce reads that the code has inbuilt functions and .sci reads we have some customized code. Its just a convention 






 In this section i will show how a  .sce  file is written and used. 
First create a ex1.sce file in your directory.
Best way is usually to use the
Scilab Editor
 You can fill it up with some code like this 
Loading ....

Friday 1 June 2012

Import data from excel to python using xlrd

To import data from excel to python the most ideal way to do is use xlrd. You can download this package from here We can create an excel file in the folder for example say "excel.xls" Then we can input data from excel in such a manner
Loading ....