Skip to content

Instantly share code, notes, and snippets.

View wecsam's full-sized avatar

David Tsai wecsam

View GitHub Profile
@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 = \
@wecsam
wecsam / AddAttachmentsToSelection.vba
Created July 12, 2018 18:16
Attach selected files to selected Outlook items
' This macro is for Microsoft Outlook. It opens a file picker dialog and then
' attaches the selected files to the selected items.
' This macro requires the object library for the installed version of Microsoft
' Word. In the VBA editor, go to Tools > References... and make sure that it is
' checked off. For Word 2016, choose the Microsoft Word 16.0 Object Library.
Sub AddAttachmentsToSelection()
Dim objMessage As Variant
Dim strPath As Variant
' Outlook does not have a file picker. Use an instance of Word instead.
Dim appWord As Word.Application
@wecsam
wecsam / orifice.py
Last active April 28, 2018 01:01
Like cat but with rate limiting
#!/usr/bin/env python3
import collections, os.path, queue, sys, textwrap, threading, time
UNITS = {
"char": lambda fin: fin.read(1),
"line": lambda fin: fin.readline(),
}
def producer(backlog, stop, unit_getter, fin):
'''
Continually does backlog.put(unit_getter(fin)). If KeyboardInterrupt