Skip to content

Instantly share code, notes, and snippets.

View kezabelle's full-sized avatar

Keryn Knight kezabelle

View GitHub Profile
@kezabelle
kezabelle / debugger.py
Last active November 4, 2015 11:08
how the hell does one debug this
def _lol(value):
vt = type(value)
isuuid = isinstance(value, UUID)
isuuidsub = None
cls = None
def idhashs():
from uuid import uuid4, UUID
return id(uuid4), id(UUID), hash(uuid4), hash(UUID)
uuid4id, uuidid, uuid4hash, uuidhash = idhashs()
@kezabelle
kezabelle / whee.sh
Created October 28, 2015 09:23
Demonstrating the value of pip-tools (https://github.com/nvie/pip-tools) to have "loose" requirements files which can be periodically updated and pinned.
> mktmpenv --python=`which python3`
> pip install pip setuptools --upgrade
> pip install pip-tools
> echo "django-imagekit" > requirements.in
> pip-compile
> cat requirements.txt
@kezabelle
kezabelle / trues_to_number.py
Created October 5, 2015 20:44
Convert a series of True/False items into a single number...
def truthynumber(*args):
"""
>>> truthynumber(True, True)
3
>>> truthynumber(True, False, 1, 3, True)
5
"""
ints = (str(int(arg)) for arg in args
if arg is True or arg is False)
bin_ints = ''.join(ints)
@kezabelle
kezabelle / qobj.py
Last active September 30, 2015 17:29
Generating a Q object for ensuring maximal depth for onetoones.
LOOKUP_SEP = '__' # fake it.
def Q(**kws): # fake it.
return kws
def make(relation_str, sep=LOOKUP_SEP):
"""
Given something like:
'a__b__c__d__e'
return a Q object composed of:
@kezabelle
kezabelle / forms.py
Created September 18, 2015 14:15
For joshuajonah in #django IRC
>>> from django.forms import Form
>>> from django.contrib.auth import get_user_model
>>> from django.forms.models import ModelMultipleChoiceField
>>> class MyForm(Form):
>>> field = ModelMultipleChoiceField(queryset=get_user_model().objects.all())
>>> myform = MyForm(initial={'field': [1]})
>>> myform.as_p()
# this should include
# <select multiple="multiple" id="id_field" name="field">\n<option value="1" selected="selected">kezabelle</option>
@kezabelle
kezabelle / crapsite_contextprocessor.py
Created September 17, 2015 08:05
Just give me the damn Site when I need it mang.
from django.contrib.sites.shortcuts import get_current_site
from django.utils.functional import SimpleLazyObject
def current_site(request):
def wrapped_get_current_site():
return get_current_site(request=request)
return {
'CURRENT_SITE': SimpleLazyObject(wrapped_get_current_site),
}
@kezabelle
kezabelle / pypy.py
Created August 21, 2015 07:29
Oh PyPy. Why you different again?
>> from collections import OrderedDict
>>> set(dir(OrderedDict())) - set(dir({}))
set(['__dict__', '__module__', '__weakref__'])
@kezabelle
kezabelle / delay-show.js
Last active August 29, 2015 14:27
Hide, say, a navigation panel until the first "pane" has been scrolled over (eg: landing "section" is full page)
(function($window, $document, $, selector) {
var readyup = function () {
var previousScroll = 0;
var showNav = false;
var didScroll = false;
var windowHeight;
var documentHeight;
var $selector = $(selector);
if ($selector.length < 1) {
@kezabelle
kezabelle / count_angular_watchers.js
Created July 22, 2015 14:55
Taken from http://stackoverflow.com/a/18526757 but I want a copy to keep around.
(function () {
var root = angular.element(document.getElementsByTagName('body'));
var watchers = [];
var f = function (element) {
angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) {
if (element.data() && element.data().hasOwnProperty(scopeProperty)) {
angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) {
watchers.push(watcher);
@kezabelle
kezabelle / prefetch_abuse.py
Created July 7, 2015 15:28
for the_drow in #django IRC. I can't remember whether any() will bail at the first `True`, which would presumably be the quickest exit.
class MyThing(Model):
def has_value_x_y(self):
assert hasattr(self, '_prefetched_objects_cache'), \
"needs prefetch_related()"
# `my_thing__set` becomes `my_thing` in the prefetch cache ...
assert 'RELATION' in self._prefetched_objects_cache, \
"needs prefetch_related('RELATION_set')"
count = (True for x in self.RELATION_set.all())
return any(count)