Skip to content

Instantly share code, notes, and snippets.

View jonathanslenders's full-sized avatar

Jonathan Slenders jonathanslenders

View GitHub Profile
@jonathanslenders
jonathanslenders / no_init_for_abc.py
Created April 4, 2024 10:14
Mypy plugin that prevents abstract classes from having an `__init__` method.
from __future__ import annotations
import typing
from mypy.nodes import FuncDef
from mypy.plugin import ClassDefContext, Plugin
__all__ = ["NoInitForABCPlugin"]
@jonathanslenders
jonathanslenders / python-webbrowser-over-ssh.py
Created January 10, 2024 11:40
python-webbrowser-over-ssh
# Example to make Python's `webbrowser` module work over SSH.
# 1. Run the following code on the host machine:
import asyncio, webbrowser
async def handler(reader, writer):
data = await reader.read()
url = data.decode()
addr = writer.get_extra_info('peername')
@jonathanslenders
jonathanslenders / download_and_index.py
Created June 19, 2022 15:04
download_and_index.py
# - Short snippet extracted from a commercial Cisco project, to justif
# adding a `str.splitlines(return_indexes=True)` feature to Python. -
async def _start_download_and_index(self) -> None:
"""
Download the file content (as a binary stream), index the lines
(keep the line index in memory), and store the file content
locally on disk. This needs to be as efficient as possible.
We're dealing with huge (>1GB) files, but users typically view
only a small portion of the file.
@jonathanslenders
jonathanslenders / line_endings.py
Created June 17, 2022 18:37
Finding line endings
"""
Output:
re solution 8.718856811523438
clever solution 3.7440919876098633
"""
from itertools import accumulate, chain
import re
import time
@jonathanslenders
jonathanslenders / cache_performance_2.py
Created June 3, 2022 10:18
Comparing the performance of a cache built using a `dict` + `__missing__` against functools.cache
* Super-optimized for small spaces - read how we shrank the memory │ Date: Tue May 31 22:09:56 2022 +0200
"""
Comparing the performance of a cache built using a `dict` and `__missing__` with functools.cache
"""
import time
from typing import TypeVar, Callable, Dict
from functools import cache
_K = TypeVar("_K")
_V = TypeVar("_V")
@jonathanslenders
jonathanslenders / cache_performance.py
Last active June 3, 2022 10:01
Comparing the performance of a cache built using a `dict` + `__missing__` against lru_cache
"""
Comparing the performance of a cache built using a `dict` + `__missing__` against lru_cache
"""
import time
from typing import TypeVar, Callable, Dict, Deque
from collections import deque
from functools import lru_cache
_K = TypeVar("_K")
_V = TypeVar("_V")
@jonathanslenders
jonathanslenders / test_prompt_toolkit_app.py
Created October 7, 2021 21:24
Test prompt_toolkit application
#!/usr/bin/env python
"""
A simple example of a calculator program.
This could be used as inspiration for a REPL.
"""
import asyncio
from typing import Tuple
from prompt_toolkit.application import Application, create_app_session
from prompt_toolkit.document import Document
@jonathanslenders
jonathanslenders / nodes2.py
Created December 21, 2018 12:43
nodes2.py
class Leaf:
def __init__(self, val):
self.val = val
def values(self):
# Following two lines for debugging.
# So that we can see how many stack frames there
# are at the deepest level while traversing the tree.
import traceback; traceback.print_stack()
{
"monday": {
"temperature_high": 67,
"temperature_low": 61,
"humidity": 79,
"description": "beautiful",
"wind": 17
},
"tuesday": {
"temperature_high": 63,
@jonathanslenders
jonathanslenders / complete.py
Created October 9, 2015 17:58
Prompt with autocompletion where the enter key accepts the input.
#!/usr/bin/env python
"""
Example of a prompt with autocompletion, where pressing the Enter key accepts
the completion.
"""
from __future__ import unicode_literals
from prompt_toolkit import prompt
from prompt_toolkit.contrib.completers import WordCompleter
from prompt_toolkit.filters import HasCompletions