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.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | 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 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_25.py | async def example():
async def get_name() -> str:
await asyncio.sleep(1)
return "Sleepy"
template = t"Hello {get_name}"
assert await async_f(template) == "Hello Sleepy"
| pydocs | pep-0750.rst:1083 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_26.py | def reusable(name: str, question: str) -> Template:
return t"Hello {name}, {question}?"
template = reusable("friend", "how are you")
template = reusable("King Arthur", "what is your quest")
| pydocs | pep-0750.rst:1110 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_27.py | def make_template(*, cheese: str) -> Template:
return t"We're all out of {cheese}."
template = make_template(cheese="Red Leicester")
# Using the f() function from the f-string example, above
assert f(template) == "We're all out of Red Leicester."
| pydocs | pep-0750.rst:1137 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_28.py | name = "World"
assert t"Hello " + t"{name}" == t"Hello {name}"
| pydocs | pep-0750.rst:1293 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_29.py | value = 42
template1 = t"{value}"
template2 = t"{value:}"
assert template1.interpolations[0].format_spec == ""
assert template2.interpolations[0].format_spec == ""
| pydocs | pep-0750.rst:1332 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_30.py | value = 42
template1 = t"{value=}"
template2 = t"value={value!r}"
assert template1.strings[0] == "value="
assert template1.interpolations[0].expression == "value"
assert template1.interpolations[0].conversion == "r"
assert template2.strings[0] == "value="
assert template2.interpolations[0].expression == "value"
assert ... | pydocs | pep-0750.rst:1344 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | template-strings | 750 | 3.14 | modern | template-strings/modern_31.py | value = 42
precision = 2
template1 = t"{value:.2f}"
template2 = t"{value:.{precision}f}"
assert template1.interpolations[0].format_spec == ".2f"
assert template2.interpolations[0].format_spec == ".2f"
| pydocs | pep-0750.rst:1361 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | class-creation-hooks | 487 | 3.6 | modern | class-creation-hooks/modern_01.py | class QuestBase:
# this is implicitly a @classmethod (see below for motivation)
def __init_subclass__(cls, swallow, **kwargs):
cls.swallow = swallow
super().__init_subclass__(**kwargs)
class Quest(QuestBase, swallow="african"):
pass
Quest.swallow
| pydocs | pep-0487.rst:66 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | class-creation-hooks | 487 | 3.6 | modern | class-creation-hooks/modern_02.py | import weakref
class WeakAttribute:
def __get__(self, instance, owner):
return instance.__dict__[self.name]()
def __set__(self, instance, value):
instance.__dict__[self.name] = weakref.ref(value)
# this is the new initializer:
def __set_name__(self, owner, name):
self.name = n... | pydocs | pep-0487.rst:107 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | class-creation-hooks | 487 | 3.6 | modern | class-creation-hooks/modern_03.py | class PluginBase:
subclasses = []
def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
cls.subclasses.append(cls)
| pydocs | pep-0487.rst:194 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | class-creation-hooks | 487 | 3.6 | modern | class-creation-hooks/modern_04.py | class Trait:
def __init__(self, minimum, maximum):
self.minimum = minimum
self.maximum = maximum
def __get__(self, instance, owner):
return instance.__dict__[self.key]
def __set__(self, instance, value):
if self.minimum < value < self.maximum:
instance.__dict__[... | pydocs | pep-0487.rst:213 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_01.py | @dataclass
class InventoryItem:
'''Class for keeping track of an item in inventory.'''
name: str
unit_price: float
quantity_on_hand: int = 0
def total_cost(self) -> float:
return self.unit_price * self.quantity_on_hand
| pydocs | pep-0557.rst:42 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | legacy | dataclasses/legacy_01.py | def __init__(self, name: str, unit_price: float, quantity_on_hand: int = 0) -> None:
self.name = name
self.unit_price = unit_price
self.quantity_on_hand = quantity_on_hand
def __repr__(self):
return f'InventoryItem(name={self.name!r}, unit_price={self.unit_price!r}, quantity_on_hand={self.quantity_on_ha... | pydocs | pep-0557.rst:55 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_02.py | @dataclass
class C:
...
@dataclass()
class C:
...
@dataclass(init=True, repr=True, eq=True, order=False, unsafe_hash=False, frozen=False)
class C:
...
| pydocs | pep-0557.rst:176 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_03.py | @dataclass
class C:
a: int # 'a' has no default value
b: int = 0 # assign a default value for 'b'
| pydocs | pep-0557.rst:265 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_04.py | @dataclass
class C:
x: int
y: int = field(repr=False)
z: int = field(repr=False, default=10)
t: int = 20
| pydocs | pep-0557.rst:342 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_05.py | @dataclass
class C:
a: float
b: float
c: float = field(init=False)
def __post_init__(self):
self.c = self.a + self.b
| pydocs | pep-0557.rst:383 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_06.py | @dataclass
class C:
i: int
j: int = None
database: InitVar[DatabaseType] = None
def __post_init__(self, database):
if self.j is None and database is not None:
self.j = database.lookup('j')
c = C(10, database=my_database)
| pydocs | pep-0557.rst:423 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_07.py | @dataclass
class Base:
x: Any = 15.0
y: int = 0
@dataclass
class C(Base):
z: int = 10
x: int = 15
| pydocs | pep-0557.rst:464 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_08.py | @dataclass
class D:
x: List = []
def add(self, element):
self.x += element
| pydocs | pep-0557.rst:519 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_09.py | @dataclass
class D:
x: list = field(default_factory=list)
assert D().x is not D().x
| pydocs | pep-0557.rst:550 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_10.py | @dataclass
class Point:
x: int
y: int
@dataclass
class C:
l: List[Point]
p = Point(10, 20)
assert asdict(p) == {'x': 10, 'y': 20}
c = C([Point(0, 0), Point(10, 4)])
assert asdict(c) == {'l': [{'x': 0, 'y': 0}, {'x': 10, 'y': 4}]}
| pydocs | pep-0557.rst:571 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_11.py | C = make_dataclass('C',
[('x', int),
'y',
('z', int, field(default=5))],
namespace={'add_one': lambda self: self.x + 1})
| pydocs | pep-0557.rst:613 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_12.py | @dataclass
class C:
x: int
y: 'typing.Any'
z: int = 5
def add_one(self):
return self.x + 1
| pydocs | pep-0557.rst:621 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | legacy | dataclasses/legacy_02.py | Time = namedtuple('Time', ['hour', 'minute'])
def get_time():
return Time(12, 0)
| pydocs | pep-0557.rst:710 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_13.py | @dataclass
class C:
x: int
@classmethod
def from_file(cls, filename):
with open(filename) as fl:
file_value = int(fl.read())
return C(file_value)
c = C.from_file('file.txt')
| pydocs | pep-0557.rst:762 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_14.py | @dataclass
class Square:
length: float
area: float = field(init=False, default=0.0)
def __post_init__(self):
self.area = self.length * self.length
s1 = Square(1.0)
s2 = replace(s1, length=2.0)
| pydocs | pep-0557.rst:831 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_15.py | @dataclass(init=False)
class ArgHolder:
args: List[Any]
kwargs: Mapping[Any, Any]
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
a = ArgHolder(1, 2, three=3)
| pydocs | pep-0557.rst:867 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | legacy | dataclasses/legacy_03.py | class Application:
def __init__(self, name, requirements, constraints=None, path='', executable_links=None, executables_dir=()):
self.name = name
self.requirements = requirements
self.constraints = {} if constraints is None else constraints
self.path = path
self.executable_li... | pydocs | pep-0557.rst:883 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | dataclasses | 557 | 3.7 | modern | dataclasses/modern_16.py | @dataclass
class Application:
name: str
requirements: List[Requirement]
constraints: Dict[str, str] = field(default_factory=dict)
path: str = ''
executable_links: List[str] = field(default_factory=list)
executable_dir: Tuple[str] = ()
additional_items: List[str] = field(init=False, default_f... | pydocs | pep-0557.rst:898 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_01.py | a = ZoneInfo(key)
b = ZoneInfo(key)
assert a is b
| pydocs | pep-0615.rst:108 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_02.py | a = ZoneInfo.no_cache(key)
b = ZoneInfo(key)
a is not b
| pydocs | pep-0615.rst:152 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_03.py | NYC0 = ZoneInfo("America/New_York")
NYC0 is ZoneInfo("America/New_York")
ZoneInfo.clear_cache()
NYC1 = ZoneInfo("America/New_York")
NYC0 is NYC1
NYC1 is ZoneInfo("America/New_York")
| pydocs | pep-0615.rst:219 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_04.py | NYC0 = ZoneInfo("America/New_York")
LA0 = ZoneInfo("America/Los_Angeles")
ZoneInfo.clear_cache(only_keys=["America/New_York"])
NYC1 = ZoneInfo("America/New_York")
LA0 = ZoneInfo("America/Los_Angeles")
NYC0 is NYC1
LA0 is LA1
| pydocs | pep-0615.rst:234 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_05.py | zone = ZoneInfo("Pacific/Kwajalein")
str(zone)
dt = datetime(2020, 4, 1, 3, 15, tzinfo=zone)
f"{dt.isoformat()} [{dt.tzinfo}]"
| pydocs | pep-0615.rst:263 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_06.py | zone = ZoneInfo.from_file(f)
str(zone)
| pydocs | pep-0615.rst:275 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_07.py | a = ZoneInfo("Europe/Berlin")
b = pickle.loads(europe_berlin_pkl)
a is b
| pydocs | pep-0615.rst:310 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_08.py | a = ZoneInfo("Europe/Berlin")
b = pickle.loads(europe_berlin_pkl_nc)
a is b
| pydocs | pep-0615.rst:324 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | zoneinfo | 615 | 3.9 | modern | zoneinfo/modern_09.py | reset_tzpath(to=["/my/custom/tzdb"])
a = ZoneInfo("My/Custom/Zone")
reset_tzpath()
b = ZoneInfo("My/Custom/Zone")
del a
del b
c = ZoneInfo("My/Custom/Zone")
| pydocs | pep-0615.rst:534 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | explicit-encoding | 597 | 3.10 | modern | explicit-encoding/modern_01.py | def read_text(self, encoding=None, errors=None):
encoding = io.text_encoding(encoding)
with self.open(mode='r', encoding=encoding, errors=errors) as f:
return f.read()
| pydocs | pep-0597.rst:174 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | tarfile-filter | 706 | 3.12 | modern | tarfile-filter/modern_01.py | with StatefulFilter() as filter_func:
my_tar.extract(path, filter=filter_func)
| pydocs | pep-0706.rst:487 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | tarfile-filter | 706 | 3.12 | modern | tarfile-filter/modern_02.py | my_tarfile.extraction_filter = (lambda member, path: member)
my_tarfile.extractall()
| pydocs | pep-0706.rst:542 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | tarfile-filter | 706 | 3.12 | modern | tarfile-filter/modern_03.py | my_tarfile.extraction_filter = getattr(tarfile, 'data_filter',
(lambda member, path: member))
my_tarfile.extractall()
| pydocs | pep-0706.rst:548 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | tarfile-filter | 706 | 3.12 | modern | tarfile-filter/modern_04.py | my_tarfile.extractall(filter=tarfile.data_filter)
| pydocs | pep-0706.rst:557 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | tarfile-filter | 706 | 3.12 | modern | tarfile-filter/modern_05.py | my_tarfile.extraction_filter = tarfile.data_filter
my_tarfile.extractall()
| pydocs | pep-0706.rst:561 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | tarfile-filter | 706 | 3.12 | modern | tarfile-filter/modern_06.py | if hasattr(tarfile, 'data_filter'):
my_tarfile.extractall(filter='data')
else:
# remove this when no longer needed
warn_the_user('Extracting may be unsafe; consider updating Python')
my_tarfile.extractall()
| pydocs | pep-0706.rst:566 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | legacy | sentinel/legacy_01.py | _sentinel = object()
| pydocs | pep-0661.rst:60 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | modern | sentinel/modern_01.py | MISSING = sentinel('MISSING')
def read_value(default=MISSING):
...
| pydocs | pep-0661.rst:185 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | modern | sentinel/modern_02.py | MISSING = sentinel('MISSING')
assert pickle.loads(pickle.dumps(MISSING)) is MISSING
| pydocs | pep-0661.rst:210 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | modern | sentinel/modern_03.py | MyClass_NotGiven = sentinel('MyClass.NotGiven')
MyClass_NotGiven
| pydocs | pep-0661.rst:221 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | modern | sentinel/modern_04.py | MISSING = sentinel('MISSING')
def foo(value: int | MISSING = MISSING) -> int:
...
| pydocs | pep-0661.rst:239 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | modern | sentinel/modern_05.py | from typing import assert_type
MISSING = sentinel('MISSING')
def foo(value: int | MISSING) -> None:
if value is MISSING:
assert_type(value, MISSING)
else:
assert_type(value, int)
| pydocs | pep-0661.rst:257 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | legacy | sentinel/legacy_02.py | class NotGivenType(Enum):
NotGiven = 'NotGiven'
NotGiven = NotGivenType.NotGiven
| pydocs | pep-0661.rst:399 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | legacy | sentinel/legacy_03.py | class NotGiven: pass
| pydocs | pep-0661.rst:432 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | sentinel | 661 | 3.15 | modern | sentinel/modern_06.py | class MyClass:
NotGiven = sentinel('MyClass.NotGiven')
MyClass.NotGiven
| pydocs | pep-0661.rst:542 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_01.py | [*it for it in its] # list with the concatenation of iterables in 'its'
{*it for it in its} # set with the union of iterables in 'its'
{**d for d in dicts} # dict with the combination of dicts in 'dicts'
(*it for it in its) # generator of the concatenation of iterables in 'its'
| pydocs | pep-0798.rst:28 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_01.py | new_list = []
for it in its:
new_list.extend(it)
new_set = set()
for it in its:
new_set.update(it)
new_dict = {}
for d in dicts:
new_dict.update(d)
def new_generator():
for it in its:
yield from it
| pydocs | pep-0798.rst:50 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_02.py | [x for it in its for x in it]
{x for it in its for x in it}
{key: value for d in dicts for key, value in d.items()}
(x for it in its for x in it)
| pydocs | pep-0798.rst:68 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_03.py | list(itertools.chain(*its))
set(itertools.chain(*its))
dict(itertools.chain(*(d.items() for d in dicts)))
itertools.chain(*its)
list(itertools.chain.from_iterable(its))
set(itertools.chain.from_iterable(its))
dict(itertools.chain.from_iterable(d.items() for d in dicts))
itertools.chain.from_iterable(its)
| pydocs | pep-0798.rst:75 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_04.py | functools.reduce(operator.iconcat, its, (new_list := []))
functools.reduce(operator.ior, its, (new_set := set()))
functools.reduce(operator.ior, its, (new_dict := {}))
| pydocs | pep-0798.rst:87 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_02.py | new_list = [*expr for x in its]
new_set = {*expr for x in its}
new_dict = {**expr for d in dicts}
| pydocs | pep-0798.rst:204 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_05.py | new_list = []
for x in its:
new_list.extend(expr)
new_set = set()
for x in its:
new_set.update(expr)
new_dict = {}
for x in dicts:
new_dict.update(expr)
| pydocs | pep-0798.rst:211 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_06.py | # equivalent to g = (*expr for x in it)
def generator():
for x in it:
for i in expr:
yield i
g = generator()
| pydocs | pep-0798.rst:233 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_07.py | def generator():
for i in (0, 2, 4):
for j in (y := [i, i+1]):
yield j
| pydocs | pep-0798.rst:269 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_03.py | g = (*(y := [i, i+1]) for i in (0, 2, 4))
y
next(g)
y
next(g)
y
next(g)
y
| pydocs | pep-0798.rst:279 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_04.py | # current:
comments = []
for token in self:
comments.extend(token.comments)
return comments
# proposed:
return [*token.comments for token in self]
| pydocs | pep-0798.rst:435 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_05.py | # current:
ignored_names = []
for pattern in patterns:
ignored_names.extend(fnmatch.filter(names, pattern))
return set(ignored_names)
# proposed:
return {*fnmatch.filter(names, pattern) for pattern in patterns}
| pydocs | pep-0798.rst:446 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_06.py | # current:
cookies = []
for domain in self._cookies.keys():
cookies.extend(self._cookies_for_domain(domain, request))
return cookies
# proposed:
return [
*self._cookies_for_domain(domain, request)
for domain in self._cookies.keys()
]
| pydocs | pep-0798.rst:457 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_07.py | # current:
inherited_slots = set(
itertools.chain.from_iterable(map(_get_slots, cls.__mro__[1:-1]))
)
# proposed:
inherited_slots = {*_get_slots(c) for c in cls.__mro__[1:-1]}
| pydocs | pep-0798.rst:479 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_08.py | # current:
return itertools.chain.from_iterable(
path.search(prepared) for path in map(FastPath, paths)
)
# proposed:
return (*FastPath(path).search(prepared) for path in paths)
| pydocs | pep-0798.rst:489 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_09.py | # current:
return _chain.from_iterable(_starmap(_repeat, self.items()))
# proposed:
return (*_repeat(elt, num) for elt, num in self.items())
| pydocs | pep-0798.rst:499 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_10.py | # current:
parents = itertools.chain.from_iterable(map(_parents, names))
# proposed:
parents = (*_parents(name) for name in names)
| pydocs | pep-0798.rst:507 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_11.py | # current:
search_locations = set(chain.from_iterable(
getattr(spec, 'submodule_search_locations', [])
for spec in specs if spec
))
# proposed:
search_locations = {
*getattr(spec, 'submodule_search_locations', [])
for spec in specs if spec
}
| pydocs | pep-0798.rst:515 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_12.py | # current:
children = (child for path in self._paths for child in path.iterdir())
# proposed:
children = (*path.iterdir() for path in self._paths)
| pydocs | pep-0798.rst:535 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_13.py | # current:
exceptions = [exc for sub in exceptions for exc in sub]
# proposed:
exceptions = [*sub for sub in exceptions]
| pydocs | pep-0798.rst:543 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_14.py | # current:
return self.__class__(e for s in (self, other) for e in s)
# proposed:
return self.__class__(*s for s in (self, other))
| pydocs | pep-0798.rst:551 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_08.py | out = []
for x in it:
out.extend(expr)
| pydocs | pep-0798.rst:574 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_09.py | # equivalent to out = {expr for x in it}
out = set()
for x in it:
out.add(expr)
# equivalent to out = {*expr for x in it}
out = set()
for x in it:
out.update(expr)
# equivalent to out = {k_expr: v_expr for x in it}
out = {}
for x in it:
out[k_expr] = v_expr
# equivalent to out = {**expr for x in it}, pro... | pydocs | pep-0798.rst:581 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_10.py | # equivalent to g = (expr for x in it)
def generator():
for x in it:
yield expr
g = generator()
# equivalent to g = (*expr for x in it)
def generator():
for x in it:
yield from expr
g = generator()
| pydocs | pep-0798.rst:605 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | modern | comprehension-unpacking/modern_15.py | f([*x for x in y]) # pass in a single list
f({*x for x in y}) # pass in a single set
f(*x for x in y) # pass in a single generator (no additional parentheses required around genexp)
f(*[*x for x in y]) # pass in elements from the list separately
f(*{*x for x in y}) # pass in elements from the set separately
f(*(*... | pydocs | pep-0798.rst:702 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
train | 3.15 | comprehension-unpacking | 798 | 3.15 | legacy | comprehension-unpacking/legacy_11.py | def yield_from(iterables):
for iterable in iterables:
yield from iterable
def explicit_loop(iterables):
for iterable in iterables:
for item in iterable:
yield item
# Both produce the same sequence of values
x = list(yield_from([[1, 2], [3, 4]]))
y = list(explicit_loop([[1, 2], [3, ... | pydocs | pep-0798.rst:941 | e449c7e446faec3ecf953e83fbe677b771bf5d7c | CC0-1.0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.