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 have unique columns with NULLs.""" | |
import os | |
import subprocess | |
import time | |
import uuid | |
import sqlalchemy as sa | |
import sqlalchemy.orm |
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
"""SQL-injection safe dynamic query with pl/pgsql.""" | |
import sqlalchemy as sa | |
UNIQUE_NULL = [('contributioncontributor', ['contribution_pk', 'contributor_pk'], []), | |
('contributionreference', ['contribution_pk', 'source_pk', 'description'], []), | |
('editor', ['dataset_pk', 'contributor_pk'], []), | |
('languageidentifier', ['language_pk', 'identifier_pk'], []), | |
('languagesource', ['language_pk', 'source_pk'], []), | |
('sentencereference', ['sentence_pk', 'source_pk', 'description'], []), |
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
"""Decorator with an optional parameter.""" | |
from collections.abc import Callable | |
import functools | |
from typing import Optional | |
FUNCS = {} | |
def register(func: Optional[Callable] = 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
"""Split a string into chunks by a pattern matching at the start of each item. | |
>>> list(itersplit(r'!', 'spam !eggs !ham')) | |
['spam ', '!eggs ', '!ham'] | |
>>> list(itersplit(r'X', 'spam !eggs !ham')) | |
['spam !eggs !ham'] | |
>>> list(itersplit(r'!', '!spam !eggs !ham')) | |
['', '!spam ', '!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
"""os.walk() variation with Google Drive API.""" | |
import os | |
from apiclient.discovery import build # pip install google-api-python-client | |
FOLDER = 'application/vnd.google-apps.folder' | |
def get_credentials(scopes, *, |
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 all sheets of a Google Docs spreadsheet as CSV.""" | |
import contextlib, csv, itertools, os | |
from apiclient.discovery import build # pip install google-api-python-client | |
SHEET = '1dR13B3Wi_KJGUJQ0BZa2frLAVxhZnbz0hpwCcWSvb20' | |
def get_credentials(scopes, *, secrets='~/client_secrets.json', storage='~/storage.json'): |
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 all available audio books from DB ICE Portal.""" | |
import json | |
import os | |
import urllib.parse | |
import urllib.request | |
BASE = 'http://iceportal.de/api1/rs/' | |
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 feed enclosure length with content-length of file url.""" | |
import urllib.request | |
import xml.etree.ElementTree as etree | |
URL = 'https://feeds.feedburner.com/thebuglefeed?format=xml' | |
with urllib.request.urlopen(URL) as f: | |
tree = etree.parse(f) |
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 __future__ import annotations | |
from typing import Optional | |
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 |