Skip to content

Instantly share code, notes, and snippets.

View josefdlange's full-sized avatar
🌱

Joey Lange josefdlange

🌱
View GitHub Profile
// This is _definitely_ my own implementation of rangeCheck and no one else's. Definitely not infringing on anything.
private static void rangeCheck(int arrayLen, int fromIndex, int toIndex) {
if (fromIndex > toIndex)
throw new IllegalArgumentException("fromIndex(" + fromIndex +") > toIndex(" + toIndex+")");
if (fromIndex < 0)
throw new ArrayIndexOutOfBoundsException(fromIndex);
if (toIndex > arrayLen)
throw new ArrayIndexOutOfBoundsException(toIndex);
}
@josefdlange
josefdlange / use_editor.py
Created May 17, 2016 15:50
Edit Python variable in $EDITOR, useful for modification of large data structure files in the middle of a CLI.
import os, sys, tempfile
def use_editor(text):
"""
Write out text to a temp file, instruct the system to open it in an editor,
and when control is returned to us, re-open that temp file and return its contents.
"""
tmp = tempfile.NamedTemporaryFile(delete=False)
tmp.write(text)
tmp.seek(0)
@josefdlange
josefdlange / hook.rb
Created May 12, 2016 18:11 — forked from asimihsan/hook.rb
Hook for letsencrypt.sh to do DNS challenges
#!/usr/bin/env ruby
require 'aws-sdk'
require 'pry'
require 'awesome_print'
# ------------------------------------------------------------------------------
# Credentials
# ------------------------------------------------------------------------------
# pick up AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY by default from
# This is a pretty lazy way to do mosh tunnelling, but it works.
mosh user@host.that.is.public.com -- mosh user2@host.that.is.private.com
# You can also do the internal session as SSH, since that connection is probably pretty solid and not subject to broken pipes.
mosh user@host.that.is.public.com -- ssh user2@host.that.is.private.com
@josefdlange
josefdlange / tests.sh
Last active December 10, 2015 23:41
Show an OS X Notification when tests are complete
#!/bin/bash
# Run your tests and throw the return code into a variable.
RESULT=$(python -m tests.__init__ $1)
# Only if osascript exists (AKA only on OS X)
if type "osascript" > /dev/null; then
SUCCESS="Successful"
MESSAGE="All Tests Complete"
@josefdlange
josefdlange / custom_serializer_class.py
Created November 30, 2015 15:54
Implementing custom JSON logic for the Python json module.
import json
from json import JSONEncoder
class MyComplexObject(object):
"""
A trivial Python object that has its own special serialized output
that for some reason would differ from __repr__.
"""
def __init__(self, arbitrary_value, optional_value=None):
set-option -g default-command "reattach-to-user-namespace -l zsh"
set -g mouse on
set -g default-terminal "screen-256color"
set-option -g allow-rename off
set-option -g renumber-windows on
source ~/.tmuxline.sh
@josefdlange
josefdlange / .tmux.conf.old
Created November 18, 2015 16:08
old tmux configuration
set-option -g default-command "reattach-to-user-namespace -l zsh"
set -g mode-mouse on
set -g mouse-resize-pane on
set -g mouse-select-pane on
set -g mouse-select-window on
set -g default-terminal "screen-256color"
set-option -g allow-rename off
@josefdlange
josefdlange / killer_logging.py
Last active October 10, 2015 00:19
Short, sweet, to-the-point logging in a Python application.
import logging
import logging.handlers
"""
Call me once at initialization of your application.
@param level A level from the logging package. You can also
pass in common levels as strings, I believe ('INFO', 'DEBUG', etc)
"""
def setup_logging(level):
@josefdlange
josefdlange / UIImage+Resizing.m
Created August 28, 2015 20:40
Resizing a UIImage, leveraging UIViewContentMode
- (UIImage *)resizedWithNewSize:(CGSize)size contentMode:(UIViewContentMode)contentMode {
CGFloat x = 0.0f;
CGFloat y = 0.0f;
CGFloat w = size.width;
CGFloat h = size.height;
BOOL scale = YES;
switch (contentMode) {
case UIViewContentModeScaleToFill: {
break;
}