Code Snippets
- Modeled after Golang's ioutil package's NopCloser method which wraps an io.Reader interface in an io.ReadCloser facade interface, this code takes an io.Writer and a nil channel and returns an io.WriteCloser interface. The example.go file demonstrates usage in an http.HandleFunc as a very over complicated counter using the worker-and-channel-over-mutex pattern.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package main import ( "io" "log" "net/http" "strconv" ) func main() { worker := NewWorker() http.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) { doneChan := make(chan struct{}) worker.request <- WriteChanCloser(w, doneChan) <-doneChan }) err := http.ListenAndServe(":8888", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } } type Worker struct { count int request chan io.WriteCloser } func (w *Worker) run() { go func() { for { select { case req := <-w.request: w.count += 1 req.Write([]byte(strconv.Itoa(w.count))) req.Close() } } }() } func NewWorker() *Worker { w := &Worker{ count: 0, request: make(chan io.WriteCloser), } w.run() return w } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characterspackage main import "io" type writeChanCloser struct { io.Writer closeChan chan struct{} } func (wcc writeChanCloser) Close() error { close(wcc.closeChan) return nil } // WriteChanCloser returns a WriteCloser with a Close method wrapping // the provided Reader r. The Close method simply closes the provided // nil channel. func WriteChanCloser(r io.Writer, c chan struct{}) io.WriteCloser { return writeChanCloser{r, c} } - Center text on an image with Python and OpenCV. Had to come up with it myself as no one was spelling this out anywhere (or google couldn't find it)This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/usr/bin/env python import numpy as np import cv2 from time import sleep # create blank image - y, x img = np.zeros((600, 1000, 3), np.uint8) # setup text font = cv2.FONT_HERSHEY_SIMPLEX text = "Hello Joseph!!" # get boundary of this text textsize = cv2.getTextSize(text, font, 1, 2)[0] # get coords based on boundary textX = (img.shape[1] - textsize[0]) / 2 textY = (img.shape[0] + textsize[1]) / 2 # add text centered on image cv2.putText(img, text, (textX, textY ), font, 1, (255, 255, 255), 2) # display image cv2.imshow('image', img) # wait so you can see the image sleep(25) # cleanup cv2.destroyAllWindows() - Terminal "utility" to copy ASCII emoji to the clipboard on OSXThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#!/bin/bash EMOTS=( "¯\_(ツ)_/¯" "༼ ༎ຶ ෴ ༎ຶ༽" "「(°ヘ°)" "(╯°□°)╯︵ ┻━┻" "༼ つ ◕_◕ ༽つ" "(✿◠‿◠)" "¯(°_o)/¯" "(͡° ͜ʖ ͡°)" "(ಠ_ಠ)" "(╯_╰)" "(─‿‿─)" "\,,/(^_^)\,,/" "(¬、¬)" "(ノ゚0゚)ノ" "(╯°□°)╯︵ ʞooqǝɔɐɟ" "(⌐■_■)" "╭∩╮(︶︿︶)╭∩╮" "c[_]" "(ง •̀_•́)ง" "(⌐■_■)︻╦╤─ " "(ಡ_ಡ)☞" "◕_◕" "(눈_눈)" "(◔_◔)" "\_(-_-)_/" "⊹╰(⌣ʟ⌣)╯⊹" ) sel=-1 while (($sel < 1)) do for (( i = 1; i <= ${#EMOTS[@]}; i++ )) do echo "${i}) ${EMOTS[($i-1)]}" done read -p "Which one? " sel [[ "$sel" =~ ^[0-9]+$ ]] || sel=-2 (($sel > ${#EMOTS[@]})) && sel=-3 done echo ${EMOTS[$sel-1]} | pbcopy echo "... copied ${EMOTS[$sel-1]} to clipboard" - Grab stats from memcachedThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
echo stats | nc 127.0.0.1 11211 - Bash function to bump minor semantic version number by even numbers.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
releaseBumpValue() { semver="${1}" local tifs=${IFS} local IFS='.' read -ra bits <<< "${semver}" if [ $((bits[1]%2)) -eq 0 ]; then echo 2 else echo 1 fi IFS=${tifs} }