Skip to content

Instantly share code, notes, and snippets.

Karan Lyons karanlyons

Block or report user

Report or block karanlyons

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@karanlyons
karanlyons / method_missing.py
Last active Aug 7, 2017
method_missing for Python: All the headaches of Ruby, now with added whitespace!
View method_missing.py
import dis
import inspect
class MethodMissingMixin:
def __getattr__(self, attr):
if hasattr(getattr(self, '__methodmissing__', None), '__call__'):
parent_frame = inspect.currentframe().f_back
instructions = dis.get_instructions(parent_frame.f_code)
@karanlyons
karanlyons / parse.html
Created Jul 9, 2017
Javascript QTFF/MP4 Parser
View parse.html
<html>
<body>
<script>
'use strict';
ArrayBuffer.prototype.toSource = function(hex) {
var buf = this,
bytes = Array.prototype.slice.call(new Uint8Array(buf)),
i = buf.byteLength,
blank = true;
@karanlyons
karanlyons / sha512.cpp
Created Jul 4, 2017
SHA-512 in C“++”
View sha512.cpp
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstring>
using namespace std;
const uint64_t k[] = {
UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
View locks.py
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
from contextlib import contextmanager
from os import getpid
from random import getrandbits
View hide_meta_opts.py
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
from __future__ import division, absolute_import, print_function, unicode_literals
from copy import deepcopy
from django.db import models
@karanlyons
karanlyons / partial_range_update.sql
Last active Nov 28, 2018
Postgres: Update only portion of range, preserving other half and bounds.
View partial_range_update.sql
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 Aug 29, 2015
PartialObject (as functools.partial is to functions)
View partial_object.py
#!/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):
View keybase.md

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 Sep 6, 2018
Poor Man’s JS XPath (With support for wildcard globbing, regex matches, and array slice notation.)
View xpath.js
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 Dec 24, 2015
Because setting up haystack would just be silly.
View stupid_django_search.py
# -*- 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
You can’t perform that action at this time.