This file contains 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
from __future__ import annotations | |
import typing | |
from mypy.nodes import FuncDef | |
from mypy.plugin import ClassDefContext, Plugin | |
__all__ = ["NoInitForABCPlugin"] | |
This file contains 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
# 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') |
This file contains 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
# - 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. |
This file contains 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
""" | |
Output: | |
re solution 8.718856811523438 | |
clever solution 3.7440919876098633 | |
""" | |
from itertools import accumulate, chain | |
import re | |
import time |
This file contains 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
* 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") |
This file contains 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
""" | |
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") |
This file contains 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
#!/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 |
This file contains 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
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() |
This file contains 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
{ | |
"monday": { | |
"temperature_high": 67, | |
"temperature_low": 61, | |
"humidity": 79, | |
"description": "beautiful", | |
"wind": 17 | |
}, | |
"tuesday": { | |
"temperature_high": 63, |
This file contains 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
#!/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 |