Skip to content

Instantly share code, notes, and snippets.

@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 <[email protected]>
# 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
@moreati
moreati / stringio_2x_demo.py
Created September 24, 2024 11:59
Python 2.x StringIO.StringIO vs io.StringIO
#1/usr/bin/env python2
import StringIO
f1 = StringIO.StringIO()
f1.write(u'unicode string')
f1.write(b'byte string')
f1.close()
import io
@moreati
moreati / ansible_playbook_keywords_table.rst
Last active September 15, 2024 12:13
Table showing Ansible playbook keywords that can be used at play, role, block, or task level

Generated using development version of Ansible on 15 Sep 2024. Details may differ from released versions.

@moreati
moreati / issue905.ini
Created September 5, 2024 12:18
Reproducer for Mitogen issue 905
[issue905]
ssh-args ansible_ssh_args="-o PermitLocalCommand=yes -o LocalCommand='touch {{ ssh_args_canary_file }}'"
ssh-common-args ansible_ssh_common_args="-o PermitLocalCommand=yes -o LocalCommand='touch {{ ssh_args_canary_file }}'"
ssh-extra-args ansible_ssh_extra_args="-o PermitLocalCommand=yes -o LocalCommand='touch {{ ssh_args_canary_file }}'"
[issue905:vars]
ansible_host=u2204.mynet
ssh_args_canary_file=/tmp/ssh_args_{{ inventory_hostname }}
@moreati
moreati / popen_resourcewarning.py
Last active August 28, 2024 22:07
Demonstration of Python subprocess.Popen() ResourceWarning
import os
import subprocess
import sys
import warnings
# By default ResourceWarning output (and other) is hidden.
# Setting environment variable PYTHONWARNINGS=default will
# show them, similar to the below.
if not sys.warnoptions:
warnings.simplefilter('default')
@moreati
moreati / tree_layers.py
Created May 31, 2024 15:50
Calculate required layers of a tree for given number of leaf nodes
import math
def tree_layers_count(leaf_nodes_count: int, degree: int) -> int:
r"""
Return the number of layers required for a given number of leaf nodes,
if each node can have degree children & nodes are filled depth first.
E.g. to have 5 leaf nodes, a degree 4 tree requires 3 layers
*
_______________ _|_ _