Skip to content

Instantly share code, notes, and snippets.

Avatar
:shipit:
It's not just good, it's good enough

Brad Erickson 13rac1

:shipit:
It's not just good, it's good enough
View GitHub Profile
@13rac1
13rac1 / err_unwrap_stack.go
Last active Apr 29, 2020
Go 1.13+ Unwrap Error stack to print type and contents to discover which error types are available for use with errors.As()
View err_unwrap_stack.go
if err != nil {
for err != nil {
fmt.Printf("%T: %#v\n", err, err)
err = errors.Unwrap(err)
}
}
# Example Output
# *net.OpError: &net.OpError{Op:"read", Net:"tcp", Source:(*net.TCPAddr)(0xc00019df80), Addr:(*net.TCPAddr)(0xc00019dfb0), Err:(*os.SyscallError)(0xc000223b60)}
# *os.SyscallError: &os.SyscallError{Syscall:"read", Err:0x68}
# syscall.Errno: 0x68
@13rac1
13rac1 / 8bit-sine.h
Created Aug 8, 2019
8 bit Sine Wave lookup array
View 8bit-sine.h
# Reproduce with Golang
# for x := 0; x < 256; x++ {
# xInRadians := float64(x) / 255 * 2 * math.Pi
# sinX := uint8(math.Round((math.Sin(xInRadians) + 1) / 2 * 255))
# fmt.Printf("%#02x,", sinX)
# if x%8 == 7 {
# fmt.Print("\n")
# }
# }
# Based on https://gist.github.com/funkfinger/965900 but more precise.
@13rac1
13rac1 / bug-singleton-global-data-access.md
Last active Jan 3, 2019
Singleton global object cannot be changed by multiple threads without a mutex or data access conflicts will occur
View bug-singleton-global-data-access.md

I was researching how to use Golang's Template.FuncMap() and found a data access issue in: https://www.calhoun.io/intro-to-templates-p3-functions/

The section in question:

# Making our functions globally useful

Next we need to define our function that uses a closure. This is basically a fancy way of saying we are
going to define a dynamic function that has access to variables that are not necessarily passed into it,
but are available when we define the function. 
@13rac1
13rac1 / get-ops-json.sh
Created Feb 26, 2017
Generate a Minecraft UUID ops.json with curl and jq given a comma separated list of usernames
View get-ops-json.sh
#!/bin/sh
# 2017 https://github.com/eosrei/
OPS=${1-notch,jeb}
echo -n "$OPS" \
| jq --raw-input --slurp 'split(",")' - \
| curl -H "Content-type: application/json" \
-d @- \
@13rac1
13rac1 / measure_string.py
Last active Jul 6, 2020
Find the display width in pixels of a string given a font, point size and DPI in Python using Freetype
View measure_string.py
"Find the display width in pixels of a string given a font, point size and DPI."
import freetype
# Uses Freetype Python bindings:http://freetype-py.readthedocs.io/en/latest/
# Based on the FreeType Tutorial examples:
# https://www.freetype.org/freetype2/docs/tutorial/step2.html
# Note: This code uses bitshift by six to divide/multiply by 64 to convert
# between 26.6 pixel format (i.e., 1/64th of pixels) and points.
THE_STRING = "Hello World!"
View keybase.md

Keybase proof

I hereby claim:

  • I am 13rac1 on github.
  • I am bde (https://keybase.io/bde) on keybase.
  • I have a public key whose fingerprint is 4FAF 283F 8E37 3663 B56D 7031 E167 BF6B 827F 24EF

To claim this, I am signing this object:

@13rac1
13rac1 / androidwear
Created Jan 4, 2016
Automate pairing Android and Wear emulators with Expect
View androidwear
#!/usr/bin/expect
# Automates pairing Android and Wear emulators.
# Complete process described here: http://stackoverflow.com/questions/25205888/pairing-android-and-wear-emulators
# Run: ./androidwear DEVICEPORT
# Expect timeout, set short.
set timeout 4
# Device port on localhost [REQUIRED]
set deviceport [lindex $argv 0]
@13rac1
13rac1 / cpy
Last active Dec 15, 2015
Using Python 3 as a screen-logging command-line calculator
View cpy
#!/usr/bin/env python3
# Save this in your ~/bin as `cpy` and chmod +x
import code
import readline
import rlcompleter
# Directly import all the math functions
from math import *
@13rac1
13rac1 / ansible-create-user-with-password
Created Mar 18, 2013
Create a user in an Ansible playbook with a set, but unknown password so the account is not locked. Makes it possible to log on the account with a public key. This example requires mkpasswd, but that can be replaced with any other password generator.
View ansible-create-user-with-password
---
- hosts: all
gather_facts: yes
##
# Create the password then create the user
#
- name: Users | Generate password for new user
shell: makepasswd --chars=20
register: user_password