Want to create a Gist from your editor, the command line, or the Services menu? Here's how.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function r(f){/in/(document.readyState)?setTimeout(r,9,f):f()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 = [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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: |
OlderNewer