Skip to content

Instantly share code, notes, and snippets.

Avatar
🦀
Playing with Rust

Michael Diamond dimo414

🦀
Playing with Rust
View GitHub Profile
@dimo414
dimo414 / README.md
Last active Mar 29, 2021
Raspberry Pi Camera + mjpg-streamer systemd setup
View README.md

This article includes setup instructions for the Raspberry Pi camera, but doesn't capture setting it up as a service (even though a few lines up it sets up OctoPrint as a service), and instead provides a pair of relatively-complex scripts to be run at startup via /etc/rc.local.

Setup

  • copy webcam.sh to the mjpg-streamer directory (or wherever you'd like, just update the MJPG_DIR variable)
  • copy pi-webcam.service to /etc/systemd/system/ (requires sudo), ensure ExecStart points to the correct path
@dimo414
dimo414 / health-plex.sh
Last active Apr 3, 2021
Plex Healthcheck
View health-plex.sh
#!/bin/bash
# Checks that a Plex server is up. For devices on a local network your
# router may have created [hostname].local or [hostname].lan DNS entries;
# otherwise you can pass an IP address or ensure your server is remotely
# accessible (https://support.plex.tv/articles/200931138/).
#
# See also
# https://old.reddit.com/r/PleX/comments/7qolre/what_do_others_do_to_monitor_plex_health/
# https://support.plex.tv/articles/201638786-plex-media-server-url-commands/
#
@dimo414
dimo414 / _README.md
Last active Jun 30, 2022
Bash array expansion patterns for use with -u
View _README.md

Expanding Bash arrays safely with set -u

Prior to Bash 4.4 set -u treated empty arrays as "unset", and terminates the process. There are a number of possible workarounds using array parameter expansion, however almost all of them fail in certain Bash versions.

This gist is a supplement to this StackOverflow post.

@dimo414
dimo414 / build.md
Last active Apr 30, 2020
Run Slack Dogebot on Raspberry Pi
View build.md

Build the Luit/dogebot binary (I prefer to do so in a Docker container) for ARM:

$ docker run -v /tmp:/mnt/tmp -it debian:testing
docker$ apt-get update && apt-get install -y git golang
# Ensure build dependencies are available locally
docker$ go get github.com/Luit/dogebot/src/dogebot
docker$ git clone https://github.com/Luit/dogebot
docker$ cd dogebot/src/dogebot
# Build a Raspberry Pi binary
@dimo414
dimo414 / getopts.sh
Last active Mar 14, 2022
getopts function helper
View getopts.sh
#!/bin/bash
#
# Helper utility to simplify using Bash's getopts utility, and make it usable with functions.
#
# Example usage:
# foo() {
# local _usage=... # optional usage string
# eval "$(parse_opts 'ab:f:v')" # provide a standard getopts optstring
# echo "f is $f" # opts are now local variables
# if (( a )); then # check boolean flags with (( ... ))
@dimo414
dimo414 / health-blink.sh
Last active Apr 3, 2021
Healthchecks.io + blink(1)
View health-blink.sh
@dimo414
dimo414 / heartbeat.sh
Last active May 2, 2022
Basic Server Heartbeat Script
View heartbeat.sh
#!/bin/bash
# Source: https://gist.github.com/dimo414/10d6f162fb6d72f517a041d28d92314f
#
# This is a basic heartbeat monitoring script suitible for adding to
# a machine's crontab to receive alerts (via https://healthchecks.io)
# when the machine is unhealthy.
#
# I use it to keep an eye on my headless Raspberry Pi, but it should
# work for most Linux machines.
#
@dimo414
dimo414 / default_detect.py
Created Mar 5, 2020
Detecting whether an argparse argument was set explicitly or not
View default_detect.py
class DefaultCapturingNamespace(argparse.Namespace):
"""An argparse Namespace that tracks the number of times the given argument
has been set. In theory this can allow you to distinguish between values
set by argparse (e.g. default values) and those specified by the user.
"""
def __init__(self, **kwargs):
type_name = type(self).__name__
argparse.Namespace.__setattr__(self, '_%s__original_values' % type_name, {})
argparse.Namespace.__setattr__(self, '_%s__set_counts' % type_name, {})
@dimo414
dimo414 / MapSpeedTest.java
Created Mar 4, 2020
Benchmark of map implementation access speeds
View MapSpeedTest.java
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
@dimo414
dimo414 / description.md
Created Mar 4, 2020
St. Petersburg Paradox Simulation
View description.md

The St. Petersburg paradox, in a nutshell, is: why would you refuse to play a game with an unlimited expected payout? Shouldn't you be willing to pay any finite price to play, since your expected return is infinite?

This simulation explores what the Stanford Encyclopedia refers to as the "Average Win" explanation, that:

Expected value is in effect average payback in the long run.... [I]f you're going to play St. Petersburg with a substantial fee per game, you're likely to have to play a very very long time before you come out with a positive net payoff. Ordinary practical considerations thus apply... You may have to play a very long time before winning once. But worse: in a series of losses, the amount needed for the next bet rises. How much bankroll would you need to guarantee success...? Whatever bankroll you take into the casino, there's a chance that this will not be enough.

The