Skip to content

Instantly share code, notes, and snippets.

@jsbueno
jsbueno / gist:bee01f0b52bc3aa7726f0e39b1414390
Created December 19, 2021 08:09
advent of code 2021 day 18
import math
from ast import literal_eval
from numbers import Number
class FN:
def __init__(self, left, right=None, parent=None, position=None):
if right is None:
left, right = left
if isinstance(left, FN):
left = literal_eval(repr(left))
@jsbueno
jsbueno / tkasync.py
Last active September 7, 2021 03:10
asyncio tkinter
"""
Sample file to enable tkinter to run with co-routine and asyncio code
with no need for extra threads or blocking the UI
(this will likely be upgraded to a full package at some point in time)
"""
@jsbueno
jsbueno / rate_limiter.py
Last active August 19, 2021 21:32
Rate Limiter for async APIs. Juste create one guard for different API in your process. Not multiprocess safe!
def create_rate_limit_guard(rate_limit=1200, safety_margin=0.9):
"""Rate limit is given in maximum requests per minute.
"""
# TBD: it would easy to have code to throttle by maximum active requests
# instead of total requests per minute.
# I will just let the accounting of concurrent_requests in place, though
class Guard:
request_interval = (60 / rate_limit) * safety_margin
@jsbueno
jsbueno / monica.html
Created June 28, 2021 20:37
monica colored unicode html image pasted from png with terminedia-paint
<!DOCTYPE html>
<head>
<meta charset="utf-8">
</head>
<body>
<div style="font-family: monospace; width:{width}em; height: {height}em; background: {background}; position: relative; white-space: pre">
<span style="position: absolute; left: 0ch; top: 0em; color: #000000; background: #000000;">█████████████████████████████████████████████████████</span
><span style="position: absolute; left: 53ch; top: 0em; color: #272020; background: #000000;">▄</span
><span style="position: absolute; left: 54ch; top: 0em; color: #232020; background: #000000;">▄</span
><span style="position: absolute; left: 55ch; top: 0em; color: #231F1F; background: #000000;">▄▄</span
@jsbueno
jsbueno / arbitrarily_ordered_dict
Last active March 17, 2021 17:12
Python User Controlable Ordered Dictionary: one can actually control the item order.
"""Class that represents an ordered dict over which
the user can actually control the item order, and
replace keys already in the dicionary in their
original insertion place.
It also does that in O[1]
If used intenselly, where memory leak of
items deleted might be a problem, the
".compact" method should be called from
@jsbueno
jsbueno / helloworld.py
Created October 22, 2020 12:49
Pygame "Hello World"
import random
import pygame
class GameOver(Exception):
pass
SIZE = 800,600
_sentinel = object()
def iter_check_last(iterable):
iterable = iter(iterable)
current_element = next(iterable, _sentinel)
while current_element is not _sentinel:
next_element = next(iterable, _sentinel)
yield (next_element is _sentinel, current_element)
current_element = next_element
@jsbueno
jsbueno / lazyquicksort.py
Last active October 15, 2020 03:54
Lazy sorter - an iterator that yields items in sorted order, lazily
from itertools import tee
def lazy_sort(v, key=lambda x: x):
v = iter(v)
try:
pivot = next(v)
except StopIteration:
return
v_pre, v_pos = tee(v)
yield from lazy_sort((item for item in v_pre if key(item) < key(pivot)), key)
@jsbueno
jsbueno / triangle.py
Last active September 11, 2020 00:31
sample triangle class
class C:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"({self.x}, {self.y})"
def __sub__(self, other):
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
@jsbueno
jsbueno / async_run_example.py
Created July 24, 2020 18:27
Example of Python code to run a slow call to several resources asynchronously and execute some callback function on return
import asyncio
def worker_that_waits_for_network_io(data, ...):
...
return result
def callback_hook(retrieved_data, ...):
...