This file contains hidden or 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
// if performance is not important | |
function compress(str) { | |
/* shorten <str> by reducing consecutive character repetitions */ | |
return str.replace(/(.)\1*/g, function(fullMatch, g1) { | |
return g1 + fullMatch.length; | |
}); | |
} | |
console.assert(compress('') === '', "blank string"); | |
console.assert(compress('a') === "a1", "single char"); |
This file contains hidden or 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
function parseURI(uri) { | |
/* parse <uri> into invidual components (loosely, some errors allowed) */ | |
// see https://regex101.com/r/l96l7F/1 for explanation | |
var regex = /(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?((?:\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?[^?#\/]*)(?:\?([^#]*))?(?:#(.*))?/, | |
groupNames = [ | |
"fullMatch", | |
"scheme", | |
"username", | |
"password", |
This file contains hidden or 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 collections import defaultdict | |
from asyncio import Semaphore | |
MAX_CONNECTIONS_PER_MX = 1 | |
leaky_bucket = defaultdict(lambda: Semaphore(MAX_CONNECTIONS_PER_MX)) | |
async def ping_server_handler(request): | |
... |
This file contains hidden or 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
function poetry() { | |
dot_env_path=$(pwd) | |
while [[ "$dot_env_path" != "" && ! -e "$dot_env_path/.env" ]]; do | |
dot_env_path=${dot_env_path%/*} | |
done | |
# if POETRY_DONT_LOAD_ENV is *not* set, then load .env if it exists | |
if [[ -z "$POETRY_DONT_LOAD_ENV" && -f "$dot_env_path/.env" ]]; then | |
>&2 echo 'Loading .env environment variables…' | |
This file contains hidden or 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 time | |
import random | |
from multiprocessing import Process | |
def f(): | |
while True: | |
time.sleep(10) | |
dice = random.randint(1, 10) |
This file contains hidden or 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 time | |
import random | |
from signal import signal, SIGINT, SIGTERM | |
from multiprocessing import Process, Event | |
""" | |
There are two examples: | |
(1) with graceful shutdown | |
(2) without graceful shutdown |
This file contains hidden or 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 time | |
import zlib | |
from io import BytesIO | |
from zipfile import ZipFile | |
import requests | |
from bs4 import BeautifulSoup | |
HEADERS = { |
This file contains hidden or 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 os | |
import time | |
from PIL import Image | |
import requests | |
from bs4 import BeautifulSoup | |
# requires brotlipy, as the server uses brotli compression | |
username = os.environ["SSM_USERNAME"] |
This file contains hidden or 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 time | |
from PIL import Image | |
import requests | |
from bs4 import BeautifulSoup | |
HEADERS = { | |
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:88.0) Gecko/20100101 Firefox/88.0", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", | |
"Accept-Language": "en-US,en;q=0.5", |
OlderNewer