Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Willmer moreati

View GitHub Profile
@moreati
moreati / lockprocs.py
Last active Nov 25, 2021
Demonstration of Lock instantiation spawning a subprocess
View lockprocs.py
#!/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()])
View cartesian_producer.py
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):
@moreati
moreati / escaped_string.py
Last active Oct 14, 2021
Trying regular epxressions purported to match double quoted strings with backslash escapes
View escaped_string.py
#!/usr/bin/env python3
"""
Trying regular epxressions purported to match double quoted strings with backslash escapes
Syntax changes made vs sources
- Changed capture groups to non-capture groups () -> (?:)
- Removed escaping of double quotes \" -> "
"""
import re
@moreati
moreati / braces.lark
Last active Oct 11, 2021
Approximation of the grammar for Bash braces expressions
View braces.lark
start : brace+
brace : [preamble] expandable [postscript]
preamble : /[^{}]+/
postscript : /[^{}]+/
expandable : "{" (range | list) "}"
range : begin ".." end [".." step]
begin : BOUND
@moreati
moreati / regex_define.py
Created Oct 10, 2021
Demonstration of regular expression (?(DEFINE) ...) for declaring resuable sub-patterns
View regex_define.py
#!/usr/bin/env python3
import regex
PATTERN = regex.compile(r'''
# Declare reusable sub-patterns
(?(DEFINE)
(?<COUNT>[0-9]+)
(?<CURRENCY>EUR|GBP|USD)
(?<QUANTITY>[0-9]+[.][0-9]+)
@moreati
moreati / flag_last.py
Created Oct 4, 2021
Python generator that flags the last item in an iterable
View flag_last.py
def flag_last(iterable):
"""
Yield (is_last, item) for each item in iterable, is_last is True on the
last item, False otherwise.
>>> list(flag_last(range(0)))
[]
>>> list(flag_last(range(1)))
[(True, 0)]
>>> list(flag_last(range(3)))
@moreati
moreati / walk.py
Last active Sep 24, 2021
Prototyping an `os.walk()` alternative
View walk.py
import os
import shutil
import string
import timeit
SKIP = True
class FakeDirEntry:
@moreati
moreati / copy_atomic.py
Last active Sep 21, 2021
ETXTBSY demonstration
View copy_atomic.py
def copy_atomic(src, dst, follow_symlinks=True):
"""
Copy src file to dst atomically, via a temporary file that is renamed.
It is intended to be a drop in replacement for `shutil.copy2()`.
"""
if os.path.isdir(dst):
dst_dir = dst
dst_name = os.path.basename(src)
dst = os.path.join(dst, dst_name)
@moreati
moreati / fastapi_app.py
Last active Jun 22, 2021
How do Python webframeworks handle URL-encoded / in the path?
View fastapi_app.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"greeting": "Hello"}
@moreati
moreati / systemd_journal_demo.py
Created Jun 15, 2021
Demonstration of logging to systemd journal with offcial Python bindings, and cysystemd
View systemd_journal_demo.py
#!/usr/bin/env python3
import logging
log = logging.getLogger('systemd_demo')
log.setLevel(logging.DEBUG)
# Official bindings
import systemd.journal