Skip to content

Instantly share code, notes, and snippets.

Avatar

David Wolever wolever

  • Sheertex
  • Montreal, QC
  • Twitter @wolever
View GitHub Profile
@wolever
wolever / battleship.html
Created Sep 21, 2021
Implementation of probabilistic Battleship playing algorithm roughly as described in https://www.datagenetics.com/blog/december32011/
View battleship.html
<html>
<head>
<script src="./battleship.js"></script>
<style>
.board,
.heatmap {
border: 1px solid black;
margin: 5px;
display: inline-block;
}
View machine.js
Machine({
id: 'KNIT',
initial: 'idle-no-flow',
states: {
'idle-no-flow': {
on: {
FLOW_ASSIGN: 'idle-with-flow'
}
},
View maybe.ts
/**
* Catches any exception which might be raised by a promise and returns a
* tuple of [result, error], where either the result or the error will be
* undefined:
*
* let [res, error] = await maybe(someApi.get(...))
* if (err) {
* return `Oh no there was an error: ${err}`
* }
* console.log('The result:', res)
@wolever
wolever / fix-mouse.lua
Created Nov 7, 2019
Hammerspoon config to use mouse button 3 for scrolling, remap button 4 to middle button
View fix-mouse.lua
-- HANDLE SCROLLING WITH MOUSE BUTTON PRESSED
local scrollMouseButton = 3
local deferred = false
overrideOtherMouseDown = hs.eventtap.new({ hs.eventtap.event.types.otherMouseDown }, function(e)
-- print(hs.eventtap.event.properties['mouseEventButtonNumber'])
local mouseButton = e:getProperty(hs.eventtap.event.properties['mouseEventButtonNumber'])
if mouseButton == scrollMouseButton then
deferred = true
return true
View token_value_at_block.sql
CREATE OR REPLACE VIEW accounts.address_token_balances AS (
SELECT
address_hash,
token_contract_address_hash,
block_number,
LEAD(block_number, 1, 2147483647) OVER (
PARTITION BY address_hash, token_contract_address_hash
ORDER BY block_number ASC
) as next_block_number,
SUM(amount) OVER (
View print-leaked-events.js
#!/usr/bin/env node
// My modifications to this code are public domain
const { writeSync, readFile } = require("fs")
const async_hooks = require("async_hooks")
function showStack() {
let e = new Error()
writeSync(1, e.stack.split('\n').slice(2).join('\n') + '\n')
@wolever
wolever / watchdir
Created Oct 24, 2018
Watch a directory for changes and run a command when it does
View watchdir
#!/bin/bash
# STOP! Before going any further, think: are you going to regret the decision
# to write this script?
# Deciding to write this in bash was not one of my better decisions.
# -- https://twitter.com/alex_gaynor/status/369892494114164736
IFS="`printf "\n\t"`"
set -u
if [[ "$#" -lt 2 ]]; then
@wolever
wolever / git-bclean
Created Mar 27, 2018
Deletes all merged branches after prompting for confirmation. Use `-r` for remote branches.
View git-bclean
#!/bin/bash
# STOP! Before going any further, think: are you going to regret the decision
# to write this script?
# Deciding to write this in bash was not one of my better decisions.
# -- https://twitter.com/alex_gaynor/status/369892494114164736
IFS="`printf "\n\t"`"
set -eu
remotes=""
@wolever
wolever / deletablemodel.py
Created Dec 5, 2017
A Django model base class that hides models instead of deleting them.
View deletablemodel.py
from django.utils import timezone
from django.db import models as m
from django.db.models.base import ModelBase
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.fields.related import SingleRelatedObjectDescriptor
from model_utils.managers import PassThroughManagerMixin
class DeletableQuerySetMixin(object):
@wolever
wolever / return-in-finally.py
Last active Sep 1, 2017
Confusing bug I was hit with today: a `return` in a `finally` that ate an exception thrown from a generator
View return-in-finally.py
"""
A demonstration of a subtle bug when a context manager uses
`return` in a `finally` block around a `yield`.
See comments on last line for an explanation.
Take-away: be very, very careful about using `return` in a
`finally` block.
"""