Skip to content

Instantly share code, notes, and snippets.

View jonashaag's full-sized avatar

Jonas Haag jonashaag

View GitHub Profile
@jonashaag
jonashaag / xkcdpass.sh
Created February 21, 2024 10:17
XKCD password
curl -L https://raw.githubusercontent.com/redacted/XKCD-password-generator/master/xkcdpass/static/eff-long \
| sort -R \
| head -n 5 \
| tr '\n' -
@jonashaag
jonashaag / sp_count.sql
Last active January 8, 2024 16:35
SQL Server quickly count number of rows in table
-- Count number of rows in a table quickly (without a full table/index scan).
-- Usage:
-- sp_count 'mydb.dbo.mytable' Get the row count of the given table.
-- sp_count 'dbo.mytable' Get the row count of the given table from the current database.
-- sp_count Get a list of tables and row counts in the current database.
USE [master]
GO
DROP PROCEDURE IF EXISTS [dbo].[sp_count]
@jonashaag
jonashaag / snowflake_unload_parquet.py
Created October 8, 2023 18:22
Snowflake Connector Python download table or query as Parquet
def unload_to_parquet(query: str, target_dir: Path, conn, stage_name: str = "unload_stage"):
conn.execute(f"CREATE TEMP STAGE {stage_name}")
conn.execute(f"COPY INTO @{stage_Name} FROM ({query}) file_format=(type='parquet') header=true")
target_dir.mkdir(parents=True)
conn.execute(f"GET @{stage_name} file://{str(target_dir)}")
@jonashaag
jonashaag / pd_shrink_dtypes.py
Created September 11, 2023 12:09
Pandas shrink dtypes
import numpy as np
import pandas as pd
from pandas.api.types import is_numeric_dtype
from pandas.core.dtypes.base import ExtensionDtype
def shrink_dtype(series: pd.Series) -> pd.Series:
smallest_dtype = get_smallest_dtype(series)
if smallest_dtype == series.dtype:
return series
import json
import sqlite3
repodata = json.load(open("497deca9.json"))
COLS = 'filename, build, build_number, depends, license, license_family, md5, name, sha256, size, subdir, timestamp, version'.split(', ')
db = sqlite3.connect("497deca9.sqlite")
db.execute("create table repodata ({}, primary key (filename))".format(','.join(COLS)))
@jonashaag
jonashaag / compiler.py
Last active June 30, 2023 12:31
Cython prematcher compiler
import textwrap
from dataclasses import dataclass
@dataclass
class Pattern:
pattern: str
prematchers: list[str]
@jonashaag
jonashaag / restarter.sh
Created June 12, 2023 13:53
Bash regularly restart a program
#!/bin/bash
set -euo pipefail
if [ $# -lt 2 ]; then
echo "Usage: $0 SCHEDULE PROG [ARGS]..." >&2
echo "SCHEDULE is used in 'date -d <SCHEDULE>'." >&2
echo "Example: $0 '1 hour' myprog --arg" >&2
exit 1
fi
@jonashaag
jonashaag / pytest_fork.py
Created June 2, 2023 09:45
Run a pytest test in a fork
def run_test_in_fork(func, *args, **kwargs):
"""Run a pytest test in a fork of the pytest process.
Useful to check behaviour of some code when run in a forked process.
The test outcome will be reported normally in the pytest parent.
"""
if not hasattr(os, "fork"):
pytest.skip("os.fork not available")
error_in_child = multiprocessing.Value("b")
child_pid = os.fork()
function __micromamba_completion
set -l args (commandline -ocp)
# TODO: micromamba completer should be able to ignore micromamba program name (first arg)
set -e args[1]
set -l suggestions ($MAMBA_EXE completer $args (commandline -t))
# TODO: micromamba completer should output lines not columnified
if echo "$suggestions" | grep -q " "
set suggestions (string split " " "$suggestions")
end
for suggestion in $suggestions
@jonashaag
jonashaag / disable_index.sql
Last active September 2, 2022 07:34
PostgreSQL temporarily disable index
UPDATE pg_index
SET indisready=false, indisvalid=false
WHERE indrelid = (SELECT oid FROM pg_class WHERE relname='<TABLE_NAME>');
... do work ...
REINDEX TABLE "<TABLE_NAME>";