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

Tuesday, March 17, 2009

"reset is not a function" - Firebug error

I am a newbee to javascripting and I was stuck at reseting a form to default values for 2 full days..


Firebug kept saying
$("#addpropertyForm")[0].reset is not a function

Turned out that I had named the reset button as reset...!!
input type="reset" name="reset".....

Friday, March 13, 2009

Django serialization - Deserialization

I have a table
desc property;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| property_id | int(11) | NO | PRI | NULL | auto_increment |
| property_name | varchar(30) | NO | | NULL | |
| property_address_1 | varchar(120) | NO | | NULL | |
| property_address_2 | varchar(120) | YES | | NULL | |
| property_city | varchar(30) | NO | | NULL | |
| property_state | varchar(30) | NO | | NULL | |
| property_pincode | int(7) | NO | | NULL | |
| property_country | varchar(30) | NO | | NULL | |
| property_user_id | int(11) | NO | MUL | NULL | |
| property_price | int(20) | NO | | NULL | |
+--------------------+--------------+------+-----+---------+----------------+

Serialize

python manage.py shell
Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49)
>>> from users.models import Property
>>> from django.core import serializers

>>> json = serializers.serialize("json", Property.objects.filter(pk=2)
>>> print json
[{"pk": 1, "model": "users.property", "fields": {"property_user_id": 1, "property_state": "aa", "property_country": "aa", "property_pincode": 323, "property_city": "aa", "property_price": 1, "property_address_1": "aa", "property_name": "aa", "property_address_2": "aa"}}]



Deserialize
>>> json = '[{"pk": 2, "model": "users.property", "fields": {"property_user_id": 2, "property_state": "aa", "property_country": "aa", "property_pincode": 323, "property_city": "aa", "property_price": 1, "property_address_1": "aa", "property_name": "aa", "property_address_2": "aa"}}]'

>>> for obj in serializers.deserialize("json", json):
... obj.save()


I finally dint use it because I dint know how to convert a UTF encoded dictionary to string.. shame on me..!!

Wednesday, March 11, 2009

foreign key across different mysql engines

I had a pre existing table(MYISAM) and I was creating new table innodb.
Dint realize for a long time that you cant have a foreign key relationship across 2 tables of different engines.
Error that you would see are

mysql> alter table property add (FOREIGN KEY(id) REFERENCES AUTH_USER(id));
ERROR 1005 (HY000): Can't create table 'sportland.#sql-134d_a' (errno: 150)
mysql> show engine innodb status;
------------------------
LATEST FOREIGN KEY ERROR
------------------------
090311 15:36:27 Error in foreign key constraint of table sportland/#sql-134d_a:
FOREIGN KEY(id) REFERENCES AUTH_USER(id)):
Cannot resolve table name close to:
(id))

took me sometime to figure out the issue..!!