Skip to content

Instantly share code, notes, and snippets.

@mofosyne
mofosyne / sexp_linter_kicadv8_style.py
Last active September 20, 2024 06:54
KiCADv8 style s-exp linter
#!/usr/bin/env python3
# KiCADv8 style s-exp linter
# By Brian Khuu 2024
# Design Goal: To match KiCADv8 formatting style as close as possible (not perfectly however as that's a bit too much work)
# This will allow for easier comparison between file format versions and to investigate kicad bugs via diffing
from os import path
import argparse
from pathlib import Path
@mofosyne
mofosyne / digikey_api_v4_product_search.py
Created September 16, 2024 15:42
Digikey Basic Product Search Script (Targeting Digikey API v4)
#!/usr/bin/env python3
# digikey_api_v4_product_search.py
# Digikey Basic Product Search Script (Targeting Digikey API v4)
# By Brian Khuu 2024
# This is a digikey api v4 script that is focused only on just the flow needed to get product information.
# Recommend Reading:
# - https://developer.digikey.com/documentation
# - Section "OAuth 20: 2 Legged Flow" has good information on basic authnetic
@mofosyne
mofosyne / autodoc-justfile.py
Created August 10, 2024 06:36
Autogenerated Documentation For Justfiles
#!/usr/bin/env python3
# Autogenerated Documentation For Justfiles
# This was created to support this issue ticket https://github.com/casey/just/issues/2033#issuecomment-2278336973
import json
import subprocess
from typing import Any
# just --dump --dump-format json --unstable | jq > test.json
json_output = subprocess.run(
["just", "--dump", "--dump-format", "json", "--unstable"],
@mofosyne
mofosyne / escape_markdown_inline_code.py
Created July 30, 2024 11:53
For markdown inline code. Find the longest contiguous sequence of backticks in the string then wrap string with appropriate number of backticks required to escape it
def escape_markdown_inline_code(value_string):
# Find the longest contiguous sequence of backticks in the string then
# wrap string with appropriate number of backticks required to escape it
max_backticks = max((len(match.group(0)) for match in re.finditer(r'`+', value_string)), default=0)
inline_code_marker = '`' * (max_backticks + 1)
# If the string starts or ends with a backtick, add a space at the beginning and end
if value_string.startswith('`') or value_string.endswith('`'):
value_string = f" {value_string} "
@mofosyne
mofosyne / generate_markdown_toc.sh
Created April 25, 2024 14:08
Generate A Markdown TOC from a pdf via this bash function (Useful for Obsidian Note Taking)
# Usage: generate_markdown_toc "AS 3000-2018 Wiring Rules.pdf" 3
generate_markdown_toc() {
local pdf_file="$1"
local max_level="$2"
pdftk "$pdf_file" dump_data | awk -v max_level="$max_level" '/BookmarkTitle:/ {gsub("BookmarkTitle: ", ""); title=$0} /BookmarkPageNumber:/ {gsub("BookmarkPageNumber: ", ""); page=$0} /BookmarkLevel:/ {gsub("BookmarkLevel: ", ""); level=$0; if (level <= max_level) printf("%s- [ ] %s (Page %s)\n", sprintf("%" level*2 "s", ""), title, page)}'
}
@mofosyne
mofosyne / strtok_escaped.c
Last active April 22, 2024 11:37
alternative to strtok but with escaped character support for deliminating a string with a single char (e.g. csv or psv)
#include <stdio.h>
#include <string.h>
char *strtok_escaped(char *str, const char *delim) {
// Tokenise a string from a single char deliminator
// (strtok can deal with a deliminator string but for my purpose of splitting a psv table I only need one char)
// (There are better ways to optimise this, but just wanted something to work for now)
// https://gist.github.com/mofosyne/81c94740c0f33259606afa823562914c
static char *last_token_end = NULL;
@mofosyne
mofosyne / llamafilerepack.sh
Last active April 17, 2024 18:27
This script facilitates the repackaging and upgrading of llamafile archives generated by Llama.cpp
#!/bin/bash
# Llama.cpp Repackaging Script
# This script facilitates the repackaging and upgrading of llamafile archives generated by Llama.cpp.
# This is particularly useful for users with limited internet access, by preserving existing gguf and .arg settings while replacing the llamafile engine.
#
# Usage: llamafilerepack [-h] [-f] <old> <new>
# -h: Display usage information.
# -f: Skip Version Check.
# -v: Verbose Mode
# <old>: The name of the old llamafile archive to be upgraded.
@mofosyne
mofosyne / minimal-autotool-setup-script.sh
Created April 16, 2024 04:32
This is a helper script to generate a minimum gnu autotools C based repo
#!/bin/bash
# INITIALIZE MINIMAL AUTOTOOLS BASED C PROGRAM (Brian Khuu 2024)
# https://briankhuu.com/blog/2024/04/11/initialise-minimal-autotools-script/
set -euo pipefail
# Function to display usage
show_usage() {
echo "Usage: $0 [PACKAGE_NAME]"
@mofosyne
mofosyne / openAI_frontmatter_updator.py
Last active December 13, 2023 16:24
This script is to assist users in filling in summaries, tags, categories and other relevant fields this is especially helpful for blog posts as it makes it easier to search for posts later on.
#!/usr/bin/env python3
# LLM Based Frontmatter updator for Jekyll and MkDocs
# By Brian Khuu (2023)
# This script is to assist users in filling in summaries, tags, categories and other relevant fields
# this is especially helpful for blog posts as it makes it easier to search for posts later on.
# You would need an OPENAI_API_KEY to use this script so make sure to include it in your ~/.bashrc startup script
import os
import re
import sys
@mofosyne
mofosyne / DailyNotes.template
Last active September 15, 2023 06:04
my obsidian daily note template
---
creation date: <%tp.date.now()%>
date: <%tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD")%>
tags: <%tp.date.now("GGGG-[W]WW-E", 0, tp.file.title, "YYYY-MM-DD")%>, <%tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD")%>
alias: <%tp.date.now("dddd Do MMMM YYYY", 0, tp.file.title, "YYYY-MM-DD")%>
---
# <%tp.date.now("dddd Do MMMM YYYY", 0, tp.file.title, "YYYY-MM-DD")%> | <%tp.date.now("[Week] W", 0, tp.file.title, "YYYY-MM-DD")%>
- ISO_8601: <%tp.date.now("YYYY-MM-DD", 0, tp.file.title, "YYYY-MM-DD")%>
- ISO_week_date: <%tp.date.now("GGGG-[W]WW-E", 0, tp.file.title, "YYYY-MM-DD")%>
- [[Index#Quick reference]]