Skip to content

Instantly share code, notes, and snippets.

@iAmGroute
iAmGroute / leak.py
Created March 24, 2024 11:23
How to leak memory in Python
# A simple test showing possible memory leaks in Python
# import gc
import time
import resource
counter = 0
def get_new_id():
global counter
@iAmGroute
iAmGroute / scatter.py
Last active March 24, 2024 11:31
scatter: easily parallelize shell commands and pipelines
#!/usr/bin/python3
'''
## What is this?
A simple way of parallelizing commands (including pipelines) in a shell, while preserving output order.
For example, we can re-write:
```
for i in {1..5}; do
@iAmGroute
iAmGroute / .xbindkeysrc.scm
Created October 21, 2021 07:08
XbindKey guile config for volume controls with extra mouse button (button 8, normally 'back')
;; This configuration is guile based.
;; https://www.gnu.org/software/guile/learn/
;; Based on the awesome script created by Zero Angel:
;; https://www.linuxquestions.org/questions/linux-desktop-74/%5Bxbindkeys%5D-advanced-mouse-binds-4175428297/
;; We'll need a named pipe later, because we can't share FDs
(if (not (file-exists? ".xbindkey.pipe"))
(run-command "mkfifo .xbindkey.pipe")
(sleep 1)
)
@iAmGroute
iAmGroute / rfind_file.py
Last active December 20, 2020 09:48
Find the last occurrence of given character in file
import os
import sys
def rfind_file(filename, value, blocksize, maxsize=0):
fd = os.open(filename, os.O_RDONLY)
try:
size = os.lseek(fd, 0, os.SEEK_END)
if maxsize < 0:
size = max(size - maxsize, 0)
elif maxsize > 0:
@iAmGroute
iAmGroute / sparse_clz.py
Last active December 13, 2020 11:40
Count Leading Zeros (CLZ) for sparse files
import os
import sys
def count_continuous(data, value):
for i in range(len(data)):
if data[i] != value: break
return i
def sparse_clz(filename, blocksize):
fd = os.open(filename, os.O_RDONLY)
@iAmGroute
iAmGroute / greet
Last active March 20, 2020 19:11
A simple C server program that reads from stdin and sends to all connected clients
(doesn't seem to work for h264)
HTTP/1.1 206
access-control-allow-origin: *
connection: keep-alive
content-type: video/h264
content-range: bytes 0-
@iAmGroute
iAmGroute / mmapDictionary.c
Last active September 7, 2019 14:58
Virtual memory as a dictionary
// An experiment of using the virtual memory as a huge directly addressed map/dictionary.
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
uint64_t setupMemory(uint64_t virtualSize);
int main()
{
@iAmGroute
iAmGroute / SmartTabs.py
Last active March 29, 2019 19:10
A simple python class to (adaptively) align text lines based on tab characters ('\t') in real time.
class SmartTabs:
def __init__(self):
self.columns = [0]
def __call__(self, line):
cells = line.split('\t')
cols = self.columns
# Pad as needed or update existing columns
@iAmGroute
iAmGroute / NatDetector.js
Created March 23, 2019 13:02
An attempt to detect NAT type with javascript and WebRTC.
// Doesn't work.
// Some NAT servers reset the port binding at new 'connections'.
let NatDetector = new (function() {
this.settings = {
server1p1: 'stun:stun1.l.google.com:19302',
server1p2: 'stun:stun1.l.google.com:19302', // TODO: change port only
server2p1: 'stun:stun2.l.google.com:19302'
// 'stun:stun.l.google.com:19302',