Skip to content

Instantly share code, notes, and snippets.

View purcell's full-sized avatar

Steve Purcell purcell

View GitHub Profile
@purcell
purcell / auto-python-venv.md
Last active July 27, 2021 19:12
Sane automatic python + virtualenv

Automatic python version plus venv

Goal

For easy editor integration and command-line usage, we'd like to be able to specify a Python version per project, with its own virtualenv to isolate its libraries from those of other projects.

We're willing to change $PATH globally once, but not per project. And we'd like to avoid having to run every python command invocation in a special subshell created by a shell wrapper. Instead, simply invoking "python" or "pip" etc. should do the right thing, based on the directory in which it is invoked.

It turns out this is possible!

@purcell
purcell / taskqueues.sql
Last active March 19, 2021 08:35
Easy task queues using PostgreSQL
-- Let's say you have a table full of work:
CREATE TABLE tasks (
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
status TEXT NOT NULL DEFAULT 'pending',
payload JSON NOT NULL, -- or just have meaningful columns!
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);
(defun key-quiz--shuffle-list (list)
"Shuffles LIST randomly, modying it in-place."
(dolist (i (reverse (number-sequence 1 (1- (length list)))))
(let ((j (random (1+ i)))
(tmp (elt list i)))
(setf (elt list i) (elt list j))
(setf (elt list j) tmp)))
list)
@purcell
purcell / HereQ.hs
Last active April 28, 2019 21:24
Quasi-quoter for postgresql-simple
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -fno-warn-missing-fields #-}
-- | Interpolate any type that inhabits both IsString and Semigroup
-- This code is based on the "here" package. It would be nice to strip
-- leading whitespace, as the "neat-interpolation" package does.
-- Additionally, "neat-interpolation" has a simpler interpolation
@purcell
purcell / Aggregation.purs
Last active November 18, 2019 09:52
Multiple aggregates in a single pass, using Purescript
-- This is based on ideas from the excellent article "Beautiful Aggregations
-- with Haskell" by Evan Borden: https://tech.freckle.com/2017/09/22/aggregations/
module Aggregation where
import Prelude
import Data.Foldable (foldMap)
import Data.Monoid.Additive (Additive(..))
import Data.Newtype (un)
@purcell
purcell / with-docker-db.sh
Last active March 6, 2018 22:54
Run a command against a PostgreSQL DB installed and started on demand using Docker
#!/bin/bash -e
image=mdillon/postgis:9.6-alpine
container_name=my-app-postgresql
if [ -z "$1" ]; then
echo "Run command with a dockerised PostgreSQL DB.
usage: $(basename "$0") command
@purcell
purcell / migrations.sql
Last active September 16, 2017 10:10
PostgreSQL schema migrations system
This code now lives in its own repo at https://github.com/purcell/postgresql-migrations
@purcell
purcell / code-font-user-styles.css
Last active October 22, 2020 22:55
User styles for coding fonts on sites with lots of code
@-moz-document domain("github.com"), domain("gist.github.com") {
tt, code, pre, .file-data pre, textarea, .blob-line-code, .blob-code, .blob-code-inner, .diff-line-code {
font-family: "Iosevka Fixed SS08", "Iosevka Fixed", "Iosevka Term", "Iosevka", "Input Mono Condensed", "PragmataPro", "Ubuntu Mono", "Menlo";
font-weight: 400;
}
}
@-moz-document domain("gitlab.com") {
tt, code, pre, textarea, .code, .file-content.code pre code {
font-family: "Iosevka Fixed SS08", "Iosevka Fixed", "Iosevka Term", "Iosevka", "Input Mono Condensed", "PragmataPro", "Ubuntu Mono", "Menlo";
@purcell
purcell / iosevka
Last active June 13, 2020 16:35
Mac custom-build of Iosevka with Pragmata-like features
#!/bin/sh -e
# https://github.com/be5invis/Iosevka#build-your-own-style
DIR=~/Projects/External/iosevka
if [ -d "$DIR" ]; then
(cd $DIR && git pull)
else
git clone --depth 1 https://github.com/be5invis/Iosevka $DIR
fi
module Puzzle where
import Data.List (permutations)
import Control.Monad (guard)
solution = do
[a,b,c,d,e,f] <- permutations "123456"
guard $ multiple 6 [a,b,c,d,e,f]
guard $ multiple 5 [a,b,c,d,e]
guard $ multiple 4 [a,b,c,d]