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 -*-

PHP: How to display contents from a file into textarea

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.

Funny and Lame Job regarding Adsense Earning

There are lot of funny job postings available at freelancing sites which are not only funny but also hilariously lame. One such job posting I use to see more frequently is "Make me a blog that will earn +15 USD per day". For such projects, buyers are always keen to pay a very little amount to the freelancers.

But that's not the issue. The issue is. If I can make a site or blog that can earn +15 USD per month, then why should I make this for you? Why don't I make it for me? Whenever I saw such job postings at oDesk or Freelancer, I just laugh at my fucking brains out! Some freelancers are even placing their bids on such stupid projects either. I don't know based on which though they have posted the bids =))

Saturday, November 9, 2013

Python: Get function name programaticaly

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.

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!

Python: Extracting an attribute value with beautifulsoup

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"})