Skip to content

Instantly share code, notes, and snippets.

@mattdeboard
mattdeboard / gist:1921384
Created February 27, 2012 04:22
How to macro?
(def connectors {:or " OR "
:and " AND "
:not " NOT "})
(defn -| [coll]
(paren-wrap
(apply str (interpose (:or connectors)
(map build-querystring coll)))))
(defn -& [coll]
(defn merge-queries [coll sep]
(paren-wrap (apply str (interpose sep (map build-querystring coll)))))
(defn -| [coll] (merge-queries coll (:or connectors)))
(defn -& [coll] (merge-queries coll (:and connectors)))
@mattdeboard
mattdeboard / slices.py
Created February 29, 2012 14:29
Recursive generation of slice start/end points for a collection for convenient iteration
def take(n, seq):
"Return first n items of the seq as a list"
return list(islice(seq, n))
def drop(n, seq):
"Return seq with the first n items removed."
return list(islice(seq, n, None, 1))
def split_by(n, seq):
return [take(n, seq), drop(n, seq)]
@mattdeboard
mattdeboard / gist:1943342
Created February 29, 2012 18:29
Differential delete from Solr to catch stale docs
(ns solr-disj.core
(:require [clojure-solr :as csolr]
[clojure.set]
[clojure.string])
(:use [solr-disj.secret :only [solr-server]]
[solr-disj.db]
[icarus.core :only [-?>]]
[korma.core]
[korma.db]
[clojure.data.xml]))
import time
from datetime import datetime
from lxml import etree
def time_handler(s):
t = time.strptime(s, "%m/%d/%Y %I:%M:%S %p")
return time.strftime("%Y-%m-%dT%H:%M:%S%Z", t)
def feed_parser(tree):
@mattdeboard
mattdeboard / kmp.py
Created March 17, 2012 00:04
Knuth-Morris-Pratt algorithm
def kmp_match(seq, subseq):
"""
Implementation of the Knuth-Morris-Pratt (KMP) algorithm in Python.
This algorithm finds valid shifts to locate subsequence `subseq` in
sequence `seq`.
"""
n = len(seq)
m = len(subseq)
@mattdeboard
mattdeboard / runner.py
Created March 30, 2012 15:05
Django test runner
from django.conf import settings
from django.test import TestCase
from django.test.simple import DjangoTestSuiteRunner, reorder_suite
from django.utils.importlib import import_module
from django.utils.unittest.loader import defaultTestLoader
class DiscoveryDjangoTestSuiteRunner(DjangoTestSuiteRunner):
"""A test suite runner that uses unittest2 test discovery."""
def build_suite(self, test_labels, extra_tests=None, **kwargs):
@mattdeboard
mattdeboard / test_settings.py
Created March 30, 2012 15:06
Django test-specific constants
BASE_PATH = os.path.dirname(__file__)
TEST_DISCOVERY_ROOT = os.path.join(BASE_PATH, "tests")
TEST_RUNNER = "tests.runner.DiscoveryDjangoTestSuiteRunner"
SOUTH_TESTS_MIGRATE = False
SKIP_SOUTH_TESTS = True
#!/home/matt/Envs/dseo/bin/python
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write(
"""
Error: Can't find the file 'settings.py' in the directory containing %r.
It appears you've customized things.
def take(n, seq):
"Return first n items of the seq as a list"
return list(islice(seq, n))
def drop(n, seq):
"Return seq with the first n items removed."
return list(islice(seq, n, None, 1))
def split_by(n, seq):
return [take(n, seq), drop(n, seq)]