Skip to content

Instantly share code, notes, and snippets.

@nealtodd
nealtodd / decorator.py
Created April 25, 2012 13:15
Python profiling decorator
from cProfile import Profile
import pstats
def profile(sort_args=['cumulative'], print_args=[10]):
profiler = Profile()
def decorator(fn):
def inner(*args, **kwargs):
result = None
@nealtodd
nealtodd / deletable_objects_snippet.py
Last active August 10, 2020 06:49
Django helper for showing related objects that would be cascade deleted on deletion of an object.
from django.conf import settings
from django.db.models.deletion import Collector
from django.db.utils import ConnectionRouter
def deletable_objects(obj):
"""
Return a generator that yields (model, instance) tuples
of instances related to obj (including obj itself).
Essentially, programmatic access to the data Django Admin
@nealtodd
nealtodd / wiible.py
Last active December 17, 2015 09:29
Test of connecting Wiimotes to a Raspberry Pi via Bluetooth. Reports button presses from a Wiimote. Couched in a trivial little game for two kids to 'remote control' each other: The sender can use the crosspad to light the leds on the receiver. The receiver walks in the direction indicated on the leds (led1 = turn left, led2 = forwards, led3=bac…
import time
import cwiid
class Wiible():
buttons = [
"BTN_2",
"BTN_1",
"BTN_B",
@nealtodd
nealtodd / wiimote.py
Last active December 17, 2015 19:19
Base class for detecting Wiimote button presses and triggering actions via methods. Written so that a subclass can be used to control a Raspberry Pi based robot.
import time
import cwiid
import threading
class WiimoteBase(object):
"""
Base class for sending Wiimote button presses to class
methods. Subclass to implement actions in the methods.
@nealtodd
nealtodd / wiibot.py
Last active December 17, 2015 23:49
Subclass of WiimoteConnect for controlling a real Pi-driven Lego-based forklift truck.
import piface.pfio as pfio
from wiimote import WiimoteConnect
class WiiBot(WiimoteConnect):
"""
Use Wiimote to control robot
Basic usage:
from wiibot import WiiBot
@nealtodd
nealtodd / minimumrequiredformset.py
Created July 2, 2013 14:29
Django formset for validating a minimum number of filled out forms.
from django import forms
class MinimumRequiredFormSet(forms.models.BaseInlineFormSet):
"""
Inline formset that enforces a minimum number of non-deleted forms
that are not empty
"""
default_minimum_forms_message = "At least %s set%s of data is required"
@nealtodd
nealtodd / u2v.py
Last active December 23, 2015 12:39
Django management command: Take a fully qualified URL and show the resolved view (accounting for decorators on the view).
from textwrap import dedent
from urlparse import urlparse
from django.core.management.base import BaseCommand
from django.core.urlresolvers import resolve, Resolver404
class Command(BaseCommand):
"""
Take a fully qualified URL and show the resolved view

Sublime Text 2 – Useful Shortcuts (PC)

Loosely ordered with the commands I use most towards the top. Sublime also offer full documentation.

Editing

Ctrl+C copy current line (if no selection)
Ctrl+X cut current line (if no selection)
Ctrl+⇧+K delete line
Ctrl+↩ insert line after
@nealtodd
nealtodd / uwsgi-init.d
Last active August 29, 2015 13:57 — forked from mariuz/emperor.sh
uwsgi init.d for Wagtail on Debian
#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides: emperor
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: uwsgi for wagtail
# Description: uwsgi for wagtail
@nealtodd
nealtodd / settings_test_snippet.py
Last active November 14, 2019 01:25
Skip migrations for a Django 1.7 test run
# If your test settings file doesn't import any other settings file
# then you can use the function directly:
def prevent_tests_migrate(db):
import django
from django.db import connections
from django.db.migrations.executor import MigrationExecutor
django.setup()
ma = MigrationExecutor(connections[db]).loader.migrated_apps
return dict(zip(ma, ['{a}.notmigrations'.format(a=a) for a in ma]))