Skip to content

Instantly share code, notes, and snippets.

@alexshpilkin
alexshpilkin / dop.py
Last active July 8, 2023 12:31
Deprecating the Observer pattern in Python
from collections.abc import Awaitable
from heapq import heappop, heappush
from math import inf
from weakref import WeakSet
try:
from functools import singledispatch
except ImportError:
from pkgutil import simplegeneric as singledispatch
@alexshpilkin
alexshpilkin / README
Last active June 15, 2023 21:46
Convert the RKN list to a Mikrotik firewall address list
Download the RKN list from https://github.com/zapret-info/z-i, massage it into
a Mikrotik script that manages a firewall address list, and put it up on a web
server. Note well that the list is gigantic---more than 80,000 entries as of
2018-04-28---so be sure to check if your router can handle it in advance (for
example, hAP Lite, with its measly 32M of memory, can’t).
On the router side, do something like
/tool fetch url="https://sheaf.site/rkn.rsc" dst-path=rkn.rsc
:import rkn.src
@alexshpilkin
alexshpilkin / drv.wsdl
Last active February 3, 2023 23:25
Access SOAP APIs of the Ukrainian DRV with Python and Zeep
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:tns="http://www.drv.gov.ua/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
targetNamespace="http://www.drv.gov.ua/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://www.drv.gov.ua/">
@alexshpilkin
alexshpilkin / cond.lua
Last active April 29, 2022 00:08
Condition handling in Lua
local M = {}
local error, unpack = error, unpack or table.unpack
local running = coroutine.running
local stderr = io.stderr
local exit = os.exit
local insert, remove = table.insert, table.remove
-- conditions
@alexshpilkin
alexshpilkin / unfuck.py
Last active September 26, 2021 19:46
Data obfuscation on the Russian Central Election Commission website
#!/usr/bin/env python3
#{ SPDX-License-Identifier: CC0-1.0 }
from collections import namedtuple
from fontTools.ttLib import TTFont
from io import BytesIO
from lxml.html import document_fromstring
from lxml.etree import tostring
from re import finditer, compile as re_compile
from requests import get
@alexshpilkin
alexshpilkin / quantiles.py
Created May 6, 2021 16:56
Quantiles of the binomial and normal distributions
#!/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):
@alexshpilkin
alexshpilkin / grades.js
Created March 14, 2021 00:27
Google Apps Script program to post student grades
// 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}`}};
@alexshpilkin
alexshpilkin / periodically.py
Last active January 8, 2021 18:53
Rate-limiting utility for tracing and such
#!/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:
@alexshpilkin
alexshpilkin / trio_priority.py
Created January 7, 2021 22:11
Trio channels with priorities
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',
@alexshpilkin
alexshpilkin / gstream.py
Created June 17, 2019 10:25
Pipe video from DVR to GStreamer
#!/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