Skip to content

Instantly share code, notes, and snippets.

@shreyansb
shreyansb / flaskapp.py
Created June 9, 2012 16:06 — forked from kennethreitz/flaskapp.py
My typical flask app base
# -*- coding: utf-8 -*-
import os
from flask import Flask
from flask_heroku import Heroku
from flask_sslify import SSLify
from raven.contrib.flask import Sentry
from flask.ext.celery import Celery
@shreyansb
shreyansb / til_python_2012_05_06.py
Created May 6, 2012 22:40
TIL: python signal, setproctitle, custom exceptions
### signals
# http://docs.python.org/library/signal.html
# example: https://github.com/binarydud/pyres/blob/master/pyres/scheduler.py
import signal
signal.signal(signal.SIGTERM, self.schedule_shutdown)
def schedule_shutdown():
self._shutdown = True
@shreyansb
shreyansb / custom_exceptions.py
Created May 6, 2012 22:08
custom exceptions in python
In [1]: def raise_exception():
...: raise Exception('the base exception')
...:
In [2]: try:
...: raise_exception()
...: except Exception, e:
...: print "caught: %s" % e
...:
caught: the base exception
@shreyansb
shreyansb / gist:2024839
Created March 12, 2012 21:38
jsonp guide
### step 1. write a javascript function that inserts a script tag
### into the <head> with the URL you want to get.
### make sure this url contains the argument callback, e.g.
### var url = http://splitmyri.de/cofi?lat=12.34&lon=45.67&callback=deal_with_request
load_jsonp_script = function(url) {
/* makes a jsonp request for url */
var e = document.createElement('script');
e.setAttribute('language','javascript');
e.setAttribute('type', 'text/javascript');
e.setAttribute('src', url);
# this
application = tornado.web.Application([
(r"/", MainHandler), # get() - homepage - link to app
(r"/cofi/.*", CofiHandler) # get()
])
# becomes
app_settings = {
'debug': True
}
@shreyansb
shreyansb / smtpexample.py
Created January 16, 2012 19:28
sample code to send a gmail using python
#!/usr/bin/python
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
gmail_user = "[email protected]"
@shreyansb
shreyansb / testview.html
Created January 16, 2012 04:24
intro to backbone: model, collection and view
<html>
<head>
<style>
#color_label {
display: inline-block;
width: 90;
text-align: right
}
#color_bar {
display: inline-block;
@shreyansb
shreyansb / gist:1468120
Created December 12, 2011 16:36
creating a user with ssh access in unix
# create a group for the new user
groupadd shreyansgroup
# create user, -d = home directory, -g = group, -s = default shell
# note, this will not create the home directory
useradd -d /home/shreyans -g shreyansgroup -s /bin/bash shreyans
# create home directory and assign it to the user
mkdir /home/shreyans
chown shreyansgroup:shreyans /home/shreyans
@shreyansb
shreyansb / gist:1411033
Created November 30, 2011 21:39
print whenever a class's attributes are accessed
class Desc(object):
def __init__(self):
self.x = 'x'
def foo(self):
print 'foo'
def __getattribute__(self, name):
print 'NAME:', name
attr = object.__getattribute__(self, name)
if callable(attr):
print 'CALLABLE'
@shreyansb
shreyansb / gist:1376607
Created November 18, 2011 14:34
improved git branch status in your bash prompt
txtred='\033[1;31m'
txtgrn='\033[1;32m'
txtylw='\033[1;33m'
end='\033[0m'
function parse_git {
branch="$(__git_ps1 "%s")"
if [[ -z $branch ]]; then
return
fi