# HG changeset patch # User Madhusudan.C.S # Date 1249801814 -19800 # Node ID 327b3f0b73bb62e87833fec45594b7958cbf99cf # Parent 0c9bdcfac9f70f27c0cbd7639ae79f82115efc0b Added data fetching scripts. diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/scripts/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/scripts/__init__.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,3 @@ +__authors__ = [ + '"Madhusudan.C.S" ', +] \ No newline at end of file diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/scripts/fetch_data.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/scripts/fetch_data.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,54 @@ +"""Module to fetch data. +""" + +__authors__ = [ + '"Madhusudan.C.S" ', +] + + +import json + + +def fetch_state_code(file_name): + """Fetch State Codes + """ + + fh = file(file_name) + states = {} + for line in fh: + name, code = line.split('\t') + name = name.strip() + code = code.strip() + states[code] = name + print json.dumps(states, indent=4) + +def fetch_district_code(file_name, write_file): + """Fetch District Codes + """ + + fh = file(file_name) + districts = {} + for line in fh: + if line == '\n' or line[:4] == 'See ' or line[:4] == 'Code': + continue + if line[:6] == '[edit]': + state = line[6:].strip().split()[-1].strip('()') + continue + district_line = line.split('\t') + code = '%s%s' % (state, district_line[0].strip()) + name = district_line[1].strip() + districts[code] = name + if len(code) > 4: + print code, name + fh.close() + district_data = json.dumps(districts, indent=6) + fh = file(write_file, "w") + fh.write(district_data) + fh.close() + +if __name__ == '__main__': + import sys + if sys.argv[1] == 'fetch_state_code': + fetch_state_code(sys.argv[2]) + if sys.argv[1] == 'fetch_district_code': + fetch_district_code(sys.argv[2], sys.argv[3]) \ No newline at end of file diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/07/arm.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/07/arm.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,6 @@ +for i in range(100, 1000): + a = i % 10 + b = (i / 10) % 10 + c = (i / 100) % 10 + if i == a ** 3 + b ** 3 + c ** 3: + print "Armstrong Number: ", i diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/07/collatz.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/07/collatz.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,7 @@ +a = 343 +while a > 1: + print a + if a % 2: + a = a * 3 + 1 + else: + a /= 2 diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/07/gcd.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/07/gcd.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,6 @@ +def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a %b) + +print gcd (10, 20) diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/07/proxy.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/07/proxy.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,93 @@ +# urllib2 opener to connection through a proxy using the CONNECT method, (useful for SSL) +# tested with python 2.4 + +import urllib2 +import urllib +import httplib +import socket + + +class ProxyHTTPConnection(httplib.HTTPConnection): + + _ports = {'http' : 80, 'https' : 443} + + + def request(self, method, url, body=None, headers={}): + #request is called before connect, so can interpret url and get + #real host/port to be used to make CONNECT request to proxy + proto, rest = urllib.splittype(url) + if proto is None: + raise ValueError, "unknown URL type: %s" % url + #get host + host, rest = urllib.splithost(rest) + #try to get port + host, port = urllib.splitport(host) + #if port is not defined try to get from proto + if port is None: + try: + port = self._ports[proto] + except KeyError: + raise ValueError, "unknown protocol for: %s" % url + self._real_host = host + self._real_port = port + httplib.HTTPConnection.request(self, method, url, body, headers) + + + def connect(self): + httplib.HTTPConnection.connect(self) + #send proxy CONNECT request + self.send("CONNECT %s:%d HTTP/1.0\r\n\r\n" % (self._real_host, self._real_port)) + #expect a HTTP/1.0 200 Connection established + response = self.response_class(self.sock, strict=self.strict, method=self._method) + (version, code, message) = response._read_status() + #probably here we can handle auth requests... + if code != 200: + #proxy returned and error, abort connection, and raise exception + self.close() + raise socket.error, "Proxy connection failed: %d %s" % (code, message.strip()) + #eat up header block from proxy.... + while True: + #should not use directly fp probablu + line = response.fp.readline() + if line == '\r\n': break + + +class ProxyHTTPSConnection(ProxyHTTPConnection): + + default_port = 443 + + def __init__(self, host, port = None, key_file = None, cert_file = None, strict = None): + ProxyHTTPConnection.__init__(self, host, port) + self.key_file = key_file + self.cert_file = cert_file + + def connect(self): + ProxyHTTPConnection.connect(self) + #make the sock ssl-aware + ssl = socket.ssl(self.sock, self.key_file, self.cert_file) + self.sock = httplib.FakeSocket(self.sock, ssl) + + +class ConnectHTTPHandler(urllib2.HTTPHandler): + + def do_open(self, http_class, req): + return urllib2.HTTPHandler.do_open(self, ProxyHTTPConnection, req) + + +class ConnectHTTPSHandler(urllib2.HTTPSHandler): + + def do_open(self, http_class, req): + return urllib2.HTTPSHandler.do_open(self, ProxyHTTPSConnection, req) + + +if __name__ == '__main__': + + import sys + + opener = urllib2.build_opener(ConnectHTTPHandler, ConnectHTTPSHandler) + urllib2.install_opener(opener) + req = urllib2.Request(url='http://google.com') + req.set_proxy('10.101.1.1:80', 'http') + f = urllib2.urlopen(req) + print f.read() + diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/07/pytriads.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/07/pytriads.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,6 @@ +def pytriads(): + for a in range(3, 100): + for b in range(a+1, 100): + if gcd(a, b) == 1: + + diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/07/test.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/07/test.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,5 @@ +a, b = 0, 1 +while b < 10: + print b, + a, b = b, a + b + diff -r 0c9bdcfac9f7 -r 327b3f0b73bb app/site-content/proposals/2009/08/08/gcd.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/site-content/proposals/2009/08/08/gcd.py Sun Aug 09 12:40:14 2009 +0530 @@ -0,0 +1,6 @@ +def gcd(a, b): + if a % b == 0: + return b + return gcd(b, a %b) + +print gcd (10, 20)