Skip to content

Instantly share code, notes, and snippets.

@pirate
pirate / uuid7.py
Last active January 25, 2025 15:11
Pure python all-in-one UUIDv7 Implementation with graceful degradation between ms, µs, and ns timestamp precision
#!/usr/bin/env python3
# A single-file pure-python implementation of UUIDv7: (e.g. 01941230-851a-77fd-9b0b-8c8eac3b2d23)
# - makes sure UUIDv7s are always generated in alphabetic order (using system time + nanoseconds + extra monotonic random bits)
# - store millisecond, microsecond, nanosecond / variable precision timestamps all using same format
# - graceful degradation in precision, falls back to monotonic urandom bytes depending on user-provided timestamp precision
# - fully compatible with standard UUIDv7 spec (48 bit millisecond unix epoch time with rand_a & rand_b monotonic randomness)
# - allows you to generate a UUIDv7 with a given timestamp (of any precision), and parse the timestamp back out from any UUIDv7
# - helps guarantee that UUIDv7s generated back-to-back in the same thread are always monotonically sortable
# - helps lower the risk of UUIDv7s colliding with other UUIDv7s generated in other threads / on other machines
@marcoslot
marcoslot / ais.sql
Last active December 12, 2024 06:19
Load AIS data into Iceberg
-- Load data from https://coast.noaa.gov/htdata/CMSP/AISDataHandler/2024/index.html into Iceberg
--
-- To prepare: create extension crunchy_spatial_analytics cascade;
-- Clean up previous creation
-- drop table if exists ais, loaded_ais_files;
-- Create the AIS Iceberg table
create table ais (
@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 August 22, 2024 21:16
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 February 20, 2025 08:53
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;
# Clean the database
DROP TABLE IF EXISTS _orders CASCADE;
DROP TABLE IF EXISTS _users CASCADE;
DROP TABLE IF EXISTS orders CASCADE;
DROP TABLE IF EXISTS users CASCADE;
# Build the database (for hard deletion)
CREATE TABLE users (
id integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name text NOT NULL