Skip to content

Instantly share code, notes, and snippets.

@rduplain
rduplain / directories.bash
Created August 26, 2011 14:51
Answering: how can I reference the scripts directory and the working directory in GNU bash?
#!/bin/bash
# `dirname` is part of GNU coreutils, but is okay to use since it's in the Unix standard.
DIRNAME=`dirname $0`
echo script is at $DIRNAME
# Alternative, use pure bash substitution.
DIRNAME=${0%/*}
echo script is at $DIRNAME
@rduplain
rduplain / gist:1487917
Created December 16, 2011 20:50
Android: view holder without a ViewHolder class, when only one view to hold.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = activity.getLayoutInflater().inflate(layoutId, null);
convertView.setTag(convertView.findViewById(textViewId));
}
TextView textView = (TextView) convertView.getTag();
textView.setText(items.get(position).getTitle());
return convertView;
}
@rduplain
rduplain / generate_menu
Created January 1, 2012 17:24
Use MenuMaker to generate a Fluxbox menu, with a personal header stub.
#!/bin/sh
# Use MenuMaker to generate a Fluxbox menu, with a personal header stub.
# Goal: Start your fluxbox menu by hand, finish it with an auto-generated menu.
STUB=$HOME/.fluxbox/menu.stub
MENU=$HOME/.fluxbox/menu
# Add other modifiers to the pipeline as desired.
mmaker -f FluxBox -c -t Xterm | grep -v '\[begin\]' | cat $STUB - > $MENU
@rduplain
rduplain / dp2px.py
Created January 5, 2012 15:53
Convert dp dimensions to px dimensions, rewriting .xml files in place.
"Convert dp dimensions to px dimensions, rewriting .xml files in place."
# Rewrite all .xml files in res/ with:
# find res -name '*.xml' | xargs python dp2px.py
import decimal
import re
import sys
# scale = getResources().getDisplayMetrics().density; // In Activity.
@rduplain
rduplain / config.py
Created January 10, 2012 00:48
Web application config which picks values based on deployment/hostname.
"Web application config which picks values based on deployment/hostname."
# Ron DuPlain <ron.duplain@gmail.com>
# Developed by Ron DuPlain and Dan Lepage for Private Practice LLC.
#
# This listing serves as an example, extracted from a real-world config.py.
# The resulting config picks values based on hostname, which can be overriden
# by the DEPLOYMENT environment variable. Add to each .wsgi as appropriate:
#
# import os
@rduplain
rduplain / README.md
Created January 23, 2012 22:36
Fast recipe for memory monitoring.

First step toward Nagios: Fast recipe for memory monitoring.

You have a Linux box. It has RAM. You'd like to log and monitor memory usage. You have many options -- here's one: use a check_mem plugin for Nagios, but use it stand-alone at first. You'll have detailed memory logging in just a minute, and will take your first step toward full system monitoring with Nagios (later when you can formally install the plugin).

Download: https://raw.github.com/justintime/nagios-plugins/master/check_mem/check_mem.pl

Read it and make sure you feel comfortable running it. Then chmod a+x check_mem.pl and see help with ./check_mem.pl -h. In one shell, run it in a 5s loop, logging to /tmp/mem.log (Control-C to stop):

@rduplain
rduplain / client.py
Created February 24, 2012 19:41
A very simple Python HTTP client based on standard library, for testing against development servers.
import cookielib
import urllib
import urllib2
class Client(object):
def __init__(self):
self.cookie_jar = cookielib.CookieJar()
self.opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self.cookie_jar))
@rduplain
rduplain / gist:2017517
Created March 11, 2012 18:19 — forked from ask/gist:2014539
celery consumer service using boot-steps.
from celery import abstract
from celery import current_app
from kombu import Exchange, Queue
from kombu.mixins import ConsumerMixin
# need to subclass the result backend so that it uses a topic exchange
# instead of direct, and send the results for tasks using a routing_key
# of the format:
@rduplain
rduplain / manage.py
Created March 21, 2012 14:13
Current Flask-Celery + Flask-Script integration.
from flask import Flask
from flask.ext.celery import install_commands
from flask.ext.script import Manager
app = Flask(__name__)
manager = Manager(app)
install_commands(manager)
@rduplain
rduplain / args.py
Created March 27, 2012 14:00
Calling positional arguments by name in Python.
def spam(a, b, c=None):
return a, b, c
if __name__ == '__main__':
print "spam(b='b', a='a', c='c') =>", spam(b='b', a='a', c='c')
print "spam(b='b', a='a') =>", spam(b='b', a='a')
try:
spam(a='a', c='c')
except TypeError, e: