Skip to content

Instantly share code, notes, and snippets.

View DylanModesitt's full-sized avatar

Dylan Modesitt DylanModesitt

View GitHub Profile
@DylanModesitt
DylanModesitt / converter.py
Last active January 11, 2018 17:52
Zinnia blog (djang) to Ghost blog (node) Article Conversion
import json
import datetime, time
import re
# date lambdas
epoch = datetime.datetime.utcfromtimestamp(0)
unix_time_millis = lambda dt: int((dt - epoch).total_seconds() * 1000.0)
zulu_time = lambda s: unix_time_millis(datetime.datetime(*map(int, re.split('[^\d]', s)[:-1])))
# zinnia data exported from the save feature
@DylanModesitt
DylanModesitt / AccepterBlockingQueue.java
Created December 22, 2017 02:45
A Java Blocking Queue that allows the consumer to block the producer until the item is consumed
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
/**
* A BlockinqQueue in which consumers may block producers (temporarily)
* to prevent them from producing more items to consume. To allow a producer
* to continue operation, an element taken from this queue must be accepted,
* by calling {@link AccepterBlockingQueue#accept(ThreadUniqueObject)}.
@DylanModesitt
DylanModesitt / randomColor.js
Last active January 4, 2018 08:24
Generate an aesthetically pleasing random color
// generate a bright (v = 0.98), mediumly saturated (s = 0.5) random color
var h=Math.random(),s=0.5,v=0.98,
golden_ratio_conjugate=0.6180339887,
f,p,q,t,r,g,b;
// subsequent calls to rand() follow a more uniform distribution with fibonacci hashing
// see http://cse.iitkgp.ac.in/~pb/algo1-pb-101111.pdf
h += golden_ratio_conjugate;
@DylanModesitt
DylanModesitt / color.swift
Created January 9, 2018 00:15
Swift Color Extensions
extension UIColor {
/**
Initialize a new UIColor from a hex value
- parameter hex: The hex integer (0x______) representation of the color
*/
convenience init(hex: Int) {
let components = (
R: CGFloat((hex >> 16) & 0xff) / 255,
@DylanModesitt
DylanModesitt / standardize.py
Last active January 11, 2018 17:51
Scale the data to have no mean and variance 1, then split that data into training and validation without implicit bias
from sklearn.preprocessing import StandardScaler
def standardize_data(data, training_validation_split=0.2):
"""
standardize features by removing the mean and scaling to unit variance
:param data: the data desired to be scaled
:param training_validation_split: The percentage of data to save for validation
:return: the standardized features and scalar in the form (training, validation, scalar)
"""
@DylanModesitt
DylanModesitt / layers.py
Created March 8, 2018 14:28
Attentive LSTM keras
from keras import backend as K
from keras import regularizers, constraints, initializers, activations
from keras.layers.recurrent import RNN, Layer, _generate_dropout_mask, _generate_dropout_ones
from keras.engine import InputSpec
from keras.legacy import interfaces
import warnings
# Copied from original keras source
def _time_distributed_dense(x, w, b=None, dropout=None,
@DylanModesitt
DylanModesitt / gentleimport.py
Created April 16, 2020 18:06
Gently import a python module, failing only at runtime if a downstream module object invokes __call__
"""
gentleimport is a context manager that patches
__import__ to allow for softly importing mocked modules. To use gentleimport,
with gentleimport():
import typing
import asdf
typing.Any # this is a real object as typing was a found module
asdf # this is a mock module as asdf was not a real module