Skip to content

Instantly share code, notes, and snippets.

@adambard
adambard / development.py
Created December 27, 2010 01:53
Sample django development.py settings.
from base import *
DEBUG = True
TEMPLATE_DEBUG = DEBUG
PROJECT_DIR = "/path/to/project/directory/"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
"""
A simpler pivot table for non-python people.
Pythonisms present:
1. You can iterate over a list of tuples using `for ... in` and map each tuple to a variable.
2. Dicts exist and are great.
3. Iterating over a dict returns the key.
4. Attempting to access a variable that doesn't exist in a dict raises a KeyError.
@adambard
adambard / gist:1190312
Created September 3, 2011 00:53
DDBot: Take Screenshot
def take_screenshot():
"""
Use GTK to get a screengrab of the current screen.
Returns an (x, y, 3) full-color pixel array
"""
w = gtk.gdk.get_default_root_window()
sz = w.get_size()
pb = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,False,8,sz[0],sz[1])
class NotDiamondDashException(Exception):
pass
def crop_dd_screenshot(pixarray):
"""
If there is no stored index, load the reference .png,
find it in the current screen, and get the coordinates.
If the reference is not found, raise a
NotDiamondDashException so we know we're not looking
# Color of each block at 10, 10
COLORS = (
(158, 221, 255), # Diamond
(247, 183, 0), #Yellow
(1, 185, 1), # Green
(186, 115, 255), # Purple
(242, 0, 16), # Red
(6, 104, 253)) # Blue
def downsample_pixarray(pixarray, factor=40.):
def find_largest_contiguous_region(countsarray):
"""
Find the contiguous regions in countsarray using the modified
flood count algorithm described in get_flood_count
"""
Q = countsarray.copy()
points_checked = []
rows, cols = Q.shape
@adambard
adambard / core.clj
Created March 20, 2012 05:06
Toy clojure factorial (for test-app)
(ns test-app.core)
(defn fact [x] (reduce * (range 1 (inc x))))
(fact 5)
@adambard
adambard / find-m-files.clj
Created March 27, 2012 16:53
Clojure script: find duplicate filenames in a nested directory structure
(ns find-m-files
(:import java.io.File))
; Wrap java.io.file methods
(defn is-file? [f] (.isFile f))
(defn is-dir? [f] (.isDirectory f))
(defn get-name [f] (.getName f))
(defn get-path [f] (.getPath f))
(defn get-m-files [d]
@adambard
adambard / java-examples.clj
Created March 27, 2012 17:23
Using Java from Clojure in plain !@#%^%ing language, with some simple goddamn examples.
; Short answer: use import to import a given class
; In java:
; import java.io.File;
(import java.io.File)
; Preferred: put it in your ns
(ns my-namespace
(:import java.io.File))
; Instantiate an object with an argument
@adambard
adambard / pegdown-test.clj
Created March 27, 2012 17:42
Quick pegdown demo
(ns pegdown-test
(:import org.pegdown.PegDownProcessor))
(defn markdown-to-html [md]
(let [pd (PegDownProcessor.)]
(.markdownToHtml pd md)))