{{ message }}

Instantly share code, notes, and snippets.

# Alex Shpilkin alexshpilkin

Created May 6, 2021
Quantiles of the binomial and normal distributions
View quantiles.py
 #!/usr/bin/env python3 # SPDX-License-Identifier: CC0-1.0 from math import exp, factorial, pi, sqrt def binom(n, k): if n - k < 0 or n + k < 0: return 0 assert (n - k) % 2 == 0 and (n + k) % 2 == 0 return factorial(n) / (2**n * factorial((n-k)//2) * factorial((n+k)//2)) def gauss(n, k):
Created Mar 14, 2021
 // SPDX-License-Identifier: CC0-1.0 const EMAIL = 3 /* C */, TUTOR = 5 /* E */, PROBLEMS = 7 /* G: */; const TOKEN = 'SLACK_TOKEN'; function slack(url, payload) { var token = PropertiesService.getDocumentProperties().getProperty(TOKEN); if (token === null) throw new Error('Slack bot token not set'); var req = {headers: {'Authorization': `Bearer \${token}`}};
Last active Jan 8, 2021
Rate-limiting utility for tracing and such
View periodically.py
 #!/usr/bin/env python3 from time import monotonic class periodically: def __init__(self, interval): self.interval = interval self._previous = None def __enter__(self): if self._previous is not None:
Created Jan 7, 2021
Trio channels with priorities
View trio_priority.py
 from heapq import heappush, heappop from math import inf from trio import BrokenResourceError, ClosedResourceError, EndOfChannel, WouldBlock from trio.abc import ReceiveChannel, SendChannel from trio.lowlevel import ParkingLot, checkpoint, checkpoint_if_cancelled, cancel_shielded_checkpoint from trio._channel import MemoryChannelStats from trio._util import NoPublicConstructor class MemoryChannelState: __slots__ = ('data', 'max_buffer_size', 'number', 'open_send_channels',
Created Jun 17, 2019
Pipe video from DVR to GStreamer
View gstream.py
 #!/usr/bin/env python3 from gi import require_version require_version('Gst', '1.0') require_version('Gtk', '3.0') from gi.repository import GObject, Gst, Gtk from os.path import abspath from sys import argv, stderr from threading import Thread
Last active May 10, 2019
View moscow.tsv
45 Город Москва 45 301 муниципальный округ Богородское 45 302 муниципальный округ Вешняки 45 303 муниципальный округ Восточное Измайлово 45 304 муниципальный округ Восточный 45 304 000 106 п Восточный 45 304 000 111 п Акулово 45 305 муниципальный округ Гольяново 45 306 муниципальный округ Ивановское 45 307 муниципальный округ Измайлово
Last active Apr 24, 2019
View diffs.sh
 #!/bin/sh -eu # NB: Contains literal tabs caption() { python -c ' from os import getenv from sys import argv from telegram import Bot from telegram.error import BadRequest, TimedOut
Last active Sep 4, 2019
Access SOAP APIs of the Ukrainian DRV with Python and Zeep
View drv.wsdl

Last active Apr 14, 2019
Aggregate multiple CVK scrapes into precinct histories
View aggregate.py
 #!/usr/bin/env python3 # NB Depends on dict preserving the insert order (CPython >= 3.6, PyPy all) from csv import DictReader from datetime import datetime as DateTime from os import scandir from simplejsonseq import dump from sys import argv, stderr, stdout import ietfcsv # ietf-tab CSV dialect
Last active Apr 3, 2019
Scrape rutracker.org forums
View ruforum
 #!/bin/sh -eu set -o pipefail TRACE=\${TRACE-}; export TRACE tracef() { fmt=\$1; shift if [ -t 2 ]; then printf '\033[0K%s'"\$fmt"'\r' "\$TRACE" "\$@" >&2; fi } trap 'tracef ""' EXIT