Skip to content

Instantly share code, notes, and snippets.

@qerub
qerub / exception_with_a_cause.rb
Created November 26, 2013 11:10
[Ruby] Exception with a cause :)
class ExceptionWithACause < StandardError
attr_reader :cause
def initialize(message = nil, cause = nil)
@message = message
@cause = cause
super(message)
end
@qerub
qerub / gist:5901842
Created July 1, 2013 15:27
Clojure functions for Java enum ordinals
(defn enum-ordinal [^Enum x]
(.ordinal x))
(defn enum-cmp [cmp & xs]
(apply cmp (map enum-ordinal xs)))
(defmacro defenumop [cmp]
`(def ~(symbol (str "enum" cmp))
(partial enum-cmp ~cmp)))
@qerub
qerub / ThriftUnixDomainSocketServer.java
Created June 23, 2013 22:03
Thrift + Unix domain socket server
import java.io.File;
import java.io.IOException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TIOStreamTransport;
import org.apache.thrift.transport.TTransport;
import org.newsclub.net.unix.AFUNIXServerSocket;
import org.newsclub.net.unix.AFUNIXSocket;
@qerub
qerub / gist:5845073
Created June 23, 2013 13:42
Making gethostbyaddr accessible in PostgreSQL via Python
CREATE PROCEDURAL LANGUAGE 'plpythonu';
CREATE OR REPLACE FUNCTION gethostbyaddr(address text)
RETURNS text
LANGUAGE plpythonu
AS $$
import socket
try:
return socket.gethostbyaddr(address)[1][0]
@qerub
qerub / js-lisps.rkt
Last active December 17, 2015 17:29
Having fun with Racket, Rackjure and a list of JavaScript Lisps
#lang rackjure
(require net/url)
(define js-lisps*
(~>> "https://gitorious.org/moritz-stuff/js-lisps/blobs/raw/master/js-lisps.sxml"
string->url
get-pure-port
read))
@qerub
qerub / cons.cs
Created May 8, 2013 19:06
Cons (i.e. prepend) for IEnumerable in C#
public static IEnumerable<T> Cons<T>(T head, IEnumerable<T> tail)
{
yield return head;
foreach (var t in tail) yield return t;
}
@qerub
qerub / vm_sync_shutdown.rb
Last active January 18, 2020 17:57
Script to shutdown a libvirt VM/domain and wait for completion
#!/usr/bin/env ruby
require "libvirt"
def vm_sync_shutdown(name)
conn = Libvirt::open("qemu:///system")
dom = conn.lookup_domain_by_name(name)
# TODO: Use dom.state instead of dom.info.state when it gets available
@qerub
qerub / throwf.clj
Created April 28, 2013 13:51
throwf (throw . format) macro for Clojure
(defmacro throwf [class-name format & args]
`(throw (new ~class-name (format ~format ~@args))))
; Before:
(throw (IllegalArgumentException. (format "Invalid value: %s" value)))
; After:
(throwf IllegalArgumentException "Invalid value: %s" value)
@qerub
qerub / gist:5474835
Last active December 16, 2015 18:00
Follow-File for PowerShell
function Follow-File([string] $path) { Get-Content -Path $path -Wait -Tail 10 }
@qerub
qerub / Example.scala
Last active December 15, 2015 07:29
An internal "DSL" in Scala for setting up authorization rules programmatically in ActiveMQ
def authorizationPlugin: BrokerPlugin = {
val dsl = new AuthorizationDSL; import dsl._
val system = Set(User("system"))
entry(Queue(">"), r = system, w = system, a = system)
entry(Topic(">"), r = system, w = system, a = system)
Seq("test").foreach { basename =>
val client = Set(User(basename + "-client"))