Skip to content

Instantly share code, notes, and snippets.

View silas's full-sized avatar

Silas Sewell silas

View GitHub Profile
@silas
silas / array_helpers.sql
Last active January 13, 2022 20:50
PostgreSQL array helper functions
CREATE OR REPLACE FUNCTION array_distinct(anyarray) RETURNS anyarray AS $$
SELECT coalesce(array_agg(x.value ORDER BY x.pos), '{}')
FROM (
SELECT DISTINCT ON (item.value) item.value, item.pos
FROM unnest($1) WITH ORDINALITY item(value, pos)
) x
$$ LANGUAGE SQL
CREATE OR REPLACE FUNCTION array_set(anyarray) RETURNS anyarray AS $$
SELECT coalesce(array_agg(x.v), '{}')
@silas
silas / frozenmap.py
Created December 31, 2021 03:47
Python FrozenMap (frozendict, immutable dict, etc...)
import collections.abc
import typing as typ
K = typing.TypeVar("K")
V = typing.TypeVar("V")
class FrozenMap(typing.Generic[K, V], collections.abc.Mapping[K, V]):
__slots__ = ("_dict", "_hash")
@silas
silas / toposort.js
Last active December 11, 2020 22:31
/*
Copyright (c) 2012 by Marcel Klehr <mklehr@gmx.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@silas
silas / README.md
Created June 20, 2019 15:15
Anki JavaScript Cards

Anki JavaScript Cards

This demonstrates one method for creating JavaScript based Anki cards.

Tested on

  • [Anki for desktop computers][anki-desktop] version 2.1.13
  • [AnkiMobile][anki-mobile] (iOS) version 2.0.48

Instructions

@silas
silas / github_keys.py
Created February 4, 2019 15:56
Gitolite tool to sync SSH keys from Github
#!/usr/bin/env python3
import glob
import hashlib
import os
import urllib.request
def update_keys(local_user, github_user):
with urllib.request.urlopen('https://github.com/%s.keys' % github_user) as f:
@silas
silas / qt.sh
Created July 18, 2018 11:31
Qt Python Example
$ mkdir test
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install PySide2
$ cat << EOF > main.py
import sys
from PySide2.QtWidgets import QApplication, QLabel
if __name__ == '__main__':
@silas
silas / build.gradle
Last active March 1, 2023 07:35
Load `.env` into gradle run
def static getenv(path = ".env") {
def env = [:]
def file = new File(path)
if (file.exists()) {
file.eachLine { line ->
line = line.trim()
if (line != "" && !line.startsWith("#")) {
def pair = line.split("=", 2)
env[pair[0].trim()] = pair.length == 2 ? pair[1].trim() : ""
@silas
silas / README.md
Last active January 10, 2019 13:27
Swagger snake case
import io.swagger.converter.ModelConverters;

ModelConverters.getInstance().addConverter(new SnakeCaseConverter());

BeanConfig config = new BeanConfig();
config.setTitle("Swagger sample app");
config.setVersion("1.0.0");
config.setResourcePackage("io.swagger.sample.resource");
config.setScan(true);
@silas
silas / enum_type.py
Created June 21, 2016 18:39
Enum type for SQLAlchemy
"""
This work is licensed under the MIT License (https://opensource.org/licenses/MIT)
Example:
class Example(Model):
class Type(enum.Enum):
one = 1
two = 2
@silas
silas / jenkins.sh
Created December 24, 2015 16:13
Jenkins docker script
jenkins() {
case "$1" in
clean)
rm -fr $( ls -d1 /tmp/*.jenkins )
return 0
;;
*)
version="${1:-latest}"; shift
dir="${1:-$( mktemp -d --suffix=.jenkins )}"; shift