Skip to content

Instantly share code, notes, and snippets.

View davidh-raybeam's full-sized avatar

David Hollis davidh-raybeam

View GitHub Profile
@davidh-raybeam
davidh-raybeam / 0_ConstEnum.scala
Created July 16, 2018 18:27
Constant Enum implementation with accessors and json serializers
import play.api.libs.json._
trait ConstEnumType[T] { self: T =>
val name: String
val jsonKey: String = "type"
lazy val reads: Reads[T] = (JsPath \ jsonKey).read[String].filter(_ == name).map(_ => self)
lazy val matcher: PartialFunction[String, Option[T]] = {
implicit class JoinableMap[K, V1](left: Map[K, V1]) {
def innerJoin[V2](right: Map[K, V2]): Map[K, (V1, V2)] = (
for {
k <- (left.keySet & right.keySet).toSeq
l <- left.get(k)
r <- right.get(k)
} yield (k -> (l, r))
).toMap
def leftJoin[V2](right: Map[K, V2]): Map[K, (V1, Option[V2])] = (
@davidh-raybeam
davidh-raybeam / unenqueue.rb
Last active August 2, 2016 20:58
Paste this function into the master's rails console to enable it. It removes an item from the queue and marks it as "in progress" without assigning it to a worker, which will cause it to fail within 30 seconds or so. I wouldn't recommend calling this on tasks that are likely to be pulled off the queue any time soon.
# Example:
# unenqueue '411a80c7-fbcf-4312-a504-ebddfdbf5b1f'
def unenqueue(runtime_id)
default_queue = Mimir::Runtime::WorkQueue.new
Redis.current.multi do
default_queue.upcoming.delete(runtime_id)
Redis.current.lpush(default_queue.in_progress.key, runtime_id)
end
end
#! /bin/bash
# USAGE ./check-container.sh $CONATINER_ID
[[ "$(docker inspect --format='{{ .State.Running }}' --type=container $1)" == "true" ]] && exit 0 || exit 1
module Kernel
def it
(Class.new(BasicObject) {
def method_missing(name, *args)
@calls ||= []
@calls << { name: name, args: args }
self
end
def to_proc
Proc.new do |rcv|
@davidh-raybeam
davidh-raybeam / .bashrc
Created October 13, 2014 18:15
A shell function to quickly initialize empty python packages.
# Creates directories and empty __init__.py files for the given python package in the current directory.
# Usage:
# pypkg a.python.package.name
pypkg () {
local pkgpath="$(echo $1 | sed 's:\.: :g')"
local fspath="."
for el in $pkgpath; do
fspath="${fspath}/${el}"
(mkdir "$fspath" && touch "${fspath}/__init__.py") &>/dev/null
done
@davidh-raybeam
davidh-raybeam / gitsh
Created August 26, 2014 14:31
Shelly (https://github.com/davidh-raybeam/shelly) script for a git shell with a nice prompt and tab completion for remotes and branches.
#!/usr/bin/env ruby
require 'shelly'
require 'shellwords'
include Shelly
$YELLOW = `tput setaf 3`
$BLUE = `tput setaf 4`
$WHITE = `tput setaf 7`
$RESET = `tput sgr0`
"foo\uFFFF".each_char.to_a.collect(&:ord).reject { |x| x > 65000 }.collect(&:chr).join
# => "foo"
@davidh-raybeam
davidh-raybeam / ohshit.rb
Created April 24, 2014 21:47
Makes something interruptible, but not otherwise killable.
begin
code_that_could_break
rescue SignalException => se
raise se
rescue Exception => e
puts e
end
@davidh-raybeam
davidh-raybeam / tputcolors.sh
Created December 6, 2013 17:02
Shamelessly copied from somewhere on the Internet. Please comment if you know the actual source, and I'll update this.
#!/bin/bash
# tputcolors
echo
echo -e "$(tput bold) reg bld und tput-command-colors$(tput sgr0)"
for i in $(seq 1 7); do
echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) \$(tput setaf $i)"
done