Skip to content

Instantly share code, notes, and snippets.

View chadselph's full-sized avatar

Chad Selph chadselph

View GitHub Profile
@chadselph
chadselph / patternmatching.py
Last active February 15, 2024 14:45
Functional language style pattern matching in python with a decorator
from collections import defaultdict
class BadMatch(NameError):
"""Exception when your args don't match a pattern"""
pass
class Any(object):
"""
>>> 'wutup' == Any()
True
@chadselph
chadselph / ReadData.scala
Created February 3, 2023 02:02
Reading data from confluent schema registry without using their library
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.serialization.{Deserializer, StringDeserializer}
import org.apache.kafka.common.utils.ByteUtils
/**
* Not recommended for production but useful when you want to write some kind of
* quick script that reads data off kafka.
*/
object ReadData {
@chadselph
chadselph / webmaildebug.py
Created October 29, 2010 20:33
yet another tool for debugging emails your application is sending. This script accepts email as an SMTP server and lets you read the emails contents via http. It can use sqlite3 or a python list to save data.
import asyncore, asynchat
import email
import re
import smtpd
import sqlite3
import socket, string
import StringIO, mimetools
# HTTP based on examples at http://effbot.org/librarybook/asynchat.htm
class HTTPChannel(asynchat.async_chat):
@chadselph
chadselph / AkkaHttpHeaderExtractor.scala
Last active July 3, 2022 16:04
akka-http directive for opentracing.io
import java.util
import java.util.Map.Entry
import akka.http.scaladsl.model.HttpHeader
import io.opentracing.propagation.TextMap
import scala.collection.JavaConverters.asJavaIteratorConverter
/**
* Used to extract an iterator of Entry[String, String] to the
@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 / 1-original.md
Created April 12, 2012 23:39
gsm encoding for python

(From http://stackoverflow.com/questions/2452861/python-library-for-converting-plain-text-ascii-into-gsm-7-bit-character-set, ran out of space in the comment section.)

Running the original file does not work in either Python 2 or 3.

In Python2, the program prints this:

64868d8d903a7390938d85

(which is wrong) because it is using the indexes of gsm which do not map to the index of their GSM encodings due to the fact that it is a bytestring with some characters taking up multiple bytes. gsm is actually equal to

@chadselph
chadselph / Server.java
Created April 29, 2021 00:43
ugly ftp2http server
package com.goswiftly.http2ftp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URL;
import java.util.Base64;
import static spark.Spark.*;
@chadselph
chadselph / Deserializers.java
Created May 4, 2020 21:32
jackson Deserializer.tryInOrder
package com.goswiftly.services.gtfspoller.jacksonutil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import io.vavr.control.Try;
import java.util.NoSuchElementException;
public class Deserializers {
@chadselph
chadselph / compare_schemas.py
Created April 18, 2020 18:15
Compare two db schemas
from typing import Generator, Tuple, Set, List
from sqlalchemy import inspect, create_engine
import sys
GeneratorOfDiscrepancies = Generator[Tuple[str, str], None, None]
class InspectorWrapper:
def __init__(self, url: str):
@chadselph
chadselph / Election.scala
Created December 19, 2018 01:13
Choice voting election
package domain
case class Choice(name: String) extends AnyVal
case class Ballot(topChoice: Choice, next: List[Choice]) {
def eliminateChoice(choice: Choice): Option[Ballot] =
if (topChoice == choice) {
next match {
case Nil => None