Skip to content

Instantly share code, notes, and snippets.

View cjgiridhar's full-sized avatar

Chetan Giridhar cjgiridhar

View GitHub Profile
@cjgiridhar
cjgiridhar / tornadoasyncex.py
Created September 17, 2014 05:35
Tornado Async Example
import tornado.ioloop
from tornado.httpclient import AsyncHTTPClient
def handle_request(response):
'''callback needed when a response arrive'''
if response.error:
print("Error:", response.error)
else:
print('called')
print(response.body)
@cjgiridhar
cjgiridhar / tornadogenex.py
Created September 17, 2014 06:04
Tornado Gen Coroutine
import tornado.web
import tornado.gen
from tornado.httpclient import AsyncHTTPClient
class GenAsyncHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self):
http_client = AsyncHTTPClient()
response = yield http_client.fetch("http://ip.jsontest.com")
print(response)
@cjgiridhar
cjgiridhar / asyncio_parallel.py
Created September 21, 2014 06:12
AsyncIO Parallel
import asyncio
@asyncio.coroutine
def factorial(name, number):
f = 1
for i in range(2, number+1):
print("Task %s: Compute factorial(%s)..." % (name, i))
yield from asyncio.sleep(1)
f *= i
print("Task %s: factorial(%s) = %s" % (name, number, f))
@cjgiridhar
cjgiridhar / Null.py
Created May 24, 2011 09:27
Null Design Pattern
# Null Object Pattern
class AbstractLogging:
def Log(self, msg):
pass
from time import asctime, localtime
class RealLogging(AbstractLogging):
def Log(self, msg):
print 'Logged at', asctime(localtime()), msg
@cjgiridhar
cjgiridhar / ReviewBoard.pm
Created June 26, 2011 05:56
ReviewBoard2.0 Perl Package
package API::ReviewBoard;
use strict;
use warnings;
use LWP;
use HTTP::Cookies;
use Carp qw(croak);
use Params::Validate qw[validate OBJECT SCALAR ARRAYREF];
use Data::Dumper;
use vars qw( @EXPORT @ISA );
@cjgiridhar
cjgiridhar / tornadohelloworld.py
Created July 25, 2012 06:02
Tornado - Hello World
## Torando Web Server hello World
import tornado.ioloop
import tornado.web
class Hello(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
@cjgiridhar
cjgiridhar / TornadorequestHandler.py
Created July 26, 2012 04:56
Tornado - Request Handling
mport tornado.ioloop
import tornado.web
class Hello(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
class User(tornado.web.RequestHandler):
def get(self, username,designation):
self.get_arguments(username, strip=True)
@cjgiridhar
cjgiridhar / mongotornado.py
Created July 27, 2012 10:21
Tornado - MongoDB
import tornado.ioloop
import tornado.web
import time
## MongoDB
import pymongo
from pymongo import Connection
conn = Connection()
db=conn['library']
collection = db['articles']
@cjgiridhar
cjgiridhar / template.html
Created August 1, 2012 08:14
Tornado - Templates
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<table border="1">
{% for key,value in dict.items() %}
<tr>
<td>{{ key }}</td>
<td>{{ value }}</td>
@cjgiridhar
cjgiridhar / custom.html
Created August 2, 2012 13:22
Tornado - Template - Extends
{%extends "templateinheritance.html" %}
{% block header %}
<table border="1">
<tr>
<td>Article2</td>
<td>Author2</td>
</tr>
{% end %}