Skip to content

Instantly share code, notes, and snippets.

Andy MacKinlay admackin

Block or report user

Report or block admackin

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@admackin
admackin / DateConversion.java
Created Mar 23, 2015
Java: Convert US customary week/date number into JodaTime DateTime object
View DateConversion.java
package net.akmy.utils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.IllegalFieldValueException;
/*
Copyright 2014 Andrew MacKinlay
Licensed under the Apache License, Version 2.0 (the "License");
@admackin
admackin / replace-emoji.py
Created Aug 21, 2014
Remove emoji characters from a file
View replace-emoji.py
#!/usr/bin/env python
from codecs import open
from contextlib import nested
import re
import sys
from os import path
# The regex ranges below may not work on narrow builds of python < 3.3
# see http://stackoverflow.com/a/25417872/1711188
@admackin
admackin / run-class.sh
Created Aug 14, 2014
Execute Java class in Maven Project using exec-maven-plugin
View run-class.sh
#!/bin/sh
# Don't forget to add the plugin to your POM - eg:
# <plugin>
# <groupId>org.codehaus.mojo</groupId>
# <artifactId>exec-maven-plugin</artifactId>
# <version>1.3.2</version>
# </plugin>
MAINCLASS=$1
@admackin
admackin / multiproc-keyboard-interrupt.py
Last active Jan 15, 2017
Shows how to fork a pool of Python processes using multiprocessing, and sensibly handle keyboard interrupts (terminate gracefully on completion, but kill all on SIGINT)
View multiproc-keyboard-interrupt.py
import multiprocessing
import time
import signal
import sys
# based on http://stackoverflow.com/a/6191991/1711188
# but instead of calling Pool.join(), we just close and manually poll for processes exiting
# also it assumes we have a finite number of jobs we want to run; if they complete
# it terminates in the normal way
@admackin
admackin / ConvertTrav.scala
Last active Dec 18, 2015
Implicit conversion for Traversable instances where the elements are convertible. Scala implicit conversions are handy (if controversial). But when methods return pre-constructed collections of a type, the implicit conversion won't work - see http://stackoverflow.com/questions/8935811/scala-implicit-conversion-of-a-generic-argument?rq=1 This so…
View ConvertTrav.scala
/** Implicit conversion for Traversable instances where the elements are convertible - possibly controversial */
implicit def convTrav[S, T, I[S] <: Traversable[S]](input: I[S])(implicit c: S => T): I[T] =
(input map c).asInstanceOf[I[T]]
/** Less controversial version of the above using the pimp-my-library pattern
- to use this, you must explicitly call .as[TypeName] on the source Traversable */
trait Convertible[M[A], A] {
def as[B](implicit f: A => B): M[B]
}
@admackin
admackin / pretty-print-ptb.py
Last active Dec 11, 2015
Pretty-print PTB trees from a file
View pretty-print-ptb.py
#!/usr/bin/env python
import sys
from contextlib import nested
from nltk import tree
def pretty_print(filenames):
for f in filenames:
with nested(open(f), open(f + '.pretty', 'w')) as (inf, outf):
for line in inf:
@admackin
admackin / .bashrc
Last active May 10, 2018
Sane SSH_AUTH_SOCK handling for Screen and Tmux, so that new SSH agents created by subsequent logons are still usable.
View .bashrc
_ssh_auth_save() {
ln -sf "$SSH_AUTH_SOCK" "$HOME/.ssh/ssh-auth-sock.$HOSTNAME"
}
alias screen='_ssh_auth_save ; export HOSTNAME=$(hostname) ; screen'
alias tmux='_ssh_auth_save ; export HOSTNAME=$(hostname) ; tmux'
@admackin
admackin / tmux-build.sh
Created Oct 15, 2012
Build tmux (iTerm2 version) as a local single-user installation on Ubuntu 10.04
View tmux-build.sh
#!/bin/sh
mkdir tmp
cd tmp
wget https://github.com/downloads/libevent/libevent/libevent-2.0.20-stable.tar.gz | tar xzf -
cd libevent-2.0.20-stable
./configure --prefix=$HOME/local && make && make install
cd ..
@admackin
admackin / rangefilter.py
Created Jul 23, 2012
Python range filter - allows quick retrieval of keys within a given range
View rangefilter.py
from blist import sorteddict, blist
class RangeFilter(sorteddict):
def filter_items(self, lt=None, lte=None, gt=None, gte=None):
"""Return the items with keys corresponding to the supplied parameters
('lt' denotes 'less than', 'gte' denotes 'greater than or equal' etc)
"""
bottom, top = self._get_range_indexes(lt, lte, gt, gte)
return self.items()[bottom:top]
@admackin
admackin / time.py
Created Apr 26, 2012
TimeDeltaCustom - adds functionality to Python timedelta objects
View time.py
from datetime import timedelta
class TimeDeltaCustom(timedelta):
"""A variant of timedelta with some neat extra features.
In particular, it supports division of TimeDeltaCustom by TimeDeltaCustom,
float multiplication, and getting the total number of seconds or hours.
"""
@classmethod
You can’t perform that action at this time.