View countDistinct.js
function countDistinct(items) {
var result = [];
for (var i = 0, l = items.length; i < l; ++i) {
var item = items[i];
var index = -1;
for (var j = 0; j < result.length; ++j) {
var existing = result[j];
if (item === existing[0]) {
index = j;
break;
View regexpsub.js
function sub(pattern, string, repl) {
// Substitute all matches of pattern in string with the value returned by repl
// given a match and the corresponding group values,
// similar to Python's re.sub function.
// Note that pattern must be a "global" RegExp of the form /.../g
var found;
var lastIndex = 0;
var result = "";
while (found = pattern.exec(string)) {
var subst = repl.apply(this, found);
View textwidth.js
function cached(func) {
cache = {};
return function() {
var args = [];
for (var i = 0; i < arguments.length; ++i)
args.push(arguments[i]);
var key = $.param({args: args});
var value = cache[key];
if (typeof value != "undefined")
return value;
View bulk_insertion.py
class BulkInsertion(object):
"""
Bulk-insert several model instances using accumulated DB statements.
Usage:
>>> with BulkInsertion('app_tablename') as inserter:
... inserter.insert(field1=value1, field2=value2)
By default, a DB statement is issued every 1000 inserts.