View shell.bat
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
@echo off | |
REM After creating a venv via py -m venv "%USERPROFILE%\.venvs\MYVENVNAMEHERE" | |
REM Save this as %USERPROFILE%\.venvs\MYVENVNAMEHERE\Scripts\shell.bat | |
REM This script is OK to call from another folder, such as %USERPROFILE% | |
@echo on | |
REM Try python -m idlelib | |
cmd /k "%~dp0\activate.bat" |
View teeinto_lockstep.py
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 greenlet | |
def teeinto_lockstep( | |
it: 'Iterable[TypeVar("Y")]', | |
*consumer_callables: 'Callable[[Iterable[TypeVar("Y")]], TypeVar("R")]' | |
) -> 'Tuple[TypeVar("R"), ...]': | |
"""tee iterable *it* into multiple non-`async def` consumers, in constant memory, without `threading`. | |
The consumer callables must take 1 argument, which is a normal (synchronous) iterable object. |
View Multiset.py
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
"""Multiset implementation. | |
""" | |
__all__ = ["Multiset"] | |
from sortedcontainers import SortedList # https://pypi.org/project/sortedcontainers/ | |
from collections import Counter, deque |
View joint_groupby.py
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 joint_groupby(*iterables, key=None, ordered=True): | |
"""Like itertools.groupby, but iterates through a collection of iterables. | |
If the value type (or the return type of key) is not comparable, you MUST pass ordered=False. | |
Example: [(x, tuple(sum(1 for _ in group) for group in groups)) for x, groups in joint_groupby(...)]""" | |
if key is None: | |
key = lambda x: x | |
iterables = [iter(it) for it in iterables] | |
stop = object() | |
buffer = [next(it, stop) for it in iterables] |
View xgcd.py
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 xgcd(a, b): | |
"https://anh.cs.luc.edu/331/notes/xgcd.pdf" | |
x, x_ = 1, 0 | |
y, y_ = 0, 1 | |
while b: | |
q, b, a = *divmod(a, b), b | |
x, x_ = x_, x - q*x_ | |
y, y_ = y_, y - q*y_ | |
#assert a0*x + b0*y == math.gcd(a0, b0) | |
return x, y |
View ez_generator_interface.py
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 _ez_generator_interface(g): | |
"""Handy wrapper for generator-driven program flows. | |
Example usage: | |
def g(questions=[("What's 3 + 3?", 6), ("What's 9 + 10?", 19)]): | |
"Example generator function that yields queries expecting a reply" | |
for question, correct_answer in questions: | |
answer = yield question # <- KEY LINE | |
if int(answer) != correct_answer: |
View postparse.py
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 email.message, email.parser, email.policy | |
import urllib.parse | |
import re | |
from collections import namedtuple | |
try: | |
from resource import getpagesize | |
except ImportError: | |
import mmap | |
def getpagesize(): | |
return mmap.PAGESIZE |
View last_qualified.py
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 last_qualified(predicate, iterable, /, return_if_exhausted=True): | |
""" | |
Consumes iterable or sequence `iterable` until finding an item | |
not satisfying `predicate`, or until exhausted, then returns the last item | |
which satisfied `predicate`. | |
Raises ValueError if `iterable` is empty. | |
Raises ValueError if the first item from `iterable` does not satisfy `predicate`. | |
View normalized_lru.py
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 functools | |
from .. import NORMALIZE_FUNC, EXPENSIVE_API_CALL | |
@(lambda t: lambda f: functools.wraps(f)(lambda x: f(t(x))))(NORMALIZE_FUNC) | |
@functools.lru_cache | |
def f(x): | |
return EXPENSIVE_API_CALL(x) |
View createxmldoc.js
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
// example: createXMLDocumentWithDeclaration('http://www.topografix.com/GPX/1/1', 'gpx', null, 'version="1.0" standalone="yes"') | |
function createXMLDocumentWithDeclaration(namespaceURI, qualifiedNameStr, documentType=null, declarationData='version="1.0"') { | |
const document = new Document().implementation.createDocument(namespaceURI, qualifiedNameStr, documentType); | |
// https://www.w3.org/TR/xml/#NT-XMLDecl | |
document.insertBefore(document.createProcessingInstruction("xml", declarationData), document.firstChild); | |
return document; | |
} |
NewerOlder