Sunday, November 26, 2017

Get function name programaticaly - Python

This little piece of code will help you to get the function name programatically. This is very helpful when you are implementing the debugging prints from your program.

import sys

def new_function():
    function_name = sys._getframe().f_code.co_name
    print function_name


You of course can write the function name manually(function_name="new_function"). But that case if you change the function's name, you have to change the string as well from the variable function_name.

Wednesday, November 27, 2013

SyntaxError: Non-ASCII character '\xe9' in file on line 22

The Error:

SyntaxError: Non-ASCII character '\xe9' in file on line 22, but no encoding declared; see for details

This is mainly occurred when you are trying to print the unicode text into the console. Basically the error itself has the solution. Browsing the link will show you what exactly have to be done.

Just writing the below code at the very first or the second line at the script. This will set the encoding of the console and will evade the error you are encountering.

# -*- coding: utf-8 -*-

How to display contents from a file into textarea - PHP

How to display contents from a file into the textarea of HTML?

This is very easy and slightly tricky. Here is the PHP code:

    print '<textarea>';
    print '</textarea>';

Basically you are including the whole contents from the file into the PHP source which later rendered as HTML. Its just a tricky but effective solution.

Saturday, November 9, 2013

Upgrading Python at Linux without YUM

Upgrading the Python at Linux box is slightly risky as the Yum is heavily dependent upon the Python. You have to be very careful while doing this on linux.

I was having the Python 2.4.3 as default with my linux installation. To upgrade the Python to 2.7.3 I have followed the below instructions. It was simple. Just downloading, compiling and installing.

Notes: Make sure you have the linux development tools, gcc, zlib and similar things are already installed. Otherwise you can not perform the following tasks.

1) Download the Python.

2) Extract the zip file.
tar xf Python-2.7.3.tar.bz2

3) Enter inside the extracted directory.
cd Python-2.7.3

4) Configuring the installation.
./configure --prefix=/usr/local

5) Make and install at the same time.
make && make altinstall

6) Add symbolic link in /usr/local/bin.
ln -s /usr/local/bin/python2.7 /usr/local/bin/python

That's it. Enjoy your upgraded Python!

Extracting an attribute value with beautifulsoup - Python

Previously I have demonstrate to parse the div content from the html source. Now I am showing you how to parse the attribute value from a desired tag from a given html.

For this example, lets consider the the attribute name is __VIEWSTATE. This is very important when you are scraping the sites of Microsoft. In this example I am going to show you how to parse this __VIEWSTATE using beautifulsoup.

soup = BeautifulSoup(content)
viewstate = soup.findAll("input", {"type": "hidden", "name": "__VIEWSTATE"})
print viewstate[0]['value']

__VIEWSTATE mainly resides as hidden parameter under input of the forms. So I have looked for the tag named input that is having a type=hidden and name=__VIEWSTATE. Example code returns all the matches from the html.

Here, I have selected the first match(means index 0). But in your case it can be in different indexes if multiple matches are found. Also, its very important to check the count before accessing the array(using function len()). Otherwise Python will popup with an Array index out of bound exception in case no matches are found.

Python: Get div content using BeautifulSoup

I am going to show you an example that will parse a div content from the given html. Lets consider the python variable content holds the html source in it.

soup = BeautifulSoup(content)
div_content = soup.find("div", {"class": "product"})
print div_content
print str(div_content)

In this example, the code is basically parsing the div tags which are only having the class=product. You can add more attributes like id=... and so on. For example,

div_content = soup.find("div", {"class": "product", "id": "myDiv"})

Python: How to install BeautifulSoup

Python library BeautifulSoup is very handy while handling the html parsing and other tasks. This makes life easier for the scrappers to parse the data, specially those who are not that much expert on REGEX. This is much simpler than REGEX and any a newbie developer can even write a scraper using this BeautifulSoup.

Here is how you can install this BeautifulSoup on your development PC.

1) Directly using the pip. The command is like below:

pip install BeautifulSoup

2) If you do not have the pip installed with your Python(it doesn't come by default with Python installation). That case you can install the BeautifulSoup soup by following ways.

a) Download the latest BeautifulSoup from the link: BeautifulSoup

b) Unzip this into a folder.

c) Enter into the folder and run the following command to install BeautifulSoup.

python install

3) Or simply you can place the file with your python source code. It will automatically be loaded from your Python script. But its not recommended as you have to move this file from folder to folder and it looks ugly.