Skip to content

Instantly share code, notes, and snippets.

@pncnmnp
pncnmnp / gist:8afb7903f61ec69a157287435a6347aa
Created December 9, 2023 17:51
Test out bazzargh's variation of "Shuffling using Fibonacci hashing"
import random
import math
import copy
import numpy as np
def shuffle_songs(songs):
"""Return a list of shuffled songs."""
num_songs = len(songs)
@jschaf
jschaf / admin.sql
Last active January 13, 2024 16:18
Postgres audit tables with uni-temporal tables
-- create_temporal_past_table creates a new table with the same structure
-- as the current table. Adds triggers to copy all changed or deleted rows
-- from the current table to the past table.
CREATE PROCEDURE admin.create_temporal_past_table(curr_tbl regclass, past_tbl text) AS $fn$
DECLARE
curr_tbl_qual text := simc.quote_regclass(curr_tbl);
past_tbl_schema text := (parse_ident(past_tbl))[1];
past_tbl_name text := (parse_ident(past_tbl))[2];
past_tbl_qual text := quote_ident(past_tbl_schema) || '.' || quote_ident(past_tbl_name);
@jszym
jszym / namespaced_token.py
Created June 28, 2023 17:57
Namespaced, random token.
from base64 import urlsafe_b64encode
from blake3 import blake3
import krock32
from time import time
import secrets
import math
def generate_token(namespace: str) -> str:
"""
@benperiton
benperiton / Dockerfile
Last active February 25, 2024 19:19
backgroundremover test
FROM python:3.6-slim
ARG UID
ARG GID
RUN groupadd -g "${GID}" app \
&& useradd --create-home --no-log-init -u "${UID}" -g "${GID}" app
RUN apt-get update && \
apt-get -y install python3-pip && \
@brandur
brandur / go.mod
Last active July 25, 2023 21:41
PartialEqual implementation
module github.com/brandur/prequire
go 1.19
require github.com/stretchr/testify v1.8.1
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
@func25
func25 / custom_struct_tag.go
Created January 15, 2023 13:56
Custom struct tag in Go
package main
import (
"fmt"
"reflect"
"strconv"
"strings"
)
type Student struct {
@Sytten
Sytten / ulid.sql
Created June 6, 2022 03:00
ULID PL/pgSQL
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE OR REPLACE FUNCTION uuid_generate_ulid ()
RETURNS uuid
AS $$
DECLARE
timestamp bytea = E'\\000\\000\\000\\000\\000\\000';
unix_time bigint;
BEGIN
unix_time = (EXTRACT(EPOCH FROM clock_timestamp()) * 1000)::bigint;
@kjmph
kjmph / A_UUID_v7_for_Postgres.sql
Last active April 23, 2024 15:26
Postgres PL/pgSQL function for UUID v7 and a bonus custom UUID v8 to support microsecond precision as well. Read more here: https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
-- Based off IETF draft, https://datatracker.ietf.org/doc/draft-peabody-dispatch-new-uuid-format/
create or replace function uuid_generate_v7()
returns uuid
as $$
begin
-- use random v4 uuid as starting point (which has the same variant we need)
-- then overlay timestamp
-- then set version 7 by flipping the 2 and 1 bit in the version 4 string
return encode(
@slotrans
slotrans / history_stuff.sql
Created August 6, 2021 23:50
Building blocks for generic history-keeping in Postgres.
/*
Replace "your_schema" with whatever schema is appropriate in your environment.
It is possible to use "public"... but you shouldn't!
*/
/*
Function to stamp a "modified" timestamp. Adjust the name to suit your environment,
but that name is hard-coded so it is assumed that you only use _one_ such name.