Skip to content

Instantly share code, notes, and snippets.

View majgis's full-sized avatar

Michael Jackson majgis

View GitHub Profile
@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 / 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
@majgis
majgis / class.js
Last active October 11, 2015 06:27
Javascript instantiable class experiments.
// Modified excerpt from:
// https://developer.mozilla.org/en-US/docs/JavaScript/Introduction_to_Object-Oriented_JavaScript
// define the Person Class
function Person(name) {
//Public property
this.name = name;
//Private property
@majgis
majgis / django_snippets.py
Created September 18, 2012 15:43
django snippets
#Compact example to get the name of the primary key field of a Django model.
loc_fields = self._model._meta.fields
loc_pk = next((f.db_column or f.name for f in loc_fields if f.primary_key))
#Get all related model classes for a model class or instance
related_models = [f.rel.to for f in loc_record._meta.fields if f.rel]
@majgis
majgis / dictutils.py
Created September 5, 2012 09:03
DictWrap: Create or access massive nested dictionaries with ease.
class DictWrap(object):
""" Wrap an existing dict, or create a new one, and access with either dot
notation or key lookup.
The attribute _data is reserved and stores the underlying dictionary.
When using the += operator with create=True, the empty nested dict is
replaced with the operand, effectively creating a default dictionary
of mixed types.
args: