Skip to content

Instantly share code, notes, and snippets.

Christopher Smith cbsmith

  • Ticketmaster
Block or report user

Report or block cbsmith

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@cbsmith
cbsmith / protoc-gen-depends.py
Created Apr 16, 2012
Makefile dependency generator plugin for protoc.
View protoc-gen-depends.py
#!/usr/bin/python
# Save this file as protoc-gen-depends, put it in your path (executable) and add "--depends-out=/whatever/path" to your protoc invocation
from google.protobuf.compiler.plugin_pb2 import CodeGeneratorRequest,CodeGeneratorResponse
from sys import stdin,stdout
req = CodeGeneratorRequest()
req.MergeFromString(''.join(stdin.readlines()))
res = CodeGeneratorResponse()
@cbsmith
cbsmith / random_selection.cpp
Last active Dec 6, 2018
Hopefully serves as a reference implementation on how to do random selection of an element from a container.
View random_selection.cpp
// -*- compile-command: "clang++ -ggdb -o random_selection -std=c++0x -stdlib=libc++ random_selection.cpp" -*-
//Reference implementation for doing random number selection from a container.
//Kept for posterity and because I made a surprising number of subtle mistakes on my first attempt.
#include <random>
#include <iterator>
template <typename RandomGenerator = std::default_random_engine>
struct random_selector
{
//On most platforms, you probably want to use std::random_device("/dev/urandom")()
@cbsmith
cbsmith / fmapp.hpp
Created Sep 5, 2018
A handy implementation of fmap that is apparently missing in the standard.
View fmapp.hpp
#ifndef FMAP_HPP__INCLUDE
#define FMAP_HPP__INCLUDE
#include <optional>
namespace nonstd {
template <typename F, typename T>
auto fmap(F f, std::optional<T> x) -> std::optional<decltype(f(std::forward<T>(*x)))> {
return (x.has_value()) ?
std::make_optional(f(std::forward<T>(*x))) :
@cbsmith
cbsmith / keybase.md
Created Mar 19, 2018
Proving who I am
View keybase.md

Keybase proof

I hereby claim:

  • I am cbsmith on github.
  • I am cbsmith (https://keybase.io/cbsmith) on keybase.
  • I have a public key ASBCAABS7zQnEiWafsyLC0dowkNMrEEANe1AoVsuiwZ0BAo

To claim this, I am signing this object:

@cbsmith
cbsmith / mkpasswd.py
Created Feb 25, 2018
A pythonic/portable password generator for when you're in a pinch
View mkpasswd.py
"""
Generic xkcd style (https://xkcd.com/936/) password generator, because you never know when you're going to need a new password.
Usage:
mkpassword.py <num_words> <dictionary_path>
"""
from random import SystemRandom
from os.path import join, isfile
import sys
@cbsmith
cbsmith / idempotent_path_add.sh
Last active Oct 16, 2017
An example on how to do idempotent PATH manipulation.
View idempotent_path_add.sh
#!/usr/local/bin/bash
#How to manipulate the path in an intelligent fashion with bash. Note: this doesn't work with Bourne shell.
function idempotent_path_add {
DIR="$1"
PREPEND=$2
if [[ ! "$PATH" =~ (^|:)"$DIR"(:|$) ]]
then
if [ $PREPEND ]
then
@cbsmith
cbsmith / config_gen.py
Created Apr 4, 2017
Python to generate all possible permutations of config options
View config_gen.py
from itertools import chain, ifilter, product
# These first few functions are just to sanitize data structures to deal with huamns
# providing data in ways that are convenient for their minds.
def is_iterable(x):
return hasattr(x, '__iter__')
def as_config_values(values):
@cbsmith
cbsmith / livetail.py
Last active Mar 26, 2017
Live tail of a file
View livetail.py
'''
Live tail of a file. Uses mmap to try to be efficient with memory.
Requires fsmonitor: https://github.com/shaurz/fsmonitor
...which unfortunately lacks a decent monitor for non-Windows/Linux
systems, so burns up CPU unnecessarily on those platforms. Despite its
reputation, python makes it surprisingly difficult to write clean and
correct code.
'''
@cbsmith
cbsmith / livecat.py
Last active Mar 26, 2017
First pass at doing a live "cat" in python
View livecat.py
'''
Live cat of file. Uses mmap to try to be efficient with memory,
though reads a byte at a time, so it burns through CPU by hitting the
python interpreter loop N times. We could add buffering, but that
would just make the code more complex, and who wants that?
stdout gets the default buffering, so consider invoking with
-u flag/PYTHONBUFFERED environment variable if not writing to
a file.
@cbsmith
cbsmith / example.com.html
Created Nov 23, 2016
This is what example.com looks like from Southwest.
View example.com.html
<!doctype html>
<html>
<head><link href="http://getconnected.southwestwifi.com/unb/unb.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://getconnected.southwestwifi.com/unb/jqr44-1.8.3.js"></script>
<script type="text/javascript">var r44_btime=new Date();var r44_smu_time=1479876620.446</script>
<script type="text/javascript" src="http://getconnected.southwestwifi.com/unb/unb.js"></script>
<title>Example Domain</title>
You can’t perform that action at this time.