Skip to content

Instantly share code, notes, and snippets.

View unixnut's full-sized avatar

Alastair Irvine unixnut

  • Warpspace IT
  • Perth, WA, Australia
View GitHub Profile
@unixnut
unixnut / generic_validator.py
Created January 18, 2026 13:13
Example of Python generic descriptor from @Carberra; source: https://youtube.com/watch?v=l0T4jvuP0H8
import abc
from typing import Generic, TypeVar, cast
T = TypeVar("T")
class Validator(Generic[T]), metaclass=abc.ABCMeta):
def __set_name__(self, owner: type, name: str) -> None:
self.name = name
self.private_name = "_" + name
# $Id: statemachine.py 10136 2025-05-20 15:48:27Z milde $
# Author: David Goodger <goodger@python.org>
# Copyright: This module has been placed in the public domain.
"""
A finite state machine specialized for regular-expression-based text filters,
this module defines the following classes:
- `StateMachine`, a state machine
- `State`, a state superclass
@unixnut
unixnut / .gitignore
Last active May 20, 2025 16:06
Thread Pool by CppNuts
*~
*.[oa]
*.l[ao]
.deps
.libs
@unixnut
unixnut / distribution.h++
Last active April 10, 2025 02:34
Compile-time random number generator by Jason Turner
template<typename T, typename Gen>
constexpr auto distribution(Gen &g, T min, T max)
{
const auto range = max - min + 1;
const auto bias_remainder = std::numeric_limits<T>::max() % range;
const auto unbiased_max = std::numeric_limits<T>::max() - bias_remainder - 1;
auto r = g();
for (; r > unbiased max; r = g());
@unixnut
unixnut / Stack.h++
Last active April 10, 2025 02:25
C++ Stack container template with constexpr support, by Jason Turner
template<typename T, std::size_t Size>
struct Stack
{
T m_data[Size]();
std::size_t pos = 0;
template<typename ... Arg>
void constexpr emplace_back(Arg &&... arg)
{
m_data[pos] = T{std::forward<Arg>(arg)...};
@unixnut
unixnut / Array2D.h++
Created April 10, 2025 02:04
Array2D, a C++ template wrapper for 2d C-style multidimensional array, with linear storage by Jason Turner
template<typename Data, std::size_t Cols, std::size_t Rows>
struct Array2D
{
constexpr static auto rows() { return Rows; }
constexpr static auto cols() { return Cols; }
constexpr Data &operator()(const std::size_t col, const std::size_t row)
{
return m_data[col + (row * Cols)];
}
@unixnut
unixnut / fib-template.cpp
Last active April 5, 2025 17:09
C++ Weekly - Ep 13 Fibonacci: You're Doing It Wrong
#include <iostream>
#include <array>
#include <utility>
#include <sstream>
template<int I>
struct Fib
{
static const int val Fib<I-1>::val + Fib<I-2>::val;
};
@unixnut
unixnut / git-change-url
Created September 4, 2020 17:54 — forked from sankalp-khare/git-change-url
Changes the git url type from https to ssh or vice versa
#!/usr/bin/env bash
# Utility to change the connection method for a git repo.
# === Colour Definitions ===
red='\e[0;31m'
green='\e[0;32m'
purple='\e[0;35m'
orange='\e[0;33m'
# No Color, i.e. turn off color
@unixnut
unixnut / Jenkinsfile
Created September 4, 2020 17:45 — forked from dhgautam/Jenkinsfile
Jenkinsfile for demo app-pipeline
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Build demo-app'
sh 'sh run_build_script.sh'
}
}
@unixnut
unixnut / sercom.py
Created March 25, 2020 05:05 — forked from mortie/sercom.py
Better serial console.
#!/usr/bin/env python3
import serial
import sys
import os
import threading
import traceback
if len(sys.argv) not in (2, 3):
print(f"Usage: {sys.argv[0]} <path> [baud]")