Skip to content

Instantly share code, notes, and snippets.

@devilholk
devilholk / connect_via_pad.py
Created April 25, 2021 01:48
Script for pcbnew to connect unconnected vias to closest pads (currently according to a filter on line 50 - just serves as example)
import pcbnew
from dataclasses import dataclass
@dataclass
class local_pad_representation:
pad: object = None
footprint: object = None
pos: object = None
friendly_name: str = None
@devilholk
devilholk / .editorconfig
Created March 17, 2021 04:17
My current default .editorconfig
root = true
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = tab
indent_size = 4
@devilholk
devilholk / hexdump.py
Last active March 11, 2021 20:12
Function for creating a human readable hexdump from an iterator of bytes (bytes, bytearray, ctypes buffers etc)
def hexdump(buf, size, minor_width=4, major_width=12, addr_len=4, ljust_char='·', unprintable_char='�', indent=''):
lines = list()
disp = ''
hexa = ''
row_start = 0
for i, c in zip(range(size), buf):
@devilholk
devilholk / timetally.py
Last active March 8, 2021 17:17
Primitive time tally tool
import time
def get_day_minutes(t):
s = time.strptime(t, '%H:%M')
return s.tm_hour * 60 + s.tm_min
def calc_duration(start_time, stop_time):
'Returns how many minutes have passed between two times expressed as HH:MM'
'Wraps around for midnight'
'Does not track DST'
@devilholk
devilholk / gunicorn_uvicorn_websocket_experiment.py
Created February 1, 2021 21:53
This is an example/test for using gunicorn with uvicorn to serve http and websockets using asynchronous handlers. Take this code with a wheelbarrow of NaCl.
import sys
if not 'gunicorn' in sys.argv[0]:
import pathlib
name = pathlib.Path(sys.argv[0]).stem
print('# To test this script:')
print(f'gunicorn -w1 -k uvicorn.workers.UvicornWorker {name}:app')
sys.exit(1)
async def handle_lifespan(scope, receive, send):
while True:
@devilholk
devilholk / watch.py
Created November 25, 2020 21:10
Watch command output on linux [WIP]
import sys, time, subprocess, threading, queue, os, fcntl
'''
For some reason, even with all the flushing, if python is running buffered (without -u), it will sometimes not flush!
Until I can figure this one out I'll use -u
Even with -u it sometimes will output stdout and stderr in different order, some buffering somewhere is messing things up.
As is this is still a decent tool for watching output that contains ANSI escapes watch -c won't manage.
'''
@devilholk
devilholk / bashcomp.py
Last active November 17, 2020 01:19
Example for bashcomp.sh
#!/usr/bin/python
import sys, os
#Usage: https://gist.github.com/devilholk/d1dcbb194086c84915c718d9ccafd95a
def combine_args(args):
result = list()
for a in args:
if result and result[-1].endswith('\\'):
result[-1] = result[-1][:-1] + ' ' + a
@devilholk
devilholk / bashcomp.sh
Last active November 17, 2020 01:17
Wrapper for using python for bash completion
#bashcomp.py: https://gist.github.com/devilholk/b176b9c62c83636eb7552b3629e0b067
_bashcomp() {
COMPREPLY=( $(eval `python /path/to/bashcomp.py $COMP_LINE`;) );
return 0;
}
complete -F _bashcomp my_command
@devilholk
devilholk / steinhart_hart.py
Created November 15, 2020 00:38
Steinhart-Hart utilities for calculating the relationship between temperature and resistance of thermistors.
from math import log, exp, sqrt
cbrt = lambda x: x**(1/3)
invert = lambda x: 1/x
def solve_steinhart_hart_eq(R1, T1, R2, T2, R3, T3):
#Solving the Steinhart-Hart equation
#Source: https://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation#Steinhart%E2%80%93Hart_coefficients
@devilholk
devilholk / scope_trickery.py
Created November 11, 2020 14:11
A workaround for a nested scope not being able to access its parent scope
#Note that this is not recommended, we may not be able to rely on locals() being mutable on other platforms or future versions
#Also note https://stackoverflow.com/questions/29333359/python-class-scoping-rules
scope_transfer = dict()
class my_namespace:
class some_thing:
pass