Skip to content

Instantly share code, notes, and snippets.

View ekreutz's full-sized avatar
📖
Reading; Donald Knuth, of course.

Emil Kreutzman ekreutz

📖
Reading; Donald Knuth, of course.
View GitHub Profile
@ekreutz
ekreutz / ansible_variable_precedence.md
Last active April 25, 2024 17:43
Ansible variable precedence (order, hierarchy)
@ekreutz
ekreutz / tmux_conda_fix.md
Last active April 17, 2024 14:32
Fix tmux messing with conda path

Fix tmux messing with conda

Problem: When running a conda environment and opening tmux on macOS, a utility called path_helper is run again. Essentially, the shell is initialized twice which messes up the ${PATH} so that the wrong Python version shows up within tmux.

Solution

If using bash, edit /etc/profile and add one line. (For zsh, edit /etc/zprofile)

...
@ekreutz
ekreutz / trim_empty_vscode.md
Last active February 2, 2024 13:33
Trim empty lines in VSCode

How to trim empty lines in VSCode

What this will do:

  • Trim empty lines (remove all whitespace)
  • Trim whitespace at the end of code lines
  • Note: it will not remove any line at all, but simply trim trailing whitespace.

Steps:

  1. Press Ctrl + F (Cmd + F on mac) to open the search and replace view.
  2. Toggle the regex button .*.
@ekreutz
ekreutz / running_max.py
Created January 18, 2024 15:42
Fast O(n) running maximum using numba
from numba import njit
from numpy.typing import NDArray as array
@njit
def running_max(values: array, w: int) -> array:
"""Fast O(n) running maximum.
For large values of `w` this solution is 100x faster or more, than the naive version.
"""
@ekreutz
ekreutz / convert.md
Created November 24, 2023 07:44
Convert OpenSSH private key to PEM file format

Convert OpenSSH private key to PEM file format

Assuming that your ssh private key is located at ~/.ssh/id_rsa. We will convert this file to the PEM format.

# Change dir to home directory
cd ~

# Make a copy of the private key and store the copy in the home directory
cp ~/.ssh/id_rsa ~/id_isa_copy
@ekreutz
ekreutz / naughty.md
Created October 18, 2023 13:04
Naughty coroutines in Python

Naughty coroutines

Unexpected behavior when working with asyncio in Python. The behavior is likely intended, but intuitive.

import asyncio


async def fine_job(i: int):
    # A fine job that is well behaved without exceptions!
@ekreutz
ekreutz / asyncio_errors.md
Last active October 18, 2023 12:41
Asyncio error antipattern

An antipattern in python asyncio error handling

Don't do this:

The try catch will catch any instance of SomeError, even if it's not emitted inside my_async_function. This happens if my_async_function runs for long enough so that SomeError is emitted elsewhere in the program, while this piece of code is still inside the try-except.

try:
@ekreutz
ekreutz / attention.md
Last active July 6, 2023 12:12
Dot-product and Multi-head attention implementation in Tensorflow 2

Dot-product and Multi-head attention

Dot-product and Multi-head attention from the paper "Attention is all you need" (2017). Implementation in modern Tensorflow 2 using the Keras API.

Example use of the implementations below:

batch_size = 10
n_vectors = 150
d_model = 512
@ekreutz
ekreutz / serial_counter.md
Last active March 20, 2023 18:23
Reset SERIAL counter for a PostgreSQL database table

Reset SERIAL counter for a PostgreSQL database table

For the table my_table with a SERIAL column my_id.

SELECT SETVAL(
  pg_get_serial_sequence('my_table', 'my_id'),
  (SELECT COALESCE(MAX(my_id), 0) FROM my_table)
);
@ekreutz
ekreutz / round_sd.py
Created February 3, 2023 09:51
Python - round a number to significant digits
from math import log10, floor
from decimal import Decimal as Dec
# Note: the method spec uses a union type float | Dec introduced in Python 3.10
# Remove the type definitions to make it work with earlier versions.
def round_sd(x: float | Dec, sd: int = 3):
"""Round a value to a specified amount of significant digits `sd`.
"""
return round(x, sd - int(floor(log10(abs(x)))) - 1)