Skip to content

Instantly share code, notes, and snippets.

@FMeinicke
FMeinicke / md5sum-oneline.py
Created November 11, 2025 10:45
A quick and dirty way to generate the MD5 hash of a file.
import sys, hashlib; from pathlib import Path; print(f'{sys.argv[1]}: {hashlib.md5(Path(sys.argv[1]).read_bytes()).hexdigest()}')
@FMeinicke
FMeinicke / async_partial.py
Created November 19, 2024 12:50
An implementation of `functools.partial()` for async functions.
async def async_partial(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Callable[..., Any]:
"""
Partially apply the given `args` and `kwargs` to the given asynchronous function `func`.
Parameters
----------
func : callable
The asynchronous function to partially apply the arguments to
args : Any
The positional arguments to apply
@FMeinicke
FMeinicke / decorators.py
Created July 4, 2024 09:32
Compilation of different methods to create function decorators with and without parameters
from __future__ import annotations
from functools import wraps
from typing import overload
def func_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
print(f"func_decorator: Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
@FMeinicke
FMeinicke / invert_dict.py
Last active June 27, 2024 10:13
A simple helper function to invert the keys and values of a Python `dict`
from __future__ import annotations
from typing import Dict, TypeVar, Callable
_K = TypeVar("_K")
_V = TypeVar("_V")
_KV = TypeVar("_KV")
def invert_dict(d: Dict[_K, _V], *, value_to_key: Callable[[_V], _KV] = lambda v: v) -> Dict[_V, _KV]:
@FMeinicke
FMeinicke / filter.cpp
Created June 10, 2024 08:30
A C++17 compatible `filter` function that uses the erase-remove idiom
#include <algorithm>
#include <functional>
/**
* @brief Filters the given container @a cont using the given predicate @a pred
* such that only elements that satisfy the predicate remain in the container. The
* container is modified in place.
*
* @tparam Cont The container type
* @param cont The container to filter
@FMeinicke
FMeinicke / __init__.py
Created May 16, 2024 14:12
Python typer CLI application template
import sys
if sys.version_info.minor == 7:
import importlib_metadata as metadata
else:
from importlib import metadata
__version__ = metadata.version(__name__)
@FMeinicke
FMeinicke / template.sh
Last active February 6, 2025 13:02
Bash script template
#!/bin/bash
#
# My bash script template
#
# inspired by the best practises found at
# https://sharats.me/posts/shell-script-best-practices/ and
# https://bertvv.github.io/cheat-sheets/Bash.html
here="${0%/*}"
self="${0##*/}"
@FMeinicke
FMeinicke / asyncio_runner.py
Created February 12, 2024 08:58
A class that can be used to run coroutines in an asyncio event loop without having to `await` them.
from __future__ import annotations
import asyncio
from typing import Coroutine, Set
class AsyncioRunner:
"""
This class can be used to run coroutines in an asyncio event loop without having to `await` them.
@FMeinicke
FMeinicke / hex.cmake
Created May 31, 2023 13:06 — forked from korzo89/hex.cmake
CMake dec<->hex conversion functions
function(from_hex HEX DEC)
string(SUBSTRING "${HEX}" 2 -1 HEX)
string(TOUPPER "${HEX}" HEX)
set(_res 0)
string(LENGTH "${HEX}" _strlen)
while(_strlen GREATER 0)
math(EXPR _res "${_res} * 16")
string(SUBSTRING "${HEX}" 0 1 NIBBLE)
string(SUBSTRING "${HEX}" 1 -1 HEX)
@FMeinicke
FMeinicke / qobjectwrapper.cpp
Created May 26, 2023 10:31
QObjectWrapper - a quick way to wrap any class and make it inherit from `QObject`
template<typename T>
class QObjectWrapper : public T, public QObject
{
Q_OBJECT
public:
explicit QObjectWrapper(QObject* parent = nullptr) : T{}, QObject{parent} {}
};