Skip to content

Instantly share code, notes, and snippets.

@alexer
alexer / importv.py
Created June 29, 2021 16:13
Like import from ImageMagick, but for video. Because someone wondered if this exists - I don't know if it did, but now it does. No options. Requires ffmpeg and python-xlib.
import Xlib.display
from Xlib import X, Xcursorfont
def XSelectWindow(dpy):
screen = dpy.screen()
root = screen.root
gc = root.create_gc(background=screen.black_pixel, foreground=screen.white_pixel, function=X.GXinvert, subwindow_mode=X.IncludeInferiors)
font = dpy.open_font('cursor')
cursor = font.create_glyph_cursor(font, Xcursorfont.crosshair, Xcursorfont.crosshair+1, (65535, 65535, 65535), (0, 0, 0))
try:
@alexer
alexer / sbstd.py
Last active October 29, 2018 16:01
"Should Be in the STDlib" - aka. my helper library, which has outgrown it's original name
# Really useful in some situations, but you can see how often it's actually been used, since cmp() doesn't even exist anymore...
sign = lambda v: cmp(v, 0)
clamp = lambda v, minv, maxv: min(max(v, minv), maxv)
# These are a bit of a misnomers, since below/above suggests lt/gt, while these are le/ge
clamp_below = lambda v, maxv: min(v, maxv)
clamp_above = lambda v, minv: max(v, minv)
div_ceil = lambda dividend, divisor: (dividend - 1) // divisor + 1
# Just for symmetry
@alexer
alexer / rtlsdr2numpy.py
Created October 25, 2018 04:57
Convert rtl-sdr uint8-complex output file to a memory-mapped numpy complex128 array
import numpy as np
def rtlsdr2numpy(datafilename, tempfilename):
real = np.memmap(datafilename, mode='r', dtype='uint8')
temp = np.memmap(tempfilename, mode='w+', dtype='float64', shape=real.shape)
temp[:] = real
temp /= 127.5
temp -= 1
return temp.view('complex128')
@alexer
alexer / gist:10009990
Created April 6, 2014 18:45
get rid of scientific notation of str() for float
def float2str(val):
val, sci, exp = str(val).partition('e')
if not sci:
return val
sign = ''
if val[0] in '-':
sign = '-'
val = val[1:]
assert val.count('.') == 1 and val[1] == '.' and val[0] != '0'