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
import itertools, typing | |
def primes(n: int, /) -> typing.Generator[int, None, None]: | |
yield(next(sieve := itertools.count(prime := 2))) | |
for _ in range(n): | |
yield (prime := next(sieve := filter(prime.__rmod__, sieve))) |
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 | |
from typing import Protocol | |
class _Int(Protocol): | |
def __int__(self) -> int: ... | |
def __pos__(self) -> _Int: ... | |
def __neg__(self) -> _Int: ... | |
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
import asyncio | |
import time | |
from time import perf_counter_ns | |
# noinspection PyPep8Naming | |
class ns_per_it: | |
__slots__ = 'n', 'res' | |
def __init__(self, n: int): |
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
import asyncio | |
class AsyncInit: | |
def __await__(self): | |
async def _(): | |
await self.__ainit__() | |
return self | |
return _().__await__() |
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
import contextlib | |
from typing import ContextManager | |
from django.core import cache | |
from django.core.cache.backends.dummy import DummyCache | |
@contextlib.contextmanager | |
def disable_caches(*aliases: str) -> ContextManager[tuple[str, ...]]: | |
"""Temporarily disable django caching backends. By default, all connected |
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 typing import Optional, TYPE_CHECKING | |
if TYPE_CHECKING: | |
from _typeshed import SupportsLessThan | |
def cmp(a: 'SupportsLessThan', b: 'SupportsLessThan') -> Optional[int]: | |
"""Return 1 if a > b, 0 if a == b, -1 if a < b, otherwise None. | |
>>> cmp(42, 69), cmp(666, 666), cmp(69, 42), cmp(0, float('nan')) |
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 FancyGeneric: | |
def __class_getitem__(cls, params): | |
if not isinstance(params, tuple): | |
params = (params,) | |
newparams = [] | |
for param in params: | |
if isinstance(param, slice): | |
assert param.start is None and param.step is None |
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
import sys | |
for x in (0.0, 1/10, -1/10, sys.float_info.max, sys.float_info.min, float('inf'), float('-inf'), float('NaN')): | |
print(f'{x}++ => {float_incr(x)}') | |
i, xi = 0, float('-inf') | |
while xi < float('inf'): | |
x = float_incr(xi) | |
assert x > xi | |
xi = x / 2 if x < 0 else x * 2 |
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
import asyncio | |
async def task(): | |
print('task start') | |
await asyncio.sleep(.5) | |
print('task done') | |
async def run_tasks(): |
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
""" | |
This is valid (but ridiculous) syntax: | |
spam_with_eggs = Spam<<'eggs'>>('ham', 'bacon') | |
""" | |
class _TraitAlias: | |
__slots__ = ('__origin__', '__traits__') |
NewerOlder