Skip to content

Instantly share code, notes, and snippets.

View etianen's full-sized avatar

Dave Hall etianen

View GitHub Profile
def search(request):
search_form = SearchForm(request.GET)
if search_form.is_valid():
data = form.cleaned_data
search_results = watson.filter(Issue, data["q"]).filter(
date__gte = data["start_date"],
date__lte = data["end_date"],
item__name = data["item_name"],
)
@etianen
etianen / gist:7526781
Created November 18, 2013 12:09
Pluralizing a label in CSS!
.spinner-count {
&:after {
content: "servings";
}
}
&.spinner-count-1 {
.spinner-count {
&:after {
content: "serving";
}
@etianen
etianen / style.js
Last active December 25, 2015 16:29
A LESS stylesheet loader and compiler for RequireJS, including NodeJS optimization.
/**
* LESS stylesheet loader.
*/
define([
"module",
"text"
], function(
module,
text
@etianen
etianen / unicode_stream_condom.py
Last active December 24, 2015 16:19
How to fix a broken XML source file that contains invalid XML unicode characters.
import tempfile, re, codecs
from contextlib import closing
from functools import partial
RE_UNICODE_CONDOM = re.compile(u"[^\x09\x0A\x0D\x20-\uD7FF\uE000-\uFFFD]")
MEMORY_BUFFER_SIZE = 1024 * 1024 # 1M
CHUNK_SIZE = 1024 * 512 # 0.5M
return not any (
frozenset(permissions_grant).difference(chain.from_iterable(
parent_permissions_grant
for parent_model_grant, parent_permissions_grant
in parent_scope
if all(
parent_model_grant_part == model_grant_part
for model_grant_part, parent_model_grant_part
in izip_longest(
model_grant,
@etianen
etianen / public-key.txt
Last active December 19, 2015 18:38
My PGP public key.
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG/MacGPG2 v2.0.19 (Darwin)
mQENBE228RIBCACuc/n1SvMxe7RQWrgOK4Hi1Pn8K9KcZNy0j+kmk319/W8GiqAC
qOKMcVIl+KA4zBy7KwCUkkEFUEJn4OwQky9NIyAJw7djyWkkMAAGSqvRWrbt+TF4
AOje8EHJTF5r+4tytLRAHl7sRAHkBWdvPJKp5I9KLjbDu55q65dj8Gkjd0lKxk5n
b5IDTn5p3tx82j+DokdJ8YnmGq4fXtG6oUmi+EXlKV24Jo3+mlrNo3MKBMMQekuO
WJLhldnJiapnkaadDKqpiJ+7thPoemou4qizqxHaSZCmThegNzA+kP9sseM1YDk5
kcy2xvxQu77BMpZ3s2zc2vviDY4S9/WMIRWnABEBAAG0HERhdmUgSGFsbCA8ZGF2
ZUBldGlhbmVuLmNvbT6JATsEEwECACUCGy8GCwkIBwMCBhUIAgkKCwQWAgMBAh4B
import random
win_on_switch = 0
win_on_stay = 0
for _ in xrange(100000):
prize_box = random.randint(0, 2)
print "Prize is in box", prize_box
initial_choice_box = random.randint(0, 2)
print "You choose box", initial_choice_box
@etianen
etianen / gist:5494849
Last active December 16, 2015 20:49
Doctesting in javascript.
var fs = require("fs");
var path = require("path");
// TODO: Code block should be indented compared to previous line?
module.exports = function(modulePath) {
// Parse the module name.
var moduleName = path.basename(modulePath, ".js");
// Load the contents.
var contents = fs.readFileSync(modulePath);
@etianen
etianen / twitter_api_proxy.conf
Created April 11, 2013 12:55
An nginx configuration to creating an authenticated, caching, Twitter API proxy.
# This defines a 10 megabyte cache for the proxy service, and needs to live
# outside of the virtual host configuration. Adjust the path according to
# your environment.
proxy_cache_path /var/cache/nginx/twitter_api_proxy levels=1:2 keys_zone=twitter_api_proxy:10m;
# The virtual host configuration.
server {
# If your want to secure your proxy with SSL, replace with the appropriate SSL configuration.
listen 80;
from itertools import izip_longest
from watson.registration import SearchEngine, _bulk_save_search_entries
def grouper(n, iterable, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)
for engine_slug, search_engine in SearchEngine.get_created_engines():
registered_models = search_engine.get_registered_models()