Skip to content

Instantly share code, notes, and snippets.

@adamv
adamv / setting.py
Created November 11, 2010 17:49
Find a setting/variable assignment-looking-thing.
def find_setting(source, setting, unquote=True):
re_setting = r'^\s*' + re.escape(setting) + r'\s*=\s*(.+)$'
m = re.search(re_setting, source, re.M)
if not m:
return None
value = m.group(1).strip()
if unquote:
if value.startswith("'") and value.endswith("'") or \
value.startswith('"') and value.endswith('"'):
@adamv
adamv / gist:674295
Created November 12, 2010 16:24
Cheesy "run command and give me status, stdout, stderr" Python function
from subprocess import Popen, PIPE
def local2(command, print_command=False):
"Run a command, returning the exit code, output, and stderr."
p = Popen(command, stdout=PIPE, stderr=PIPE)
if print_command: print " ".join(command)
output, errput = p.communicate()
return p.returncode, output, errput
@adamv
adamv / maven-nexus.py
Created November 18, 2010 17:11
Uploading to Nexus using Maven from the command line (in Python). Requires Maven and Curl.
def local2(command, print_command=False):
"Run a command, returning the exit code, output, and stderr."
from subprocess import Popen, PIPE
p = Popen(command, stdout=PIPE, stderr=PIPE)
if print_command: print " ".join(command)
output, errput = p.communicate()
return p.returncode, output, errput
def print_warning(msg):
print fabric.colors.red("WARNING: ") + msg
def print_error(msg):
print fabric.colors.red("ERROR: ") + msg
def print_label(msg):
print fabric.colors.blue("==> ", bold=True) + msg
@adamv
adamv / gist:736636
Created December 10, 2010 19:09
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
# Override username field require email address
class UserCreationForm2(UserCreationForm):
email = forms.CharField(max_length=75, required=True)
import httplib
import urllib
class WebServer(object):
def __init__(self, host):
self.host = host
def _make_request(self, method, url, data, headers):
conn = httplib.HTTPConnection(self.host)
@adamv
adamv / pairedlist.java
Created August 10, 2011 18:40
Is this dumb?
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicReference;
public class PairedList<T> {
private ConcurrentLinkedQueue<T> list1;
private ConcurrentLinkedQueue<T> list2;
private AtomicReference<ConcurrentLinkedQueue<T>> activeList;
public PairedList() {
public class RetryCounter {
private final int maxAttempts;
private int attempts;
private boolean successful;
public RetryCounter(final int maxAttempts) {
this.maxAttempts = maxAttempts;
this.attempts = 0;
this.successful = false;
}
/**
* Allows an operation to be retried up to a maximum number of tries.
*/
public class RetryCounter {
/** The maximum number of retries. */
private final int maxAttempts;
/** The number of attempts so far. */
private int attempts;
/**
* A Counter can issue sequential ids, and return the largest id issued so far.
*/
public class Counter {
private long nextValue = 0;
/**
* @return the next value from this counter.
*/
public long nextValue() {