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
"""Download and parse ISO 639-3 code tables from https://www.sil.org.""" | |
from __future__ import annotations | |
from collections.abc import Iterable, Iterator, Mapping | |
import contextlib | |
import csv | |
import enum | |
import fnmatch | |
import functools |
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
"""Implement `urllib.urlretrieve(url, filename)` with requests library.""" | |
import contextlib | |
import os | |
import urllib | |
import requests | |
def urlretrieve(url: str, |
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
"""Download and parse https://www.ethnologue.com code files.""" | |
from __future__ import annotations | |
from collections.abc import Iterable, Iterator, Mapping | |
import contextlib | |
import csv | |
import enum | |
import fnmatch | |
import functools |
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
"""Longest common prefix.""" | |
import itertools | |
def common_prefix(left: str, right: str) -> str: | |
"""Return the case-insensitive longest common prefix of two strings. | |
>>> common_prefix('spam', 'spameggs') | |
'spam' |
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
"""Pure-python replacement for scipy.stats.pearsonr.""" | |
from collections.abc import Sequence | |
import itertools | |
import math | |
import operator | |
def pearsonr(X: Sequence[int], Y: Sequence[int]) -> float: | |
"""Return the correlation coefficient between the variable sequences X and Y. |
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
"""Search/replace func from string roughly like 'sed -r s/old/new/g'.""" | |
from collections.abc import Callable | |
import functools | |
def search_replace(cmd: str, *, _cache={}) -> Callable[[str], str]: | |
"""Return a callable from sed/Perl-style search-replace pattern string. | |
>>> search_replace('s/ham/spam/g')('ham-eggs-ham') |
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
"""Apply search-replace rules with re.""" | |
from collections.abc import Iterable | |
import re | |
class Replace: | |
"""Multiple search-replace with first-matching regex.""" | |
def __init__(self, pairs: Iterable[tuple[str, str]]) -> None: |
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
"""Iterate over dicts with variable keys in predefined order.""" | |
from __future__ import annotations | |
from collections.abc import Iterable | |
class KeyOrder(dict): | |
"""Key -> index mapping for iterating over dicts (unknown keys last).""" |
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
"""More convenient context manager for mmap.mmap.""" | |
from collections.abc import Iterator | |
import contextlib | |
import mmap | |
import os | |
@contextlib.contextmanager | |
def memorymapped_compat(path: os.PathLike | str) -> Iterator[mmap.mmap]: |
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
"""Find all subclasses of a class with queue or stack.""" | |
import collections | |
from collections.abc import Collection, Iterator | |
def itersubclasses(parent: type, *, | |
proper: bool = True, | |
exclude: Collection[type] = (type,)) -> Iterator[type]: | |
"""Yield `parent` subclasses recursively in breadth-first order.""" |