Skip to content

Instantly share code, notes, and snippets.

View plammens's full-sized avatar
🎯

Paolo Lammens plammens

🎯
View GitHub Profile
@plammens
plammens / huffman.py
Created December 18, 2020 18:59
Command-line script to generate a quick Huffman tree
#!/usr/bin/env python3
import argparse
import functools
import heapq
from collections import Counter
from dataclasses import dataclass
from typing import *
import more_itertools as mitt
import networkx as nx
@plammens
plammens / flattendict.py
Created December 18, 2020 18:12
Utility to flatten a dictionary with string values
from typing import Any, Dict, Union
NestedStringDict = Dict[str, Union[Any, "NestedStringDict"]]
def flatten_dict(nested: NestedStringDict, /, *, sep: str = "_") -> Dict[str, Any]:
"""
Flatten a nested dictionary with string keys.
@plammens
plammens / unionenum.py
Last active May 9, 2024 21:34
Enum union in Python
"""
Enum union based on and compatible with the standard library's `enum`.
"""
# MIT License
#
# Copyright (c) 2020 Paolo Lammens
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@plammens
plammens / random_mask.ipynb
Last active August 3, 2020 15:33
Benchmark for methods for creating a random mask in numpy
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@plammens
plammens / plothistory.py
Created June 29, 2019 14:17
Function to plot keras metrics from training history
import keras
import matplotlib.pyplot as plt
def plot_history(history: keras.callbacks.History):
metrics = [metric for metric in history.history.keys() if not metric.startswith('val_')]
stride = len(history.epoch)//20
plotted_epochs = history.epoch[::stride]
fig, subplots = plt.subplots(len(metrics), figsize=(8, 4*len(metrics)))
@plammens
plammens / getcode.py
Last active June 25, 2019 17:55
Python script to gather all of the code in an `RMarkdown` file's code chunks and print it to stdout
import re
import argparse
START_PATT = re.compile(r'^```\{r([ \w]*).*\}$')
END_PATT = re.compile(r'^```$')
HEADER_PATT = re.compile(r'^(?P<level>#+) (?P<name>.+)$')
@plammens
plammens / pngwriter_fix.py
Last active May 18, 2019 10:08
Quick throwaway script for temporarily fixing https://github.com/pngwriter/pngwriter/issues/136
import glob
import re
pattern = re.compile(r"-D(?= *-D)")
for filename in glob.glob("CMakeFiles/*.dir/flags.make"):
print("Inspecting '{}'. ".format(filename), end='')