Skip to content

Instantly share code, notes, and snippets.

View karanlyons's full-sized avatar

Karan Lyons karanlyons

View GitHub Profile
@karanlyons
karanlyons / partial_range_update.sql
Last active September 16, 2022 15:11
Postgres: Update only portion of range, preserving other half and bounds.
UPDATE <TABLE> SET
<COLUMN>=<RANGE_TYPE>(
lower(<COLUMN>), -- Swap out for actual value
upper(<COLUMN>), -- Swap out for actual value
concat(
CASE WHEN lower_inc(<COLUMN>) THEN '[' ELSE '(' END,
CASE WHEN upper_inc(<COLUMN>) THEN ']' ELSE ')' END
)
)
WHERE <CONDITION>;
@karanlyons
karanlyons / partial_object.py
Last active August 29, 2015 14:18
PartialObject (as functools.partial is to functions)
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
import inspect
from collections import OrderedDict
class PartialManager(type):
@karanlyons
karanlyons / keybase.md
Created March 13, 2015 02:59
Keybase Proof

Keybase proof

I hereby claim:

  • I am karanlyons on github.
  • I am karanlyons (https://keybase.io/karanlyons) on keybase.
  • I have a public key whose fingerprint is 58E3 3A33 462A 60BC AB6E 8E86 69B3 9688 5CB5 219B

To claim this, I am signing this object:

@karanlyons
karanlyons / xpath.js
Last active September 6, 2018 08:26
Poor Man’s JS XPath (With support for wildcard globbing, regex matches, and array slice notation.)
var slice_re = new RegExp(/^(.*?)\[(-?\d*?)(:?)(-?\d*?)(:?)(-?\d*?)\]$/);
function xpath(path, objects) {
var selectors, selector, is_array_selector, array_components, array_components_length, array_rules, j, i, is_regex_selector, tail_path, objects_length, heap, object, matches, key, matches_length, match, array_start, array_end, array_interval, _, k;
if (!Array.isArray(objects)) {
objects = [objects];
}
selectors = path.split('.');
@karanlyons
karanlyons / stupid_django_search.py
Last active December 24, 2015 13:09
Because setting up haystack would just be silly.
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
import operator
from django.contrib.admin.util import lookup_needs_distinct
from django.contrib import admin
from django.db.models import Q
@karanlyons
karanlyons / thesaurus.py
Last active December 14, 2015 03:09
For when a dictionary is too boring.
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from decimal import Decimal
from difflib import get_close_matches
class Thesaurus(dict):
'''
For when a dictionary is too boring.
@karanlyons
karanlyons / tal_music.json
Last active November 17, 2022 00:34
Music played in This American Life episodes, scraped from their transcripts.
[
{
"music": [
{
"artist": "Dinah Washington",
"song": "Destination Moon",
"time": "00:05:40.49"
},
{
"artist": "Frank Sinatra",
@karanlyons
karanlyons / gevent_fib.py
Created June 4, 2012 14:52
Node vs. Gevent (Sync and Async)
#!/usr/bin/env python
# encoding: utf-8
# $ brew install libev
# $ pip install http://gevent.googlecode.com/files/gevent-1.0b2.tar.gz
# $ chmod +x ./gevent_fib.py
# $ ./gevent_fib.py
import time
@karanlyons
karanlyons / duct.py
Created May 28, 2012 15:47
duct(): Almost like dict(), but worse.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
class _DuctView(object):
"""
*Almost* like `DictView`, but worse.
"""
@karanlyons
karanlyons / tail_call_decorator.py
Last active May 29, 2016 10:40
Tail Call Decorator for Python
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
from sys import _getframe
except ImportError:
import sys
try: