Skip to content

Instantly share code, notes, and snippets.

@d1manson
d1manson / reloadd.py
Last active November 15, 2023 16:50
Python - reload a module and update the methods for pre-existing versions of its class(es).
# -*- coding: utf-8 -*-
import sys
from types import ModuleType, ClassType
def can_reload_methods(klass):
klass.__CAN_RELOAD_METHODS__ = True
return klass
def reloadd(m):
{% test enum_distribution(model, column_name, expected, allow_other=False, fudge=0.1) %}
SELECT
COALESCE(expected.enum_val, actual.enum_val) AS enum_val,
expected.fraction AS expected_fraction,
actual.fraction AS actual_fraction
FROM (
{% for enum_val, expected_fraction in expected.items() -%}
SELECT '{{enum_val}}' AS enum_val, {{expected_fraction}} AS fraction
{%- if not loop.last %} UNION ALL {% endif %}
// For info on usage, development and debugging,
// see https://docs.google.com/a/landtech.co/document/d/1eB2oH3d7mpDfK8gxTiYphxPgBM7EDu5MeUNKrQwYIrQ/ . [private]
const Painter = require('./render/painter'),
Style = require('./style/style'),
EXTENT = require('./data/extent'),
Evented = require('./util/evented'),
TileCoord = require('./source/tile_coord'),
mat4 = require('@mapbox/gl-matrix').mat4,
Source = require('./source/source'),
@d1manson
d1manson / bridged-worker.js
Last active August 31, 2022 16:18
BuildBridgedWorker is a function that makes communication between worker and main thread simple. It also means you can keep all related code in the same file, which is convenient.
/* EXAMPLE:
var workerCode = function(){
"use strict;" //this will become the first line of the worker
CalculateSomething(a,b,c,d){
var v = a+b+c+d; //trivial calculation
main.DisplayResult(v,"hello");
}
@d1manson
d1manson / if_equal.py
Last active October 20, 2021 23:09
Prefect control flow helper, similar to `case`. See https://github.com/PrefectHQ/prefect/issues/5071#issuecomment-947846404
from typing import Any, TYPE_CHECKING, Dict
import prefect
from prefect import Task, Flow
from prefect.triggers import all_successful
from prefect.tasks.control_flow.conditional import CompareValue
from prefect.engine import signals
if TYPE_CHECKING:
from prefect.engine import state # noqa
from prefect import core # noqa
@d1manson
d1manson / simulate_3dsecure_action.js
Last active August 31, 2021 17:22
for use in testing stripe intents server side. note this is for use with a test mode key. relies on implementation details that might change
const { STRIPE_PUBLIC_KEY } = process.env,
axios = require("axios"),
qs = require("querystring").stringify,
{ JSDOM } = require("jsdom");
module.exports.simulateAction = async function(
intentId,
clientSecret,
mode = "setup_intent"
) {
@d1manson
d1manson / anova.py
Last active July 30, 2021 12:27
perform two-way repeated measures anova in python using {car} package in R, via rpy2
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
_dummy_data_anova2 = pd.DataFrame(np.random.rand(30,4) + [[0., 1., 0.2, 0.8]],
columns=pd.MultiIndex.from_tuples([
('a0','b0'),('a0','b1'),('a1','b0'),('a1','b1')],
names=['factor_a', 'factor_b']),
index=range(30))
@d1manson
d1manson / pso.py
Last active November 20, 2018 11:07
fork of pyswarm (particle swarm optimization in python) - this version has been vectorised/optimised more than the original
import numpy as np
def pso(func, lb, ub, ieqcons=[], f_ieqcons=None, args=(), kwargs={},
func_takes_multiple=False, swarmsize=100, omega=0.5, phip=0.5, phig=0.5,
maxiter=100, minstep=1e-8, minfunc=1e-8, debug=False, info=True):
"""
Perform a particle swarm optimization (PSO)
Parameters
==========
@d1manson
d1manson / BlobWorker.js
Last active June 27, 2018 11:28
Using an HTML5 Blob, you can get around the same-origin security restriction for Web Workers. The only slight complication is the need to return a Worker-like interface synchrounously. But this is actually fairly simple. Requires jQuery $ for ajax call. IE doesn't permit making workers from blob-urls, but other browsers do.
Worker = (function(){
var nativeWorker = Worker;
var BlobWorker = function(){
this.queuedCallList = [];
this.trueWorker = null;
this.onmessage = null;
}
BlobWorker.prototype.postMessage = function(){
if(this.trueWorker)