Skip to content

Instantly share code, notes, and snippets.

@mmourafiq
mmourafiq / 1-Enumerate.py
Created August 12, 2012 17:34
Make your code pythonic
#example of bad code
for indice in xrange(0, len(l)):
print "l[%d] = %r" % (indice, l[indice])
# ==>
#the best way to perform such operation (get indices and corresponding values) is by using enumerate
for indice, value in enumerate(l):
print "l[%d] = %r" % (indice, value)
#since enumerate returns a list of tuples :
@mmourafiq
mmourafiq / gist:3353379
Created August 14, 2012 21:58
cron for sphinx search rotate/merge
*/15 * * * * /home/user/sphinx/bin/indexer --config /home/user/webapps/project-name/sphinx.conf --rotate profiles_delta
0 0 * * * /home/user/sphinx/bin/indexer --config /home/user/webapps/project-name/sphinx.conf --merge profiles profiles_delta --merge-dst-range deleted 0 0 --rotate
@mmourafiq
mmourafiq / 1-list.append(element).py
Created August 17, 2012 21:20
Using list in python
#appen an element to a list
li = [1, 2, 3, 4, 5]
li.append(6)
print li # [1, 2, 3, 4, 5, 6]
@mmourafiq
mmourafiq / gist:3491343
Created August 27, 2012 18:58
finding prime factors for x
def get_primes(x):
non_primes = set()
primes = set()
for i in range(2, x+1):
if x%i == 0 and i not in non_primes:
primes.add(i)
j = i
while j*i <= x:
non_primes.add(i*j)
j += 1
@mmourafiq
mmourafiq / gist:3491346
Created August 27, 2012 18:59
finding prime numbers less than x
def primes_less(x):
non_prime = set()
i = 2
while i*i <= x:
if i not in non_prime:
j = i
while j*i <= x:
non_prime.add(j*i)
j += 1
i += 1
@mmourafiq
mmourafiq / gist:3491358
Created August 27, 2012 19:00
multiplicities of prime factors less than x
def get_primes_multiplicities(x):
"""
Returns factors for x!
"""
p_multi = {}#factors multiplicities
for p in primes_less(x):
temp = x
mult = 0
while temp!= 0:
temp /= p
@mmourafiq
mmourafiq / gist:3491510
Created August 27, 2012 19:22
multiplicities of prime factors less than x for x!^2
def nbr_factors_sqr(x):
nbr = 1
for f in get_primes_multiplicities(x).values():
nbr *= (2*f + 1)
return nbr
@mmourafiq
mmourafiq / fabfile.py
Created September 26, 2012 21:29
fabric
from fabric.api import local
from fabric.api import lcd
def prepare_deployment(branch_name):
local('python manage.py test myapp')
local('git add -p && git commit')
local('git checkout master && git merge ' + branchname)
def deploy():
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
class Meta:
app_label = 'myapp'
class Album(models.Model):
artist = models.ForeignKey('Musician')
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
class Meta:
app_label = 'myapp'