Skip to content

Instantly share code, notes, and snippets.

@vadimkantorov
vadimkantorov / csharpfrompython.cs
Last active December 31, 2025 14:59
Run a C# function from Python using Python.NET from https://pythonnet.github.io/pythonnet/python.html Call csharpfrompython.py which first calls the compiler and then calls the functions, feature request of better compiler bindings at https://github.com/pythonnet/pythonnet/issues/2196 ; includes two examples of NumPy -> C# array marshalling
namespace CSharpFromPythonNamespace
{
public class CSharpFromPythonClass
{
public string Hi(string x)
{
return "Hi " + x;
}
public static string Hello(string x)
@vadimkantorov
vadimkantorov / log_file_access_dynamic.c
Last active November 15, 2025 21:19
Trace certain libc and stdio file access function calls / syscalls via LD_PRELOAD-based interception
// gcc -shared -fPIC log_file_access_dynamic.c -o log_file_access_dynamic.so -ldl; LD_PRELOAD=$PWD/log_file_access_dynamic.so /usr/bin/cat log_file_access_dynamic.c
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <dlfcn.h>
#include <sys/stat.h>
@vadimkantorov
vadimkantorov / gdb.sh
Last active September 25, 2025 23:06
A Bash alias to run a Python script under gdb
# Usage:
# gdbpython script.py my_script_arguments
# gdbbacktrace ./my_program arg1 arg2
alias gdbpython="gdb -ex run --args python"
alias gdbbacktrace="gdb -batch -ex run -ex bt --args"
alias gdbbacktracequit="gdb -ex 'set pagination off' -ex 'set confirm off' -iex 'set debuginfod enabled on' -ex run -ex 'thread apply all bt' -ex quit --args"
@vadimkantorov
vadimkantorov / ps.sh
Last active September 13, 2025 01:17
Various ps commands
alias cmdline='ps --no-headers -o comm,args -p'
cmdline 1
# can produce JSON, but without process args
alias notbrokenjson='ps --no-headers -o '\''{"pcpu": %C, "group": "%G", "PPID": %P, "user": "%U", "comm": "%c", "rgroup": "%g", "nice": "%n", "pid": %p, "pgid": %r, "etime": "%t", "ruser": "%u", "time": "%x", "tty": "%y", "vsz": %z}'\'' -p'
notbrokenjson 1 > notbroken.json
# does not remove escape sequences or quotes in args - this will break JSON format for some processes
alias brokenjson='ps --no-headers -o '\''{"pcpu": %C, "group": "%G", "PPID": %P, "user": "%U", "args": "%a", "comm": "%c", "rgroup": "%g", "nice": "%n", "pid": %p, "pgid": %r, "etime": "%t", "ruser": "%u", "time": "%x", "tty": "%y", "vsz": %z}'\'' -p'
brokenjson 1 > brokenjson.json
@vadimkantorov
vadimkantorov / pyproject.toml
Last active September 4, 2025 05:25
Install and pin nighly vllm using pyproject.toml and uv
# git clone https://gist.github.com/vadimkantorov/fe63f8628ff6cad460e934e1d7ed650b
# cd fe63f8628ff6cad460e934e1d7ed650b
# uv venv
# uv sync
# https://github.com/vllm-project/vllm/pull/20358#issuecomment-3247178818
# https://github.com/vllm-project/vllm/issues/9244
# https://github.com/astral-sh/uv/issues/8082
# https://github.com/vllm-project/vllm/issues/24126
@vadimkantorov
vadimkantorov / logging_jsonl.py
Last active August 29, 2025 21:57
Logging as jsonl using vanilla Python logging
# https://stackoverflow.com/questions/71944328/how-to-implement-json-format-logs-in-python
import json
import logging
# https://docs.python.org/3/library/logging.html#logrecord-attributes
formatter = type('JsonFormatter', (logging.Formatter, ), dict(format = lambda self, record: json.dumps(dict(time = self.formatTime(record), level = record.levelname, message = record.getMessage(), module = record.module, lineno = record.lineno)) ))()
# simpler version below does not escape quotes in message and does not delete newlines in message
# formatter = logging.Formatter('{\"time\": \"%(asctime)-s\", \"level\": \"%(levelname)-s\", \"message\": \"%(message)s\", \"module\": \"%(module)s\", \"lineno\": %(lineno)d}')
@vadimkantorov
vadimkantorov / rdv_naturalisation.sh
Last active August 26, 2025 16:06
A Bash script to monitor naturalisation rendez-vous availability at Prefectures Bobigny, Nanterre. Consider renting one or several EC2 micro instances at Paris AWS data center to avoid bans.
# Usage:
# 1. Put in your email/SendGrid credentials or phone/Twilio credentials
# 2. Run as: "bash rdv_naturalisation.sh nanterre" or "bash rdv_naturalisation.sh bobigny" or "bash rdv_naturalisation.sh cre"
# 3. Get your RDV! The script will dump the HTML of the page and will continue execution even after the first RDV is found.
SENDGRID_BEARER_TOKEN='' # should start with "SG."
SENDGRID_EMAIL='' # put your e-mail here
TWILIO_SID='' # should start with "AC"
TWILIO_AUTH_TOKEN=''
TWILIO_FROM_NUMBER='' # put your Twilio Trial Phone Number here, should start with +
echo '{"site": {"hello": "world"}}' > foo.json
echo 'Hello {{ site.hello }}' > foo.txt.j2
bash j2.sh -t foo.txt.j2 -i foo.json -o foo.txt
cat foo.txt
source function.sh
j2 -t foo.txt.j2 -i foo.json -o foo.txt
cat foo.txt
unset -f j2
@vadimkantorov
vadimkantorov / yaml_loads.py
Last active August 12, 2025 10:51
Simple string-valued parser for YAML supporting
# supports only strings, dicts and lists
# does not support multiline strings as the first list-item key `- run: |`
# does not preserve whitespaces in " |" literal string blocks as described in : https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
def yaml_loads(content, convert_bool = True, convert_int = True, convert_dict = True, convert_none = True):
def procval(val):
read_until = lambda tail, chars: ([(tail[:i], tail[i+1:]) for i, c in enumerate(tail) if c in chars] or [(tail, '')])[0]
val = val.strip()
is_quoted_string = len(val) >= 2 and ((val[0] == val[-1] == '"') or (val[0] == val[-1] == "'"))
@vadimkantorov
vadimkantorov / base64_torch.py
Last active August 10, 2025 18:55
Base64 decoding in PyTorch
# https://en.wikipedia.org/wiki/Base64
# 00123456 00ABCDEF 00abcdef 00uvwxyz
# 123456AB CDEFabcd efuvwxyz
# this code does not support batches. adapting for e.g. concatenated varlen format is possible, but need to handle/preserve varlen information and paddings in some way
import torch
def base64_encode_padded(input_as_uint8_tensor):
base64_alphabet, base64_pad = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', '='
device = input_as_uint8_tensor.device