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
| """ | |
| Simple reactivity system for Python. | |
| Not thread safe. | |
| License: MIT | |
| """ | |
| from collections.abc import Callable | |
| from dataclasses import dataclass | |
| from typing import Generic, Protocol, TypeVar, final |
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
| # Instructions: | |
| # 1. apt install ffmpeg | |
| # 2. Install this file as: /etc/systemd/system/babymonitor.service | |
| # 3. Use `arecord -L` and adjust this file so that it reflects the right audio device (`-i` flag). | |
| # 4. Change User/Group and/or make sure this user is part of the 'audio' group. | |
| # 5. systemctl enable babymonitor | |
| # 6. systemctl start babymonitor | |
| [Unit] | |
| Description=Baby monitor mp3 stream | |
| After=network.target |
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
| from __future__ import annotations | |
| import typing | |
| from mypy.nodes import FuncDef | |
| from mypy.plugin import ClassDefContext, Plugin | |
| __all__ = ["NoInitForABCPlugin"] | |
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
| # 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 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
| # - 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 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
| """ | |
| Output: | |
| re solution 8.718856811523438 | |
| clever solution 3.7440919876098633 | |
| """ | |
| from itertools import accumulate, chain | |
| import re | |
| import time |
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
| * 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 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
| """ | |
| 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 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
| #!/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 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
| 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() |
NewerOlder