Skip to content

Instantly share code, notes, and snippets.

export const keyComparator = <ItemT extends object, ComparableT extends number>(
keyTransform: (item: ItemT) => ComparableT
) => {
const _cache = new WeakMap<ItemT, ComparableT>();
const cachedTransform = (item: ItemT) => {
if (!_cache.has(item)) {
_cache.set(item, keyTransform(item));
}
return _cache.get(item) as ComparableT;
};
@ezheidtmann
ezheidtmann / lif.py
Created April 22, 2022 19:27
Linear Interpolated Function in Python and Swift
from operator import itemgetter
from typing import List, NamedTuple, Tuple
from common.pairwise import pairwise
class XY(NamedTuple):
x: float
y: float
def _quote_sam_parameter_value(value: str):
"""Quote a parameter value for SAM CLI as best we can
Trying to reverse SAM's _unqoute_wrapped_quotes()
https://github.com/aws/aws-sam-cli/blob/56bba34a5e4739c87e0831d6bf73f6a683ba6134/samcli/cli/types.py#L40
- Fail if the value contains a literal backslash; there's no way to safely
handle this if the backslash occurs in the following situations:
- is the last character in the string
@ezheidtmann
ezheidtmann / cachedattribute.py
Created May 15, 2019 23:23
Cached attribute pattern example
# A basic cached attribute pattern
def compute_a_value():
return 123 + 45
class A:
@property
def a(self):
if not hasattr(self, '_a'):
self._a = compute_a_value()
@ezheidtmann
ezheidtmann / lots.geojson
Created April 15, 2019 02:47
Land currently in use by Wentworth Subaru & Chevy, based on what the buildings say. I haven't looked at tax or ownership records
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ezheidtmann
ezheidtmann / geojson_remove_z_values.py
Created February 28, 2019 03:47
Remove third and greater coordinates from geojson
#!/usr/bin/env python
import json, sys
def _stripZ(coordinates):
if isinstance(coordinates, list) and not any([isinstance(el, list) for el in coordinates]) and len(coordinates) > 2:
return coordinates[:2]
else:
return [_stripZ(el) for el in coordinates]
@ezheidtmann
ezheidtmann / index.js
Created October 3, 2017 21:28
Render static Mapbox GL maps with custom style
/*
* Proof of concept; example code for generating a snapshot of a custom style
*/
let URL = require('url');
let sharp = require('sharp');
let fs = require('fs');
let path = require('path');
let mbgl = require('@mapbox/mapbox-gl-native');
let request = require('request');
@ezheidtmann
ezheidtmann / decorators.py
Created August 8, 2017 20:55
Receive Django signals for a model (workaround for bug in built-in sender filtering)
def model_instance_signal_receiver(signal, Model):
"""Register signal receiver for a specific model
When using QuerySet.defer(), the model objects are instances of a subclass, so
the built-in sender matching doesn't work. This approach uses that subclassing
to ensure we run the receiver for both deferred and non-deferred instances.
As a side effect, this also allows you to register a receiver for all
subclasses of an abstract model.
from datetime import datetime, timedelta, date
from unittest import TestCase
def get_week_start_day(dt, firstday='monday'):
if firstday == 'monday':
return dt.date() - timedelta(days=(dt.weekday() + 0)%7)
elif firstday == 'sunday':
return dt.date() - timedelta(days=(dt.weekday() + 1)%7)
class TestWeekMath(TestCase):
@ezheidtmann
ezheidtmann / routing-notes.md
Created October 20, 2016 23:54
Notes for an LTS-based routing engine, with eventual integration with Ride Report stress data

The Vision

Cycling can be a great way to get around a city, but it's often difficult to find good routes that feel safe. We believe that more people would bike if they had access to better bike directions.

Existing bicycle routing technologies can be great, but they have serious drawbacks:

  • No standard ranking system for streets -- riders don't know what to expect from the algorithm
  • No transparency of tradeoffs between speed, ease of memorization, and stressfulness -- riders don't know whether a given route will feel safe or not.
  • No personalization based on stress tolerance -- riders can't provide their parameters to find a low-stress ride