Skip to content

Instantly share code, notes, and snippets.

@asottile
asottile / GhettoRetry.hpp
Created March 20, 2013 15:47
Ghetto Retry for c++
#ifndef GHETTO_RETRY_H
#define GHETTO_RETRY_H
template<typename TEx, TCall>
void ghettoRetry(TCall call, int count = 1) {
// Retry count number of times
// Note the last one will throw on failure
for (int i = 0; i < count - 1; i += 1) {
try {
call();
@asottile
asottile / Range.cpp
Created April 2, 2013 17:02
Python like xrange in c++ with range fors!
#include <iostream>
using std::cout;
using std::endl;
class Range {
public:
explicit Range(int endValue) : start(0), endValue(endValue) { }
Range(int start, int endValue) : start(start), endValue(endValue) { }
@asottile
asottile / gist:6265617
Last active December 21, 2015 06:39
Patching out modules in Python
import contextlib
import collections
import mock
import sys
def fake_module(**args):
return (collections.namedtuple('module', args.keys())(**args))
def get_patch_dict(dotted_module_path, module):
patch_dict = {}
import random
import re
simple_word_regex = re.compile('^[a-z]+$')
word_list = []
with open('/usr/share/dict/american-english', 'r') as words_file:
for word_line in words_file:
word = word_line.strip()
@asottile
asottile / SassMeister-input.scss
Created January 30, 2016 07:25
Generated by SassMeister.com.
// ----
// Sass (v3.4.20)
// Compass (v1.0.3)
// ----
a, b {
&:not(c) {
d: e;
}
}
@asottile
asottile / check_upgradeable.py
Created June 16, 2016 16:11
Check whether a yelp-cheetah template is trivially upgradable to 0.17.0
import argparse
import ast
import io
from Cheetah.compile import compile_source
from Cheetah.Template import Template
template_self_vars = {var for var in dir(Template) if not var.startswith('_')}
import pipes
import subprocess
pythons = (
'Python27',
'Python27_x86',
'Python34',
'Python34_x86',
'Python35',
@asottile
asottile / search_rtf_in_directory.py
Created August 19, 2016 15:36
Search rtf files in a directory. (At the time requires python2 (due to pyth))
import argparse
import os
from pyth.plugins.rtf15.reader import Rtf15Reader
from pyth.plugins.plaintext.writer import PlaintextWriter
def get_text(filename):
with open(filename) as f:
doc = Rtf15Reader.read(f)
@asottile
asottile / gerthurb.user.js
Last active February 15, 2017 02:59
@chriskuehl hates the dark header
// ==UserScript==
// @name gerthurb
// @namespace asottile
// @include https://*.github.com/*
// @include https://github.com/*
// @version 1
// @grant none
// ==/UserScript==
document.querySelector('.header-dark').classList.remove('header-dark');
@asottile
asottile / ntpp.py
Last active December 22, 2018 22:23
import pprint
def ntpp(x, *, l=0):
indent = l * 4 * ' '
if hasattr(x, '_fields'):
return ''.join((
type(x).__name__ + '(\n',
*(
indent + ' ' +