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 Additive(type): | |
def __add__(self, x): | |
return type("Additive", (self, x), {}) | |
class A(object): | |
__metaclass__ = Additive | |
def foo(self): | |
print "foo" | |
class B(object): |
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 Foo(object): | |
# ... | |
# is syntactic sugar for | |
# Foo = type('Foo', (object,), {...}) | |
# And with a metaclass Meta: | |
# Bar = Meta('Bar', (object,), {...}) | |
def metafunc(name, bases, dict): | |
print "I created a new class %s%s from %s" % (name, bases, dict) | |
return type(name, bases, dict) |
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) |
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
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 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
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
// 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
<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
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) | |
OlderNewer