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
| """Use advanced XPath features of lxml (see also scrapy parsel).""" | |
| from collections.abc import Callable | |
| import functools | |
| from typing import Any, Self, overload | |
| import urllib.request | |
| import lxml.etree | |
| import lxml.html |
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
| """Compare ways to return HTML tree streamed and parsed from a given URL.""" | |
| import contextlib | |
| from typing import Literal, overload | |
| import urllib.request | |
| import xml.etree.ElementTree as etree | |
| import certifi | |
| import html5lib | |
| import lxml.html |
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
| """Download and parse ISO 639-3 code tables from https://iso639-3.sil.org download link.""" | |
| from collections.abc import Callable, Iterable, Iterator, Mapping | |
| import contextlib | |
| import csv | |
| import fnmatch | |
| import functools | |
| import html.parser | |
| import http.client | |
| import io |
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
| """Implement `urllib.urlretrieve(url, filename)` with requests library.""" | |
| import contextlib | |
| import os | |
| import urllib | |
| import requests | |
| def urlretrieve(url: str, |
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
| """Download and parse code tables from https://www.ethnologue.com download link.""" | |
| from collections.abc import Callable, Iterable, Iterator, Mapping | |
| import contextlib | |
| import csv | |
| import fnmatch | |
| import functools | |
| import html.parser | |
| import http.client | |
| import io |
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
| """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 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
| """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 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
| """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 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
| """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 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
| """Iterate over dicts with variable keys in predefined order.""" | |
| from collections.abc import Iterable | |
| from typing import Self | |
| class KeyOrder(dict): | |
| """Key -> index mapping for iterating over dicts (unknown keys last).""" | |
| @classmethod |