Skip to content

Instantly share code, notes, and snippets.

View valsteen's full-sized avatar

Vincent Alsteen valsteen

View GitHub Profile
@valsteen
valsteen / option.go
Created December 30, 2022 15:39
Option generics with (un)marshalling
package main
import (
"encoding/json"
"fmt"
)
type TenantProfileEntity struct {
SomeOtherValue string
}
@valsteen
valsteen / chain.go
Created November 6, 2022 12:59
Chaining calls until something fails, with error mapping
package main
import (
"errors"
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
@valsteen
valsteen / queue_processing.go
Created October 15, 2022 20:47
Queue processing, using errgroup
/*
This demonstrates a task scheduling loop where tasks can send back other tasks to the queue.
Program input: a nested list of items that can either be integers, or nested slices:
- when it's an integer, wait for 25 - value seconds
- when it's a slice, wait for len(slice) seconds, then put back each item in the queue to be processed next
waiting time simulates some processing, on which a concurrency limit is applied ( both waiting times share the same
limit ). pushing back to the queue should not be blocking.
@valsteen
valsteen / Generics_golfing.go
Last active October 8, 2022 22:58 — forked from plorenz/example.go
go generics test
package main
import (
"errors"
"fmt"
)
type Example interface {
Init(config map[string]interface{})
}
@valsteen
valsteen / mermaidify.py
Last active September 23, 2022 10:28
Quick and dirty : converting from sequencediagram.org charts to mermaid
import re
from sys import stdin
def main():
result = ""
title_re = re.compile("^title (.*)$")
right_arrow_re = re.compile(r"(.+?)\s*(-?)->\s*(.+?)(:(.+))?$")
left_arrow_re = re.compile(r"(.+?)\s*<-(-?)\s*(.+?)(:(.+))?$")
empty = re.compile("^\s*$")
@valsteen
valsteen / queue_processing.go
Last active March 20, 2023 02:47
Self-feeding processing queue
/*
This demonstrates a task scheduling loop where tasks can send back other tasks to the queue.
Program input: a nested list of items that can either be integers, or nested slices:
- when it's an integer, wait for 25 - value seconds
- when it's a slice, wait for len(slice) seconds, then put back each item in the queue to be processed next
waiting time simulates some processing, on which a concurrency limit is applied ( both waiting times share the same
limit ). pushing back to the queue should not be blocking.
@valsteen
valsteen / queue_processing.py
Last active November 27, 2022 10:41
Self-feeding task queue using python's asyncio
"""
This demonstrates a task scheduling loop where tasks can send back other tasks to the queue.
Program input: a nested list of items that can either be integers, whose processing is simulated by waiting
a proportional amount of time, or another list, in which case after waiting a time proportional to its length,
sub-items are rescheduled for processing.
Solution is inspired by goroutines, channels and waitgroups.
Asyncio's Queue can serve both as channel and waitgroup, thanks to Queue.join() that blocks until the queue is empty.
@valsteen
valsteen / Cargo.toml
Last active September 12, 2022 19:49
Self-feeding processing stream proof of concept
[package]
name = "processing_stream"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
futures = "0.3.24"
tokio = { version = "1.21.0", features = ["full"] }
@valsteen
valsteen / pointercontraint.go
Created August 8, 2022 14:18
Go: constraint a generic type to implement an interface with a pointer receiver
package main
// Demonstrates how to constraint a generic type to implement an interface with a pointer receiver
// inspired by this SO answer: https://stackoverflow.com/a/71444968
import (
"fmt"
"strconv"
"strings"
)
@valsteen
valsteen / concurrency_limit.py
Last active July 25, 2022 09:48
How to limit concurrency with Python asyncio?
import asyncio
from typing import Awaitable, Callable, Coroutine, Iterator
from asyncio_pool import AioPool
import pytest as pytest
from more_itertools import peekable
"""
Different approaches to "How to limit concurrency with Python asyncio?"
https://stackoverflow.com/questions/48483348/how-to-limit-concurrency-with-python-asyncio/48484593#48484593