This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class QuerySetManager(models.Manager): | |
"""Add new QuerySet methods using a Model inner class | |
Reference: | |
http://djangosnippets.org/snippets/734/ | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class C(object): | |
def __init__(self, x=None): | |
self.x = x | |
@property | |
def x(self): | |
return self._x | |
@x.setter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |