Programming

Work queues with CouchDB

For my startup project darticle, we have several "jobs" which need to be taken care of outside of standard web page serving, including e-mailing and web scraping. For scalability, these jobs are handled asynchronously and concurrently, with the goal of the job queue being able to scale across various servers. Here I will write about darticle's solution for implementing such a job management system with CouchDB.

DOPE: Deleting on process exit

One of the side effects of needing a temporary directory is needing to clean up after yourself. In my case, I extract a plugin into its own temporary directory (though it's currently not sandboxed) and load assemblies (kinda like ELF files) from that directory. When my plugin objects are disposed of (IDisposable and all that), the directory needs to be deleted.

Remove sound from video

Want to remove the sound tracks from your video files? Download script here. Drag-and-drop video files into strip-sound.bat.

For rationale and other details, read more.

USACO template

Entries for the USACO contests require a template header and some boilerplate code. To make things easier, I put boilerplate generation into a little script.

#!/bin/sh
 
FILENAME=$1
TASKNAME=$(echo "$1" | sed -e 's/\..*$//')
TEMPLATENAME=$(echo "$1" | sed -e 's/^.*\.//')
 
sed -e 's/#TASKNAME#/'"$TASKNAME/" < template."$TEMPLATENAME" > "$FILENAME"

Template files (e.g. template.c) look like this:

/*
ID: your_id_here
LANG: C
TASK: #TASKNAME#
*/
#define PROG "#TASKNAME#"
 
#include <stdio.h>
 
int main() {
    FILE *fin  = fopen(PROG ".in", "r");
    FILE *fout = fopen(PROG ".out", "w");
 
    return 0;
}

Just give the Bash script the name of the file you need and it will make the file with the appropriate language (based off the extension given) and task name.

Twitter on irssi

Update: Twitter changed its authentication scheme so this plugin no longer works.

A while ago, I wondered why I had a Twitter account. I didn't use it, and everything interesting I was doing I put in my away message in irssi (with, in addition to IRC, my IM accounts, thanks to BitlBee).

So I thought, why not Twitter my away messages? I made a Perl script for irssi to do just that.

Tokenizing

I've found that when beginning to write a tokenizer from scratch, it's best to lay out the definitions of your token types. It doesn't have to be in some magic format; just something you can understand will work.

For each token type, create a function to read that token and no more, and return that token as an object. (Be sure to add error handling and such.) Different types of tokens should use the same base class. (You can use an enum or subclasses. I chose an enum for its simplicity.) As for the name, Read<em>Type</em> is what I chose, as it does exactly that: reads a number, string, etc. from the input.

Then have a function to figure out what token follows, and have it return the token using the function you created earlier. ReadToken is a descriptive name.

It's a simple while loop to read in multiple tokens. Be sure to skip whitespace between tokens if it's unimportant. Naturally, this goes in a function called Tokenize or similar.

And now you have your tokenizer. Simple, wasn't it?

Syndicate content