Skip to content

Instantly share code, notes, and snippets.

@NelsonMinar
NelsonMinar / bug-demo.py
Created January 20, 2015 01:51
Python bug with logging and multiprocessing with maxtasksperchild=1
'''Demo of a bug with multiprocessing.Pool, logging, and maxtasksperchild=1.
This program uses Pool.imap_unordered() to execute 200 tasks. Each worker
task writes a log message and sleeps a short time. The master process
uses a timeout on next() to log a status message occasionally.
When it works, 200 jobs are completed quickly. When it breaks, roughly
195 of 200 jobs will have completed and next() never raises StopIteration.
If everything logs to logging.getLogger() and maxtasksperchild=1, it
@NelsonMinar
NelsonMinar / mp.py
Last active December 5, 2023 15:26
multiprocessing Pool example
#!/usr/bin/env python2
"Demonstrate using multiprocessing.Pool()"
import multiprocessing, time, logging, os, random, signal, pprint, traceback
logging.basicConfig(level=logging.DEBUG)
_L = logging.getLogger()
class JobTimeoutException(Exception):
@NelsonMinar
NelsonMinar / thread-logging.py
Created January 14, 2015 01:24
Demonstration of logging messages from different threads to different files
#!/usr/bin/env python2
"""Demonstration of a Python logging handler that creates a separate output
file for each stream. The key thing here is the class MultiHandler. It picks
the file name based on the thread name; could use anything in threading.local()
"""
import threading, logging, time, random, os
# Global object we log to; the handler will work with any log message
@NelsonMinar
NelsonMinar / checkCsv.py
Created January 10, 2015 17:25
Quickie script to check an OpenAddresses source CSV file for a consistent number of rows
#!/usr/bin/env python3
"""Check a CSV source for consistent number of rows
Usage: checkCsv.py encoding filename"""
import sys
counts = {}
fp = open(sys.argv[2], encoding=sys.argv[1])
for l in fp:
@NelsonMinar
NelsonMinar / bulkinsert.js
Last active October 1, 2021 02:14
A demonstration of slow sqlite3 bulk inserts in node.js
// Demonstration that bulk insertsin Node.js sqlite3 using prepared statements is very slow.
// Usage: run with one command line argument, one of "db", "reuse", "finalize"
// Details: http://nelsonslog.wordpress.com/2014/11/16/node-js-sqlite3-very-slow-bulk-inserts/
var sqlite3 = require('sqlite3').verbose();
var start = Date.now();
var db = new sqlite3.Database('inserttest.sqlite');
var mode = process.argv[2], runs = "100";
db.serialize(function() {
@NelsonMinar
NelsonMinar / SugarAgent.m
Created September 17, 2014 00:56
SugarScape agent in Swarm, July 1997
// Sugarscape in Swarm. Copyright (C) 1997 Nelson Minar
// This program is distributed without any warranty; without even the
// implied warranty of merchantability or fitness for a particular purpose.
// See file LICENSE for details and terms of copying.
#import "SugarAgent.h"
#import <simtools.h>
#import "ModelSwarm.h"
@implementation SugarAgent

Gruber: Have you seen this thing where there's this group that wants to turn Markdown into an IETF standard?

Marco: No, w-- let me guess -- Jeff Atwood?

Gruber: I don't even know, you know what, it broke last weekend. I was out of town with Amy and wasn't paying attention, and I've been busy this week on other stuff. I haven't even paid attention to it. I don't know if it's associated with Atwood's crusade or not. And, there's talk from some people -- and the funny thing is, they're doing it on a mailing list that I still host and I haven't participated on in years. And I don't know why I haven't pulled the plug on the damn mailing list, but I still host the markdown-discuss mailing list, and there's people saying that they should just take the name Markdown from me because I've been such a lousy steward of it and whatever. Meanwhile, you know, there's the web pages on my site for Markdown describing the syntax and everything are more popular every single day. It's more popular -- I could actually -- I've t

@NelsonMinar
NelsonMinar / demo.html
Created May 18, 2014 18:37
Demo of HTML5, dropping a file and processing contents
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="utf-8">
<title>Simple drag and drop demo</title>
<style type="text/css">
html, body { height: 100%; }
body { margin: 0; font-family: Helvetica, Arial, sans; }
#help {
width: 100%; height: 100%;
@NelsonMinar
NelsonMinar / lastframe.sh
Created May 17, 2014 16:30
Extract last frame of a video file
#!/bin/bash
fn="$1"
of=${fn%.*}.jpg
lf=`ffprobe -show_streams "$fn" 2> /dev/null | awk -F= '/^nb_frames/ { print $2-1 }'`
rm -f "$of"
ffmpeg -i "$fn" -vf "select='eq(n,$lf)'" -vframes 1 "$of" 2> /dev/null
@NelsonMinar
NelsonMinar / README.md
Last active August 29, 2015 14:00
Simple D3 modular chart

A very, very simple modular chart based on [Mike Bostock's recommendation](// A very, very simple D3 modular chart based on http://bost.ocks.org/mike/chart/)

Creates a new chart type "histobar" that when insantiated, renders the data as a string in an SVG. Obviously not useful in itself, but the simplest possible reusable chart as a place to start.