Skip to content

Instantly share code, notes, and snippets.

@stantonk
stantonk / ConfigLoader.java
Created October 7, 2015 16:43
Using Jackson + Hibernate Validation to load YAML or Java config files and validate configuration key/val pairs are valid.
package default;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import org.hibernate.validator.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@stantonk
stantonk / date_xrange.py
Created August 19, 2015 18:24
python datetime xrange generator
def date_xrange(s_dt, e_dt, inclusive=False):
ONE_DAY = timedelta(days=1)
days = (e_dt - s_dt).days
days = days + 1 if inclusive else days
return (s_dt + i*ONE_DAY for i in range(0, days))
@stantonk
stantonk / loop.py
Last active August 29, 2015 14:24
Indefinitely cycle through some sequence in Python, e.g. for object pools.
# loop foreva-eva
def loop(sequence):
while True:
for elem in sequence:
yield elem
# example usage, imagine you have a set of things you want
# to cycle through, removing bad ones as they appear
class ObjectPoolEmptyError(Exception):
@stantonk
stantonk / run-tests-hook.sh
Last active August 29, 2015 14:23
In a mercurial or git pre-commit or pre-push hook: detect the type of project and invoke the appropriate test runner for various projects. So far Maven and Django projects are supported. Trivial to add others. No-ops on unknown project types.
#!/usr/bin/env bash
################################################################################
# Use as a global mercurial/git pre commit or pre push hook to detect the type
# of project and run unittests before allowing a commit or push to a remote
# repository.
#
# Handy so that you don't have to remember to add test running hooks to every
# repo clone/fork you have.
#
# ********** MERCURIAL SETUP *************
@stantonk
stantonk / get_valid_tlds.py
Last active July 11, 2022 22:36
Updated list of valid TLDs (Top Level Domains). Python script grabs the IANA database and formats it into a set datatype.
# requirements.txt
# pip install requests
# pip install BeautifulSoup4
import codecs
import requests
from bs4 import BeautifulSoup
PER_LINE = 12
@stantonk
stantonk / eplot.rb
Created April 30, 2015 21:37
featureful command line plotting of data in a Linux shell, made easy with eplot, a gnuplot wrapper. This version is modified slightly, per the instructions found here: http://stackoverflow.com/questions/123378/command-line-unix-ascii-based-charting-plotting-tool#answer-12868778. eplot in its original form can be found here: http://liris.cnrs.fr/…
#!/usr/bin/ruby
# **************************************************************************
# ### modified based on:
# http://stackoverflow.com/questions/123378/command-line-unix-ascii-based-charting-plotting-tool
# ###
# eplot
# Written by Christian Wolf
#
# eplot ("easy gnuplot") is a shell script which allows to pipe data easily
# through gnuplot and create plots quickly, which can be saved in postscript,
@stantonk
stantonk / KeyValueStore.java
Last active August 14, 2016 17:29
Simple threadsafe LevelDB-backed key-value store for Java
/**
<dependency>
<groupId>org.fusesource.leveldbjni</groupId>
<artifactId>leveldbjni-all</artifactId>
<version>1.7</version>
</dependency>
*/
import com.google.common.base.Optional;
import org.iq80.leveldb.DB;
@stantonk
stantonk / pom.sh
Last active August 29, 2015 14:11
open Java Maven project in IntelliJ from the OS X terminal
#!/usr/bin/env bash
open -a /Applications/IntelliJ\ IDEA\ 13\ CE.app/ pom.xml
# best as a bash alias :)
@stantonk
stantonk / quick-collapse.sh
Last active August 29, 2015 14:11
Mercurial hg: Quickly squash a recent, unpushed commit with some un-committed changes. This is really handy when you forgot to do something, or a linter or CI suite catches errors before you push / merge.
#!/usr/bin/env bash
# N.B. requires patch queues (mq) enabled in your .hgrc. to finish this up
# and turn into a real commit, do `hg qfinish -a`
now=`date +"%s"`
patch_name='$now-temporary'
hg qnew $patch_name
hg qpop
import sys
class TestTriedToAccessNetwork(BaseException):
pass
if 'test' in sys.argv:
import socket
class PatchedSocket(socket.socket):