Skip to content

Instantly share code, notes, and snippets.

from operator import mul
def euler8(digits, n=4):
i = 0
_max = 0
while i <= len(digits)-n:
_r = reduce(mul, [int(x) for x in digits[i:i+n]])
if _r > _max:
_max = _r
i += 1
@juliengrenier
juliengrenier / genprimes.py
Created September 2, 2015 15:28
Prime generator
def genprimes(upto):
primes = {}
q = 2
i = 0
while i < upto:
if q not in primes:
yield q
i += 1
primes[q*q] = [q]
else:
@juliengrenier
juliengrenier / gist:3958777
Created October 26, 2012 13:20
celery retry_task decorator. Depends on django transaction.
def retry_task(name, max_retries=3, ignore_result=True, queue="celery", countdown=10, exceptions=[]):
"""
This decorator allows you to retry a celery task if it raised an exception of type defined in <exceptions>.
Tasks are also wrapped by a commit_on_success decorator to avoid incomplete data in the database.
arguments are :
name: The name of the task
max_retries: The number of retries before giving up [default: 3]
ignore_result: Should celery ignores the result [default: True]
queue: The queue name [default: "celery"]
@juliengrenier
juliengrenier / gist:3749492
Created September 19, 2012 12:56
javascript tree generator
function randomRange(lowerBound, upperBound){
return lowerBound + Math.random() * (upperBound-lowerBound);
}
function draw() {
var canvas = document.getElementById("canvas");
var c = canvas.getContext("2d");
function drawBranch(l, direction) {
c.save();
c.fillRect(-1, 0, 2, -l);
@juliengrenier
juliengrenier / gist:2655143
Created May 10, 2012 19:01
NullProxy and Borg objects
class NullProxy(object):
"""Pretend you have a model object with three attributes:
class Person(object):
name = String(40)
age = Integer()
email = Email()
And have a sub-class adding extra attributes:
@juliengrenier
juliengrenier / gist:1561439
Created January 4, 2012 18:51
String to binary representation
str = 'a string'
binary_representation = ''.join([bin(ord(c))[2:].zfill(8) for c in str])
@juliengrenier
juliengrenier / gist:1545528
Created December 31, 2011 22:28
Binary to string
str = '01001000011000010111000001110000011110010010000001001110011001010111011100100000010110010110010101100001011100100111001100100001'
print ''.join([chr(int(str[i:i+8],2)) for i in range(len(str)) if i % 8 ==0])