cut
stringclasses
1 value
version
stringclasses
10 values
idiom_id
stringclasses
53 values
pep
stringclasses
53 values
introduced_in
stringclasses
11 values
form
stringclasses
2 values
file
stringclasses
887 values
code
stringclasses
916 values
source
stringclasses
1 value
origin
stringlengths
15
17
origin_revision
stringclasses
10 values
license
stringclasses
1 value
train
3.14
dict-union
584
3.9
legacy
dict-union/legacy_17.py
d2 = DEFAULT_VALUE.copy() d2.update(dict(("ext_"+ext, False) for ext in EXTENSIONS)) d2.update(d) d = d2
pydocs
pep-0584.rst:912
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
modern
dict-union/modern_21.py
d = DEFAULT_VALUE | dict(("ext_"+ext, False) for ext in EXTENSIONS) | d
pydocs
pep-0584.rst:919
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
legacy
dict-union/legacy_18.py
clash = {} clash.update(clash1) clash.update(clash2) return clash1, clash2, clash
pydocs
pep-0584.rst:928
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
modern
dict-union/modern_22.py
return clash1, clash2, clash1 | clash2
pydocs
pep-0584.rst:935
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
legacy
dict-union/legacy_19.py
dct = MaximaHelpers.__dict__.copy() dct.update(name_dict) obj = sympify(str, locals=dct)
pydocs
pep-0584.rst:944
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
modern
dict-union/modern_23.py
obj = sympify(str, locals=MaximaHelpers.__dict__|name_dict)
pydocs
pep-0584.rst:950
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
legacy
dict-union/legacy_20.py
self.known_functions = dict(known_functions) userfuncs = settings.get('user_functions', {}) self.known_functions.update(userfuncs)
pydocs
pep-0584.rst:959
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
modern
dict-union/modern_24.py
self.known_functions = known_functions | settings.get('user_functions', {})
pydocs
pep-0584.rst:965
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
legacy
dict-union/legacy_21.py
globs = globs.copy() if extraglobs is not None: globs.update(extraglobs)
pydocs
pep-0584.rst:974
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
dict-union
584
3.9
modern
dict-union/modern_25.py
globs = globs | (extraglobs if extraglobs is not None else {})
pydocs
pep-0584.rst:980
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_01.py
eg = ExceptionGroup( "one", [ TypeError(1), ExceptionGroup( "two", [TypeError(2), ValueError(3)] ), ExceptionGroup( "three", [OSError(4)] ) ] ) import traceback traceback.print_exception(eg) type_errors = eg.subgroup...
pydocs
pep-0654.rst:182
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_02.py
class MyExceptionGroup(ExceptionGroup): def __new__(cls, message, excs, errcode): obj = super().__new__(cls, message, excs) obj.errcode = errcode return obj def derive(self, excs): return MyExceptionGroup(self.message, excs, self.errcode)
pydocs
pep-0654.rst:308
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_03.py
eg = MyExceptionGroup("eg", [TypeError(1), ValueError(2)], 42) match, rest = eg.split(ValueError) print(f'match: {match!r}: {match.errcode}') print(f'rest: {rest!r}: {rest.errcode}')
pydocs
pep-0654.rst:328
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_04.py
class MyExceptionGroup(BaseExceptionGroup): pass eg = MyExceptionGroup("eg", [ValueError(1), KeyboardInterrupt(2)]) match, rest = eg.split(ValueError) print(f'match: {match!r}') print(f'rest: {rest!r}')
pydocs
pep-0654.rst:344
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_05.py
def f(v): try: raise ValueError(v) except ValueError as e: return e try: raise ExceptionGroup("one", [f(1)]) except ExceptionGroup as e: eg = e raise ExceptionGroup("two", [f(2), eg])
pydocs
pep-0654.rst:384
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_06.py
def leaf_generator(exc, tbs=None): if tbs is None: tbs = [] tbs.append(exc.__traceback__) if isinstance(exc, BaseExceptionGroup): for e in exc.exceptions: yield from leaf_generator(e, tbs) else: # exc is a leaf exception and its traceback # is the concatenati...
pydocs
pep-0654.rst:464
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_07.py
import traceback def g(v): try: raise ValueError(v) except Exception as e: return e def f(): raise ExceptionGroup("eg", [g(1), g(2)]) try: f() except BaseException as e: eg = e for (i, (exc, tbs)) in enumerate(leaf_generator(eg)): print(f"\n=== Exception #{i+1}:") traceba...
pydocs
pep-0654.rst:489
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_08.py
try: ... except* SpamError: ... except* FooError as e: ... except* (BarError, BazError) as e: ...
pydocs
pep-0654.rst:541
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_09.py
try: low_level_os_operation() except* OSError as eg: for e in eg.exceptions: print(type(e).__name__)
pydocs
pep-0654.rst:581
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_10.py
try: raise ExceptionGroup("problem", [BlockingIOError()]) except* OSError as e: # Would catch the error print(repr(e)) except* BlockingIOError: # Would never run print('never')
pydocs
pep-0654.rst:602
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_11.py
try: raise ExceptionGroup( "eg", [ ValueError('a'), TypeError('b'), ExceptionGroup( "nested", [TypeError('c'), KeyError('d')]) ] ) except* TypeError as e1: print(f'e1 = {e1!r}') except* Exception as e2: print(f'e...
pydocs
pep-0654.rst:619
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_12.py
try: try: raise ExceptionGroup( "msg", [ ValueError('a'), TypeError('b'), TypeError('c'), KeyError('e') ] ) except* ValueError as e: print(f'got some ValueErrors: {e!r}') except* TypeError as e: print(f'got some TypeEr...
pydocs
pep-0654.rst:647
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_13.py
try: raise BlockingIOError except* OSError as e: print(repr(e))
pydocs
pep-0654.rst:679
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_14.py
try: try: raise ValueError(12) except* TypeError as e: print('never') except ValueError as e: print(f'caught ValueError: {e!r}')
pydocs
pep-0654.rst:691
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_15.py
try: try: raise ExceptionGroup( "eg", [ ValueError(1), TypeError(2), OSError(3), ExceptionGroup( "nested", [OSError(4), TypeError(5), ValueError(6)]) ] ) ex...
pydocs
pep-0654.rst:753
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_16.py
try: raise ExceptionGroup( "eg", [ ValueError(1), TypeError(2), OSError(3), ExceptionGroup( "nested", [OSError(4), TypeError(5), ValueError(6)]) ] ) except* ValueError as e: print(f'*ValueError: {e!r}') ...
pydocs
pep-0654.rst:792
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_17.py
try: raise ExceptionGroup("one", [ValueError('a'), TypeError('b')]) except* ValueError: raise ExceptionGroup("two", [KeyError('x'), KeyError('y')])
pydocs
pep-0654.rst:854
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_18.py
try: raise TypeError('bad type') except* TypeError as e: raise ValueError('bad value') from e
pydocs
pep-0654.rst:897
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_19.py
try: raise TypeError(1) except* TypeError: raise ValueError(2) from None # <- not caught in the next clause except* ValueError: print('never')
pydocs
pep-0654.rst:922
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_20.py
try: raise ExceptionGroup("eg", [ValueError('a')]) except* ValueError: raise KeyError('x') try: raise ExceptionGroup("eg", [ValueError('a'), TypeError('b')]) except* ValueError: raise KeyError('x')
pydocs
pep-0654.rst:943
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_21.py
try: low_level_os_operation() except* OSError as errors: exc = errors.subgroup(lambda e: e.errno != errno.EPIPE) if exc is not None: raise exc from None
pydocs
pep-0654.rst:999
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_22.py
eg = ExceptionGroup("eg", [TypeError(12)]) eg.foo = 'foo' try: raise eg except* TypeError as e: e.foo = 'bar' # ^----------- ``e`` is an ephemeral object that might get # destroyed after the ``except*`` clause. eg.foo
pydocs
pep-0654.rst:1017
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_23.py
try: ... except ExceptionGroup: # <- This works pass try: ... except* ExceptionGroup: # <- Runtime error pass try: ... except* (TypeError, ExceptionGroup): # <- Runtime error pass
pydocs
pep-0654.rst:1052
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_24.py
try: async with asyncio.TaskGroup() as g: g.create_task(task1); g.create_task(task2) except* KeyError: ... # handling KeyError here is meaningless, there's # no context to do anything with it but to log it.
pydocs
pep-0654.rst:1304
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_25.py
try: try: ... except SomeError: ... # handle the naked exception except* SomeError: ... # handle the exception group
pydocs
pep-0654.rst:1346
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-groups
654
3.11
modern
exception-groups/modern_26.py
try: async with trio.open_nursery() as nursery: # Make two concurrent calls to child() nursery.start_soon(child) nursery.start_soon(child) except* ValueError: pass
pydocs
pep-0654.rst:1399
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
legacy
async-generators/legacy_01.py
class Ticker: """Yield numbers from 0 to `to` every `delay` seconds.""" def __init__(self, delay, to): self.delay = delay self.i = 0 self.to = to def __aiter__(self): return self async def __anext__(self): i = self.i if i >= self.to: raise S...
pydocs
pep-0525.rst:43
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_01.py
async def ticker(delay, to): """Yield numbers from 0 to `to` every `delay` seconds.""" for i in range(to): yield i await asyncio.sleep(delay)
pydocs
pep-0525.rst:66
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_02.py
async def coro(): # a coroutine function await smth() async def asyncgen(): # an asynchronous generator function await smth() yield 42
pydocs
pep-0525.rst:98
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_03.py
async def genfunc(): yield 1 yield 2 gen = genfunc() assert gen.__aiter__() is gen assert await gen.__anext__() == 1 assert await gen.__anext__() == 2 await gen.__anext__() # This line will raise StopAsyncIteration.
pydocs
pep-0525.rst:126
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_04.py
async def square_series(con, to): async with con.transaction(): cursor = con.cursor( 'SELECT generate_series(0, $1) AS i', to) async for row in cursor: yield row['i'] ** 2 async for i in square_series(con, 1000): if i == 100: break
pydocs
pep-0525.rst:152
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_05.py
async def gen(): try: yield finally: await asyncio.sleep(1) # Can use 'await'. yield # Cannot use 'yield', # this line will trigger a # RuntimeError.
pydocs
pep-0525.rst:188
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_06.py
async def gen(): await asyncio.sleep(0.1) v = yield 42 print(v) await asyncio.sleep(0.2) g = gen() await g.asend(None) # Will return 42 after sleeping # for 0.1 seconds. await g.asend('hello') # Will print 'hello' and # raise StopAsyncIteration...
pydocs
pep-0525.rst:304
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_07.py
async def gen(): try: await asyncio.sleep(0.1) yield 'hello' except ZeroDivisionError: await asyncio.sleep(0.2) yield 'world' g = gen() v = await g.asend(None) print(v) # Will print 'hello' after # sleeping for 0.1 seconds. v = await g.ath...
pydocs
pep-0525.rst:324
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_08.py
N = 10 ** 7 async def agen(): for i in range(N): yield i class AIter: def __init__(self): self.i = 0 def __aiter__(self): return self async def __anext__(self): i = self.i if i >= N: raise StopAsyncIteration self.i += 1 return i
pydocs
pep-0525.rst:490
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_09.py
async def g1(): yield 1 yield 2 async def g2(): async for v in g1(): yield v
pydocs
pep-0525.rst:551
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_10.py
@async_context_manager async def ctx(): await open() try: yield finally: await close() async with ctx(): await ...
pydocs
pep-0525.rst:568
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
async-generators
525
3.6
modern
async-generators/modern_11.py
async def ticker(delay, to): for i in range(to): yield i await asyncio.sleep(delay) async def run(): async for i in ticker(1, 10): print(i) import asyncio loop = asyncio.get_event_loop() try: loop.run_until_complete(run()) finally: loop.close()
pydocs
pep-0525.rst:597
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_01.py
# Declare a context variable 'var' with the default value 42. var = ContextVar('var', default=42)
pydocs
pep-0567.rst:147
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_02.py
ctx = contextvars.copy_context()
pydocs
pep-0567.rst:231
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_03.py
var = ContextVar('var') var.set('spam') def main(): # 'var' was set to 'spam' before # calling 'copy_context()' and 'ctx.run(main)', so: # var.get() == ctx[var] == 'spam' var.set('ham') # Now, after setting 'var' to 'ham': # var.get() == ctx[var] == 'ham' ctx = copy_context() # Any changes ...
pydocs
pep-0567.rst:241
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_04.py
ctx = contextvars.copy_context() # Print all context variables and their values in 'ctx': print(ctx.items()) # Print the value of 'some_variable' in context 'ctx': print(ctx[some_variable])
pydocs
pep-0567.rst:276
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_05.py
def call_soon(self, callback, *args, context=None): if context is None: context = contextvars.copy_context() # ... some time later context.run(callback, *args)
pydocs
pep-0567.rst:309
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_06.py
class Task: def __init__(self, coro): ... # Get the current context snapshot. self._context = contextvars.copy_context() self._loop.call_soon(self._step, context=self._context) def _step(self, exc=None): ... # Every advance of the wrapped coroutine is done in ...
pydocs
pep-0567.rst:320
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_07.py
var = ContextVar('var') ctx = copy_context() ctx[var] = 'value' print(ctx[var]) # Prints 'value' print(var.get()) # Raises a LookupError
pydocs
pep-0567.rst:708
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_08.py
ctx = copy_context() def func(): ctx[var] = 'value' # Contrary to the previous example, this would work # because 'func()' is running within 'ctx'. print(ctx[var]) print(var.get()) ctx.run(func)
pydocs
pep-0567.rst:718
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_09.py
# Framework: current_request: ContextVar[Request] = \ ContextVar('current_request') # Later, while handling an HTTP request: request: Request = current_request.get() # Work with the 'request' object: return request.method
pydocs
pep-0567.rst:752
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_10.py
# Framework: current_request: ContextVar[Optional[Request]] = \ ContextVar('current_request', initial_value=None) # Later, while handling an HTTP request: request: Optional[Request] = current_request.get() # Check if the current request object was set: if request is None: raise RuntimeError # Work with the ...
pydocs
pep-0567.rst:771
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
legacy
context-vars/legacy_01.py
class PrecisionStorage(threading.local): # Subclass threading.local to specify a default value. value = 0.0 precision = PrecisionStorage() # To set a new precision: precision.value = 0.5 # To read the current precision: print(precision.value)
pydocs
pep-0567.rst:813
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_11.py
precision = contextvars.ContextVar('precision', default=0.0) # To set a new precision: precision.set(0.5) # To read the current precision: print(precision.get())
pydocs
pep-0567.rst:828
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
context-vars
567
3.7
modern
context-vars/modern_12.py
executor = ThreadPoolExecutor() current_context = contextvars.copy_context() executor.submit(current_context.run, some_function)
pydocs
pep-0567.rst:843
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
breakpoint
553
3.7
legacy
breakpoint/legacy_01.py
foo() import pdb; pdb.set_trace() bar()
pydocs
pep-0553.rst:27
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
breakpoint
553
3.7
modern
breakpoint/modern_01.py
foo() breakpoint() bar()
pydocs
pep-0553.rst:70
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
breakpoint
553
3.7
modern
breakpoint/modern_02.py
os.environ['PYTHONBREAKPOINT'] = 'foo.bar.baz' breakpoint() # Imports foo.bar and calls foo.bar.baz()
pydocs
pep-0553.rst:141
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
legacy
str-affix-removal/legacy_01.py
if test_func_name.startswith("test_"): print(test_func_name[5:]) else: print(test_func_name)
pydocs
pep-0616.rst:104
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
modern
str-affix-removal/modern_01.py
print(test_func_name.removeprefix("test_"))
pydocs
pep-0616.rst:111
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
legacy
str-affix-removal/legacy_02.py
if funcname.startswith("context."): self.funcname = funcname.replace("context.", "") self.contextfunc = True else: self.funcname = funcname self.contextfunc = False
pydocs
pep-0616.rst:123
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
modern
str-affix-removal/modern_02.py
if funcname.startswith("context."): self.funcname = funcname.removeprefix("context.") self.contextfunc = True else: self.funcname = funcname self.contextfunc = False
pydocs
pep-0616.rst:132
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
modern
str-affix-removal/modern_03.py
self.contextfunc = funcname.startswith("context.") self.funcname = funcname.removeprefix("context.")
pydocs
pep-0616.rst:141
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
legacy
str-affix-removal/legacy_03.py
def strip_quotes(text): if text.startswith('"'): text = text[1:] if text.endswith('"'): text = text[:-1] return text
pydocs
pep-0616.rst:150
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
modern
str-affix-removal/modern_04.py
def strip_quotes(text): return text.removeprefix('"').removesuffix('"')
pydocs
pep-0616.rst:159
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
legacy
str-affix-removal/legacy_04.py
creationDate = header['POT-Creation-Date'] # peel off the escaped newline at the end of string if creationDate.endswith('\\n'): creationDate = creationDate[:-len('\\n')]
pydocs
pep-0616.rst:168
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
modern
str-affix-removal/modern_05.py
creationDate = header['POT-Creation-Date'].removesuffix('\\n')
pydocs
pep-0616.rst:176
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
str-affix-removal
616
3.9
modern
str-affix-removal/modern_06.py
s = 'Foo' * 100 + 'Bar' prefix = 'Foo' while s.startswith(prefix): s = s.removeprefix(prefix) s
pydocs
pep-0616.rst:205
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-notes
678
3.11
modern
exception-notes/modern_01.py
try: raise TypeError('bad type') except Exception as e: e.add_note('Add some information') raise
pydocs
pep-0678.rst:67
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
exception-notes
678
3.11
modern
exception-notes/modern_02.py
@contextlib.contextmanager def add_exc_note(note: str): try: yield except Exception as err: err.add_note(note) raise with add_exc_note(f"While attempting to frobnicate {item=}"): frobnicate_or_raise(item)
pydocs
pep-0678.rst:325
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_01.py
template: Template = t"Hello {name}"
pydocs
pep-0750.rst:34
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_02.py
evil = "<script>alert('evil')</script>" template = t"<p>{evil}</p>" assert html(template) == "<p>&lt;script&gt;alert('evil')&lt;/script&gt;</p>"
pydocs
pep-0750.rst:90
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_03.py
attributes = {"src": "shrubbery.jpg", "alt": "looks nice"} template = t"<img {attributes} />" assert html(template) == '<img src="shrubbery.jpg" alt="looks nice" />'
pydocs
pep-0750.rst:99
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_04.py
from string.templatelib import Template template = t"This is a template string." assert isinstance(template, Template)
pydocs
pep-0750.rst:122
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_05.py
name = "World" template = t"Hello {name}" assert template.strings[0] == "Hello " assert template.interpolations[0].value == "World"
pydocs
pep-0750.rst:190
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_06.py
name = "World" template = t"Hello {name}" assert template.interpolations[0].value == "World"
pydocs
pep-0750.rst:228
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_07.py
name = "World" template = t"Hello {name}" assert template.interpolations[0].expression == "name"
pydocs
pep-0750.rst:237
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_08.py
name = "World" template = t"Hello {name!r}" assert template.interpolations[0].conversion == "r"
pydocs
pep-0750.rst:259
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_09.py
value = 42 template = t"Value: {value:.2f}" assert template.interpolations[0].format_spec == ".2f"
pydocs
pep-0750.rst:270
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_10.py
value = 42 precision = 2 template = t"Value: {value:.{precision}f}" assert template.interpolations[0].format_spec == ".2f"
pydocs
pep-0750.rst:280
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_11.py
assert list(t"") == [] assert list(t"Hello") == ["Hello"] name = "World" template = t"Hello {name}!" contents = list(template) assert len(contents) == 3 assert contents[0] == "Hello " assert contents[1].value == "World" assert contents[1].expression == "name" assert contents[2] == "!"
pydocs
pep-0750.rst:333
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_12.py
first = "Eat" second = "Red Leicester" template = t"{first}{second}" contents = list(template) assert len(contents) == 2 assert contents[0].value == "Eat" assert contents[0].expression == "first" assert contents[1].value == "Red Leicester" assert contents[1].expression == "second" # However, the strings attribute cont...
pydocs
pep-0750.rst:351
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_13.py
from string.templatelib import Template, Interpolation def lower_upper(template: Template) -> str: """Render static parts lowercased and interpolations uppercased.""" parts: list[str] = [] for item in template: if isinstance(item, Interpolation): parts.append(str(item.value).upper()) ...
pydocs
pep-0750.rst:378
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_14.py
name = "World" assert isinstance(t"Hello " + t"{name}", Template) assert (t"Hello " + t"{name}").strings == ("Hello ", "") assert (t"Hello " + t"{name}").values[0] == "World"
pydocs
pep-0750.rst:410
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_15.py
name = "World" assert isinstance(t"Hello " t"{name}", Template) assert (t"Hello " t"{name}").strings == ("Hello ", "") assert (t"Hello " t"{name}").values[0] == "World"
pydocs
pep-0750.rst:420
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_16.py
name = "World" # Treat `name` as a static string part template = t"Hello " + Template(name) # Treat `name` as an interpolation template = t"Hello " + Template(Interpolation(name, "name"))
pydocs
pep-0750.rst:437
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_17.py
name = "World" template = t"Hello {name=}" assert template.strings[0] == "Hello name=" assert template.interpolations[0].value == "World" assert template.interpolations[0].conversion == "r"
pydocs
pep-0750.rst:480
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_18.py
trade = 'shrubberies' template = rt'Did you say "{trade}"?\n' assert template.strings[0] == r'Did you say "' assert template.strings[1] == r'"?\n'
pydocs
pep-0750.rst:503
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_19.py
name = "World" value = 42 templated = t"Hello {name!r}, value: {value:.2f}" formatted = f"Hello {name!r}, value: {value:.2f}" assert f(templated) == formatted
pydocs
pep-0750.rst:580
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_20.py
import logging import sys logger = logging.getLogger(__name__) message_handler = logging.StreamHandler(sys.stdout) message_handler.setFormatter(MessageFormatter()) logger.addHandler(message_handler) values_handler = logging.StreamHandler(sys.stderr) values_handler.setFormatter(ValuesFormatter()) logger.addHandler(val...
pydocs
pep-0750.rst:757
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_21.py
attributes = {"id": "main"} attribute_value = "shrubbery" content = "hello" template = t"<div {attributes} data-value={attribute_value}>{content}</div>" element = html(template) assert str(element) == '<div id="main" data-value="shrubbery">hello</div>'
pydocs
pep-0750.rst:989
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_22.py
name = "World" content = html(t"<p>Hello {name}</p>") template = t"<div>{content}</div>" element = html(template) assert str(element) == '<div><p>Hello World</p></div>'
pydocs
pep-0750.rst:1013
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_23.py
name = "World" template = t"Hello {(lambda: name)}" assert callable(template.interpolations[0].value) assert template.interpolations[0].value() == "World"
pydocs
pep-0750.rst:1042
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0
train
3.14
template-strings
750
3.14
modern
template-strings/modern_24.py
async def example(): async def get_name() -> str: await asyncio.sleep(1) return "Sleepy" template = t"Hello {await get_name()}" # Use the f() function from the f-string example, above assert f(template) == "Hello Sleepy"
pydocs
pep-0750.rst:1067
cec9cb6ae57ddb740dc344666866839f5e8eec8c
CC0-1.0