Javascript: calling asynchronously-initialized libraries from synchronous code

With the power of await, this isn't so difficult: let nacl, scrypt; function main() { /* Synchronous code that calls nacl and scrypt */ } (async () => { [nacl, scrypt] = await Promise.all([new Promise(nacl_factory.instantiate), new Promise(scrypt_module_factory)]); return main(); })();

Auto-updating userscripts hosted on Github Gist

In the process of crafting this small set of improvements to Advance Wars By Web, I found out about the proper URL for auto-updating userscripts which are hosted on Microsoft's "Gist" service: // @updateURL https://gist.githubusercontent.com/USERNAME/GIST_ID/raw/FILENAME.user.js This will always redirect to the latest version of the file.

Javascript: getElementByXPath

function getElementByXPath(path, context=null, _document=undefined) { if( _document === undefined ) _document = document; let res = document.evaluate(path, _document, context, XPathResult.FIRST_ORDERED_NODE_TYPE); if( !res ) return null; return res.singleNodeValue; } function getTextByXPath(path, context=null, _document=undefined) { let e = getElementByXPath(path, context, _document); if( !e ) return undefined; return e.textContent; } function getTextByQuerySelector(selector) { let e = document.querySelector(selector); if( !e ) return undefined; return e.textContent; }

Python: Using XOFs for general-purpose Random

As always, one's own stack overflow answers make the best blog posts. In this case, we craft a version of random.Random with a few modifications: Pulls its data from an arbitrary stream (in our case, a DRBG such as a hash function or deterministic CSPRNG) Is slightly more efficient at generating random integers Has fixed code for .shuffle, on the offchance CPython ever changes theirs, and to make it work …

Python: Rounding fractions half-up

By default, Python's fractions.Fraction rounds halves to the nearest even number. If you, instead, want to round a fraction but send halves up, here's how that's done: from math import floor from fractions import Fraction def roundhalfup(x: Fraction) -> int: """ Rounds x to the nearest integer, with ties being rounded towards positive infinity """ return floor(x + Fraction(1, 2)) def divroundhalfup(a: int, b: int) -> int: """ Returns the …

My WordPress custom CSS

Posted for administrative use as well as archive/reference /* Customizations */ .site-title a { text-decoration: none; } .entry-content aside { font-style: italic; display: inline; } .entry-content .language-python, .entry-content .language-javascript { tab-size: 4; -moz-tab-size: 4; } .entry-content .language-sql, .entry-content .language-css { tab-size: 2; -moz-tab-size: 2; } .entry-content.category-transcript a { color: revert; } body.page-id-1388 { background: url('https://timecube.2enp.com/grid.gif'); } body.page-id-1388 #canvas, body.page-id-1388 #page{ background: revert; } /* undo weird Suki design choices …

Javascript: Parsing query parameters

There are at least 2×5 things you could mean by "parsing" a query string in Javascript What data type do you want returned? Map Object How do you want duplicate keys handled? Just the first item Just the last item All the items, as a Set All the items, as an Array Throw an error As a quick exercise, here is a sampler of all those (if you're in …

Python: Nested in-line "for" statements

I'll explain with an example. Let's say that you've got some existing nested for-loop that you want to inline: for bytestring in x: for i in bytestring: for k in reversed(range(8)): yield ((i & (0b1 << k)) >> k) Intuitively, you might think that you would type something like "bit for k in bits for bits in in bytes for bytes in x" — but this is wrong. Instead, …

WordPress: Restrict MathJax to a class

I write on a variety of topics, only some of which it's appropriate to have mathematical typesetting for. To avoid triggering a math rendering engine on unrelated use of $spurious dollar signs$, while activating it when $\text{necessary}\wedge\text{appropriate}$, I decided to restrict them to only elements with .language-mathjax. Here's how that's done: MathJax = { tex: { inlineMath: [ ['$', '$'] ] }, startup: { elements: ['.language-mathjax'] } }; I …