Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / stash.zsh
Created March 14, 2024 18:38
A zsh function to list git stashes using fzf with a preview of the changed files for quickly identifying the relvant stash
function stash() {
# Check if stash is empty
if ! git stash list &>/dev/null; then
echo "No stashes found."
return
fi
# Fetch relative dates and messages from git stash
local relative_dates=$(git stash list --format="%ar")
local messages=$(git stash list --format="%gs" | sed 's/^On //; s/^WIP on //')
@thomasaarholt
thomasaarholt / gaussian.py
Last active February 6, 2024 11:19
Fastest found numpy method of generating a 2D gaussian kernel of size n x n and standard deviation std.
import numpy as np
from scipy import signal
def gaussian_kernel(n, std, normalised=False):
'''
Generates a n x n matrix with a centered gaussian
of standard deviation std centered on it. If normalised,
its volume equals 1.'''
gaussian1D = signal.gaussian(n, std)
gaussian2D = np.outer(gaussian1D, gaussian1D)
@thomasaarholt
thomasaarholt / polars_datatypes_json.py
Created December 20, 2023 14:04
Serialize and deserialize polars datatypes to json
import json
import polars as pl
def dtype_to_json(dtype: pl.DataType) -> str:
return json.dumps(str(dtype))
def json_to_dtype(json_dtype_str: str) -> pl.DataType:
from polars.datatypes.classes import ( # noqa F401
Array,
@thomasaarholt
thomasaarholt / languages.toml
Last active December 14, 2023 09:19
Ruff and pyright in helix
[[language]]
name = "python"
scope = "source.python"
injection-regex = "python"
file-types = ["py","pyi","py3","pyw",".pythonstartup",".pythonrc"]
shebangs = ["python"]
roots = [".", "pyproject.toml", "pyrightconfig.json"]
comment-token = "#"
language-servers = ["pyright", "ruff"]
indent = { tab-width = 4, unit = " " }
@thomasaarholt
thomasaarholt / polars_pydantic.py
Created November 28, 2023 20:18
Polars type serialization in a pydantic model
from typing import Any
import polars as pl
import json
from pydantic import BaseModel
original_expression = (pl.col("foo") * 2) == pl.col("bar")
class Column(BaseModel, arbitrary_types_allowed=True):
@thomasaarholt
thomasaarholt / languages.toml
Created November 26, 2023 13:31
Working pyright and ruff LSPs for helix languages.toml configuration
[[language]]
name = "python"
scope = "source.python"
injection-regex = "python"
file-types = ["py","pyi","py3","pyw",".pythonstartup",".pythonrc"]
shebangs = ["python"]
roots = [".", "pyproject.toml", "pyrightconfig.json"]
comment-token = "#"
language-servers = ["pyright", "ruff"]
indent = { tab-width = 4, unit = " " }
@thomasaarholt
thomasaarholt / struct.py
Last active November 23, 2023 19:01
Structlog example showing how to serialize polars and pandas dataframes and pydantic models
from typing import Any
import json
import structlog
import pandas as pd # pip install pandas
import polars as pl # pip install polars
from pydantic import BaseModel
class PydanticModel(BaseModel):
a: int
b: str
@thomasaarholt
thomasaarholt / languages.toml
Created August 4, 2023 18:51
Helix languages.toml setup for Python with pyright, ruff and black
[[language]]
name = "python"
scope = "source.python"
injection-regex = "python"
file-types = ["py","pyi","py3","pyw",".pythonstartup",".pythonrc"]
shebangs = ["python"]
roots = [".", "pyproject.toml", "pyrightconfig.json"]
comment-token = "#"
language-servers = ["pyright", "ruff"]
indent = { tab-width = 4, unit = " " }
@thomasaarholt
thomasaarholt / gist:4c63d9f15a6895a4153e9bfa98f75360
Created September 27, 2021 10:24
jupyter lab settings with auto closing brackets enabled in docker image
cat /root/.jupyter/lab/user-settings/@jupyterlab/notebook-extension/tracker.jupyterlab-settings
{
// Notebook
// @jupyterlab/notebook-extension:tracker
// Notebook settings.
// **************************************
// Code Cell Configuration
// The configuration for all code cells.
"codeCellConfig": {
@thomasaarholt
thomasaarholt / is_notebook.py
Created August 30, 2019 07:02
Check if code is running in jupyter notebook
def is_notebook():
try:
from IPython import get_ipython
if "IPKernelApp" not in get_ipython().config: # pragma: no cover
raise ImportError("console")
return False
if "VSCODE_PID" in os.environ: # pragma: no cover
raise ImportError("vscode")
return False