Skip to content

Instantly share code, notes, and snippets.

View chadselph's full-sized avatar

Chad Selph chadselph

View GitHub Profile
@chadselph
chadselph / RubyTest.java
Created August 12, 2016 04:59
JRuby instance_evals
import org.jruby.embed.ScriptingContainer;
public class RubyTest {
public static void main(String[] args) {
ScriptingContainer container = new ScriptingContainer();
// From a string
Object myclassString = container.runScriptlet("" +
"class MyClass\n" +
object Test extends App {
trait BitPrinter[B <: BitDescription] {
def printBits(value: Long): (List[String], Long)
}
trait BitDescription
trait BitField extends BitDescription
/**
* A command ID for SMPP
*/
trait FieldHelpers {
val className = getClass.getName.dropRight(1)
val missingLabel = "unknown"
@chadselph
chadselph / JacksonJsonSupport.scala
Created November 19, 2014 02:30
Spray JSON Support with Jackson
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import spray.http.{ContentTypes, HttpCharsets, HttpEntity, MediaTypes}
import spray.httpx.marshalling.Marshaller
import spray.httpx.unmarshalling.Unmarshaller
/**
* Use Jackson directly to avoid json4s's dependencies
*/
@chadselph
chadselph / flaskwithcron.py
Last active April 21, 2022 10:20
flask with "cron"-like loop
from flask import Flask, render_template, jsonify, request
from threading import Timer, Thread
from time import sleep
app = Flask(__name__)
@app.route("/api/<method>")
def api(method):
data = {
@chadselph
chadselph / Smppeezus.scala
Created May 13, 2014 20:31
smpp client to test binds
import java.net.{URL, InetSocketAddress}
import javax.sound.sampled.{AudioSystem, LineListener, LineEvent}
import scala.concurrent.{Promise, Future, Await}
import scala.concurrent.duration.Duration
import scala.concurrent.ExecutionContext.Implicits.global
import akkasmpp.actors.{SmppClient, SmppClientConfig}
import akkasmpp.protocol.{Pdu, BindTransceiverResp, CommandStatus, COctetString, DeliverSm, DeliverSmResp}
import akka.actor.ActorSystem
import akka.pattern.ask
@chadselph
chadselph / PlayAudio.scala
Created May 13, 2014 20:21
Audio Futures
import java.net.URL
import javax.sound.sampled.{AudioSystem, LineListener, LineEvent}
import scala.concurrent.{Promise, Future, Await}
import scala.concurrent.duration.Duration
object PlayAudio extends App {
val f = playUrl(
new URL("http://www.wavsource.com/snds_2014-05-05_4169601282614752/movies/airplane/wrong_week4.wav")
)
/**
* SCALA!!
*/
object DisjointGraphs extends App {
val disjoints = findThem(List((1,2), (2,3), (5,6), (7,8), (1,8)))
println(disjoints)
def findThem(edges: List[(Int, Int)]) = {
val initial = List[Set[Int]]() // empty list of int sets
@chadselph
chadselph / runningwc.py
Last active December 11, 2015 21:09
Like `wc -l` but for `tail -f`. Example: tail -f some.log | grep ERROR | ./runningwc.py -i 5
#!/usr/bin/python -u
from twilio.rest import TwilioRestClient
import sys
import time
import threading
import argparse
total = 0
class Reporter(threading.Thread):
@chadselph
chadselph / okcupidgirl.py
Created January 3, 2013 23:36
TCP server that listens for connections but never replies. Useful for testing certain kinds of timeout behaviors.
# okcupidgirl.py ignores you
from __future__ import print_function
import argparse
import socket
import select
def listen(port):
print("Listening on port {}".format(port))
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)