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
# HTTP view decorators inspired by property.setter | |
# See also a class-based solution in http://gist.github.com/85410 | |
class MethodDispatcher(object): | |
def __init__(self): | |
self.views = {} | |
def __call__(self, request, *args, **kwargs): | |
m = request.method.upper() | |
if m in self.views: |
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
DAY, WEEK, MONTH, YEAR = map(timedelta, (1, 7, 30, 365)) | |
def newer_than(fieldname, delta) | |
# positive values go to past, negative to future. counterintuitive? | |
time = datetime.now() - delta | |
fieldname = fieldname + "__gte" | |
return Q(**fieldname: past) | |
# For more granularity. Probably YAGNI |
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
class W(object): | |
def __init__(self, i): | |
self.i = iter(i) | |
def __iter__(self): return self | |
def next(self): return self.i.next() | |
def __getslice__(self, start, end): | |
return islice(self.i, start, end) | |
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
<html> | |
<head> | |
<title>This is shit</title> | |
<style type="text/css"> | |
#box { | |
position: absolute; | |
top: 50px; | |
bottom: 20px; | |
background: #f77; | |
color: white; |
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
// Alustus: | |
var dict:Dictionary = new Dictionary(); | |
var keys = new Array("tekniikka", "..."); | |
for (var key in keys) { | |
dict[key] = {"name": key, "score": 0} | |
} | |
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 cartesian(*lists): | |
if not lists: | |
yield () | |
return | |
for item in lists[0]: | |
for tup in cartesian(*lists[1:]): | |
yield (item,) + tup | |
# >>> list(cartesian('abc', [1, 2])) | |
# [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)] |
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 choices(name, choicemap): | |
"""{name: value} -> C.name = value """ | |
members = choicemap.copy() | |
members[name] = tuple((k, k) for k in choicemap.values()) | |
return type('%s_choices' % name, (), members) | |
# sexes = {'MALE': 'male', 'FMLE': 'female'} | |
# class FooModel(models.Model, choices('SEXES', sexes)) |
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.http import HttpResponseNotAllowed | |
METHODS = ('get', 'post', 'delete', 'head') | |
class ViewDispatcher(object): | |
def __init__(self): | |
self.supported = [m for m in METHODS if hasattr(self, m)] | |
def __call__(self, request, *args, **kwargs): | |
method_func = getattr(self, request.method.lower(), None) |
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 find_maxlen(values, count): | |
for i in range(count): | |
yield max(len(v[i][1]) for v in values) | |
shit = find_maxlen(marks.values(), len(data)) # Ehkä list() ympärille | |
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 is_true(self, request): | |
items = Item.objects.filter(content_type=ContentType.objects.get_for_model(User), | |
object_id=self.get_owner(request).pk, | |
owner=request.user) | |
return bool(items) | |
# vs | |
def is_true(self, request): | |
ctype = ContentType.objects.get_for_model(User) |