Skip to content

Instantly share code, notes, and snippets.

View turtlemonvh's full-sized avatar

Timothy turtlemonvh

View GitHub Profile
@turtlemonvh
turtlemonvh / tasks.py
Created August 8, 2014 21:14
Dynamically add celery tasks
## THIS IS UNTESTED
from worker.models import TaskType
from website.celery import app
import importlib
# Dynamically add registered tasks
# Celery._tasks is a task registry object
# https://github.com/celery/celery/blob/34c43244b1681a59540936748800aaa504786a35/celery/app/base.py#L162 - _tasks
@turtlemonvh
turtlemonvh / README.md
Created October 7, 2014 13:00
Django + mongoengine: Isolated test database

Isolated Test database for Django

This gist contains a few snippets that make creating an isolated test database in Django simple when using mongoengine.

The database identified by settings.MY_TEST_DB_NAME will be deleted every time a test is run.

If your test class needs additional tearDown functionality, make sure to call the super(MyClassName, self).tearDown() method within your overridden version of tearDown to handle deleting the database when you are finished.

@turtlemonvh
turtlemonvh / go_get_stash.md
Last active January 22, 2016 17:54
Go Get from Stash

Go Get from Stash

Add this function to your ~/.bashrc.

Use like

# Just use the url that stash recommends for cloning over ssh
go-get-stash ssh://git@stash.in.ionicsecurity.com:7999/il/common.git

You can use this package in your go files like

@turtlemonvh
turtlemonvh / tornado_all.md
Last active August 31, 2015 15:21
Tornado All

Usage

Call the All class with a list of futures. Returns a future that can be iterated over, yielding each future item in the order it was added.

Inspired by jquery.When.

@turtlemonvh
turtlemonvh / install_duc_osx.sh
Last active November 4, 2016 12:08
Installing duc on OSX
#!/usr/bin/bash
# DUC: http://duc.zevv.nl/
TKC_VERSION=1.4.48
DUC_VERSION=1.4.0
GLFW_VERSION=3.1.2
if ! which brew ; then
echo "ERROR: brew is required"
@turtlemonvh
turtlemonvh / show_props.md
Last active January 22, 2016 17:53
Python list object properties

A function to get the values of all non-private properties of an object

showprops = lambda p: [(prop, getattr(p, prop)) for prop in [lprop for lprop in dir(p) if not lprop.startswith("_")]]

Example usage from an ipython shell

# Parse a complicated uri
In [1]: from urlparse import urlparse
In [2]: uri = "postgresql+psycopg2://user:password@postgres:5432/default"

In [3]: p = urlparse(uri)

@turtlemonvh
turtlemonvh / sequence_alignment.md
Last active January 22, 2016 17:52
Sequence alignment

Sequence alignment

While watching some videos from Udacity describing sequence alignment I was having a bit of a hard time understanding the algorithm as it was explained, so I decided to write it in code.

This is a fairly simple approach and uses recursion (so stack depth may be a problem for larger sequences), but it does use dynamic programming to keep things relatively fast. The code uses the same N, NW, W notation the video uses to describe the problem graph.

If there are branches in the problem graph (i.e. 2 alignments are have the same score) the program just takes the first solution.

Sequence alignment video series (part of the "Computability Complexity and Algorithms" class on Udacity): https://www.youtube.com/watch?v=3NLYH44F6SU&index=5&list=PLAwxTw4SYaPkbWSEj_1iO7rILlWDJImW4

@turtlemonvh
turtlemonvh / find_replace.py
Last active February 10, 2019 19:19
A python replacement for a basic usage of sed: find and replace pattern
#!/usr/bin/env python
# From: https://gist.github.com/turtlemonvh/0743a1c63d1d27df3f17
from __future__ import print_function
import argparse
import fileinput
import re
import sys
@turtlemonvh
turtlemonvh / Gin.WrapHH.go
Last active June 27, 2019 08:02
A pattern for wrapping functions that accept an http handler in gin
// A wrapper that turns a http.ResponseWriter into a gin.ResponseWriter, given an existing gin.ResponseWriter
// Needed if the middleware you are using modifies the writer it passes downstream
// FIXME: Wrap more methods: https://golang.org/pkg/net/http/#ResponseWriter
type WrappedResponseWriter struct {
gin.ResponseWriter
writer http.ResponseWriter
}
func (w *WrappedResponseWriter) Write(data []byte) (int, error) {
return w.writer.Write(data)
@turtlemonvh
turtlemonvh / README.md
Created March 4, 2016 20:10
Example of shifting the time components of a bson object id from mgo

Go retime objectid experiment

The mgo library has a function for generating an id with a timestamp (https://godoc.org/labix.org/v2/mgo/bson#NewObjectIdWithTime), but all the non-time related fields are zeroed out. This is useful for time range comparisons, but kind of annoying if you want to generate a new ObjectId for a particular time range that may be in the past or future.

This example shows how to take an existing unique ObjectId and adjust its origin time.

Example output