Skip to content

Instantly share code, notes, and snippets.

// Demo for connecting to Bonsai.io using the Elasticsearch client for Go.
// Make sure to match your Bonsai cluster version to the Go client version.
// Validated using a Bonsai cluster running Elasticsearch 6.2.3.
//
// 1. go get github.com/olivere/elastic
// 2. go build bonsai.go
// 3. ./bonsai
// 4. Program will create an index called `test` on your Bonsai cluster.
//
// Feel free to tweak/clean up as needed, golang isn't my specialty.
@robsears
robsears / aoc-day1.rs
Last active November 30, 2017 07:15
Rust solution for the 2016 Advent of Code Day 1, parts 1 and 2. My original solution was in Java, but thought I'd give Rust a try.
// Advent of Code 2016: Day 1
// http://adventofcode.com/2016/day/1
// Data structure representing the elf navigating the city.
struct Elf {
x: i32,
y: i32,
heading: f32,
positions: Vec<(i32,i32)>,
revisit: bool
@robsears
robsears / yt2mp3
Created November 20, 2013 17:27
A simple shell script for downloading audio from YouTube and saving to MP3 format, with ID3 information.
#!/bin/bash
# Simple shell script for downloading audio from YouTube as .mp3 file
# Requires:
# * youtube-dl (http://rg3.github.io/youtube-dl/)
# * id3tag (Ubuntu libid3 package, based on http://id3lib.sourceforge.net/)
if [ -z "$1" ]; then
echo "Usage: yt2mp3 <file>"
echo "File supplied must have a list of URLs to YouTube videos and ID3"
echo "information, comma-separated."
@robsears
robsears / monty.php
Created November 18, 2013 01:23
Monty Hall Program
<?php
// This program simulates a number of games involving
// The Monty Hall Problem, and compares two approaches:
// sticking with the player's original choice, or
// switching to the other option. The program measures
// how successful each approach is and prints the
// result to the screen. I used PHP since it is easy to
// read and understand, but this program could easily
// be ported to any number of other languages.
@robsears
robsears / find_all_modules.sh
Created September 22, 2013 06:33
A simple BASH script to get a list of kernel modules and their description and dependencies.
#!/bin/bash
# This code should not be run as `sh find_all_modules.sh`, as some distros
# link sh to dash, which is POSIX-compliant and doesn't do arrays.
# Adapted from Linux Kernel in a Nutshell: http://www.kroah.com/lkn/
# By Rob Sears, Sept 22, 2013.
# Get a newline-separated list of modules in use:
MODLIST=`for i in $(find /sys/ -name modalias -exec cat {} \; 2>/dev/null); do /sbin/modprobe --config /dev/null --show-depends $i 2> /dev/null;done | rev | cut -f 1 -d '/' | rev | sort -u`
# Convert the newline-separated list to an array:
@robsears
robsears / mysql2solr.php
Last active December 20, 2015 10:48
This is just a quick and simple script for importing MySQL data into a Solr index. Very basic and only handles data in a single table; if your data is spread across multiple tables using JOINs, etc, then you're out of luck. Feel free to repurpose any of this code to fit your specific needs.
<?php
// A quick and dirty script for bulk importing MySQL data into a Solr index
// Edit this array with the MySQL fields that will be sent to the SOLR index
// The key is the name of the MySQL column and the value is its respective Solr field
//
// Example: 'mysql_col_name' => 'solr_field'
//
$field_mappings = array(
'id' => 'id',
@robsears
robsears / tilemapping.py
Last active August 25, 2016 20:15
Given latitude and longitude coordinates (say, from a GPS unit), this little script calculates what map tile to select, and a pixel location closest to the given coordinates. By itself, this script is pretty useless. The intent is to begin a framework to be used in later scripts dealing with geolocation.
# Given: a lat/lng coordinate
# Return: the tile and pixel coordinates of given position
# By Rob Sears, Feb 2013
import math
GPS_Lat = 37.364101 # A neat little SR-71
GPS_Lng = -120.577612 # Blackbird in California
zoom = 21
n = math.pow(2, zoom)
@robsears
robsears / wxHello.cpp
Created December 16, 2012 07:07
A simple Hello World program for wxWidgets. Taken without alteration from http://www.wxwidgets.org/docs/tutorials/hworld.txt
/*
* hworld.cpp
* http://www.wxwidgets.org/docs/tutorials/hworld.txt
*/
#include <wx\wx.h>
class MyApp: public wxApp
{
virtual bool OnInit();
@robsears
robsears / gist:4260561
Created December 11, 2012 17:44
Overlay a transparent PNG on video feed using OpenCV and wxPython
# Adapted from http://www.aishack.in/2010/07/transparent-image-overlays-in-opencv/
import wx
from cv2 import *
capture = cv.CaptureFromCAM(0)
overlay = cv.LoadImage("ghost.png")
posx = 0
posy = 0
xdir = 1
@robsears
robsears / OverlayPNG.py
Created December 11, 2012 17:24
Overlay a transparent PNG in OpenCV using Python
# Adapted from http://www.aishack.in/2010/07/transparent-image-overlays-in-opencv/
# Note: This code assumes a PNG with a Color->Transparency, with black as the alpha color
from cv2 import *
src = cv.LoadImage("image.jpg") # Load a source image
overlay = cv.LoadImage("ghost.png") # Load an image to overlay
posx = 170 # Define a point (posx, posy) on the source
posy = 100 # image where the overlay will be placed
S = (0.5, 0.5, 0.5, 0.5) # Define blending coefficients S and D