Skip to content

Instantly share code, notes, and snippets.

@jterrace
jterrace / gist:1823320
Created February 14, 2012 03:42
Automatically log in user after django-registration activation
from registration.signals import user_activated
from django.contrib.auth import login, authenticate
def login_on_activation(sender, user, request, **kwargs):
"""Logs in the user after activation"""
user.backend = 'django.contrib.auth.backends.ModelBackend'
login(request, user)
# Registers the function with the django-registration user_activated signal
user_activated.connect(login_on_activation)
@jterrace
jterrace / remove_dup_tris.py
Created November 3, 2011 19:27
Removing duplicate triangles with numpy
import numpy as np
def remove_duplicates(array_data, return_index=False, return_inverse=False):
"""Removes duplicate rows of a multi-dimensional array. Returns the
array with the duplicates removed. If return_index is True, also
returns the indices of array_data that result in the unique array.
If return_inverse is True, also returns the indices of the unique
array that can be used to reconstruct array_data."""
unique_array_data, index_map, inverse_map = np.unique(
array_data.view([('', array_data.dtype)] * \
@jterrace
jterrace / aggregate_mesh.py
Created November 1, 2011 18:47
Aggregate vertex data with pycollada
import collada
import sys
import os.path
from itertools import chain
import numpy
def uniqify_multidim_indexes(sourcedata, indices, return_map=False):
"""Just like numpy.unique except that it works with multi-dimensional arrays.
Given a source array and indexes into the source array, will keep only unique
indices into the source array, rewriting the indices to point to the new
@jterrace
jterrace / python IndexedList
Created March 30, 2011 19:34
Class that combines a list and a dict into a single class given a list of properties of the objects in the container
class IndexedList(list):
"""
Class that combines a list and a dict into a single class
- Written by Hugh Bothwell (http://stackoverflow.com/users/33258/hugh-bothwell)
- Original source available at:
http://stackoverflow.com/questions/5332841/python-list-dict-property-best-practice/5334686#5334686
- Modifications by Jeff Terrace
Given an object, obj, that has a property x, this allows you to create an IndexedList like so:
L = IndexedList([], ('x'))
o = obj()