Skip to content

Instantly share code, notes, and snippets.

View cbsmith's full-sized avatar

Christopher Smith cbsmith

View GitHub Profile
@cbsmith
cbsmith / livecat.py
Last active March 26, 2017 22:12
First pass at doing a live "cat" in python
'''
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 / livetail.py
Last active March 26, 2017 22:31
Live tail of a file
'''
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 / config_gen.py
Created April 4, 2017 05:47
Python to generate all possible permutations of config options
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 / mkpasswd.py
Created February 25, 2018 00:39
A pythonic/portable password generator for when you're in a pinch
"""
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 / keybase.md
Created March 19, 2018 20:28
Proving who I am

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 / fmapp.hpp
Created September 5, 2018 02:34
A handy implementation of fmap that is apparently missing in the standard.
#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))) :