Skip to content

Instantly share code, notes, and snippets.

@vivekn
vivekn / gist:1062488
Created July 3, 2011 19:02
Broken pipe error generated by Django when a browser times out the connection
Traceback (most recent call last):
File "/usr/local/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 273, in run
self.finish_response()
File "/usr/local/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 312, in finish_response
self.write(data)
File "/usr/local/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 389, in write
self.send_headers()
File "/usr/local/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 441, in send_headers
self.send_preamble()
File "/usr/local/lib/python2.5/site-packages/django/core/servers/basehttp.py", line 372, in send_preamble
@vivekn
vivekn / celeryconfig.py
Created July 3, 2011 19:27
Configuration for Celery using Redis
CELERY_RESULT_BACKEND = "redis"
REDIS_HOST = "localhost"
REDIS_PORT = 6379
REDIS_DB = 0
BROKER_BACKEND = "redis"
BROKER_HOST = "localhost" # Maps to redis host.
BROKER_PORT = 6379 # Maps to redis port.
BROKER_VHOST = "0" # Maps to database number.
@vivekn
vivekn / celery_example.py
Created July 3, 2011 19:35
Example for creating tasks in celery
from celery.decorators import task
...
@task
def time_consuming_task(foo, bar):
# code here
return something # This function must return a value
@vivekn
vivekn / example.py
Created July 4, 2011 07:45
python magic
def __getattr__(self, key):
if key in ['prefix', 'id']:
return super(Model, self).__getattribute__(key)
else:
_key = "%s.%s" % (self.prefix, key)
if key in self._sets:
return get_set(_key)
elif key in self._lists:
return get_list(_key)
elif key in self._zsets:
@vivekn
vivekn / models.py
Created July 4, 2011 07:54
Rorm example
from rorm import *
class User(Model):
info = Field('hash')
followers = Field('set')
following = Field('set')
"""
>>> vivek = User.create() #Auto increment ids
@vivekn
vivekn / gist:1147133
Created August 15, 2011 16:32
Validators.js example
<form>
<table width="400px">
<tr> <td>First name</td> <td><input type="text" id="first"/></td></tr>
<tr> <td>Last name</td> <td> <input type="text" id="last"/></td> </tr>
<tr> <td>Email address</td> <td> <input type="text" id="email"/></td> </tr>
<tr> <td>Password</td> <td> <input type="password" id="pass"/></td> </tr>
<tr> <td> <input type="button" value="Submit" onclick="validateSubmit()"> </input> </td></tr>
<table>
</form>
...
@vivekn
vivekn / gist:1147177
Created August 15, 2011 16:51
jQuery Submit
$("form").submit(function () {
var status = Validators({
"input": new EmptyValidator(),
"#email": new EmailValidator("Please enter a valid email address"),
"#pass": new LengthValidator(6, "The password should be atleast 6 characters")
});
return status;
}
@vivekn
vivekn / gist:1147296
Created August 15, 2011 17:44
value validator
function foo(value) {
return value==42;
}
...
Validators({
"#field_item": new ValueValidator("errorClass",
"This field is not the answer to life, the universe and everything", foo)
});
@vivekn
vivekn / gist:1148620
Created August 16, 2011 07:36
Regex validator
Validators({
"#field_item": new RegexValidator("errorClass",
"Please enter a valid postal code", /^\d{5}$/)
});
@vivekn
vivekn / sendgrid.py
Created October 15, 2011 10:00
Sendgrid example
def sendgrid(from_email, to_email, subject, message):
msg = MIMEText(message)
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
# Login credentials to sendgrid
username = '**********'
password = "**********"