Skip to content

Instantly share code, notes, and snippets.

from typing import Dict, Any, DefaultDict, Optional, TYPE_CHECKING
from collections import defaultdict
if TYPE_CHECKING:
TreeDict = Dict[Any, Union[Any, "TreeDict"]]
TreeDefaultDict = DefaultDict[Any, Union[Any, "TreeDefaultDict"]]
def tree_dict_factory(nested_dict: Optional["TreeDict"] = None) -> "TreeDefaultDict":
"""
@laundmo
laundmo / random_outside.rs
Created August 25, 2024 05:22
generate random points outside the screen bevy
use bevy::prelude::*;
use rand::{thread_rng, Rng};
fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(1.0, 0.0, 0.0)))
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, point_out)
.run();
@laundmo
laundmo / triple.py
Last active August 27, 2024 20:59
largest pythagoras triple which fits in 10000 chars
# theory: starting at 3,4,5 use the "maximise" function to compute what a multiple of this triple exactly inside the limit looks like
# this is far from a ideal triple, as the quotient of a/b closer to 1 means a more ideal triple starting point (triangle sides which are the most equal possible)
# so we generate all triples with a, b=a+1, c using the method described in this link:
# https://math.stackexchange.com/questions/3399189/looking-for-the-best-way-to-find-pythagorean-triples-where-b-a-pm1
# then we maximise the found triple and check whether its product is larger than the previous largest
# we stop once all triples which unmaximised fit into the limit have been checked, as any larger ones can't be used anyways
# largest value which will fit
limit = int("9" * 10_000)
@laundmo
laundmo / gist:a5a007d99b0f087079346f3479afe7e9
Created June 20, 2024 14:51
Copy a functions/methods signature for typing without specifying each individual argument type
from typing import Callable, ParamSpec, TypeVar, cast
P = ParamSpec("P")
T = TypeVar("T")
def copy_sig(source_func: Callable[P, T]) -> Callable[[Callable[..., T]], Callable[P, T]]:
"""Decorator to copy the function signature from source_func
Especially useful when overwriting methods with many arguments
@laundmo
laundmo / lerp.rs
Created December 28, 2023 14:47
lerp, inverse_lerp and remap in Rust
trait Lerp<T> {
fn lerp(self, from: T, to: T) -> T;
fn inv_lerp(self, from: T, to: T) -> T;
fn remap(self, from_range: (T, T), to_range: (T, T)) -> T;
}
macro_rules! lerp_impl {
($one:literal, $typ:ty) => {
impl Lerp<$typ> for $typ {
#[inline(always)]
@laundmo
laundmo / stylus.css
Created November 24, 2023 21:53
twitch table layout
.tw-tower {
flex-flow: row wrap;
flex-direction: column;
}
.tw-tower article {
flex-direction: row-reverse !important;
}
.tw-tower article > div > div > div {
@laundmo
laundmo / chainmap.py
Last active October 19, 2023 08:13
Pydantic: ChainMap as a custom type
"""
Copyright 2023 laundmo
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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER D
@laundmo
laundmo / safe.sh
Last active May 4, 2023 13:35
compose up safety
docker-compose() {
if [[ $@ == "up" ]]; then
read -p "Without a -d flag, all containers will attach to the current terminal and will need to be stopped to detach. Are you sure?"
command docker-compose "$@"
else
command docker-compose "$@"
fi
}
@laundmo
laundmo / Dockerfile
Created April 12, 2023 19:22
Rust dockerfile caching
FROM rust:1.65 as rust-builder
WORKDIR /usr/src/gt_bot
# Copy cargo
COPY ./Cargo.toml .
COPY ./Cargo.lock .
# Create fake main.rs file in src and build for dependencies
RUN mkdir ./src && echo 'fn main() { println!("Dummy!"); }' >./src/main.rs
RUN cargo build --release
@laundmo
laundmo / openfilepath.js
Last active October 19, 2024 01:28
Trilium widget to open italicized file paths. Only works in Desktop app. Add as "JS Frontend" note and make sure it has the #widget property.
/* License: MIT https://opensource.org/licenses/MIT
* Made by: GEOsens GmbH 2023
*
* Usage: Italicize the file path. The italicized file or folder can be opened with a double click.
*
* Note: there is not indication that a path is clickable, thanks ckeditor.
*/
async function onClickOpenPath(event) {
// check if event was on Italicised