Skip to content

Instantly share code, notes, and snippets.

View majgis's full-sized avatar

Michael Jackson majgis

View GitHub Profile
@majgis
majgis / .zshrc
Last active May 16, 2022 10:38
automatically call nvm use if .nvmrc is present
# Taken from here:
# https://github.com/creationix/nvm#zsh
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
@majgis
majgis / fake_request.py
Created November 28, 2012 21:08
Fake Django WSGIRequest Object
from django.core.handlers.wsgi import WSGIRequest
from StringIO import StringIO
from django.contrib.auth.models import AnonymousUser
def GetFakeRequest(path='/', user=None):
""" Construct a fake request(WSGIRequest) object"""
req = WSGIRequest({
'REQUEST_METHOD': 'GET',
'PATH_INFO': path,
'wsgi.input': StringIO()})
@majgis
majgis / encoders.py
Created December 4, 2012 04:07
JSON Encoder and Decoder for datetime and timedelta
# Taken from http://taketwoprogramming.blogspot.com/2009/06/subclassing-jsonencoder-and-jsondecoder.html
class DateTimeAwareJSONEncoder(JSONEncoder):
"""
Converts a python object, where datetime and timedelta objects are converted
into objects that can be decoded using the DateTimeAwareJSONDecoder.
"""
def default(self, obj):
if isinstance(obj, datetime):
return {
@majgis
majgis / while.js
Created February 21, 2013 17:09
Javascript while loop to deplete an array from either direction.
var x = [1,2,3]
//back to front
while(x.length){
var value = x.pop();
console.log(value);
}
//array is now empty, lets replace it
var x = [1,2,3]
@majgis
majgis / README.md
Created March 6, 2012 05:00
Redirect a Python Import to a Relative Directory

Summary

Redirect a Python import by using a dummy Python module that deletes its own reference and replaces it with relative module or package.

Purpose

Allows more control over a relative directory structure

Example

@majgis
majgis / .zshrc
Last active May 31, 2018 05:56
docker run helper function
# Execute docker run with --rm --network shared
# The shared network will be created if it dosn't already exist
dr () {
docker network inspect shared > /dev/null 2>&1
if [ $? -ne 0 ]
then
docker network create shared > /dev/null 2>&1
fi
local DOCKER_CMD="docker run --rm --network shared $@"
echo
// var depPairs = [
// "KittenService: ",
// "Leetmeme: Cyberportal",
// "Cyberportal: Ice",
// "CamelCaser: KittenService",
// "Fraudstream: Leetmeme",
// "Ice: "
// ];
//
// Turn that into: `'KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream'`
const storedTemplate = (a,b,c) => `a:${a}, b:${b}, c:${c}`
storedTemplate(1,2,3)
@majgis
majgis / collection.py
Last active December 17, 2015 01:38
A defaultnamedtuple which creates a namedtuple with default values when none are given.
""" Additional collections to augment Python's collections module.
"""
from collections import namedtuple
def defaultnamedtuple(
typename,
field_names,
verbose=False,
rename=False,
@majgis
majgis / interview.py
Last active December 16, 2015 19:28
Answer to an interview question.
""" Interview question
Write a method that takes a string, and returns the string in reverse.
For instance, if passed abc123 it should return 321cba
"""
class StringUtil(object):
""" Utility for strings