Shantanu's Blog

Corporate Consultant

July 30, 2012


Search By Image add-on for firefox

This extension allows you to initiate a Google search using any image on the web. Right click on an image and choose "search google with this image". This add-on is written by google team.



python tips 1

finally is executed always:

def divide(x, y):
         result = x / y
     except ZeroDivisionError:
         print "division by zero!"
         print "result is", result
         print "executing finally clause"

Predefined Clean-up Actions:

for line in open("myfile.txt"):
    print line

The problem with this code is that it leaves the file open for an indeterminate amount of time after the code has finished executing. This is not an issue in simple scripts, but can be a problem for larger applications. The with statement allows objects like files to be used in a way that ensures they are always cleaned up promptly and correctly.

with open("myfile.txt") as f:
    for line in f:
        print line

After the statement is executed, the file f is always closed, even if a problem was encountered while processing the lines.


python at command prompt

Python accepts positional parameters just like shell script as shown below.

$ cat
# Fibonacci numbers module

def fib(n):    # write Fibonacci series up to n
    a, b = 0, 1
    while b < n:
        print b,
        a, b = b, a+b

def fib2(n): # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        a, b = b, a+b
    return result

if __name__ == "__main__":
    import sys

$ python 50
1 1 2 3 5 8 13 21 34


July 28, 2012


Python interface to AWS

Boto is a Python (2.6) package that provides interfaces to Amazon Web Services. In addition, boto provides support for other public services such as Google Storage and other private cloud systems like Eucalyptus.

easy_install boto
>>> import boto
>>> conn = boto.connect_ec2('', '')
>>> conn.run_instances('ami-cc5af9a5',key_name='test_jul28',instance_type='t1.micro',security_groups=['redis'])

>>> conn.stop_instances(instance_ids=['i-e286909a'])
>>> conn.start_instances(instance_ids=['i-e286909a'])
>>> conn.terminate_instances(instance_ids=['i-e286909a'])

Labels: ,

July 26, 2012


Collect images and media files from a site

FoxySpider firefox extension is your personal web crawler! It can crawl into any website and find what you really want (video clips, images, music files, etc.). FoxySpider displays the located items in a well-structured thumbnail gallery for ease of use.

July 16, 2012


Amazon SNS

Amazon Simple Notification Service is a web service that makes it easy to set up, operate, and send notifications in the cloud. To start using Amazon SNS, create a topic. Topics allow you to publish notifications to subscribers.

Here is the script that will do the needful.

# Simple Notification Service
export AWS_SNS_HOME=/home/sns
#export AWS_CREDENTIAL_FILE=/root/credential-file
export JAVA_HOME=/usr/java/default
export PATH=${PATH}:${AWS_SNS_HOME}/bin
sns-publish arn:aws:sns:us-east-1:123:NotifyMe --message "A server started as on $START $DATA" --subject "AWS server alert!"

Once the topic is created, click on "edit topic" to change the following:

Allow these users to publish messages to this topic: Everyone

Allow these users to subscribe to this topic:
Only users with endpoints that match: *

The administrator can add email addresses of the employees to the list. The employees will receive the alerts along with the unsubscribe link. They can stop getting the alerts any time.


July 04, 2012



Entity–attribute–value model (EAV) also known as vertical database model is a data model to describe entities where the number of attributes are not known or are frequently changing. This is fundamentally different than SQL model and better than key-value store like Redis. Installation Cassandra is straight forward and commands are similar to redis set / get.

create keyspace abc;

use abc;

create column family users with comparator=AsciiType and column_metadata=[{column_name: full_name, validation_class: AsciiType}, {column_name: state, validation_class: AsciiType, index_type: KEYS}, {column_name: birth_date, validation_class: LongType, index_type: KEYS}];

assume users VALIDATOR as ascii;  
assume users COMPARATOR as ascii;  
assume users KEYS as ascii;  
assume users SUB_COMPARATOR as ascii;  

set users[bsanderson][full_name] = 'Brandon Sanderson';
set users[bsanderson][birth_date] = 1975;
set users[prothfuss][full_name] = 'Patrick Rothfuss';
set users[prothfuss][birth_date] = 1973;
set users[htayler][full_name] = 'Howard Tayler';
set users[htayler][birth_date] = 1968;

set users[bsanderson][state] = 'UT';
set users[prothfuss][state] = 'WI';
set users[htayler][state] = 'UT';

get users where birth_date = 1973;
get users where state = 'UT';
get users where state = 'UT' and birth_date > 1970;

Labels: , ,

July 03, 2012


Route 53

Route 53 allows us to have dynamic IP assigned to load balancers. For e.g.
I can have a domain name "" that will point to a mysql load-balancer hosted in the region uswest2 named
I can also have a domain "" that will point to another load balancer in some other region, for e.g. usEast

All the delivery boxes from that respective zones will talk to their own address (like and not the load balancer address. The advantage is that if uswest mysql balancer has some issues, the traffic can easily be routed to useast mysql load balancer by making chagnes to route 53. Another advantage is that we can replace the load balancer without affecting the operations.
Here is an image from flickr.

This can also be used for Creating Latency Resource Record Sets.

Route 53 latency-based routing lets you use DNS to route end-user requests to the EC2 region that will give your users the fastest response.

July 01, 2012


Instance Stats

Amazon instance statistics can be returned by querying internally to an IP address as shown in this shell script. Run this script from instance or execute following curl

curl | sh


ami_id=`curl 2>/dev/null`
ami_launch_index=`curl 2>/dev/null`
ami_manifest_path=`curl 2>/dev/null`
block_device_mapping=`curl 2>/dev/null`
hostname=`curl 2>/dev/null`
instance_action=`curl 2>/dev/null`
instance_id=`curl 2>/dev/null`
instance_type=`curl 2>/dev/null`
kernel_id=`curl 2>/dev/null`
local_hostname=`curl 2>/dev/null`
local_ipv4=`curl 2>/dev/null`
mac=`curl 2>/dev/null`
metrics=`curl 2> /dev/null`
network_interfaces_macs=`curl 2>/dev/null`
placement_zone=`curl 2>/dev/null`
profile=`curl 2>/dev/null`
public_hostname=`curl 2>/dev/null`
public_ipv4=`curl 2>/dev/null`
public_keys=`curl 2>/dev/null`
ramdisk_id=`curl 2>/dev/null`
reservation_id=`curl 2>/dev/null`
security_groups=`curl 2>/dev/null`

# print the variables
echo "ami_id: $ami_id"
echo "ami_launch_index: $ami_launch_index"
echo "ami_manifest_path: $ami_manifest_path"
#echo "block_device_mapping: $block_device_mapping"
echo "hostname: $hostname"
echo "instance_action: $instance_action"
echo "instance_id: $instance_id"
echo "instance_type: $instance_type"
echo "kernel_id: $kernel_id"
echo "local_hostname: $local_hostname"
echo "local_ipv4: $local_ipv4"
echo "mac: $mac"
#echo "metrics: $metrics"
#echo "network_interfaces_macs: $network_interfaces_macs"
echo "placement_zone: $placement_zone"
echo "profile: $profile"
echo "public_hostname: $public_hostname"
echo "public_ipv4: $public_ipv4"
echo "public_keys: $public_keys"
echo "ramdisk_id: $ramdisk_id"
echo "reservation_id: $reservation_id"
echo "security_groups: $security_groups"



June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017  

This page is powered by Blogger. Isn't yours?