Skip to content

Instantly share code, notes, and snippets.

View majgis's full-sized avatar

Michael Jackson majgis

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / initkwargs.py
Last active October 13, 2015 15:08
Pass kwargs defined on class to class initialization
def DefaultKwargs(**init_kwargs):
"""Decorator factory for passing default kwargs to the decorated function.
Returns:
decorator function
"""
def decorator(func):
"""Recieves a function, then wraps it with wrapper_func
@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 / 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 / property.py
Created October 17, 2012 01:35
Python get and set methods
class C(object):
def __init__(self, x=None):
self.x = x
@property
def x(self):
return self._x
@x.setter