Skip to content

Instantly share code, notes, and snippets.

View emiel's full-sized avatar

Emiel van de Laar emiel

View GitHub Profile
@emiel
emiel / docker-ports.md
Created May 11, 2023 15:10
docker: Publish ports

Publish ports on all interfaces. The ports are then accessible from "outside" unless the firewall blocks traffic.

$ docker run -p 80:80 nginx

CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                               NAMES
16f16f77fc90   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   dreamy_neumann

$ ss -t -n state listening 'sport :80'
@emiel
emiel / Dockerfile
Created May 3, 2023 10:36
Demonstrate ARG scoping in child stages
# syntax=docker/dockerfile:1
FROM busybox as parent
ARG FOO=123
FROM parent as child
RUN echo "FOO=${FOO}" # "FOO=123"
FROM busybox as final
RUN echo "FOO=${FOO}" # "FOO="
@emiel
emiel / flake.nix
Last active October 6, 2022 19:44
A very basic flake
{
description = "A very basic flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
import pandas as pd
import dbconn
engine = dbconn.get_engine_by_name("foo")
result = engine.execute("""
select table_name, column_name, referenced_table_name, referenced_column_name
from information_schema.key_column_usage
where constraint_schema = 'smartpr_api'
@emiel
emiel / argb.js
Created September 14, 2017 17:52
RGBA color space (javascript)
decode_argb = function(argb_val)
{
/*
- https://en.wikipedia.org/wiki/RGBA_color_space
- ARGB (word-order)
- https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
*/
return {
a: (argb_val & 0xff000000) >>> 24,
create table queue
(
id integer,
diff integer
);
create table cache
(
id integer primary key,
count integer not null,
@emiel
emiel / sendgrid_eid.sql
Last active April 11, 2018 15:16
Decode SendGrid Event ID in Postgres
create or replace function sendgrid_eid24_to_uuid(eid text)
returns uuid language sql immutable strict parallel safe
as $function$
select encode(decode(translate(eid, '-_', '+/'), 'base64'), 'hex')::uuid;
$function$;
create or replace function sendgrid_eid48_to_uuid(eid text)
returns uuid language sql immutable strict parallel safe
as $function$
@emiel
emiel / sendgrid_eid.py
Last active April 11, 2018 15:16
Decode SendGrid Event ID in Python
def decode_eid(eid):
"""
Decodes a SendGrid event id (sg_event_id) and returns as uuid
"""
eid_len = len(eid)
if eid_len == 22:
res = uuid.UUID(bytes=base64.urlsafe_b64decode(eid + "=="))
elif eid_len == 48:
res = uuid.UUID(base64.urlsafe_b64decode(eid).decode("ascii"))
@emiel
emiel / jsonb_delete_array.sql
Created May 24, 2017 09:39
Delete multiple key/value pairs or string elements from left operand
--
-- Coming in PostgreSQL 10
-- https://www.postgresql.org/docs/10/static/functions-json.html
--
create or replace function jsonb_delete_array(data jsonb, keys text[])
returns jsonb immutable language sql
as $function$
select coalesce((
select jsonb_object_agg(key, value)
from jsonb_each(data)
@emiel
emiel / nested_function_overhead.py
Last active February 20, 2017 08:58
Python nested function overhead
import timeit
t1 = timeit.Timer("doit()", """
def doit():
def foo(l):
return [x**2 for x in l]
def bar(l):
return [x**2 for x in l]