Operator overloading in Python

Very recently my friend Hari wrote an article about operator overloading in ruby, and he asked me to implement the same program which he used in his article in python. His aim was to compare the two hottest languages of technology lovers.

Though our example is very simple and straight forward, we consider this as a stepping stone for our comparative study. Below we are giving our example which compares the operator overloading for ‘+’ (addition).
I expect you know about operator overloading. If you dont know, in simple words, it is just like giving special meaning to a language’s operators(‘+’, ‘-‘,’/’,’*’ etc) via new definition though class methods.Read more about operator overloading from here.
In python, operator overloading is achieved via overriding python’s special methods from a python class. If you want to do operator overloading in python you should know about it’s special methods ( which is also called magic methods). Python has a number of special methods, which should be of the form __xxx__. The list is very big, but most commonly used ones are, __add__, __sub__, __repr__, __str__, __len__, __gt__, __lt__ etc. you will get the complete list from here.
lets see how we can give special meaning to __add___. I hope you should be knowing that we cant add two objects, we can add only two numbers. In python you can use ‘+’ to append two strings too. But look how we can add special meaning to ‘+’ to add two objects.
class Add(object):
    def __init__(self, val):
        self.val = val
 
    def __add__(self, obj):
        return self.val + obj.val
 
a = Add(10)
b = Add(20)
 
print a + b
as you expect this will print 30. In order to understand how this works, you can read the print statement like this.  print a.__add__(b)
a = Add(‘Hello’)
b = Add(‘ World’)
print a + b   #This will print ‘Hello World’.
 
The above examples are straight forward use of  ‘+’ operator. We can further extend our __add__ method to intelligently add different data types. Normally python wont allow to add different datatypes. ( try some examples on your python shell)
Here is our extended __add__  method. And see the examples.
class Add(object):
    def __init__(self, val):
        self.val = val
 
    def __add__(self, obj):
        if isinstance(self.val, str) or isinstance(obj.val, str):
            return str(self.val) + str(obj.val)
        return self.val + obj.val
 
a = Add(10)
b = Add(20)
 
print a + b  #30
 
c = Add(‘Hello’)
d = Add(‘ World’)
 
print c + d  # Hello world
 
e = Add(5)
f = Add(‘ is my number.’)
 
print e + f  # 5 is my number
 
In the above definition of __add__ we are checking whether any of the value is a string, if then we will append the two values, otherwise add the values.

Basic authentication mechanism using OAuth.

The current third party apps revolution led to the scenario where apps need to access user’s private data from different websites. In order to access user’s data from other sites (especially personal data) , we have to go through a basic authentication mechanism. Many of them follows OAuth.  You can read more about OAuth from here.

Here am just sharing a diagrammatic representation of how we are establishing a secure connection with a distant website using OAuth. My intention is to give an high level overview of OAuth mechanism.

Inline image 1

1) Initially we will send a get request to the url from where you intend to get data and you will provide some information such as our application id, scope of data access ( r/w permission for different data sets, eg read_products, write_tweet), and redirect_uri(url to redirect after authentication) for authentication purpose.

2)If user grant the access to the specified scope,  we will receive a code(long id) and some other details as response from the site.
3) We are interested only in the code, which we will send back to the website/url(normally this url will be given in developer Api) for getting an access_token.  We use this access token for establishing a secure connection. We can save this access_token for future use also. That is once a connection is established and we got access token we can receive user data at anytime until access_token expires or user revoke their permission.

A little about HTACCESS

Introduction.
htaccess is simply a server side configuration file, used for webserver configuration management. Which contain some instructions in a text file. And those instructions can be used to improve your server settings and do many other helpful things such as customize your own 404 page, rewrite long urls with more readable ones, disable directory listing, put security restrictions for a directory etc.

How to add htaccess file to Server:
What we will do is, just create a file with the name .htaccess and put it in our webserver directory. This file would contain some instructions (we call them rules ) in a pre-specified format, that webserver can understand. We would discuss about the rules and its structure in more detail.

Things we can acheive with htaccess rules:

  • Redirect / Rewrite urls
  • Password protection for directories
  • Deny visitors by IP address
  • Adding MIME types ( instruct the server how to treat different varying file types)
  • Disable directory listings
  • Preventing access to your PHP includes files
  • Prevent access to php.ini
  • Forcing scripts to display as source code
  • Ensuring media files are downloaded instead of played
  • Setting up Associations for Encoded Files
  • Preventing requests with invalid characters

Lets write a simple .htaccess file:
We will write a simple .htaccess file to show how we can redirect one url to another.
Some times our url will be long and complex to the user. In those times you can simply replace such long urls with more readable and search engine optimised urls .

This is how we acheive url redirection through htaccess rule.

RewriteEngine On
RewriteRule  ^myurl.*$  http://example.com/   [NC,L]

This rule in .htaccess file will redirect request http://www.example.com/myurl to http://www.example.com/

Explanation:

a general format of the rule for url redirect will be
Redirect  /old_url/   http://www.yourdomain.com/new_url [flags]

Now lets do cross section in the real example:
RewriteEngine On  : will turn on the rewriting engine. We will write this line only once in a
.htaccess file.

RewriteRule   ^myurl.*$    http://example.com/   [NC,L]

The above line specify the rule we want to apply.
^myurl.*$ :  The server will compare the url of all the requests to  this pattern. If it matches
then it will replace the pattern with the following argument in rewrite rule. ( here it is
http://www.example.com/ ).

[NC,L] : These are  “Flags”, that tell Apache how to apply the rule. “NC”, tells Apache that this rule should be case-insensitive, and “L” tells Apache not to process any more rules if this one is used.

Condition
Now lets try another example, To redirect an old domain to a new domain. That is if you have chnaged your website domain name to a new name with out changing its directory structure, you can put this .htaccess file in your webserver , which will redirect all requests to old domain to new domain.

RewriteEngine On
RewriteCond  %{HTTP_HOST}    old_domain\.com [NC]
RewriteRule  ^(.*)$    http://www.new_domain.com/$1 [L,R=301]

Here we have a RewriteCond (rewrite condition) preceded by RewriteRule. Which is the most common case in htaccess files. By using rewriteCond before rewrite rule we can limit the RewriteRule only if that condition is met. We can also write multiple condition using and, or.

How RewriteCond works?
General format:   RewriteCond server variable string

The RewriteCond operates in a similar way with RewriteRule. The string to test can be a variety of things. Such as which browser used, the IP address etc, it depends on the server variable we have specified. In the above example we have used the HTTP_HOST variable. It verifies whether the given string matches the host domain, and if it does do RewriteRule.

List of server variable that we can use:

HTTP Headers
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT

Connection Variables
REMOTE_ADDR
REMOTE_HOST
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD
SCRIPT_FILENAME
PATH_INFO
QUERY_STRING
AUTH_TYPE

Server Variables
DOCUMENT_ROOT
SERVER_ADMIN
SERVER_NAME
SERVER_ADDR
SERVER_PORT
SERVER_PROTOCOL
SERVER_SOFTWARE

Dates and Times
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME

Special Items
API_VERSION
THE_REQUEST
REQUEST_URI
REQUEST_FILENAME
IS_SUBREQ

List of flags that we can use:

C (chained with next rule)
CO=cookie (set specified cookie)
E=var:value (set environment variable var to value)
F (forbidden – sends a 403 header to the user)
G (gone – no longer exists)
H=handler (set handler)
L (last – stop processing rules)
N (next – continue processing rules)
NC (case insensitive)
NE (do not escape special URL characters in output)
NS (ignore this rule if the request is a subrequest)
P (proxy – i.e., apache should grab the remote content specified in the substitution section and return it)
PT (pass through – use when processing URLs with additional handlers, e.g., mod_alias)
R (temporary redirect to new URL)
R=301 (permanent redirect to new URL)
QSA (append query string from request to substituted URL)
S=x (skip next x rules)
T=mime-type (force specified mime type)

References:
http://www.addedbytes.com/for-beginners/url-rewriting-for-beginners/
http://perishablepress.com/stupid-htaccess-tricks/
http://www.htaccess-guide.com/
http://www.freewebmasterhelp.com/tutorials/htaccess

Linux commands for daily usage

File/Basic commands
These are some useful basic linux commands. For every command I followed this convention.
1)The name of the command with a short description is given.
2)Some of the useful options that can be used with the command is given after the command.

ls – list the current directory contents.
-l long listing
-a list including hidden files
-t list by last modified

cat – show the content of a file.
format: cat [options] [file name(s)]
-n show the line numbers also.

head – output the first 10 lines of each FILE to standard output
format: head [options] [file name(s)]
–bytes=5k or -c 5k
-n specify the number of lines.

tail – output last part of file
same as head

sort – sort lines of text files
sort [OPTION]… [FILE]…
-n numerical sort
-f ingnore case
-r reverse the results
-R random
-d in dictinary order

free – Display amount of free and used memory in the system
free [-b|-k|-m|-g]
-b bytes
-k kilo bytes
-m mega bytes
-g giga bytes

Search commands:

find – search for files in a directory hierarchy
find [directory to search] [option] [filename]
eg:- find . -name abc.py

find [directory] -group gname (File belongs to group gname)

grep – print lines matching a pattern
grep [options] [pattern] [filename]
eg:
grep ‘head’ words.txt
grep ‘^head’ words.txt
grep ‘head$’ words.txt
grep ‘^head$’ words.txt
grep ‘^….$’ words.txt
grep ‘^h.*d$’ words.txt

sed – stream editor for filtering and transforming text
sed [OPTION]… {script-only-if-no-other-script} [input-file]…
two examples:
sed ‘s/cat/dog/g’ data ( substitute all cat with dog globally )
sed 2,4d data ( delete lines from 2 to 4 in file data)

Getting details from Youtube URL

We can easily get many details from a youtube url using some of Python libraries. Here am going to show you how we can get
1)Video id
2)Video title
3)Author of video
4)Video thumbnails

We use Python’s urlparse, urllib and simplejson libraries to do this.

import urlparse
import urllib
import simplejson
url_data = urlparse.urlparse("http://www.youtube.com/watch?v=z_AbfPXTKms&NR=1")
video_id = urlparse.parse_qs(url_data.query)["v"][0]  # this is the video id
 
# from this url you will get all data you needed about video, by just replacing the video id
url = 'http://gdata.youtube.com/feeds/api/videos/%s?alt=json&v=2' % video_id 
json = simplejson.load(urllib.urlopen(url))

# this is the title of the video
title = json['entry']['title']['$t']  

# author of video               
author = json['entry']['author'][0]['name']['$t']
print "id:%s\nauthor:%s\ntitle:%s" % (id, author, title)

You will get thumbnails from this url: http://img.youtube.com/vi/video_id/image_id.jpg
image_id can be 0,1,2 or 3.