Skip to content

Instantly share code, notes, and snippets.


Carl Johnson carlmjohnson

View GitHub Profile
carlmjohnson /
Last active Dec 19, 2018
How to chain exceptions in Python
# Thing that creates exceptions
def r(n):
raise Exception(f"error: {n}")
# Add exceptions to a list
errors = []
for n in range(10):
carlmjohnson / git
Last active Dec 19, 2018
Git Standards
View git

Git standards Creative Commons License

Git is the democracy of programming: it is the worst tool for version control, except all those other tools that have been tried from time to time. Among other problems, Git’s command names are obtuse (what is a “rebase”?) and non-orthogonal (how is “resetting” different than “checking out”?), and Git generally impedes the creation of an accurate mental model, but without understanding its underlying directed acyclic graph, one can’t move from beginner to intermediate user. Still, it is a necessary tool in every developer’s toolbox, and following good Git practices leads to smoother, more productive development. This guide assumes you already know how to use Git and discusses some of the higher level issues around standards for collaboration.


[<img s

View html-scan.go
package main
import (
const html = `
View As() and Is() are useful for optional interfaces beyond

EDIT: On further thought, the implications of using iface.As, e.g., for io.WriterTo are quite subtle. If you were trying, say, to add logging to all Read calls, you wouldn't want to simply pass through to the underlying io.WriterTo because the call would go unlogged. Instead you'd need to implement the As method on your type so as to only return a WriterTo under certain conditions. Still, I think this idea is worth exploring more.

The Go 2 process has produced a proposal for extending the errors standard package, which is currently in testing.

My observation about the experimental errors package is: As() and Is() solve problems that exist well beyond the error interface.

Essentially the problem is this, many packages begin by requiring a certain interface, such as error, io.Reader, or

View combinations.go
package main
import (
func Combinations(n int, combs []int) (next func() bool) {
k := len(combs)
if k == 0 || n < k {
View utils.js
const onAnimate = (eventType, callback) => {
let timeout;
window.addEventListener(eventType, ev => {
if (timeout) {
timeout = window.requestAnimationFrame(callback);
carlmjohnson /
Created Sep 3, 2019
For SVG files with bloated PNG embeds
def xtract_pngs_from_svgs(name):
fname = name +'.svg'
with open(fname) as f:
raw =
prefix = 'xlink:href="data:image/png;base64,'
suffix = '"'
chunks = []
found = []
View credit-card.vue
<div id="app">
Value is "{{ value }}". <br />
<input v-model="value" @input="handleChange($event)" />
function trimAndSpace(val) {
return val
View Article Bits.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
carlmjohnson / random.js
Created Jan 24, 2020
Generate random hex digits
View random.js
function randomHex(n) {
let bytes = new Uint8Array(n);
return Array.from(bytes).map(b => b.toString(16).padStart(2, '0')).join('');
You can’t perform that action at this time.