Skip to content

Instantly share code, notes, and snippets.

# /// script
# requires-python = ">=3.10"
# ///
def oxford_comma(x: list[str]) -> str:
'''
>>> oxford_comma([])
''
>>> oxford_comma(['foo'])
'foo'
@moreati
moreati / foo.py
Created November 28, 2025 10:04
Ansible versions vs INTERPRETER_PYTHON_FALLBACK using Tox
import ansible
import ansible.constants
print(f'{ansible.__version__}: {ansible.constants.INTERPRETER_PYTHON_FALLBACK}')
@moreati
moreati / xz.ksy
Created September 9, 2025 08:36
WIP: Kaitai Struct definition for .xz file format
# This definition is incomplete, several fiels are hardcoded.
# See https://github.com/kaitai-io/kaitai_struct_formats/issues/719
meta:
id: xz
title: XZ compressed file
file-extension: xz
xref:
forensicswiki: xz
justsolve: XZ
@moreati
moreati / demo.md
Created September 5, 2025 11:07
Multiple members are allowed in a gzip file

A gzip (.gz) file typically contains a single member, but as per RFC 1952 it can contain many members.

2.2. File format

A gzip file consists of a series of "members" (compressed data sets)

GNU gzip and Apple gzip both appear to decompress multiple members to a single output file.

Demo

  1. Create some test files
@moreati
moreati / integer_id.py
Created July 26, 2025 17:23
A minimal integer ID type that intentionally doesn't support mathematical operations
# A minimalist integer ID type that intentionally doesn't support mathematical
# operations. Subclass it to make e.g. UserID type, FileDescriptor type
class ID(object):
__slots__ = ('_v',)
def __init__(self, v):
if not isinstance(v, int):
raise TypeError('int required, got: %s"' % type(v))
if v < 0:
raise ValueError('cannot be negative', v)
@moreati
moreati / yield_from_seq.py
Created May 26, 2025 09:26
Python's "yield from" cna be used with a sequence
TODAY = [
'Haircut',
'Buy milk',
]
TONIGHT = [
'Try to take over the world!',
]
@moreati
moreati / stdio_closed.py
Created May 15, 2025 11:27
Python subprocesses with closed stdio
import os
import subprocess
import sys
PRINT_STDERR_REPR_PY = 'import sys; print(f" {sys.stderr=}\\n")'
print('Child stderr -> our stderr')
subprocess.run([sys.executable, '-c', PRINT_STDERR_REPR_PY])
print('Child stderr -> /dev/null')
@moreati
moreati / CentOS-Base.repo
Created February 24, 2025 11:26
Plain http proxy for https://vault.centos.org using Apache httpd
[base]
name=CentOS-$releasever - Base
baseurl=http://centos-vault-proxy:8090/5.11/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5
[updates]
name=CentOS-$releasever - Updates
baseurl=http://centos-vault-proxy:8090/5.11/updates/$basearch/
gpgcheck=1
@moreati
moreati / connection_timeout.yml
Created December 16, 2024 10:16
Test ansible PR 84240 wrt connection timeout option
- hosts: d12.mynet
connection: ssh_noisy_get_option
gather_facts: false
vars:
ansible_python_interpreter: python3
tasks:
- meta: reset_connection
- ping:
@moreati
moreati / tracerealpath.py
Created September 25, 2024 18:53
tracerealpath - Like traceroute, but for symlinks
#!/usr/bin/env python3
# Copyright (c) 2001 - 2023 Python Software Foundation
# Copyright (c) 2024 Alex Willmer <alex@moreati.org.uk>
# License: Python Software Foundation License Version 2
# Derived from https://github.com/python/cpython/blob/v3.12.6/Lib/posixpath.py#L423-L492
"""
Show the route taken through symlinks to a resolved file path