Skip to content

Instantly share code, notes, and snippets.

@moreati
moreati / escaped_string.py
Last active October 14, 2021 19:02
Trying regular epxressions purported to match double quoted strings with backslash escapes
#!/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 October 11, 2021 21:58
Approximation of the grammar for Bash braces expressions
start : brace+
brace : [preamble] expandable [postscript]
preamble : /[^{}]+/
postscript : /[^{}]+/
expandable : "{" (range | list) "}"
range : begin ".." end [".." step]
begin : BOUND
@moreati
moreati / regex_define.py
Created October 10, 2021 10:43
Demonstration of regular expression (?(DEFINE) ...) for declaring resuable sub-patterns
#!/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
Last active April 26, 2024 22:53
Python generator that flags the last item in an iterable
import collections
from typing import Generator, Iterable, TypeVar
T = TypeVar('T')
def flag_last(iterable:Iterable[T]) -> Generator[None, tuple[T, bool], None]:
"""
Yield (item, is_last) for each item in iterable, is_last is True on the
last item, False otherwise.
@moreati
moreati / walk.py
Last active July 29, 2022 20:22
Prototyping an `os.walk()` alternative
import os
import shutil
import string
import timeit
class FakeDirEntry:
def __init__(self, path):
self.path = path
self.name = path
@moreati
moreati / copy_atomic.py
Last active July 28, 2022 00:01
ETXTBSY demonstration
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 June 22, 2021 18:31
How do Python webframeworks handle URL-encoded / in the path?
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def root():
return {"greeting": "Hello"}
@moreati
moreati / systemd_journal_demo.py
Created June 15, 2021 20:44
Demonstration of logging to systemd journal with offcial Python bindings, and cysystemd
#!/usr/bin/env python3
import logging
log = logging.getLogger('systemd_demo')
log.setLevel(logging.DEBUG)
# Official bindings
import systemd.journal
#!/usr/bin/env python3
"""
Determine whether stderr is connected to systemd journal, and hence whether
structured events can be written to the journal.
Based on https://systemd.io/JOURNAL_NATIVE_PROTOCOL/#automatic-protocol-upgrading
"""
import os
import sys
#!/usr/bin/env ansible-playbook
---
- name: Test behaviour of version test
hosts: localhost
gather_facts: false
tasks:
- name: Expected results, comparing a.b.c <-> x.y.z, or a.b <-> x.y
debug:
msg:
- '"1.0" is version("1.0", "==") -> {{ "1.0" is version("1.0", "==") }}'