Skip to content

Instantly share code, notes, and snippets.

View juanriaza's full-sized avatar
🎯
Focusing

Juan Riaza juanriaza

🎯
Focusing
View GitHub Profile
def nested_commit_on_success(func):
"""Like commit_on_success, but doesn't commit existing transactions.
This decorator is used to run a function within the scope of a
database transaction, committing the transaction on success and
rolling it back if an exception occurs.
Unlike the standard transaction.commit_on_success decorator, this
version first checks whether a transaction is already active. If so
then it doesn't perform any commits or rollbacks, leaving that up to
@juanriaza
juanriaza / gist:813592
Created February 6, 2011 18:41
Algoritmo Euclides
def euclides(a,b):
return a if b == 0 else euclides(b, a%b)
def euclides_ext(a,b):
if b == 0:
return [1,0,a]
else:
x,y,d = euclides_ext(b, a%b)
return [y, x - (a//b)*y, d]
@juanriaza
juanriaza / domready.js
Created February 24, 2011 09:46 — forked from ded/domready.js
function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()}

Gist Clients

Want to create a Gist from your editor, the command line, or the Services menu? Here's how.

Editor Support

from django.db import models
from django.db.models import signals
class DeletingFileField(models.FileField):
"""
FileField subclass that deletes the refernced file when the model object
itself is deleted.
WARNING: Be careful using this class - it can cause data loss! This class
makes at attempt to see if the file's referenced elsewhere, but it can get
"""
jQuery templates use constructs like:
{{if condition}} print something{{/if}}
This, of course, completely screws up Django templates,
because Django thinks {{ and }} means something.
Wrap {% verbatim %} and {% endverbatim %} around those
blocks of jQuery templates and this will try its best
from collections import Counter
from itertools import product
print('\n'.join('*'*(c//2000) for i,c in
sorted(Counter(map(sum, product(range(6), repeat=8))).items())))
@juanriaza
juanriaza / gist:899587
Created April 2, 2011 15:39
Persistent connection to PostgreSQL database
# Custom DB backend postgresql_psycopg2 based
# implements persistent database connection using thread local storage
from threading import local
from django.db.backends.postgresql_psycopg2.base import DatabaseError, \
DatabaseWrapper as BaseDatabaseWrapper, IntegrityError
from psycopg2 import OperationalError
threadlocal = local()
# -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()
from gevent.pool import Group
import urllib2
# Estos urls estan en una lista, pero podrias leerlos de un archivo o obtenerlos de cualquier lado
urllist = [
@juanriaza
juanriaza / geventreactor.py
Created April 26, 2011 22:08 — forked from inportb/geventreactor.py
Twisted reactor running on gevent (libevent+greenlet)
## Copyright (C) 2011 by Jiang Yio <http://inportb.com/>
## Please find instructions at <http://wiki.inportb.com/python:geventreactor>
## The latest code is available at <https://gist.github.com/848058>
##
## Permission is hereby granted, free of charge, to any person obtaining a copy
## of this software and associated documentation files (the "Software"), to deal
## in the Software without restriction, including without limitation the rights
## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
## copies of the Software, and to permit persons to whom the Software is
## furnished to do so, subject to the following conditions: