Skip to content

Instantly share code, notes, and snippets.

View masci's full-sized avatar

Massimiliano Pippi masci

View GitHub Profile
@masci
masci / boundingbox.py
Created September 10, 2012 13:46
Bounding Box
Point = namedtuple('Point', 'x, y, z')
class BoundingBox(object):
def __init__(self,bottom,top):
self.top = Point._make(top)
self.bot = Point._make(bottom)
def _point_contained(self, other_point):
over_bot = all(map(lambda x: x[0]<=x[1], zip(self.bot,other_point)))
under_top = all(map(lambda x: x[0]>=x[1], zip(self.top,other_point)))
return over_bot and under_top
@masci
masci / gist:3143930
Created July 19, 2012 13:33
Django JSONField
# -*- coding: utf-8 -*-
from django.db import models
from django.core.serializers.json import DjangoJSONEncoder
from django.utils import simplejson as json
class JSONField(models.TextField):
"""
JSONField is a generic textfield that neatly serializes/unserializes
JSON objects seamlessly.
Django snippet #1478
@masci
masci / step_1
Created May 16, 2012 21:53
Pygame pong
# Per funzionare il programma ha bisogno della libreria pygame
#
# Le istruzioni per installare pygame sono a questo indirizzo:
# http://pygame.org/download.shtml
#
import pygame, random
pygame.init()
random.seed()
screen = pygame.display.set_mode((400, 400))
@masci
masci / mixinutils.py
Created February 22, 2011 17:01
utility functions for mix/unmix classes
def mixIn (base, addition):
"""Mixes in place, i.e. the base class is modified.
Tags the class with a list of names of mixed members.
"""
assert not hasattr(base, '_mixed_')
mixed = []
for item, val in addition.__dict__.items():
if not hasattr(base, item):
setattr(base, item, val)
@masci
masci / windowmeta.py
Created February 22, 2011 16:59
Metaclass for automating uic operation when using the "Multiple Inheritance Approach" in Qt Widgets
from PyQt4 import uic
from PyQt4.QtCore import pyqtWrapperType
UI_DIR='path_to_ui_files'
class WindowMeta (pyqtWrapperType, type):
"""This is the metaclass of all Qt-Windows. It automatically
sets the correct base classes, so they do not have to be set
by the programmer.
@attention: The class has to have the same name as the .ui-file.