Skip to content

Instantly share code, notes, and snippets.

@lemon24
lemon24 / contextmanagers.py
Created October 30, 2016 12:49
Shows how the with statement / context managers are semantically equivalent with a try-except-finally block.
"""
Shows how the with statement / context managers are semantically equivalent
with a try-except-finally block.
https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement
https://docs.python.org/3/reference/compound_stmts.html#the-with-statement
https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers
https://docs.python.org/3.7/library/sys.html#sys.exc_info
"""
Scoping for https://github.com/lemon24/reader/issues/67 "Tumblr feeds not working".
Output looks like:
$ python3 tumblr.py
initial None
have tumblr redirect
{}
$ python bench.py time
entries runs get_entries /?show=all ratio
32 8 0.01 0.20 32.7
64 8 0.01 0.30 28.4
128 8 0.02 0.51 25.2
256 8 0.04 0.94 23.1
512 8 0.08 1.78 22.0
1024 4 0.09 1.77 20.1
2048 2 0.11 1.85 17.4
4096 1 0.13 1.84 13.8
### render()
$ python3 bench.py time
entries runs get_entries /?show=all ratio /?show=all#100kb
32 8 0.02 0.33 17.5 0.33
64 8 0.03 0.51 19.4 0.51
128 8 0.04 0.87 20.0 0.86
256 8 0.08 1.57 20.7 1.57
512 8 0.15 2.99 19.6 2.97
1024 4 0.17 3.00 17.9 2.98
@lemon24
lemon24 / run_httpd.py
Last active January 21, 2019 20:14
Run an HTTP(S) server with various customizations using only stuff from the Python stdlib.
"""
Run an HTTP(S) server with various customizations using only stuff from stdlib.
See make_httpd() and run_httpd() for details.
"""
import http.server
import contextlib
import threading
import textwrap
import collections
class BaseQuery:
indent = ' '
separators = collections.defaultdict(
lambda: ',',
WHERE=' AND',
def example():
print('Example!')
@lemon24
lemon24 / search.sql
Last active July 12, 2019 18:02
reader full text search prototype; https://github.com/lemon24/reader/issues/122
CREATE TABLE entries (
id TEXT NOT NULL,
feed TEXT NOT NULL,
title TEXT,
summary TEXT,
content TEXT,
PRIMARY KEY (id, feed)
);
@lemon24
lemon24 / search-trigger-update-time.py
Last active March 13, 2020 12:04
reader full text search prototype #2; more detailed, timing triggers; https://github.com/lemon24/reader/issues/122
import timeit
import shutil
import textwrap
import functools
import bs4
# reader 0.18
from reader import make_reader
@lemon24
lemon24 / seach-shadow-table.py
Created March 14, 2020 19:46
reader full text search prototype #3; shadow table, with timing; https://github.com/lemon24/reader/issues/122
import timeit
import shutil
import textwrap
import functools
import bs4
# reader 0.18
from reader import make_reader