Skip to content

Instantly share code, notes, and snippets.

@kbaikov
kbaikov / syslog_parser_stream.py
Created October 27, 2023 14:40
Parse syslog message (stream), using Octet counting or Non-Transparent-Framing. See: https://datatracker.ietf.org/doc/html/rfc6587#page-7
"""Parse syslog message"""
import unittest
from io import BytesIO
from typing import Callable, TypeAlias, TypeVar
# Our tokens are bytes, so we use a memoryview as a stream of bytes.
Tokens: TypeAlias = BytesIO
# No error reporting, we just state that we failed.
@kbaikov
kbaikov / syslog_parser_bytes.py
Created October 27, 2023 14:38
Parse syslog message, using Octet counting or Non-Transparent-Framing. See: https://datatracker.ietf.org/doc/html/rfc6587#page-7
"""Parse syslog message"""
import unittest
from typing import Callable, TypeAlias, TypeVar
# Our tokens are bytes, so we use a memoryview as a stream of bytes.
Tokens: TypeAlias = memoryview
# No error reporting, we just state that we failed.
Failure = None
@kbaikov
kbaikov / script-template.sh
Created November 19, 2021 07:37 — forked from m-radzikowski/script-template.sh
Minimal safe Bash script template - see the article with full description: https://betterdev.blog/minimal-safe-bash-script-template/
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -p param_value arg1 [arg2...]
@kbaikov
kbaikov / fast file_byte_iterator.py
Created August 16, 2019 14:10
fast file_byte_iterator
# taken from https://stackoverflow.com/questions/1035340/reading-binary-file-and-looping-over-each-byte
from pathlib import Path
from functools import partial
from io import DEFAULT_BUFFER_SIZE
def file_byte_iterator(path):
"""given a path, return an iterator over the file
that lazily loads the file
"""