Skip to content

Instantly share code, notes, and snippets.

@kunalb
kunalb / mpq.py
Last active May 5, 2024 19:15
Values put into a multiprocessing queue are not safe
import multiprocessing as mp
def target(q):
while val := q.get():
print(len(val))
if __name__ == "__main__":
mp.set_start_method('forkserver')
@kunalb
kunalb / pane.sh
Last active April 30, 2024 22:48
zsh + tmux + fzf + rg - autocomplete words in command
function _print_all_panes() {
for pane_id in $(tmux list-panes -F '#{pane_id}'); do
tmux capture-pane -p -J -S 0 -E - -t "$pane_id" | tr ' ' '\n' | sort -u | rg '[a-zA-Z0-9]+'
done
}
_tmux_pane_words() {
local current_word="${LBUFFER##* }"
local new_rbuffer="${RBUFFER/#[^ ]##/}"
local prompt="${LBUFFER% *} ␣ $new_rbuffer "
_tmux_pane_words() {
local current_word="${${=LBUFFER}[-1]}"
local prompt="${LBUFFER% *} ␣ $RBUFFER "
local selected_word=$(tmux capture-pane -p -J -S 0 -E - | tr ' ' '\n' | sort -u | rg '[a-zA-Z0-9]+' | fzf --select-1 --exit-0 --query="$current_word" --prompt="$prompt" --height=20 --layout=reverse)
if [[ -n $selected_word ]]; then
BUFFER="${LBUFFER% *} $selected_word$RBUFFER"
fi
zle redisplay
@kunalb
kunalb / args_tables.sh
Created October 19, 2023 00:49
Testing out argument handling
#!/bin/bash
set -u
suffixes=( "-" ":-" "+" ":+" "+x" ":+x" )
function header {
printf "%10s | " "\$#"
printf "%10s | " "\$*"
for predicate in "-n" "-z"; do
for suffix in "${suffixes[@]}"; do
@kunalb
kunalb / capture.py
Created September 9, 2023 17:28
Capture stderr
import ctypes
import os
import threading
class StderrCapture:
def __init__(self, f):
self.libc = ctypes.CDLL('libc.so.6')
self.memfd_create = self.libc.memfd_create
self.memfd_create.argtypes = [ctypes.c_char_p, ctypes.c_uint]
self.memfd_create.restype = ctypes.c_int
--- /etc/systemd/logind.conf 2022-03-26 11:58:23.739031451 -0400
+++ /etc/systemd/logind.conf.dpkg-new 2022-09-09 14:47:16.000000000 -0400
@@ -1,13 +1,16 @@
# This file is part of systemd.
#
-# systemd is free software; you can redistribute it and/or modify it
-# under the terms of the GNU Lesser General Public License as published by
-# the Free Software Foundation; either version 2.1 of the License, or
-# (at your option) any later version.
+# systemd is free software; you can redistribute it and/or modify it under the
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kunalb
kunalb / Terrorist Scanner 2.ipynb
Created May 9, 2021 14:29
BRML Terrorist Scanner Exercise
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kunalb
kunalb / Terrorist Scanner 2.ipynb
Created May 9, 2021 14:26
BRML Exercise: Terrorist Scanner
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@kunalb
kunalb / Terrorist Scanner.ipynb
Created May 9, 2021 09:47
BRML Terrorist Scanner
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.