Skip to content

Instantly share code, notes, and snippets.

View the-moog's full-sized avatar
💭
Not looking for a new job

Jay the-moog

💭
Not looking for a new job
  • Qualcomm/QTI, UK, Cambridge
  • Cambridge, UK
  • 05:12 (UTC +01:00)
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@the-moog
the-moog / signed_hex.py
Created January 4, 2022 12:10
A Python class that produces proper 2's compliment binary/hex representations of integers.
"""
Python's handling of negative hex numbers is rubbish
It has a two's compliment but that does not know about bytes
This class fixes that by allowing a negative hex number that is not just a hex number
with a minus in front of it
doctest below here....
Negative of 127
@the-moog
the-moog / inner.py
Created April 22, 2022 12:04
Using concurrent.futures to call secondary Python scripts with parameters and collect the results
# inner.py
# Note we did not load sys module, it gets passed to us
script_name = sys.argv[0]
import time
import random
state = "!! RESTARTED !!" if restarted else "loaded"
runtime = 1 if not restarted else 15
@the-moog
the-moog / fixexec.py
Last active June 8, 2024 10:59
Python script to fix the "exec format error" seen with tools like gzip in WSL with Ubuntu 22.04 in Q1/Q2 2022
#!/usr/bin/env python
r"""
This script can be used to fix the "exec format error" seen with tools like gzip in
WSL with Ubuntu 22.04 in Q1/Q2 2022
A hacky fix for broken executables in WSL/Ubuntu 22.0
see https://github.com/microsoft/WSL/issues/8219
@the-moog
the-moog / round_vs_floor.py
Last active June 18, 2022 19:11
Comparing Python round() vs math.floor()
import timeit
import sys
import platform
# Expected result values vs output values (we always expect rounded down)
du = {
1.231234: 1.23,
1.239876: 1.23,
1.999999: 1.99,
1.00000001: 1.00,
@the-moog
the-moog / cards.c
Created June 20, 2022 13:24
Sidestepping limitation of variable length member of fixed array sizes
#include <stdio.h> // for printf
#include <string.h> // for strcmp
// Define a card
typedef struct card
{
int value; // This is an int, so it's storage size does not change
char * name; // This is a pointer, so it's storange size does not change
int rule_change;
} card_t;
@the-moog
the-moog / Makefile
Created January 4, 2023 15:13
makefile to see if it's possible to detect source of shell invocation with the make run
# use a bash as sh is sh*#
.SHELL = /bin/bash -e
# Turn off make's legacy cruft
.SUFFIXES:
# Unless otherwise stated
.DEFAULT_GOAL := diff
# TOOLS
@the-moog
the-moog / build.bash
Last active March 19, 2023 00:15
Improved fix for dynamic build and load of shared library support when using pyenv virtualenv (Python)
### pyenv hook script to prepare a build env
#
# 0: Intro
# $MYLOC points to your local sideload root (usually ~/.local)
MYLOC=$HOME/.local
#
export PYTHON_CONFIGURE_OPTS="--with-openssl=$MYLOC --enable-shared --enable-optimizations --with-lto"
export PYTHON_CFLAGS="-march=native -mtune=native"
echo ""
echo "This is $(basename $0) hook 'build.bash':"
@the-moog
the-moog / get_real_login.sh
Created June 1, 2023 18:58
Bash script to find the true UID and HOME of a user once they have used sudo or are not in an interractive login.
#!/bin/bash
# Bash script, related to https://stackoverflow.com/a/76384868/1364242
# get_real_login.sh
# Get the name at the time of login of the user who is running this script
# MIT License
LICENSE="
Copyright (c) <2023> <https://github.com/the-moog>
Permission is hereby granted, free of charge, to any person obtaining a copy
@the-moog
the-moog / WSL2-DNAT-HowTo.md
Created June 6, 2023 19:48
Make WSL2 networking play nicely with DNAT/dest-NAT/portproxy and/or give WSL a static IP address

Port forwarding from host (aka WSL2 Static IP)

I have read many articles about WSL networking that try and sort DNAT and static IP and none that I've found seem to work or only work for some. So here is my attempt....

It seems getting port forwarding to work in WSL is not easy. I think WSL has unusual hidden network mechanisms and that confuses the matter. Also when WSL boots the WSL init mechanism gives the guest a random address, which is no use for static NAT

This worked for me. Try it yourself and let me know if I've finally cracked it !!