Skip to content

Instantly share code, notes, and snippets.

@xuru
xuru / rate_limit.py
Created October 7, 2014 16:38
memcache rate limiter
import webapp2
from functools import wraps
from google.appengine.api import memcache
def rate_limit(seconds_per_request=1):
def rate_limiter(function):
@wraps(function)
def wrapper(self, *args, **kwargs):
added = memcache.add('%s:%s' % (self.__class__.__name__, self.request.remote_addr or ''), 1,
@xuru
xuru / setup-managed-vms
Created November 21, 2014 20:35
setup-managed-vms error
Select the runtime to download the base image for:
[1] Go
[2] Java
[3] Python27
[4] All
Please enter your numeric choice (4): 3
Pulling base images for runtimes [python27] from Google Cloud Storage
Pulling image: google/appengine-python27
Traceback (most recent call last):
@xuru
xuru / smartcd.cfg
Created June 29, 2015 19:36
smartcd buildout
[smartcd]
recipe = mr.scripty
install =
... import sys
... import os.path
... sys.path.insert(0, self.buildout['omelette']['location'])
... import pexpect
...
... script_dir = os.path.join(
@xuru
xuru / commit-msg
Last active September 16, 2015 15:40
A commit message that adds the commit message to a jira ticket as a comment. Must have the jira ticket number in the commit message, or the branch name.
#!/usr/bin/env python
import distutils.spawn
import re
import sys
if '/usr/local/lib/python2.7/site-packages' not in sys.path:
sys.path.insert(0, '/usr/local/lib/python2.7/site-packages')
if '/Library/Python/2.7/site-packages' not in sys.path:
@xuru
xuru / ubuntu-12.04-lts.erb
Created June 20, 2012 22:08
Chef bootstrap script for ubuntu 12.04 LTS that actually works
bash -c '
<%= "export http_proxy=\"#{knife_config[:bootstrap_proxy]}\"" if knife_config[:bootstrap_proxy] -%>
if [ ! -f /usr/bin/chef-client ]; then
apt-get update
apt-get install -y libreadline5 libruby1.9.1 ruby1.9.1 ruby1.9.1-dev build-essential wget rubygems1.9.1
fi
LC_CTYPE=en_US.UTF-8 LANG=en_US.UTF-8 export LC_CTYPE LANG
@xuru
xuru / apt-upgrade.sh
Created July 10, 2012 17:42
non-interactive debian/ubuntu upgrade
#! /bin/sh
PATH=/usr/sbin:/sbin:/usr/bin:/bin
cat <<EOF > /etc/apt/apt.conf.noninteractive
APT::Get::Assume-Yes "true";
APT::Get::Show-Upgraded "true";
APT::Quiet "true";
DPkg::Options {"--force-confmiss";"--force-confold"};
DPkg::Pre-Install-Pkgs {"/usr/sbin/dpkg-preconfigure --apt";};
@xuru
xuru / has_remote_file_modified.rb
Created September 28, 2012 18:56
has_remote_file_modified: Ruby method to examine the http header and see if a file is newer on the remote system
class Chef
class Recipe
def has_remote_file_modified(uri, filepath)
require 'net/http'
http = Net::HTTP.new(uri.host, uri.port)
req = Net::HTTP::Head.new(uri.path)
if uri.user
req.basic_auth uri.user, uri.password
end
@xuru
xuru / argparseexample.py
Created November 12, 2012 22:52
argparse for the impatient
import argparse
parser = argparse.ArgumentParser(description='Spawn an army of test-robot '
'agents for the purpose of automated testing')
parser.add_argument('-c', '--create-test', action='store_true',
help='Create a new test...')
parser.add_argument('-t', '--test-case', dest='testcase',
default="robot_script.json" help='Test case file name (i.e. robot_script.json)')
# the default action here is to store False in opts.dumptest, and is not
@xuru
xuru / json_diff.py
Created November 28, 2012 17:00
Determines if two python objects differ (only json types supported)
def json_diff(obj, other, order_matters=True):
""" This method assumes json types: dict, list, strings, numbers, booleans and null """
if isinstance(obj, dict) and isinstance(other, dict):
if set(obj.keys()) != set(other.keys()):
return True
for key in sorted(obj.keys()):
if json_diff(obj[key], other[key], order_matters):
return True
elif isinstance(obj, list) and isinstance(other, list):
@xuru
xuru / observable_dict.py
Created December 7, 2012 17:23
Using the observer pattern, ObservableDict can be used to monitor changes to the dictionary.
class Observable:
'''Mixin for objects that need to be observed.'''
_observers = None
def observe(self, observer):
if not self._observers:
self._observers = []
self._observers.append(observer)