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 / 7z.md
Created June 17, 2024 11:28
Using encrypted 7z archives (Ubuntu, zip, 7zip)

Using 7zip to create encrypted archives

Let's use 7zip for strong AES256 encrypted archives with a password. The guide assumes you're on Ubuntu 22 or newer. Note: this creates .7z archives, which is not the same as the regular .zip!

# Install 7zip. Note: don't install "p7zip-full" or "p7zip" (it's an older version!)
# Source, version things: https://askubuntu.com/questions/1465853/difference-between-several-command-line-tools-provided-for-7-zip-compression-li
sudo apt update
sudo apt install 7zip
@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 / 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 / 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)
@ekreutz
ekreutz / update_conda.md
Last active August 20, 2021 07:52
How to update conda

Update conda

Or, how to update miniconda or Anaconda.

Simply run the following command:

conda update -n base conda
@ekreutz
ekreutz / pixel_aspect_ratios.md
Last active May 27, 2022 05:47
Pixel aspect ratios - a potential issue in object detection?

Pixel aspect ratios - a potential issue in object detection?

Updated: Jan 25, 2021

Quick write-up about pixel aspect ratios in video files, and how they might affect performance in an object detection pipeline. I found very little good information about this online, so I decided to write this to summarize my findings.

Tl;dr: scroll down to the "final solution" code, for how to deal with this issue, using Python/OpenCV.