Skip to content

Instantly share code, notes, and snippets.

View majgis's full-sized avatar

Michael Jackson majgis

View GitHub Profile
@majgis
majgis / frdelta.py
Last active December 10, 2015 19:29
Packaged relative deltas for convenience
"""Floored Relative Delta(frdelta)
This module contains ready-made relativedeltas. The kwargs class contains
all the key word arguments used to create the relativedeltas
All relativedeltas are floored, ie. all lesser absolute information is set
to zero.
If you need more than one forward(FWD) or backward(BWD), use multiplication:
This:
@majgis
majgis / LICENSE
Last active December 10, 2015 19:29
BSD 2-Clause license for Michael A. Jackson which applies to all gists.
Copyright (c) 2012, Michael A. Jackson
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
@majgis
majgis / expire_date.py
Created January 9, 2013 01:55
construct a relativedelta from string argument
expire_deltas = {
'minutes': frdelta.FWD_MIN,
'hours' : frdelta.FWD_HR,
'days': frdelta.FWD_DAY,
'months': frdelta.FWD_MO,
'years': frdelta.FWD_YR,
}
def GetExpireDate(expire_date='months', now=None):
"""Convert expire_date arguments to date
@majgis
majgis / safe.py
Last active December 10, 2015 20:49
Safely access mutable attributes on class based configurations.
""" Inherit SafeReadOnly for safe access to mutable objects on classes
* Deep copy of mutable attributes on accessing uninstantiated class
* No assignment to class attributes on uninstantiated class
* Deep copy of mutable attributes when class is instantiated
"""
from collections import Hashable
@majgis
majgis / managers.py
Created January 16, 2013 16:26
Add new QuerySet methods using a Model inner class
from django.db import models
class QuerySetManager(models.Manager):
"""Add new QuerySet methods using a Model inner class
Reference:
http://djangosnippets.org/snippets/734/
"""
@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
@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,
const storedTemplate = (a,b,c) => `a:${a}, b:${b}, c:${c}`
storedTemplate(1,2,3)
// var depPairs = [
// "KittenService: ",
// "Leetmeme: Cyberportal",
// "Cyberportal: Ice",
// "CamelCaser: KittenService",
// "Fraudstream: Leetmeme",
// "Ice: "
// ];
//
// Turn that into: `'KittenService, Ice, Cyberportal, Leetmeme, CamelCaser, Fraudstream'`
@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