Skip to content

Instantly share code, notes, and snippets.

View sbrl's full-sized avatar

Starbeamrainbowlabs sbrl

View GitHub Profile
@sbrl
sbrl / glove.py
Last active December 7, 2021 15:20
GloVe word embedding handling code for Python - improved fork of https://gist.github.com/ppope/0ff9fa359fb850ecf74d061f3072633a
import time
import io
import sys
from loguru import logger
import numpy
import tensorflow as tf
from ..polyfills.string import removeprefix, removesuffix
from .normalise_text import normalise as normalise_text
@sbrl
sbrl / ensure-network.service
Last active June 9, 2024 18:38
Bash script to automatically reboot if the network connection goes down
[Unit]
Description=Reboot if the network connection is down
After=network.target
[Service]
Type=simple
# Because it needs to be able to reboot
User=root
Group=root
EnvironmentFile=-/etc/default/ensure-network
@sbrl
sbrl / main.js
Created May 26, 2020 19:35
Multi-process line-by-line reading from a single pipe in Node.js
"use strict";
import EventEmitter from 'events';
import child_process from 'child_process';
/**
* Helper method that waits for an event to be fired on a given object.
* @param {EventEmitter} obj The object that will fire the event - must inherit from EventEmitter
* @param {string} event_name The name of the event to wait for.
* @return {Promise} A promise that resolves when the specified event is fired on the given object.
@sbrl
sbrl / ErrorWrapper.mjs
Last active February 13, 2020 16:43
[ErrorWrapper.mjs] Error wrapper class - wrap errors like C♯! #error-handling
class ErrorWrapper extends Error {
constructor(message, inner_exception) {
super(message);
this.inner_exception = inner_exception;
}
toString() {
return `${super.toString()}\n***Inner Exception ***\n${this.inner_exception}`;
}
}
@sbrl
sbrl / Retry.mjs
Created February 13, 2020 16:42
[Retry.mjs] Retry a function automatically a specified number of times #utility
"use strict";
import l from './Log.mjs';
import { ErrorWrapper } from './Errors.mjs';
/**
* Tries to execute a given function, and retried it a specified number of
* times if it throws an error.
* @param {Number} times The number of times to retry before giving up and throwing an error ourselves.
* @param {Function} func The function to execute.
@sbrl
sbrl / CliParser.mjs
Last active March 9, 2020 18:24
[CliParser.mjs] Minimal CLI parser for Node.js - uses Ansi.mjs #library
"use strict";
import fs from 'fs';
import a from './Ansi.mjs';
/**
* Represents a program and all it's arguments and subcommands.
* TODO: Publish on npm under the name "applause-cli", since it's inspired by clap?
* @license MPL-2.0
@sbrl
sbrl / Stopwatch.mjs
Created December 22, 2019 00:52
[Stopwatch.mjs] C#'s System.Diagnostics.Stopwatch (roughly) implemented in C#.
"use strict";
/**
* Stopwatch class for timing things.
* Based on System.Diagnostics.Stopwatch from .NET: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stopwatch
*/
class Stopwatch {
/**
* Creates a new (stopped) stopwatch.
*/
@sbrl
sbrl / Log.mjs
Last active January 12, 2020 21:28
[Log.mjs] Logging utility class
"use strict";
import a from './Ansi.mjs';
const LOG_LEVELS = {
DEBUG: 0,
INFO: 1,
LOG: 2,
WARN: 4,
ERROR: 8,
@sbrl
sbrl / errorimage.php
Created October 22, 2019 20:54
[errorimage.php] Generate error images on the fly in portable PHP
/**
* Creates an images containing the specified text.
* Useful for sending errors back to the client.
* @package core
* @param string $text The text to include in the image.
* @param int $target_size The target width to aim for when creating
* the image. Not not specified, a value is
* determined automatically.
* @return resource The handle to the generated GD image.
*/
@sbrl
sbrl / JsonStorageBox.php
Last active September 21, 2019 20:17
[JsonStorageBox.php]
<?php
/*
███████ ████████ ██████ ██████ █████ ██████ ███████ ██████ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██ ██ ██████ ███████ ██ ███ █████ ██████ ██ ██ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
███████ ██ ██████ ██ ██ ██ ██ ██████ ███████ ██████ ██████ ██ ██
*/