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 abc import ABCMeta, abstractmethod | |
class AbstractModelMeta(ABCMeta, type(models.Model)): | |
pass | |
class ABCModel(models.Model): | |
__metaclass__ = AbstractModelMeta | |
class Meta: |
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
(define (zip-cons a b) | |
(if (null? a) | |
'() | |
(cons (cons (car a) (car b)) | |
(zip-cons (cdr a) (cdr b))))) | |
(define (p c r) | |
(cond | |
((null? c) (map (lambda (x) '()) r)) | |
((eq? (car c) (caar r)) |
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
CREATE OR REPLACE FUNCTION check_fk_child() RETURNS trigger AS $$ | |
DECLARE | |
fk_local TEXT := TG_ARGV[0]; | |
parent_table TEXT := TG_ARGV[1]; | |
fk_val INT; | |
is_valid BOOLEAN; | |
query TEXT; | |
BEGIN | |
-- fk_val = getattr(NEW, fk_local) | |
EXECUTE format('SELECT $1.%I', fk_local) USING NEW INTO fk_val; |
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
import React, { Component } from 'react'; | |
import PropTypes from 'prop-types'; | |
const {Provider: AudioContextProvider, Consumer: AudioConsumer} = React.createContext(); | |
class AudioProvider extends Component { | |
static propTypes = { | |
children: PropTypes.arrayOf(PropTypes.node).isRequired, | |
} |
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
import contextlib | |
@contextlib.contextmanager | |
def bulk_object_creator(limit=100): | |
""" | |
Allows creation of model objects in bulk, intelligently not using too much memory. | |
with bulk_object_creator() as foo_creator: | |
for i in range(100000): |
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.core.cache import cache | |
class RateLimiter(object): | |
def __init__(self, timeout, max_failures): | |
self.timeout = timeout | |
self.max_failures = max_failures | |
def get_key(self, t): | |
return ':'.join(str(s) for s in t) |
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
var group_reduce = function (dependentKey, key_func, reduce) { | |
return Ember.reduceComputed.call(null, dependentKey, { | |
initialValue: Ember.A, | |
initialize: function(initialValue, changeMeta, instanceMeta) { | |
instanceMeta.meta = new Ember.Map(); | |
}, | |
addedItem: function (accumulatedValue, obj, changeMeta, instanceMeta) { | |
var key = key_func(obj), reduced_object; | |
if (instanceMeta.meta.get(key) === undefined) { |
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 itertools import tee | |
class consume_once(object): | |
""" | |
Takes an iterator and returns an iterable that can be consumed | |
multiple times while only consuming the original a single time. | |
>>> x = consume_once(i for i in range(3)) | |
>>> list(x) | |
[0, 1, 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
from math import ceil | |
def split_up(s, length=160): | |
""" | |
Splits the string s into pages of no longer than length. Includes | |
pages numbers. Tries not to split in the middle of a word. | |
>>> split_up("abc", 5) | |
['abc'] | |
>>> split_up("abcdef", 5) |
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 DecoratorMixin(decorator): | |
""" | |
Converts a decorator written for a function view into a mixin for a | |
class-based view. | |
:: | |
LoginRequiredMixin = DecoratorMixin(login_required) | |
class MyView(LoginRequiredMixin): |
NewerOlder