Skip to content

Instantly share code, notes, and snippets.

View edvardm's full-sized avatar

Edvard Majakari edvardm

  • Rakettitiede Oy
  • Finland
  • 15:18 (UTC +03:00)
View GitHub Profile
@edvardm
edvardm / day02.hs
Created December 3, 2023 12:07
aoc2023, day 2
module Main where
import Control.Applicative (liftA2)
import Data.Char (toLower, toUpper)
import Data.Function (on)
import qualified Data.Map.Strict as Map
import Text.Parsec
import Text.Parsec.String (Parser)
data Color
@edvardm
edvardm / buffered_op.py
Created May 15, 2023 10:16
add items one at a time, buffering entries and flushing those automatically when there are enough entries
from typing import Any, TypeVar, Callable, Generator
from contextlib import contextmanager
T = TypeVar("T")
class BufferedOp:
"""Class for adding elements to a _buffer and flushing when full.
Note that last batch may is not usually flushed automatically as usually
@edvardm
edvardm / describe.py
Last active March 12, 2023 18:50
more informative describe()
# Credits to answer in https://stackoverflow.com/questions/53173927/pandas-extensive-describe-include-count-the-null-values
def stats(df: pd.DataFrame) -> pd.DataFrame:
"""Like describe() but return dataframe, with datatypes and ratio of NaN values"""
st = df.describe()
total_n = len(df)
st.loc["dtype"] = df.dtypes
st.loc["n"] = total_n
st.loc["nan_n"] = total_n - df.count() # number of NaNs
@edvardm
edvardm / mp_produce_consume.py
Last active April 28, 2022 20:28
single producer/consumer skeleton for keeping Queue full of db rows to process
# Using sample data from https://www.gov.uk/government/statistical-data-sets/price-paid-data-downloads
# See https://wiki.postgresql.org/wiki/Sample_Databases for table schema & instructions how to populate
# Simple script to test the idea of keeping Queue full of database rows ready for consumption, using appropriate-sized
# chunks of rows
import multiprocessing as mp
import queue
import psycopg2
@edvardm
edvardm / retry_decorator.py
Created July 27, 2021 14:57
simple retry decorator with exp backoff
import time
import sys
def exp_backoff_retry(
fun=None,
*,
retry_exceptions=(BaseException,),
max_retries=3,
exp_base=2.0,
max_seconds=300,
@edvardm
edvardm / pycontract_json.py
Last active October 29, 2020 22:05
Show power of PyContracts through mutually recursive typedef
from contracts import contract, new_contract
import pytest
from contracts.interface import ContractNotRespected
import json
# There are probably bugs, but this should check that given
# argument is a valid object which can be encoded as json.
#
# It assumes the following Python structure, if type hints
# could be recursive:
@edvardm
edvardm / row_estimate.sql
Last active October 27, 2020 22:49
timescaledb, estimated row count
SELECT h.schema_name,
h.table_name,
h.id AS table_id,
h.associated_table_prefix,
((round(row_estimate.row_estimate::numeric / 1000000, 3))::text || 'M') as estimated_rows
FROM _timescaledb_catalog.hypertable h
CROSS JOIN LATERAL ( SELECT sum(cl.reltuples) AS row_estimate
FROM _timescaledb_catalog.chunk c
JOIN pg_class cl ON cl.relname = c.table_name
WHERE c.hypertable_id = h.id
@edvardm
edvardm / pristine-setup.sh
Last active August 11, 2020 15:15
setup python prerequisities
#!/usr/bin/env bash
set -eu
PYTHON_VERSION=3.7.5
POETRY_URL=https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
has() {
type $1 > /dev/null 2>&1
}
@edvardm
edvardm / pristine-setup.sh
Created August 11, 2020 15:12
setup python prerequisities
#!/usr/bin/env bash
set -eu
PYTHON_VERSION=3.7.5
POETRY_URL=https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py
has() {
type $1 > /dev/null 2>&1
}
@edvardm
edvardm / mktask.sh
Created May 27, 2020 14:38
Shell script to quickly create an invoke task (stub)
mktask () {
name=$1
cmd="${@:2}"
preamble="import os\nimport sys\nfrom invoke import task"
if [ -z $cmd ]
then
echo "mktask <name> <cmd with any args>"
return 0
fi