Skip to content

Instantly share code, notes, and snippets.

View LewisGaul's full-sized avatar
🪲

Lewis Gaul LewisGaul

🪲
View GitHub Profile
@LewisGaul
LewisGaul / __init__.py
Last active October 29, 2022 18:17
Pytest setup that runs first during fixture setup stage
__all__ = ()
@LewisGaul
LewisGaul / conftest.py
Last active October 29, 2022 17:13
Pytest --fail-unstable using unstable_fail fixture
import warnings
from typing import List, Callable
import pytest
def pytest_addoption(parser: pytest.Parser) -> None:
parser.addoption("--fail-unstable", action="store_true")
@LewisGaul
LewisGaul / run_cmd.py
Created September 14, 2022 14:24
Wrapper around subprocess.run()
__all__ = ("run_cmd",)
import contextlib
import os.path
import logging
import shlex
import subprocess
import tempfile
from typing import List
#!/usr/bin/env python3
import sys
def distance(word1: str, word2: str) -> int:
# Initialise the matrix.
distances = [[0] * (len(word2) + 1) for _ in range(len(word1) + 1)]
for t1 in range(len(word1) + 1):
distances[t1][0] = t1
@LewisGaul
LewisGaul / pycheck.sh
Last active April 23, 2021 11:43
Check which versions a Python file is valid syntax for
#!/bin/bash
#
# Script to check which versions of python a file is valid syntax for.
#
# The intention is to catch things like 'print' as a keyword (python2), type
# hints, f-strings or async that aren't valid on 3.5, or other new syntax such
# as pattern matching.
#
# This can be useful for identifying python2 files, and should give zero false
@LewisGaul
LewisGaul / memleak.zig
Last active March 21, 2021 00:14
Weird Zig bug with memory leak reporting?
const std = @import("std");
const testing = std.testing;
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const StringArray = std.ArrayList([]const u8);
pub const ValueTree = struct {
@LewisGaul
LewisGaul / zig-blog-posts.md
Last active March 28, 2024 06:53
Collection of blog posts about the Zig programming language
@LewisGaul
LewisGaul / adt.py
Last active February 17, 2021 02:03
Implementation of Algebraic Data Type metaclass in Python
from typing import Tuple, Type
# ------------------------------------------------------------------------------
# Implementation code
# ------------------------------------------------------------------------------
def _make_field(adt_cls_name: str, field_base_cls: Type, name: str, typ: Tuple):
def __init__(self, *args):
if len(args) != len(typ):
@LewisGaul
LewisGaul / gh-archive
Last active January 31, 2021 18:24
Fetch files from GitHub using the REST API, emulating 'git archive', which is not supported natively by GitHub.
#!/usr/bin/env python3
"""
Fetch files from GitHub using the REST API, emulating 'git archive', which is
not supported natively by GitHub.
Supports Python 3.6+, Linux. Only external dependency is the 'curl' executable.
API docs are at https://docs.github.com/en/rest, we use the following:
@LewisGaul
LewisGaul / sh_example.py
Created September 9, 2019 09:28
Example usage of 'sh' library, illustrating ability to interact over stdin
import os.path
import sh
from textwrap import dedent
this_line = []
nums = [4, 3, 2, 1]