Tuesday, January 5, 2010

ScreenCast in Ubuntu

a. Install gtk-recordmydesktop
Record your first screen cast using this software

b. Install Audacity: To remove noise
Get a wav file to be used with Audacity
ffmpeg -i out.ogv out.wav

c. Merge the edited wav file with original video file(Also convert it to avi)
mencoder -audiofile out.wav -oac mp3lame -ovc xvid -xvidencopts fixed_quant=4 -o out.avi out.ogv

Friday, August 7, 2009

Postgres from python

This snippet contains no validation whatsoever.

import psycopg2

connection = psycopg2.connect('dbname=product user=postgres host=127.0.0.1')
mark = connection.cursor()
query = """INSERT INTO PRODUCT(product_id, product, attribute) VALUES(%d, '%s', '%s')"""
statement = query % (1, "name", "attribute")
mark.execute(statement)
connection.commit()

Thursday, August 6, 2009

GeoIP with Maxmind's Geolite

I was searching for a python tool to determine approximate location from IP address.


You need to download 3 tars:

a. Geolite C library- Python module internally uses this C library
http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz

tar -xvzf GeoIP.tar.gz
cd GeoIP-1.4.6
./configure --libdir=/usr/lib/python2.5/site-packages/
make
make install

b. Python api:
http://geolite.maxmind.com/download/geoip/api/python/GeoIP-Python-1.2.4.tar.gz

tar -xvzf GeoIP-Python-1.2.4.tar.gz
cd GeoIP-Python-1.2.4

# Edit setup.py to adjust the library_dirs and include_dirs
library_dirs = ['/usr/lib/python2.5/site-packages/'],

python setup.py build
sudo python setup.py install

c. Download Data file (This you probably need to update periodically)
http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz

Test your setup
--------------------------
(From: http://stackoverflow.com/questions/1163136/how-to-detect-the-country-and-city-of-a-user-accessing-your-site)

I do not completely understand how variables in library are accessed from python.. So I export this variable
export LD_LIBRARY_PATH=/usr/lib/python2.5/site-packages/

import GeoIP
gi = GeoIP.open("GeoLiteCity.dat", GeoIP.GEOIP_INDEX_CACHE | GeoIP.GEOIP_CHECK_CACHE)
print gi.record_by_name("74.125.67.100") # a www.google.com IP

{'city': 'Mountain View', 'region_name': 'California', 'region': 'CA', 'area_code': 650, 'time_zone': 'America/Los_Angeles', 'longitude': -122.05740356445312, 'country_code3': 'USA', 'latitude': 37.419200897216797, 'postal_code': '94043', 'dma_code': 807, 'country_code': 'US', 'country_name': 'United States'}

Monday, July 6, 2009

Simple unit convertor in python

This function uses the google calculator api
http://www.google.com/ig/calculator?hl=en&q=1EUR%3D%3FUSD

import urllib, urllib2
import socket

# Crawl header #
default_header = { 'User-Agent' :
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.5) Gecko/2008121622 Ubuntu/8.10 (intrepid) Firefox/3.0.5;'}

def getpage(pageurl, pageargs = None, pageheaderargs = {}):
fail = 0
content = reply_header = None
pageheaders = default_header
pageheaders.update(pageheaderargs)
while True:
print pageurl
retry = 0
try:
params = urllib.urlencode(pageargs) if pageargs else None
req = urllib2.Request(pageurl, params, pageheaders)
page = urllib2.urlopen(req)
content = page.read()
reply_header = str(page.headers).split('\r\n')
page.close()
except urllib2.HTTPError, e:
retry = 1
except IOError, e:
retry = 1
except socket.timeout, e:
retry = 1
except socket.sslerror, e:
retry = 1
if retry:
fail += 1
if fail == 5:
print 'Failed to retrieve' + pageurl + ': ' + str(e)
return (reply_header, content)
else:
continue
break
return (reply_header, content)

def convert(unit1, unit2):
query = '1%s=?%s' % (unit1, unit2)
(header, content) = getpage('http://www.google.com/ig/calculator?hl=en&q=' + query)
reresult = re.search('rhs: \"(.*?) ', content)
result = reresult.expand(r'\1') if reresult else 'Error'
return result if result != '' else 'Error'

Wednesday, June 24, 2009

Python string strip function..

I just realized a super blunder, I have committed all my life

url = 'abcdc.com'
print url.strip('.com')
Expect: abcdc
Resut: abcd

Instead try
url.rsplit('.com', 1)

strip function takes in an array of characters and chops any pattern of characters in the array.. :(

Friday, June 12, 2009

Lambda function and class in python

I had a hash with hash value an object. I wanted to sort by one of the elements (integer) of the class.. Here is neat way..

class cl:
def __init__(self, val):
self.val = val

arr = {'a' : cl(10), 'ab' : cl(75), 'bd' : cl(50)}

g = lambda (x, y): y.val
for (x, y) in sorted(arr.iteritems(), key = g):
print x + ' ' + str(y.val)

Wednesday, June 3, 2009

Mercurial on code.google

I played with mercurial on code.google.. Here are some simple instructions

a. Create the project in the interface
b. Create a local repository

hg clone https://USERNAME@PROJECT.googlecode.com/hg PROJECTDIR

When it prompts for a password, key-in the password available on
http://code.google.com/hosting/settings (Profile > Settings)

I created trunk directory and stored all the code.. (to allow branching).. This probably not mercurial way and is svnish..

c. I prefer to edit wiki through mercurial..

hg clone https://wiki.PROJECT.googlecode.com/hg wiki-PROJECTDIR