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
def example():
print('Example!')
@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
@lemon24
lemon24 / process_multiplexer.py
Last active July 25, 2020 08:37
process_multiplexer.py (from threads to Curio)
"""
Read commands from stdin, run them in parallel, and send the output to stdout.
Command output looks like:
<command_id> <pid> (stdout|stderr) <original_line>
When a command exits, a single line containing the status is output:
<command_id> <pid> exited <status_code>
@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)
);
import textwrap
import collections
class BaseQuery:
indent = ' '
separators = collections.defaultdict(
lambda: ',',
WHERE=' AND',
@lemon24
lemon24 / sqlite-fts5-highlight-group-by.sql
Last active November 8, 2023 04:34
SQLite FTS5: "unable to use function highlight in the requested context"; https://github.com/lemon24/reader/issues/122
-- fts5
CREATE VIRTUAL TABLE entries USING fts5(
id UNINDEXED,
content
);
INSERT INTO entries
VALUES
('one', 'one'),