Skip to content

Instantly share code, notes, and snippets.

View MarcinKonowalczyk's full-sized avatar
😴
Sleeping

Marcin Konowalczyk MarcinKonowalczyk

😴
Sleeping
View GitHub Profile
@MarcinKonowalczyk
MarcinKonowalczyk / main.py
Last active May 7, 2024 16:05
generic namedtuple
from typing import TYPE_CHECKING, TypeVar, Generic, Tuple
from typing_extensions import reveal_type
T1 = TypeVar("T1")
T2 = TypeVar("T2")
if TYPE_CHECKING:
# This is how Group looks like to the type checker
class Group(Tuple[T1, T2], Generic[T1, T2]):
def __new__(cls, key: T1, group: T2) -> "Group[T1, T2]": ...
package main
import (
"fmt"
"math"
"math/rand"
)
func makeData(p []float64, N int, noise float64) [][]float64 {
// Make a bunch of points on the XY plane according to the function Z = (X - xo)^2 + (Y - yo)^2 + noise
@MarcinKonowalczyk
MarcinKonowalczyk / joinable_queue_pingpong.py
Last active April 2, 2024 13:45
Pingpong between two processes using a single JoinableQueue
from typing import cast
from multiprocessing import Manager, Process
from multiprocessing.queues import JoinableQueue
import time
class Palette(Process):
def __init__(
self,
queue: JoinableQueue,
@MarcinKonowalczyk
MarcinKonowalczyk / sequence_sampling_experiment.py
Created February 5, 2024 02:43
Empirically determined how many subsequence samples it takes to sample entire sequence
# Test how many subsequences we want to sample from a sequence to, approximately, cover the entire sequence.
import numpy as np
# ======
N = 1982 # sequence length
L = 79 # sequence length
# ======
R = 100 # number of repetitions to average out randomness
@MarcinKonowalczyk
MarcinKonowalczyk / typed_print.py
Last active January 5, 2023 10:49
Slow print
import time, random
__python_print = print # save the original print function
def typed_print(*values, sep=' ', end='\n', file=None, flush=True):
""" Prints the values with a random delay between each character to make it look like it is being typed. The flush parameter must be True for this to work, otherwise it will just use the normal print function. """
if flush == False:
# without flush this wont work so just use the normal print
__python_print(*values, sep=sep, end=end, file=file, flush=flush)
import inspect
from operator import itemgetter
def var_name(var, *, ignore_underscore: bool = True, fix: bool = True):
""" Find all the variable names in caller's parent scope whos value is 'var'
Example
-------
>>> def meta_printer(var):

One liner to upgrade pip, install pip-chill and use to create requirements.txt file for the current project.

The actual file crearted is requirements.~txt in order not to accidentally overwrite an existing requirements file.

pip install -Uq pip && pip install -Uq pip-chill && pip-chill -v | grep -v pip-chill | tee 'requirements.~txt'
@MarcinKonowalczyk
MarcinKonowalczyk / RUN_SH.md
Last active July 6, 2021 12:51
vscode script to run .vscode/run.sh script on the current file

run.sh

Vscode keybinding script to run .vscode/run.sh in bash, passing the current file as an input.

instructions

Add the keybinding to keybindings.json, then in your project make the .vscode folder (if there isnt one there already and add run.sh file to it). It will be run each time you press the keybinding shift+enter with an editor focus.

[ -d ".vscode" ] || mkdir ".vscode"
@MarcinKonowalczyk
MarcinKonowalczyk / rect_circ_collision.pde
Created January 18, 2021 02:49
[Processing] Fast collision detection between axis-aligned rectangle and a circle
Circle c;
Rectangle r;
void setup() {
size(500,500);
frameRate(600);
rectMode(CORNERS);
c = new Circle(width/2, height/2, 80);
r = new Rectangle(width/2, height/2, 160, 80);
}
@MarcinKonowalczyk
MarcinKonowalczyk / dependency_check.m
Last active September 17, 2020 16:05
[Matlab] Check all the files in the current folder for dependencies
close all; clear; clc;
%% Find all the .m files
all_files = dir(pwd);
m_files = {};
for j = 1:numel(all_files)
f = all_files(j).name;
if numel(f)>2 && strcmp(f(end-1:end),'.m')
if ~strcmp(f(1:end-2),mfilename)
m_files(end+1) = {f}; %#ok<SAGROW>