Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
carymrobbins / classproperty.py
Last active August 29, 2015 13:56
Python class properties, instance properties, and optional laziness.
# noinspection PyPep8Naming
class classproperty(object):
def __init__(self, function, name=None):
self.__name__ = name or function.__name__
self._class_property = function
self._class_property_is_lazy = False
self._cached_class_property_result = None
self._instance_property = None
self._instance_property_is_lazy = False
self._cached_instance_property_result = None
@carymrobbins
carymrobbins / unique.py
Last active August 29, 2015 13:56
Unique iterator for Python.
def identity(x):
return x
class unique(object):
def __init__(self, xs, predicate=identity):
""" :type xs: Iterable """
self._xs = xs
self._predicate = predicate
@carymrobbins
carymrobbins / partialmethod.py
Last active April 22, 2023 11:25
Partial method for Python 2.7
from functools import partial
class partialmethod(partial):
def __get__(self, instance, owner):
if instance is None:
return self
return partial(self.func, instance,
*(self.args or ()), **(self.keywords or {}))
@carymrobbins
carymrobbins / access_m2m_model.py
Created February 13, 2014 16:35
Access a Many to Many model in Django.
from django.db import models
# Given the following classes:
class Foo(models.Model):
name = models.TextField()
class Bar(models.Model):
name = models.TextField()
foos = models.ManyToManyField(Foo)
@carymrobbins
carymrobbins / until.html
Created February 13, 2014 19:03
JavaScript until function - an improved setInterval.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
/**
* A better version of setInterval. Usage:
* until([
* { action: function() { $('#foo').length > 1; },
* cleanup: function() { $('#foo').remove(); },
* interval: 1000 }
@carymrobbins
carymrobbins / left_join.py
Last active August 29, 2015 13:56
Perform a left join in Python without a database.
from collections import Iterable
from itertools import chain
def left_join(xss, yss, on=None):
"""
:type xss: Iterable[Iterable]
:type yss: Iterable[Iterable]
:type on: Iterable[int] | (Iterable, Iterable) -> bool
:rtype: generator[tuple[list]] |
@carymrobbins
carymrobbins / install_xapian_inside_virtualenv_osx.sh
Last active June 12, 2019 17:54 — forked from asyncee/install xapian inside virtualenv
Install xapian in a virtualenv on Mac OS X using Homebrew.
#/bin/bash
set -e
if [ -z "$(which brew)" ]; then
echo "This script requires Homebrew."
exit
fi
if [ -z "$VIRTUAL_ENV" ]; then
@carymrobbins
carymrobbins / function_cache.py
Created February 23, 2014 20:00
Yet another function cache decorator for Django.
from functools import wraps
from django.core.cache import cache
def cached(key, timeout=None):
"""Decorator to cache result of function call for timeout seconds if the
result is not None.
"""
def wrapper(f):
@wraps(f)
@carymrobbins
carymrobbins / excel_tuple.py
Created February 25, 2014 03:40
Convert an Excel cell address to a tuple of (row, col)
import re
def excel_tuple(address):
"""
Takes an Excel address and converts it to a tuple. Used as a helper for
looking up (row, column) to pass into sheet.cell(row, col).
>>> assert excel_tuple('A1') == (0, 0)
>>> assert excel_tuple('Z10') == (9, 25)
>>> assert excel_tuple('AAA999') == (998, 702)
@carymrobbins
carymrobbins / inner_inherit.py
Last active August 29, 2015 13:57
Auto inherit inner classes
class M(type):
def __new__(mcs, name, bases, attrs):
if bases:
b = attrs.pop('B', None)
if b:
bs = tuple(s for s in (c.__dict__.get('B') for c in bases) if s)
attrs['B'] = type('B', bs, b.__dict__)
return super(M, mcs).__new__(mcs, name, bases, attrs)