Skip to content

Instantly share code, notes, and snippets.

Neal Todd nealtodd

Block or report user

Report or block nealtodd

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@nealtodd
nealtodd / decorator.py
Created Apr 25, 2012
Python profiling decorator
View decorator.py
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 Nov 20, 2018
Django helper for showing related objects that would be cascade deleted on deletion of an object.
View deletable_objects_snippet.py
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 Dec 17, 2015
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…
View wiible.py
import time
import cwiid
class Wiible():
buttons = [
"BTN_2",
"BTN_1",
"BTN_B",
@nealtodd
nealtodd / wiimote.py
Last active Dec 17, 2015
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.
View wiimote.py
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 Dec 17, 2015
Subclass of WiimoteConnect for controlling a real Pi-driven Lego-based forklift truck.
View wiibot.py
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 Jul 2, 2013
Django formset for validating a minimum number of filled out forms.
View minimumrequiredformset.py
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 Dec 23, 2015
Django management command: Take a fully qualified URL and show the resolved view (accounting for decorators on the view).
View u2v.py
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
View gist:6883345

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 Aug 29, 2015 — forked from mariuz/emperor.sh
uwsgi init.d for Wagtail on Debian
View uwsgi-init.d
#!/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 Oct 26, 2016
Skip migrations for a Django 1.7 test run
View settings_test_snippet.py
# 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]))
You can’t perform that action at this time.