This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) | |
""" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pygame | |
class GameOver(Exception): | |
pass | |
SIZE = 800,600 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
def worker_that_waits_for_network_io(data, ...): | |
... | |
return result | |
def callback_hook(retrieved_data, ...): | |
... | |