Skip to content

Instantly share code, notes, and snippets.

View Hugovdberg's full-sized avatar

Hugo Lapré Hugovdberg

  • Brabant Water
  • Netherlands
View GitHub Profile
@Hugovdberg
Hugovdberg / positional_only.py
Created January 28, 2023 10:37
Positional only argument to prevent argument name clashes
from typing import Callable, Any, ParamSpec, TypeVar
a = TypeVar("a")
b = TypeVar("b")
c = TypeVar("c")
P = ParamSpec("P")
def delay_call(
func: Callable[P, a], *args: P.args, **kwargs: P.kwargs
from __future__ import annotations
import itertools
import warnings
from typing import (
Any,
Callable,
Generator,
Generic,
Iterable,
@Hugovdberg
Hugovdberg / missing_env_vars.py
Created December 3, 2021 08:42
Check if multiple environment variables are present and list all missing
import os
MANDATORY_ENV_VARS = {'TEMP', 'USER'}
# Python 3.8 or higher, using empty set is falsy:
if missing_vars := MANDATORY_ENV_VARS.difference(os.environ):
raise EnvironmentError(f"The following variables were not set: {missing_vars}")
# Python 3.7 or lower:
if not MANDATORY_ENV_VARS.issubset(os.environ):
missing_vars = MANDATORY_ENV_VARS.difference(os.environ)
@Hugovdberg
Hugovdberg / zelfdodingen_CBS_1970-2019.py
Created January 25, 2021 12:05
Aantal zelfdodingen per jaar in Nederland
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
zelfdodingen = (
pd.read_excel(
"https://www.cbs.nl/-/media/_excel/2020/27/aanvullende-statistiek-zelfdoding-2019.xls",
usecols=[0, 3, 7], # Alleen kolommen met jaartal en totaal aantal zelfdodingen
skiprows=4, # Eerste rijen met kolomkoppen overslaan
names=["Jaar", "Totaal", "Totaal per 100.000"],
@Hugovdberg
Hugovdberg / subsurface_cross_section.py
Created December 18, 2019 09:11
Alternative implementation xsboringen
from abc import ABC, abstractmethod
from collections.abc import Sequence
import copy
from shapely.geometry import asShape, LineString
class CopyMixin:
def copy(self, deep=False):
if deep:
return copy.deepcopy(self)
import pandas as pd
tellerstanden = pd.read_excel('Tellerstanden_PIAF.xlsx', sheet_name='Blad3')
tellerstanden = tellerstanden.melt(id_vars=['datum '], var_name='tagcode', value_name='waarde')
tellerstanden.columns = ['datum', 'tagcode', 'waarde']
tellerstanden.to_excel('Tellerstanden_PIAF_long.xlsx', index=False)
@Hugovdberg
Hugovdberg / remify_cornify_thedailywtf.js
Last active August 21, 2018 09:32
Remify & Cornify The Daily WTF
// ==UserScript==
// @name Remify & Cornify The Daily WTF
// @namespace https://github.com/Hugovdberg
// @version 0.2.1
// @description Replace comments with dark green text and highlight the cornify spans.
// @author Hugo van den Berg
// @match thedailywtf.com/*
// @grant none
// ==/UserScript==
@Hugovdberg
Hugovdberg / altair_interactive_multi.tpl
Last active March 16, 2019 12:04
nbconvert template for altair/vegalite output to html, with a hacky way to ensure unique visualisation numbers
{% extends 'full.tpl' %}
{% set altair = {'vis_number': 0} %}
{% block header %}
<script src="https://cdn.jsdelivr.net/npm/vega@3"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@2"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@3"></script>
{{super()}}
{% endblock header %}
@Hugovdberg
Hugovdberg / altair_interactive.tpl
Created August 3, 2018 12:58
nbconvert template with support for (interactive) altair plots
{% extends 'full.tpl' %}
{% block header %}
<script src="https://cdn.jsdelivr.net/npm/vega@3"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@2"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@3"></script>
{{super()}}
{% endblock header %}
{%- block data_priority scoped -%}
library(microbenchmark)
library(ggplot2)
HugovdBerg <- function(x, method = "one", na.rm = FALSE) {
x <- unlist(x)
if (na.rm) {
x <- x[!is.na(x)]
}
# Get unique values