Skip to content

Instantly share code, notes, and snippets.

@IlianIliev
IlianIliev / fabfile.py
Created May 28, 2012 11:04
Fabric script that eases the creation of new Django Project
"""
This fabric script automates the creation of a virtual environment and a Django
project. The result will be virtual environtment with the name of the project.
The folder namer where the project code will be placed is specified in
SOURCE_DIRECTORY_NAME, a static root folder will be created and settings.py
will be updated.
"""
try:
from fabric.api import env, run, local
from fabric.context_managers import lcd, prefix
@IlianIliev
IlianIliev / gist:2988404
Created June 25, 2012 12:51
Project Euler #4
max = 999
min = 100
check = lambda x: True if str(x) == str(x)[::-1] else False
def tmp(max, min):
for x in xrange(max*max, min*min, -1):
if check(x):
for i in xrange(max, min, -1):
if x%i==0 and min < x/i < max :
@IlianIliev
IlianIliev / gist:4654335
Created January 28, 2013 10:00
Measuring execution of multiple functions with timeit
import timeit
NUM = 10**10000
def f1():
x = [chr(i**i) for i in xrange(NUM) if i%2==1]
return x
@IlianIliev
IlianIliev / tests.py
Last active December 12, 2015 12:08
Tweeter timeline time formatter
import unittest
from datetime import datetime, timedelta
from tweetime import timelinetime
class TimeLineTimeTest(unittest.TestCase):
def test_timelinetime(self):
now = datetime.now()
@IlianIliev
IlianIliev / gist:5056570
Last active December 14, 2015 08:18
just for testing
# views.py
from .models import News
from django.shortcuts import get_object_or_404
def single_news(request, slug):
news = get_object_or_404(News.objects.public(), slug=slug)
return render_to_response('news/single_news.html', {'news': news})
@IlianIliev
IlianIliev / Functions in Python - homework.py
Last active December 15, 2015 00:49
The "homework" from the presentation "Function in Python" - http://ilian.i-n-i.org/functions-in-python-presentation/
# Problem 1
def f(*args, **kwargs):
return sum(list(args) + kwargs.values())
# Sample run
# f()
# f(1, 2, 3)
# f(a=1, c=3, b=2)
# f(1, b=2, c=3)
@IlianIliev
IlianIliev / gist:6056557
Created July 22, 2013 19:00
Timing different methods for string reversion in Python
import timeit
string = "Hello, World!"
def reverse(string):
result = ""
for i in xrange(len(string), 0, -1):
result = result+string[i-1]
@IlianIliev
IlianIliev / mixins.py
Created September 6, 2013 11:16
Mixing that allows you to define short list of fields for the list view when using Django REST Framework
class ShortListSerializerMixin(object):
def get_fields(self):
use_list_fields = self.context['view'].action == u'list' \
and getattr(self.Meta, 'list_fields')
if use_list_fields:
detail_fields = self.opts.fields
self.opts.fields = self.Meta.list_fields
fields = super(ShortListSerializerMixin, self).get_fields()
# facebook-sdk==0.4.0
def facebook_share():
import facebook
graph = facebook.GraphAPI('you_key_here')
profile = graph.get_object("me")
# friends = graph.get_connections("me", "friends")
@IlianIliev
IlianIliev / bisect_test.py
Created February 11, 2015 00:01
bisect vs append/sort
import bisect
import timeit
import random
def test_bisect(num):
data_list = list()
for i in range(1,num):
var=random.randint(1,num*100)
loc=bisect.bisect(data_list,var)