Thursday 23 April 2015

Extracting Live Cricket Scores

One thing every beginner should know about Python is that only with a few lines of code one can pull off pretty interesting stuff. 

When I meet people who want to start learning python, I typically ask them about what they are interested in apart from work/academics. I find some people crazy about gadgets, some people who are just too much into movies, some fitness freaks while some are interested in travelling. I believe everyone can be motivated to learn python (yes, it is that cool!)

Here is what worked for me when we were to build a python dev team with these guys, everyone was asked to write a small piece of code that would scrap some sort of data from the web:

- The gadget guy had to list the top 10 mobiles on flipkart.com
- The fitness freak had to list out the top 10 proteins' supplements on healthkart.com
- The movie maniac had to list top 10 movies on imdb.com
- The traveler had to list top 10 destinations in India on tripadvisor.com

They all came up with wonderful pieces of python code and were motivated to learn python further!


Let us take another similar example. Since the IPL season is on (and since I am a big Fan) in this post we shall see how one can print live scores using Python. The approach to solve this problem is quite similar. Here is what we need:

- A data source which maintains live cricket scores [www.espncricinfo.com]
- A way by which one can request for data from the selected source [python requests]
- A mechanism to extract only the required data [regular expressions]

If we think about it, the above three requirements are pretty generic and a simple python code can be written that would scrap (extract) the required information from the web. Below is the code that did the job for me:


Note: This was meant for a quick script that could easily do the job. For more complex web content parsing and scraping, consider using BeautifulSoup and Scrapy!

Keep Coding! :)

Saturday 22 November 2014

Getting started with Python

There are a lot of IDEs out there for developing standard (fancy) code in python. You can always follow the clearly listed out steps on their respective websites and get your favorite one set up. But hey! you are just getting started, and if you an impatient freak like myself, then you may not want to spend two hours just to install a fancy IDE and write your first "Hello World!" code. Let us do it the quick way first and then we can jump in to more serious stuff. [Pycharm 4 just got released. Yay!]

The fastest way is to get hands on something that will let you write and execute simple one line python code. IPython notebook is an obvious choice for such purposes. Below are the steps to install IPython on your windows machine:

Step 1. Get Python

Got to https://www.python.org/downloads/ and get Python 2.7.8


You may wonder why 2.7.8 and not 3.4.2. Python is currently being supported both in the legacy 2.x.x versions and the new 3.x.x versions. Here are a few reasons to still go with 2.x.x :

  • All the ready made linux distributions still have 2.x.x on them
  • More help available via documentation, blogs and stack overflow
  • And the most important one, you are a beginner and it doesn't matter! :P 

Step 2. Install setup tools


Get the installation script from https://bootstrap.pypa.io/ez_setup.py

Just run the "ez_setup.py" by double clicking on the downloaded ez_setup.py file
You shall see a window pop up like this:


Step 3. Install IPython

In command prompt type :
  •  cmd> C:\Python27\Scripts\easy_install  tornado 
  • cmd> C:\Python27\Scripts\easy_install  pyzmq
  • cmd> C:\Python27\Scripts\easy_install  jinja2
  • cmd> C:\Python27\Scripts\easy_install  ipython



Step 4. Run IPython 
  • cmd> C:\Python27\Scripts\ipython.exe notebook
  • The NotebookApp shall get started in your default web browser
  • Click on add notebook
  • Type: print "Hello World!"
  • Press Shit+Enter


                                                   


Sunday 9 November 2014

Life is short, you need python

Let us start with a dramatic background, it is always fun that way. When I was in college, they taught me how to code in C/C++. I was expected to solve the traveling salesman problem, the knapsack problem, Graph isomorphism problem etc. Like everyone else, I too was solving (copying the code from somewhere) all such problems although I had no motivation to do so.  The biggest problem probably was the nature of the problems I was banging my head against. They were simply not relevant for me then (of course these are meant to develop fundamental computer science problem solving skills, but no, I was in college and I had to be doing something cooler than that).

Maybe they should have started with something simple yet interesting. How about a piece of code that will get my friends’ birthday and contact information from social media? Or how cool would have been an assignment asking me to create a program that would download latest songs from the internet?  

Engineering students are stubborn by nature and hence they create things beating all odds and probabilities. They can’t be instructed to learn concepts and formulae. They rather have to be fooled in to creating something which they believe is cool and on their way of crafting that something, hope they’d learn.

Cooking food or doing laundry, there are essentially many correct ways of carrying out tasks human beings may have to do in their lives. For us engineers, it works in a little different way. (Warning, incoming strong opinion) There are some easy but inefficient ways of programming (which most of us including yours truly just loved to go for back in college), then there are tough but efficient ways (which are good but take a toll on the most critical resource we have in the corporate, time) and then there exist the pythonic ways (I believe if God is a programmer, he uses python). Life is too short to be coding in traditional programming languages anymore, you need python (Yes, I will get offended, find where you live and maybe tell your mom if you do not agree with me) :P

Picture Source - xkcd.com

Though this blog I intend to share the true love and whatever little experience I have with python. Keep watching this space for more!