Skip to content

Instantly share code, notes, and snippets.

@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='')
@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 / 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 / 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 / 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 / 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 / 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 / innerclass.py
Last active December 22, 2020 12:04
Inner classes in Python
"""
Metaclass for inner classes in Python.
"""
# 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 / rapply.py
Last active January 2, 2021 01:49
Recursive apply: apply a function recursively to all sub-objects
"""
Algorithm to recursively apply a function to a composite object.
Dependencies:
- multimethod (https://pypi.org/project/multimethod/)
"""
# MIT License
#
# Copyright (c) 2020 Paolo Lammens
@plammens
plammens / koch.py
Last active March 25, 2021 13:57
Animation of the Koch Snowflake wiht polygonal segments
# MIT License
#
# Copyright (c) 2021 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
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions: