Skip to content

Instantly share code, notes, and snippets.

@noahlt
noahlt / oh_shit_that_sometimes_happens.txt
Created July 31, 2017 23:11
oh shit that sometimes happens: a play
OH SHIT THAT SOMETIMES HAPPENS
SAM: yo, can you help me out
CHUCK: hey what do you need
SAM: I don’t know if I told you but I recently became a camp counselor

Python’s lru_cache is sensitive to order of keyword arguments

In case you were wondering, Python’s built-in LRU cache function decorator doesn’t cache function calls with the same keyword arguments written in a different order. That is, if you cache a function and then call it twice, with the same keyword arguments but written in a different order, on the second call you won’t get the cached result and the function will be re-run.

proof/example

Let’s construct an example. We’re interested in keyword arguments, so we’ll make our function only take keyword arguments (1). We want some side-effect to tell us whether it was a cache hit or miss, so we’ll print each keyword when we iterate over it (2). We want to cache a result, so we’ll return the sum of the values of the keyword arguments (3). Finally, we apply the lru_cache decorator to the function with a maxsize=None to let the cache grow unbounded. Putting all that together, we get:

@noahlt
noahlt / os_renaissance.json
Created March 29, 2017 00:32
Tracery source for OS renaissance name generator
{
"origin": ["#color# #animal# #os#"],
"color": ["Blue", "Green", "Red", "Yellow", "Orange", "Maroon", "Pink", "Black", "White"],
"animal": ["Lion", "Bear", "Seal", "Shark", "Dolphin", "Eagle", "Falcon", "Rhino", "Wolf", "Elk", "Ox", "Rabbit", "Trout"],
"os": ["OS/2", "DOS", "RISC OS", "AmigaOS", "NeXTSTEP", "BeOS", "Plan 9", "CTOS", "CP/M", "HP-UX", "TOPS-20", "Ultrix", "Multics", "Minix", "Xinu"]
}
@noahlt
noahlt / go_multiple_return.go
Created August 26, 2016 21:29
Passing through multiple-value-return doesn't work well in Go
package main
import "fmt"
var m = map[string]int{"a": 1, "b": 2, "c": 3}
func lookup(k string) (int, bool) {
// Can't do this :(
//
//return m[k]
@noahlt
noahlt / prctx.go
Created August 24, 2016 23:19
Print context of string around a point
// Print context of string around a point
func prctx(buf string, point int) {
if point < 0 || point >= len(buf) {
log.Printf("context for point: out of bounds (%d/%d)", point, len(buf))
return
}
min := func(a int, b int) int {
if a < b {
@noahlt
noahlt / remote_env_vars.sh
Created May 17, 2016 23:34
Using environment variables from remote servers
#!/usr/bin/env bash
# A couple of handy bash functions that I have in my .bashrc:
# get_env uses ssh to copy an environment variable from a remote
# server to your current machine. Example usage:
#
# get_env SOME_VAR_NAME example.com
get_env () {
VARNAME=$1

A bit easier to read:

var installed = WindowController.getInstalledPlugins();
return {
  installedPlugins: _.intersection(conf.SUPPORTED_EDITORS, installed),
  uninstalledPlugins: _.difference(conf.SUPPORTED_EDITORS, installed),
}
@noahlt
noahlt / SafeReactComponent.js
Created March 9, 2016 18:21
Catch errors in render() with SafeReactComponent
var React = require('react');
var _ = require('underscore');
// Catches errors in render() and returns null so that we don't
// break the entire React app.
var SafeReactComponent = function(classObj) {
var originalRender = classObj.render;
var displayName = classObj.displayName || 'unknown component';
return React.createClass(_.extend(classObj, {
render: function() {
@noahlt
noahlt / pick.py
Created November 17, 2015 18:12
Pick keys/values from dict
def pick(d, filter_tree):
'''Returns a new dictionary by picking keys/values from d per filter_tree.
Works recursively: values in filter_tree can be either True or dictionaries
which are their own filters.
>>> d = {'a': 1, 'b': 2, 'c': {'foo': -1, 'bar': -2}, 'd': {'r': 'red', 'b': 'blue'}}
>>> pick(d, {'a': True, 'b': True})
{'a': 1, 'b': 2}
>>> pick(d, {'a': True, 'b': True, 'd': True})
{'a': 1, 'b': 2, 'd': {'r': 'red', 'b': 'blue'}}
@noahlt
noahlt / react_dont_update_children.html
Created October 17, 2015 00:52
Hack to block re-rendering child when parent was updated
<!DOCTYPE html>
<html>
<head>
<title>Hack to block re-rendering child when parent was updated</title>
<script src="https://fb.me/react-0.13.0.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.0.js"></script>
</head>
<body>
</body>