Skip to content

Instantly share code, notes, and snippets.

View James-E-A's full-sized avatar

James E. A. James-E-A

View GitHub Profile
@James-E-A
James-E-A / async_currentScript_shim.html
Last active July 30, 2025 19:58
Javascript currentScript in async script
<script>(async (currentScript) => { // scoped to this IIFE, does not pollute global namespace or vanish on future event loop ticks
// You are 100% free to do async stuff in this block
await new Promise(window.requestAnimationFrame);
window.alert(currentScript);
// ... //
})(document.currentScript).then((value) => {if (value !== undefined) console.debug(value);}, (error) => {console.error(error); debugger;});</script>
@James-E-A
James-E-A / galois_arithmetic.mjs
Last active July 21, 2025 14:20
Galois fields of non-prime order in Javascript
/**
* A nonnegative integer.
* @typedef {number} NaturalNumber
*/
/**
* Datatype representing a polynomial as an array of coefficients.
* e.g. "5X^3 + X + 2" is represented by [2, 1, , 5].
* e.g. "0" is represented by [].
* Must have no trailing zeroes. Array slots containing 0 are equivalent to empty (sparse) array slots.
@James-E-A
James-E-A / gregorianDateInteger.mjs
Last active June 24, 2025 17:32
Javascript get number of days since start of Gregorian calendar
/*
* Calculate the number of days since the start of the Gregorian calendar.
* @param {Date} [date] - The reference date. Defaults to the current instant.
* @param {boolean} [useLocalTime] - Whether to calculate in local time. WARNING: By default, UTC is used; this will, for instance, give a Tuesday result when run at 10pm on a Monday in the United States.
* @returns {number}
*/
export function gregorianDateInteger(d, useLocalTime) {
if (d === undefined) d = new Date();
if (useLocalTime === undefined) useLocalTime = false;
@James-E-A
James-E-A / thread_start_immed.py
Last active August 14, 2025 18:47
Python run a block of code in a background thread immediately
"""Technically, you don't need this library. You *could* just DIY it with a clever pile of `lambda` decorators:
>>> import threading, functools
>>> @lambda t: t.start() or t
... @lambda f: threading.Thread(target=f, daemon=True)
... def worker_1():
... import time
... time.sleep(1)
... print(threading.current_thread())
@James-E-A
James-E-A / csv_iterate.mjs
Last active June 24, 2025 16:32
Javascript parse CSV
const END_OF_FIELD = Symbol('\u001f');
const END_OF_RECORD = Symbol('\u001e');
export async function* csv_iterate(text_stream) {
// public iterator.
if (typeof text_stream === 'string')
text_stream = [text_stream];
@James-E-A
James-E-A / logical_xor.py
Last active April 7, 2025 16:15
Python boolean XOR
#!/usr/bin/env python3
__all__ = ['logical_xor']
def logical_xor(a, b):
# technically, all of the parentheses on the following line may be removed
return ((not b) and a) or ((not a) and b)
# correct
assert logical_xor(True, False)
@James-E-A
James-E-A / is_dir_writable.py
Last active March 6, 2025 15:41
python check directory writable on Windows
def is_dir_writable(path='.', *, _q=None):
import multiprocessing as mp
from queue import Empty
import tempfile
if _q is None:
# caller
_q = mp.Queue()
p = mp.Process(
target=is_dir_writable, # IF YOU RENAME THE FUNCTION ENSURE YOU ALSO RENAME ITS INVOCATION HERE
args=[path],
@James-E-A
James-E-A / wintypes_guid.py
Last active April 23, 2025 15:58
Python wintypes missing GUID structure
import ctypes
from ctypes import GetLastError, WinError, byref, create_unicode_buffer
from uuid import UUID
__all__ = ['wintypes_GUID']
_CLSIDFromString = ctypes.windll.Ole32['CLSIDFromString']
_CLSIDFromString.argtypes = [ctypes.c_wchar_p, ctypes.c_void_p]
_CO_E_CLASSSTRING = ctypes.c_int(0x800401F3).value # nominally positive, but physically negative on most platforms
@James-E-A
James-E-A / demo_kem_trans.py
Created February 12, 2025 18:07
Python simple KEM-TRANS example
#!/usr/bin/env python3
from Crypto.Cipher import AES # https://pypi.org/project/pycryptodome/
from Crypto.Hash import SHAKE256
from types import SimpleNamespace
def _demo():
# DEMO
from pqc.kem import mceliece6960119f as kemalg # https://pypi.org/project/pypqc/
@James-E-A
James-E-A / util_web.fragment.ex
Last active January 17, 2025 16:42
Elixir parse HTML datetime form input values
defmodule FooWeb.Util do
# https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-local-date-and-time-string
@spec parse_web_time!(String.t()) :: NaiveDateTime.t()
@spec parse_web_time!(String.t(), Calendar.time_zone()) :: DateTime.t()
@spec parse_web_time!(String.t(), Calendar.time_zone(), Calendar.time_zone_database()) :: DateTime.t()
def parse_web_time!(<<s::binary-size(16)>>) do
Timex.parse!(s, "{YYYY}-{0M}-{0D}T{h24}:{m}")
end
def parse_web_time!(<<s::binary-size(19)>>) do
Timex.parse!(s, "{YYYY}-{0M}-{0D}T{h24}:{m}:{s}")