Skip to content

Instantly share code, notes, and snippets.

@moreati
moreati / blob2tar
Created January 28, 2023 10:29
Sperate a tarball into blobs and headers, then recombine to recreate the original tarball
#!/usr/bin/env python3
'''
Construct a tarball from a blob summary
'''
import argparse
import tarfile
import ruamel.yaml
@moreati
moreati / tar_sum.py
Last active January 23, 2023 21:01
List sha256 digests and names in a .tar piped to stdin
#!/usr/bin/env/python3
import hashlib
import sys
import tarfile
with tarfile.open(fileobj=sys.stdin.buffer, mode='r|') as tf:
for member in tf:
if member.isfile():
with tf.extractfile(member) as mf:
@moreati
moreati / scandirat.c
Created September 14, 2022 18:25
Example calling scandirat() in glibc
#define _DEFAULT_SOURCE
#define _GNU_SOURCE
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Compile with
def corange(end):
i = 0
while i < end:
v = yield i
if isinstance(v, int):
end = v
i += 1
for i in (r := corange(10)):
print(i)
@moreati
moreati / cm.py
Created July 30, 2022 09:01
Combined Python context manager, iterator, and iterable; with a generator-like send() method
EVENS = object()
STOP = object()
class CM():
def __init__(self, n=10):
print('__init__() called')
self.n = n
self._it = iter(range(n))
self._evens = False
@moreati
moreati / fudge_datetime.py
Last active March 5, 2022 11:12
Hacky wrapper for dateparser.parse() that handles christmas and new year
import datetime
import re
import dateparser
def fudge_datetime(s: str, now: datetime.datetime = None) -> datetime.datetime:
"""
Return a best guess datetime for the free-form string s, relative to now.
>>> fudge_datetime("Christmas Day 2020")
from collections import namedtuple
from dataclasses import dataclass
from typing import NamedTuple
import pyperf
@dataclass
class DataclassPet:
legs: int
noise: str
@moreati
moreati / timedelta_format.py
Created February 26, 2022 09:45
Prototype of datetime.timedelta.__format__() method
import datetime
import re
class timedelta(datetime.timedelta):
def __format__(value, format_spec):
if not format_spec:
return str(value)
pattern = re.compile(r'%([WdDhHmMsSuUT%])')
@moreati
moreati / lockprocs.py
Last active November 25, 2021 22:29
Demonstration of Lock instantiation spawning a subprocess
#!/usr/bin/env python3
import multiprocessing
import pprint
import sys
import psutil
print("Before", flush=True)
pprint.pprint([(p.pid, p.status(), ' '.join(p.cmdline())) for p in psutil.Process().children()])
import collections
class S(collections.UserList):
def __mul__(self, other):
if isinstance(other, S):
return S([(a, b) for a in self for b in other])
return super().__mul__(other)
def __add__(self, other):
if isinstance(other, S):