Skip to content

Instantly share code, notes, and snippets.

View pilcrow's full-sized avatar

Mike Pomraning pilcrow

View GitHub Profile
@pilcrow
pilcrow / MapEntryUtils.java
Created October 19, 2023 14:22
MapEntryUtils unroll map entry (java)
/**
* Public Domain
* 2023-10-19 Mike Pomraning, first version
*/
package local;
import java.util.Map;
import java.util.AbstractMap.SimpleEntry;
import java.util.AbstractMap.SimpleImmutableEntry;
def coalesce(*args):
# return first(lambda x: x is not None, args)
return next(_ for _ in args if _ is not None)
def first(selector, iterable, default = None):
return next((_ for _ in iterable if selector(_)), default)
@pilcrow
pilcrow / upto.sh
Created August 21, 2020 18:54
upto.sh – POSIX sh function to repeat a failing command up to N times
# Public Domain
# Written 2020-08-21, Mike Pomraning
#
# upto N command [args...]
#
# Repeatedly execute command until successful, up to N times, returning the
# exit status of the last invocation of command.
#
upto () {
NumAttempts=$1;
@pilcrow
pilcrow / natatime.py
Created January 16, 2017 13:43
Python iterate N items at a time
import itertools
def natatime(n, iterable, fillvalue = None):
"""Returns an iterator yielding `n` elements at a time.
:param n: the number of elements to return at each iteration
:param iterable: the iterable over which to iterate
:param fillvalue: the value to use for missing elements
:Example:
@pilcrow
pilcrow / table_info_types.t
Created June 17, 2014 04:26
DBI test: consistent object TYPEs from table_info()
#! /usr/bin/perl
# Test whether a DBD returns sensible object TYPE listings for a given
# database.
#
#
# Usage: perl table_info_types.t DSN [USER [PASS]]
# -or-
# perl table_info_types.t DBIx_CONFIG_SPEC
# -or-
@pilcrow
pilcrow / util_range.sql
Created March 30, 2011 14:45
Firebird util$range stored procedure
-- UTIL$RANGE(start, stop, step)
--
-- This file is in the Public Domain.
-- Firebird selectable stored procedure for producing integer ranges.
-- (Mike Pomraning; 2011-03-30)
--
CREATE EXCEPTION util$err_range_zero_step 'step size may not be zero';
SET TERM !!;
CREATE PROCEDURE util$range("Start" INTEGER, "Stop" INTEGER, "Step" INTEGER)
RETURNS (i INTEGER) AS
@pilcrow
pilcrow / udf_fb_sleep.c
Created March 16, 2011 19:56
Firebird 2.1 FB_SLEEP UDF
/* FB_SLEEP() UDF - pause an FB thread for a number of seconds */
/* Mike Pomraning 2011 for FB 2.1; this code is in the Public Domain */
#include <sys/select.h>
/*
DECLARE EXTERNAL FUNCTION fb_sleep
DOUBLE PRECISION
RETURNS INTEGER BY VALUE
ENTRY_POINT 'UDF_fb_sleep' MODULE_NAME 'udf_fb_sleep';
@pilcrow
pilcrow / fb_udf_gettimeofday.c
Created March 16, 2011 17:48
Firebird 2.1 GETTIMEOFDAY UDF
/* Quick GETTIMEOFDAY() UDF, because FB TIMESTAMPs are not TIME ZONE aware */
/* Mike Pomraning 2011 for FB 2.1; this code is in the Public Domain */
#include <sys/time.h>
/*
DECLARE EXTERNAL FUNCTION gettimeofday
RETURNS DOUBLE PRECISION BY VALUE
ENTRY_POINT 'UDF_gettimeofday' MODULE_NAME 'udf_gettimeofday';
-- Let PG compute as many of the DBI attributes as is practicable --
SELECT
a.attname AS name,
t.typname AS type_name,
CASE
WHEN a.attlen > 0 THEN a.attlen
WHEN a.atttypmod > 65535 THEN a.atttypmod >> 16
WHEN a.atttypmod >= 4 THEN a.atttypmod - 4
ELSE NULL
END AS precision,