Skip to content

Instantly share code, notes, and snippets.

View TheRolfFR's full-sized avatar
💼
Working

TheRolf TheRolfFR

💼
Working
View GitHub Profile
@TheRolfFR
TheRolfFR / MinecraftVersionSorter.js
Last active September 16, 2024 18:55
Minecraft version array sorter for js
// MinecraftVersionSorter.js 1.0 by TheRolf
// to use it, just just need to pass it as arg of the sort function for an array
// ["1.4.9","1.7","1.7.10","1.12.2","1.17.1","1.16.210","1.18"].sort(MinecraftSorter)
const MinecraftVersionSorter = (a, b) => {
const aSplit = a.split('.').map(s => parseInt(s))
const bSplit = b.split('.').map(s => parseInt(s))
if(aSplit.includes(NaN) || bSplit.includes(NaN)) {
return String(a).localeCompare(String(b)) // compare as strings
@TheRolfFR
TheRolfFR / retry_context.py
Last active August 13, 2024 15:43
Python Retry manager with a generator and context manager
class RetryContext:
"""Retry context manager creator
created by TheRolf"""
def __init__(self, retries=1, exceptions=(Exception,), delay=0.0, on_fail=None) -> None:
"""Creates retry context creator
Args:
retries (int, optional): Number of retries allowed. Defaults to 1.
@TheRolfFR
TheRolfFR / guide.md
Created June 3, 2024 17:15
Install Home Assistant SkyConnect to Home Assistant virtual machine with virsh

How to attach Home Assistant SkyConnect to your Home Assistant VM

  1. Buy the godamn module
  2. Get your VM with your ssh

Attach a device via virsh

This guide is based of this guide from RedHat. I must say it was pretty useful.

  1. In order to locate the SkyConnect device via USB. first run lsusb before plugging the module:
@TheRolfFR
TheRolfFR / str_rstrip.c
Created February 22, 2024 16:07
Simple C rstrip implementation
/**
* @brief Strips all the characters at the end
* @author TheRolf
*
* @param str target string
* @param chars chars to find and remove
*/
void str_rstrip(char* str, const char* chars)
{
size_t size_str = strlen(str);
@TheRolfFR
TheRolfFR / save_restore_set.md
Last active July 25, 2023 13:28
Quick save, restore and set with multiline lambda

Create temporary setup for pytest with context managers

My need

To create a temporary setup for pytest, you may think fixtures. But my case isn't like yours.
I needed to change tempolarly a value in the middle of my test so I could get faster results.

The clean solution

We will use the with statement. It will make a dedicated "scope" to that temporary setup. I'm sure that you already used them to open files. It is particularly powerful as it creates a context manager, which increases readibility, and protects developers from themselves.

Why do clean? Because it's an addiction since I discovered C++ destructors, but above all, Rust drop trait.

@TheRolfFR
TheRolfFR / install.md
Created December 30, 2022 18:52
Modify Apache2::Imager::Resize to fix iCCP: known incorrect sRGB profile error

Modify Apache2::Imager::Resize to fix iCCP: known incorrect sRGB profile error

First this error is not your module or your library in fault, it's the libpng library fault.

Since libpng 1.6 release, the library became more aggresive about incorrect iCCP data chunks. To fix that problem, you could go to a prior version or modify the images so that the iCCP chunk is removed.


Install ImageMagick with your packet manager:

@TheRolfFR
TheRolfFR / dark_theme_store.js
Last active December 13, 2022 09:01
Dark theme color scheme store with watch
/**
* Dark theme color scheme store with watch
* full demo at https://codepen.io/TheRolf/pen/xxzNreP
* @author TheRolf
*/
window.DarkThemeStore = {
VALUES: {
AUTO: 'auto',
DARK: 'dark',
LIGHT: 'light'
@TheRolfFR
TheRolfFR / SmartPointer.h
Created November 23, 2022 08:50
Smart Pointer implementation
template <class Type>
class SmartPointer
{
private:
Type * pointer;
public:
SmartPointer() : pointer(nullptr) {}
SmartPointer(Type * p) : pointer(p) {}
Type * set(Type * p) { pointer = p; return p; }
Type * get() { return pointer; }
@TheRolfFR
TheRolfFR / tiny_string.cpp
Created October 20, 2022 13:43
C++ convert int to string
std::string from_int(int value) {
int tmp = value;
std::string valueAsString = tmp < 0 ? "-" : "";
size_t pos = valueAsString.length();
if(tmp < 0) tmp = -tmp;
else if(tmp == 0) valueAsString = "0";
while(tmp != 0) {
int digit = tmp%10;
valueAsString.insert(pos,1,'0' + digit);
@TheRolfFR
TheRolfFR / xml_ie_format.js
Created September 22, 2022 15:23
XML ie format
let value;
let spaces;
spaces = 0;
console.log(value.split('\n').filter(f => f.trim().length !== 0).map(cur => {
if(cur.startsWith('-')) spaces ++;
else if(cur.startsWith('</')) spaces --;
else if(cur.startsWith('<')) spaces++;
let sp = '';