Skip to content

Instantly share code, notes, and snippets.

@trundev
trundev / stdout_timestamp.py
Last active September 30, 2025 08:22
Python script to capture process stdout
"""Pass-thru process output by adding stdout/stderr tags and timestamps
This is an 'async' version, it uses aiostream.stream.merge() to merge
stdout and stderr streams.
Requires https://github.com/vxgmichel/aiostream:
pip install aiostream
"""
import sys
import asyncio, aiostream
@trundev
trundev / numpy-fluid-simulation.py
Last active March 28, 2024 15:36
"Fluid Simulation for Dummies" using numpy
# Code from "Fluid Simulation for Dummies" paper using numpy
# https://mikeash.com/pyblog/fluid-simulation-for-dummies.html
#
# See also:
# * Real-Time Fluid Dynamics for Games
# http://graphics.cs.cmu.edu/nsp/course/15-464/Fall09/papers/StamFluidforGames.pdf
# * Lily Pad: Towards Real-time Interactive Computational Fluid Dynamics
# https://arxiv.org/abs/1510.06886
# https://github.com/weymouth/lily-pad
#
@trundev
trundev / numpy_life.py
Last active November 3, 2024 16:34
Tools for brute force reversal of GOL state
# Game of life game on python using numpy
import numpy as np
import numpy.typing as npt
import matplotlib.pyplot as plt
from collections.abc import Iterable, Generator
# Roll around edges: True - Original "Game of Life" behavior
ROLL_EDGES = True
# Sleep between frames
"""Clifford algebra experiments
Uses https://pypi.org/project/clifford/ module:
pip install clifford
"""
from clifford import g2, g3
import clifford # type-hints
import numpy as np
def nabla_clifford(space: np.array, *, blades: tuple or None=None, axes: tuple or None=None) -> np.array:
@trundev
trundev / music_intervals.py
Last active May 12, 2022 13:37
Music intervals as rational numbers
"""Approximation of the tempered musical intervals via rational numbers
See https://en.wikipedia.org/wiki/Interval_(music)
"""
import math
import music_math
# Closest ratios for intervals in the octave
# as rational numbers, format: (<divident>, <divisor>)
RATIONAL_ARRAY = [(1, 1), # 0: Unison
@trundev
trundev / poly_approx.py
Last active November 23, 2021 08:28
Extrapolate signal value, by keeping track of its derivatives over time
"""Simple polynomial approximation tool"""
import math
class approximator:
"""Approximate signal value, by keeping track of its derivatives over time"""
# Vector of the signal value deltas tuple(val, time):
# 0 - values itself (zero-derivative):
# delta0: value
# time0: time
# 1 - first mean derivative for the interval 'time1' to 'time0':
@trundev
trundev / add-date.sh
Created October 29, 2020 08:15
Calculate date from another one, by adding specified number of seconds
echo "Adding $2 secs to $1"
epoch=$(date -d "$1" +%s)
result=$(expr $epoch + $2)
echo $epoch + $2 = $result
date -d @$result
@trundev
trundev / gdal-setenv.bat
Last active April 1, 2021 16:49
Python GDAL environment
@echo off
setlocal EnableDelayedExpansion
set BASE_DIR=%~dp0release-1928-x64-gdal-3-2-1-mapserver-7-6-2
set BIN_BASE_DIR=%BASE_DIR%\bin
set GDAL_DRIVER_PATH=%BIN_BASE_DIR%\gdal\plugins
for /F %%x IN (
'py -c "from importlib import util; osgeo=util.find_spec('osgeo'); print(osgeo.submodule_search_locations[0] if osgeo else '')"'
@trundev
trundev / intergrator.py
Last active August 28, 2020 11:25
Python classes to integrate differentiate a signal
class integrator:
def __init__(self, start, tau):
self.output_dot_tau = start * tau
self.tau = tau
def __call__(self, input, period=1):
# Optimized from "period * (input - self.output) / self.tau + self.output"
self.output_dot_tau += period * (input - self.output_dot_tau / self.tau)
return self.output_dot_tau / self.tau