Skip to content

Instantly share code, notes, and snippets.

View wecsam's full-sized avatar

David Tsai wecsam

View GitHub Profile
@wecsam
wecsam / UnskipByNumbers.gs
Created December 14, 2024 01:07
A Google Slides app script that skips/unskips slides based on the user's input
function onOpen() {
SlidesApp.getUi()
.createMenu("Scripts")
.addItem("Skip/Unskip by Numbers", "unskipByNumbers")
.addToUi();
}
/** Prompts the user for slide numbers. Unskips all of the specified slides. Skips the rest. */
function unskipByNumbers() {
var ui = SlidesApp.getUi();
@wecsam
wecsam / Scrutineering.gs
Created October 31, 2024 04:19
Custom Google Sheets functions to compile judges' inputs via Google Forms at a ballroom dance competition
// The following are custom Google Sheets functions to scrutinize judges' data for a ballroom competition.
// They are designed to work with Google Forms responses. See each function's description below for
// instructions on setting up the Google Forms question.
//
// More info on custom functions: https://developers.google.com/apps-script/guides/sheets/functions
// More info on the system for scoring ballroom competitions: https://en.wikipedia.org/wiki/Skating_system
/**
* Counts the number of marks for each competitor in a qualifying (non-final) round of a single-dance event.
*
@wecsam
wecsam / slowly_feed_onedrive.py
Last active May 14, 2024 19:41
Wait for free space when moving files
#!/usr/bin/env python3
import argparse
import datetime
import humanfriendly # pip install humanfriendly
import os
import shutil
import subprocess
import sys
import time
@wecsam
wecsam / ipc.py
Last active July 18, 2019 04:32
Basic IPC demo with non-parent-child relationship in Python
#!/usr/bin/env python3
'''
This script demonstrates some basic inter-process communication. Typical usage:
1. Run this script with the "make" argument. On Windows, a new console window
will appear; that is the worker. Copy the last line of the output from this
window to the clipboard.
2. Run this script with the "query" argument and paste the last line of the
output from the last step as the second argument. Repeat this step as many
times as you want. After the worker finishes, repeating this step will cause
@wecsam
wecsam / njt_tssp.user.js
Created June 8, 2019 20:12
This script makes the origin and destination fields on the NJ Transit train schedule page persistent.
// ==UserScript==
// @name NJ Transit Train Schedule Station Persister
// @namespace https://www.wecsam.com/
// @version 0.1
// @description This script fills the origin and destination fields with the last values that you entered.
// @author You
// @match https://www.njtransit.com/sf/sf_servlet.srv?hdnPageAction=TrainTo
// @match https://www.njtransit.com/sf/sf_servlet.srv?hdnPageAction=TrainTo#
// @grant none
// ==/UserScript==
@wecsam
wecsam / simple_upload_server.py
Last active May 14, 2024 19:40
A simple Flask app for uploading files
#!/usr/bin/env python3
import flask
app = flask.Flask(__name__)
HTML_HOME = """<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
@wecsam
wecsam / archive_log.py
Last active February 4, 2019 02:08
This tool moves all but the last N log entries from the specified file to another file.
#!/usr/bin/env python3
import argparse, collections, os.path, sys
def move_lines(fin, fout, lines_to_leave, preserve_first_line):
# For CSV files, preserve the first line, which is the header row.
if preserve_first_line:
first_line = next(fin)
else:
first_line = ""
# If the output file is new, write the first line into it.
@wecsam
wecsam / password_sum_and_product.py
Last active June 25, 2020 01:50
This script finds a password of printable ASCII characters given a desired sum and product of the ASCII character codes in the password.
#!/usr/bin/env python3
# pip install z3-solver
import z3
class OrdAt:
'''
Z3 has no way of arbitrarily getting a character at an index from a string.
However, this problem requires us to get ASCII character codes from a
string. When an object of this class is called, the return value is a
BitVec with a number of bits that you specify; the 8 least significant bits
@wecsam
wecsam / filetype.py
Created December 3, 2018 01:29
This class is like argparse.FileType, except that it supports all keyword arguments for open() and returns a function that opens a handle rather than returning the handle itself.
#!/usr/bin/env python3
import argparse, gettext, sys
class FileType(argparse.FileType):
'''
This class is just like argparse.FileType, except that it supports all
keyword arguments for open() and returns a function that opens a handle
rather than returning the handle itself. This means that the file is not
actually opened until that function is called. This also means that the
result can be used with a "with" block.
@wecsam
wecsam / prevent_parallel.py
Created September 11, 2018 04:49
Prevents concurrent instances of a Python script
#!/usr/bin/env python3
'''
Import this module to ensure that at most one instance of the main script can
be running at once. If another instance is detected, sys.exit() will be called.
The psutil package is required: pip install psutil
'''
import atexit, logging, math, os, psutil, socket, sys
logger = logging.getLogger("prevent_parallel")
PID_FILE = \