Skip to content

Instantly share code, notes, and snippets.

View valsteen's full-sized avatar

Vincent Alsteen valsteen

View GitHub Profile
@valsteen
valsteen / task_executor_with_queue_size_limit.py
Created May 28, 2021 12:23
Task queue executor with queue size limit
import random
from concurrent.futures import ThreadPoolExecutor
from queue import Queue
from time import sleep
class ThreadPoolExecutorWithQueueSizeLimit(ThreadPoolExecutor):
# limiting queue size allows to keep under control the memory used by the producer
# inspired by https://stackoverflow.com/a/48327162/34871
def __init__(self, max_queue_size: int, *args, **kwargs):
@valsteen
valsteen / .gitignore
Last active May 25, 2021 18:54
Example of concurrent processing with rate limit and cancellation with async-std
/target
@valsteen
valsteen / adventofcode2020_day23_part_2.rs
Created February 28, 2021 19:43
Advent of code 2020 day 23 part 2
/*
solution to the second part of https://adventofcode.com/2020/day/23
it executes in less than a minute in release mode, but it ain't pretty
main points:
- double linked list to easily insert/split/remove
- the nodes are not individual values, but ranges instead
- because looking up a value in a linked list takes so long, there is a hashmap pointing to the
slices for each value. everytime a slice is added or changed, all the values of the slice
@valsteen
valsteen / Main.java
Last active November 9, 2020 11:36
java enterprise ternary operator
package com.company;
/**
java -cp . com.company.Main "4" "3" "2"
4 is even
3 is not even
2 is even
*/
@valsteen
valsteen / reorder.py
Created April 22, 2016 12:24
stupid utility that receives timestamped lines in any order and various formats, then outputs them reordered
import dateutil.parser
import re
import sys
import pytz
tz = pytz.timezone("Europe/Vienna")
res = []
for line in sys.stdin:
@valsteen
valsteen / adventofcode23.py
Created December 23, 2015 15:58
Python solution for Advent of Code day 23 http://adventofcode.com/day/23
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import re
from collections import defaultdict
from functools import partial
i = """jio a, +18
inc a
tpl a
@valsteen
valsteen / controlsurface.py
Created September 20, 2015 16:34
MIDI to sysex
def reply_sysex(self, message):
from array import array
try:
raw = array('B')
raw.fromstring(message.encode('utf-16-le'))
raw = raw.tolist()
raw.insert(0, 240)
raw.append(247)
self._send_midi(tuple(raw), False)
@valsteen
valsteen / gist:29a22d6e7cd57360b174
Last active September 20, 2015 16:23
how to keep python interpreter busy in ableton
def pause(self):
# helper for rconsole. This blocks ableton inside python on purpose, otherwise the console thread
# is super slow ( python interpreter is only active when needed )
def lock():
if self.console_event.isSet():
return
# this trick works with Event and not with just time.sleep, because in python2 Event.wait is a loop+sleep, which keeps the interpreter busy
self.console_event.wait(0.1)
self.schedule_message(3, lock)
@valsteen
valsteen / gist:d3669888ed5c37b7ccf9
Created December 24, 2014 12:39
django-autocomplete-light==2.0.3 + jquery.formset.js 1.3-pre
<script src="{% static 'js/jquery.formset.js' %}"></script>
<script>
$(function () {
$('#myformset tbody tr').formset({
'added': function (row) {
var widget = $(".autocomplete-light-widget", row);
widget.yourlabsWidget('destroy');
widget.find('input').yourlabsAutocomplete('destroy');
widget.trigger('initialize');
}
@valsteen
valsteen / myclass.py
Last active December 31, 2015 09:09
Demonstration of private variables, one using name mangling of attributes prepended by a double underscore, and using a closure like in javascript
def myclass_factory():
"""
Run this test with nosetests --with-doctest myclass.py
>>> myclass = myclass_factory()
>>> myclass.__private_attribute
Traceback (most recent call last):
...
AttributeError: 'MyClass' object has no attribute '__private_attribute'