Skip to content

Instantly share code, notes, and snippets.

I have had to understand Python packaging in depth on a number of occasions lately, both in my private development efforts and at my place of employment. For my own sake and simply to help others, I'd like to share what I consider valuable insight and information -- to save others time and effort, if mine was anything to judge by.

Python packaging has come further since people had to express everything with setup.py and more importantly without pyproject.toml. PEP 517 standardises how tools like Pip will execute building, in fact, and the standardisation basically makes it an entirely customizable process starting already with the so-called build backend. That's the value of build-backend in your pyproject.toml. A number of build backends have been developed now -- Poetry, Flit, Hatchling and others. Setuptools, a further development of disttools that used to ship with Python, has also been standardized to offer a build backend -- if the pyproject.toml specifies `build-backend = "setuptools

@amn
amn / README.md
Created October 25, 2024 08:26
A "reverse list" function for [GNU] Make

To reverse a list of words separated by white-space, the reverse can be called as a function with e.g. $(call reverse, a b c).

@amn
amn / vpath-resolve.mk
Created October 24, 2024 14:19
A decent "resolve like using VPATH" [GNU] Make expression
vpath-resolve = $(foreach path,$(1),$(or $(firstword $(wildcard $(path) $(addsuffix /$(path),$(patsubst %/,%,$(VPATH))))),$(error `$(path)` not found in any of `VPATH`.)))
@amn
amn / sorting.js
Last active October 24, 2024 17:26
An implementation of the well-known insertion sort algorithm with search space partitioning (aka binary space partitioning)
/**
* Sort a sequence using the binary insertion sort algorithm.
*
* The traditional insertion sort algorithm will look for a place to re-insert "current" element by simply walking towards the beginning of the sequence, through the already sorted portion. This implementation drastically reduces number of comparisons thus required to determine where in the [already sorted] portion of the sequence to re-insert current element, by using so-called binary space/search partitioning. This yields a worst-case `n * Math.log2(n)` performance vs traditionally `n * n`, all the more noticable for large enough sequence and/or where cost of sorting order comparison (see the `fn` parameter) is non-negligible.
*
* The sequence is sorted in-place, which is why this procedure does not return anything. Sorting is guarateed to be "stable" -- any two items for which `compare` does not indicate sorting (by returning zero), retain their relative positions in the sequence. Worst case performance is, unlike regular insertion sort
@amn
amn / toggle-windows-theme.ps1
Last active March 16, 2025 20:26
Calculate local sunset time and switch Windows theme accordingly
# Calculate local sunset time and set Windows theme to "dark" if it's past sunset time or "light" if it's before sunset time
# Author: Armen Michaeli <[email protected]>
# May be run on a schedule (e.g. every 15 minutes) or manually.
Add-Type -AssemblyName System.Device
$gcw = New-Object System.Device.Location.GeoCoordinateWatcher
$gcw.Start()
@amn
amn / MediaRecorderDataStream.js
Created September 23, 2021 19:17
A class of readable streams that vend data chunks generated by a media recorder
export default class MediaRecorderDataStream extends ReadableStream {
constructor(recorder) {
const signaler = new AbortController(), { signal } = signaler;
super({
start: controller => {
recorder.addEventListener("dataavailable", ev => {
controller.enqueue(ev.data);
}, { signal });
recorder.addEventListener("stop", ev => {
controller.close();
@amn
amn / uuid.js
Last active September 24, 2021 10:08
A [`crypto.getRandomValues` based,] "copy avoiding" UUID-4 generator for the Web
export const uuid4 = () => {
const ho = (n, p) => n.toString(16).padStart(p, 0); /// Return the hexadecimal text representation of number `n`, padded with zeroes to be of length `p`
crypto.getRandomValues(new Uint8Array(16)); /// Fill a data buffer with random data
data[6] = (data[6] & 0xf) | 0x40; /// Patch the 6th byte to reflect a version 4 UUID
data[8] = (data[8] & 0x3f) | 0x80; /// Patch the 8th byte to reflect a variant 1 UUID (version 4 UUIDs are)
const view = new DataView(data.buffer); /// Create a view on the data buffer
return `${ho(view.getUint32(0), 8)}-${ho(view.getUint16(4), 4)}-${ho(view.getUint16(6), 4)}-${ho(view.getUint16(8), 4)}-${ho(view.getUint32(10), 8)}${ho(view.getUint16(14), 4)}`; /// Compile the canonical representation of the data
};
@amn
amn / solarized.vim
Last active September 19, 2021 13:33
An alternative, "austere" implementation of the Solarized color scheme for Vim
" URL: https://gist.github.com/amn/f77f67df54ad4471f2fa12262d7addf5
hi clear
if exists("syntax_on")
syntax reset
endif
let g:colors_name = "Solarized"
@amn
amn / set-security-info-by-sddl.c
Last active March 15, 2019 09:19
Demonstrates how using SetFileSecurity does not result in a [file] ACL with ACEs inherited from parent [folder], while using SetNamedSecurityInfo does, as is proper. Disable (comment) the `SetNamedSecurityInfo` call along with its parent `if` statement and enable (uncomment) the following `SetFileSecurity` call (and its parent `if` statement, ob…
/*
Demonstrates how using SetFileSecurity does not result in a [file] ACL with ACEs inherited from parent [folder], while using SetNamedSecurityInfo does, as is proper.
Disable (comment) the `SetNamedSecurityInfo` call along with its parent `if` statement and enable (uncomment) the following `SetFileSecurity` call (and its parent `if` statement, obviously) to switch the behavior and observe different resultant ACL on the file.
The Windows application entry point in this snippet expects two command line arguments -- the file path of the file you want to set security information on, and the actual security (specified in SDDL format) information desired for the file.
*/
#include <windows.h>
#include <shellapi.h>
@amn
amn / IntersectionObserver.js
Created December 10, 2018 20:27
An implementation of IntersectionObserver class, after http://w3c.github.io/IntersectionObserver, with some deviations
/** An implementation of the IntersectionObserver class, as specified by http://w3c.github.io/IntersectionObserver. There are some open issues with the later specification, but an attempt is made here to follow it to the best of ability, unless it is found too difficult to interpret -- the motivation is, after all, to have a working implementation, and as certain elements of specification weren't clarifying in themselves, these parts of implementation may turn out to be lacking or even incorrect.
This was initially written to plug in in user agents that do not provide their own implementation, like Safari on iOS 9.3.5. Hence, the used syntax flavour is such as to work in said user agent. */
IntersectionObserver = (function() {
/** Find intersection product of two rectangles `a` and `b`, itself a rectangle. Essentially, this function is the logical AND operation for rectangles. A rectangle with zero width and/or height is returned if `a` and `b` are adjacent but do not overlap in that axis. No rectangle is re