Skip to content

Instantly share code, notes, and snippets.

import enum
import time
import uuid
import logging
from typing import Any, Callable, Dict
from concurrent.futures import ProcessPoolExecutor, Future, Executor
class Status(enum.Enum):
ONGOING = "ongoing"
QUEUED = "queued"
@freol35241
freol35241 / setup.py
Last active February 14, 2023 08:32
Setup.py allowing for reading version string from an environment variable
import setuptools
from packaging.version import Version
with open("README.md", "r") as fh:
long_description = fh.read()
with open("requirements.txt") as f:
required = f.read().splitlines()
# Try to read version string from env variable
@freol35241
freol35241 / CMakeLists.txt
Last active May 29, 2021 09:03
CPM.cmake + Boost (header libs only)
CPMAddPackage(
NAME Boost
VERSION 1.76.0
URL https://boostorg.jfrog.io/artifactory/main/release/1.76.0/source/boost_1_76_0.tar.gz
DOWNLOAD_ONLY True
)
if(Boost_ADDED)
# Define the header-only Boost target
add_library(Boost::boost INTERFACE IMPORTED GLOBAL)
target_include_directories(Boost::boost SYSTEM INTERFACE ${Boost_SOURCE_DIR})
@freol35241
freol35241 / polyfit.hpp
Last active May 26, 2021 14:22
Polyfit function in C++ using ordinary least squares fit
#include <vector>
#include "Eigen/Dense"
template <int... Orders>
Eigen::ArrayXd polyfit1D(const Eigen::ArrayXd& x, const Eigen::ArrayXd& y) {
std::vector<int> orders{{Orders...}};
if (x.size() != y.size()) {
throw std::runtime_error("Polyfit failed: x and y must have same size!");
}
@freol35241
freol35241 / call_every.py
Created June 15, 2021 04:52
A decorator for calling the decorated function at a specific interval
# Requires:
# pip install fastapi
# pip install psutil
import asyncio
import time
from typing import Callable
import os
import psutil
import logging
import warnings
# MIT License
# Copyright (c) 2021 freol35241
# 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:
@freol35241
freol35241 / upy_wsl2_dev_setup.md
Last active July 26, 2022 13:38
Micropython dev setup on WSL2

Micropython development setup on WSL2 (Ubuntu 20.04 on Windows 10)

  1. Make sure your WSL2 distro can see the USB devices: https://devblogs.microsoft.com/commandline/connecting-usb-devices-to-wsl/
  2. Install some very handy udev rules (from the platformIO project) to give your user permissions to talk with the connected board: https://docs.platformio.org/en/latest/core/installation/udev-rules.html#platformio-udev-rules
  3. TODO...

Initial board setup

  1. Connect board via usb
  2. Attach usb to WSL as described in:
@freol35241
freol35241 / Nordpool.h
Last active October 4, 2022 10:19
Arduino code for ESP8266/ESP32 for fetching future electricity prices from the nordic energy price market "Nordpool" using Vattenfall's API
// MIT License
//
// Copyright (c) 2022 freol35241
//
// 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:
@freol35241
freol35241 / logger.h
Last active October 4, 2022 10:19
A simple logging macro for the Arduino framework
// MIT License
//
// Copyright (c) 2022 freol35241
//
// 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:
from itertools import zip_longest
SEPARATOR = "/"
SINGLE = "+"
ALL = "#"
def mqtt_match(pattern: str, topic: str) -> bool:
"""Evaluate if a topic matches a pattern