Skip to content

Instantly share code, notes, and snippets.

Russell Cohen rcoh

View GitHub Profile
@rcoh
rcoh / safelythrowable.scala
Last active Jun 15, 2020
Safely Catch Throwable
View safelythrowable.scala
def safely[T](handler: PartialFunction[Throwable, T]): PartialFunction[Throwable, T] = {
case ex: ControlThrowable => throw ex
// case ex: OutOfMemoryError (Assorted other nasty exceptions you don't want to catch)
//If it's an exception they handle, pass it on
case ex: Throwable if handler.isDefinedAt(ex) => handler(ex)
// If they didn't handle it, rethrow. This line isn't necessary, just for clarity
case ex: Throwable => throw ex
}
@rcoh
rcoh / converter.py
Created Aug 6, 2016
Strava JSON to GPX converter
View converter.py
### pip install gpxpy
import sys
import gpxpy
import gpxpy.gpx
import json
import datetime
def convert(f):
gpx = gpxpy.gpx.GPX()
@rcoh
rcoh / otp.py
Last active May 23, 2019
An implementation of Google Authenticator Compatible 2-factor Codes
View otp.py
"""
An implementation of TOTP as described in https://tools.ietf.org/html/rfc6238#section-4 aka Google Authenticator Style 2-factor Auth
"""
import base64
import datetime
import hashlib
import hmac
import sys
import struct
import time
@rcoh
rcoh / scala-patterns.scala
Last active Sep 13, 2018
Scala patterns for compiler construction
View scala-patterns.scala
// Useful Scala Patterns for Compiler Design
// 1. Case Classes
// [For full details, see Programming in Scala 2ed, page 310]
// Case classes are syntactic sugar around normal Scala classes. They come prebaked with the following properties:
// * Factory constructor (don't need new) :
case class Foo(bar: String)
View javascript_assesment.md

Assesment 1: (~4-6h)

Tic-Tac-Toe: The goal is to implement a basic tic-tac-toe game. We will build it in the terminal using Node. The game will support 2 players, X and O, that make alternating moves. You're free to google anything you want! Develop this like you would work on any other project but please work on it indepdently. This project is tough! It's meant to be hard and to stretch your skills.

  1. Use ES6 classes to create a board class. The class should be able to store a 3x3 grid of tic-tac-toe pieces.

  2. Add a method to the class to print the board to the terminal (console.log) The format is up to you, but it should be clear to the user which pieces are where. Here's a possibility:

X|-|O
@rcoh
rcoh / sshupdate.py
Created Feb 3, 2013
Automatically update your SSH config to allow ssh-ing into EC2 instances by their name. Works on Linux and Mac OS X.
View sshupdate.py
import os
import subprocess
import boto.ec2
import itertools
AWS_KEY = os.getenv('AWS_ACCESS_KEY')
AWS_SECRET = os.getenv('AWS_SECRET_KEY')
# Tweak as necessary for your infrastructure
regions = ['us-east-1', 'us-west-1', 'eu-west-1']
@rcoh
rcoh / inkenablesimple.m
Created Aug 13, 2013
Ink Enable, nondynamic
View inkenablesimple.m
INKBlob *blob = [INKBlob blobFromData:content];
blob.uti = @"public.png"
[myView INKEnableWithBlob:blob];
@rcoh
rcoh / returntoacaller.m
Last active Dec 20, 2015
Return to calle
View returntoacaller.m
if ([INK appShouldReturn]) {
// 3 choices (choose 1)
// Return with data unchanged
[Ink return];
// Return with updated data
[INK returnBlob:newBlob];
// Return with error: (You must indicate to the user what went wrong before calling this)
@rcoh
rcoh / registeractions.m
Last active Dec 20, 2015
Registering actions for Ink
View registeractions.m
// Get the UUID for your action from the developer portal
INKAction *store = [INKAction actionWithUUID:@"store-asdfasd-sdfads"];
[Ink registerAction:store withTarget:self selector:@selector(storeBlob:action:error:)];
@rcoh
rcoh / enableview.m
Last active Dec 20, 2015
Enabling a View
View enableview.m
[self.view INKEnableWithUTI:myFile.uti dynamicBlob:^INKBlob *{
INKBlob *blob = [INKBlob blobFromData:[myFile getData]];
blob.uti = myFile.uti;
blob.filename = myFile.fileName;
return blob;
} returnBlock:^(INKBlob *blob, INKAction *action, NSError *error) {
if ([action.type isEqualToString: INKActionType_Return]) {
// Return save action. Save back the blob
} else {
// Return cancel action
You can’t perform that action at this time.