Skip to content

Instantly share code, notes, and snippets.

@cfmonkey
cfmonkey / pyramid_notfound.py
Created June 8, 2012 03:21
url redirect when it's not found.
from pyramid.renderers import render_to_response
from pyramid.view import notfound_view_config
from pyramid.httpexceptions import HTTPFound, HTTPNotFound
from pyramid.interfaces import IRoutesMapper
def find_route(path, request):
q = request.registry.queryUtility
mapper = q(IRoutesMapper)
if mapper is None:
@cfmonkey
cfmonkey / gist:722014
Created November 30, 2010 17:25
bitmap sort
#-*- coding: utf-8 -*-
def bitsort(inlist):
tmplist = list([0] * 100)
for item in inlist:
tmplist[item] = 1
outlist = [i for i, item in enumerate(tmplist) if item == 1]
return outlist
@cfmonkey
cfmonkey / gist:703700
Created November 17, 2010 17:32
Using timedelta to calculate two datatime objects
def diff_time(curr_time):
curr_time_obj = datetime.strptime(curr_time, "%a, %d %b %Y %H:%M:%S GMT")
now_obj = datetime.now()
res = curr_time_obj - now_obj
days = int(abs(res.total_seconds()) / (3600 * 24))
hours = int((abs(res.total_seconds()) - days * 3600 * 24)/3600)
minutes = int((abs(res.total_seconds()) - days * 3600 * 24 - hours * 3600)/60)
ret = u"From now "
if days > 0:
ret += str(days) + "days "