Skip to content

Instantly share code, notes, and snippets.

View majgis's full-sized avatar

Michael Jackson majgis

View GitHub Profile
@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 / ToolValidator.py
Created April 26, 2012 23:42
ArcGIS ToolValidator - Import package or module relative to .tbx file
""" ArcGIS 10.0 ToolValidator - Import package or module relative to .tbx file
Rules for packages:
1. You can't import a subpackage (__init__.py) directly for example:
# This works:
import myPackage
myPackage.mySubPackage.doSomething()
# This doesn't work
@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:
@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 / 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 / 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 / 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 / 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 / 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: