Skip to content

Instantly share code, notes, and snippets.

@moreati
moreati / fromisoformat.md
Created April 8, 2024 15:51
Python 3.11 datetime.datetime.fromisoformat()

In Python 3.11+ datetime.datetime.fromisoformat() accepts any number of decimal places for the seconds component. If there are more than 6 then the resulting datetime object is truncated (not rounded) to microsecond precision.

Python 3.11.8 (main, Feb  6 2024, 21:21:21) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
>>> import datetime
>>> datetime.datetime.fromisoformat('2011-11-04T00:05:23.12345Z')
datetime.datetime(2011, 11, 4, 0, 5, 23, 123450, tzinfo=datetime.timezone.utc)
>>> datetime.datetime.fromisoformat('2011-11-04T00:05:23.1234567Z')
datetime.datetime(2011, 11, 4, 0, 5, 23, 123456, tzinfo=datetime.timezone.utc)
@moreati
moreati / macos.nix
Created March 3, 2024 21:24
My first baby steps with Nix on a macOS machine
# To apply changes run
# nix-env --install --remove-all --file ~/macos.nix
# nix-env -irf ~/macos.nix
#
# Notes
# - Binaries are made available in ~/.nix-profile/bin/
#
# TODO
# - Install ~/.nix-profile/bin/{python,python-config}{3.8,3.9,3.10,3.11,3.12},
# but *not* .../{python{,3}
@moreati
moreati / issue115911_mp.py
Last active February 29, 2024 13:54
importlib PathFinder monkeypatch for Python issue 115911 (getcwd() EACCESS)
# Monkeypatch that replaces the importlib PathFinder in sys.meta_path
# with one that handles EACCES returned by libc getcwd() on macOS.
# Workaround for https://github.com/python/cpython/issues/115911
import errno
import sys
try:
import _frozen_importlib_external
except ImportError:
pass
@moreati
moreati / importlib_importer_poc.py
Created January 31, 2024 22:34
Minimal example driving Python importlib machinery to import a runtime created module
#!/usr/bin/env python3
import importlib.machinery
import sys
import types
class Finder:
def find_spec(self, fullname, path, target=None):
if fullname != 'poc_module':
@moreati
moreati / ansible-playbook-paths-auditted
Last active July 27, 2023 15:22
ansible-playbook wrapped with audit hooks for file path operations
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import collections
import os
import re
import sys
EVENTS_OF_INTEREST = frozenset({
'glob.glob',
'open',
@moreati
moreati / sudowrapper.py
Created July 6, 2023 19:29
Wrap an invocation of sudo, saving the input bytes including any passwords
#!/usr/bin/env python
"""
Wrap an invocation of sudo, saving the input bytes including any passwords.
Write a repr of the input bytes to stdout, following any output from the
sudo command invocation.
"""
import io
import os
import pty
@moreati
moreati / is_leap_bench.py
Created June 12, 2023 22:59
Python benchmarks of is_leap(year) variations
import datetime
import random
import pyperf
random.seed(42)
YEARS = [random.randint(-400, 399) for _ in range(65536)]
IS_LEAPS = dict(
b4_100_400 = lambda year: year % 4 == 0 and (year % 100 != 0 or year % 400 == 0),
@moreati
moreati / cal.py
Last active June 8, 2023 22:28
Year, month, day, hour, minute, and second packed into 64 bits
# 00000000 11111111 22222222 33333333 44444444 55555555 66666666 77777777
# YYYYYYYY YYYYYYYY YYYYYYYY YYYYYYYY YYYYYYMM MMDDDDDh hhhhmmmm mmssssss
class cal64:
__slots__ = ('_cal64')
_cal64: int
def __new__(cls, year:int, month:int, day:int, hour:int, minute:int, second:int):
assert 0 <= year <= 2**38-1
assert 1 <= month <= 12
@moreati
moreati / .ssh_config
Last active April 16, 2023 01:40
Playing Guess Who with Ansible
Host rpi1
HostName raspberrypi1.local
User pi
Host rpi2
HostName raspberrypi2.local
User pi
@moreati
moreati / ssh-3-way.sh
Last active March 2, 2023 19:20
Field expedient `scp -3 src:largefile dest:largefile` without scp
# `ssh -3 src.host.example:largefile dest.host.example:largefile` copies
# largefile from the source host to the destination, using the local machine
# as a relay. The file can be arbitrarily large. What if we don't have `scp`?
# We have
# - ssh connectivity from local machine to src.host.example
# - ssh connectivity from local machine to dest.dest.example
# - `ssh` client on local machine
# - `nc` command on remote machines
# - A large file called largefile on src.host.example