sync from Atlas
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import cast
|
||||
|
||||
def find_first_str(a: list[object]) -> str:
|
||||
index = next(i for i, x in enumerate(a) if isinstance(x, str))
|
||||
# We only get here if there's at least one string in a
|
||||
# We only get here if there's at least one string
|
||||
return cast(str, a[index])
|
||||
# end::CAST[]
|
||||
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# tag::ABS_DEMO[]
|
||||
import math
|
||||
from typing import NamedTuple, SupportsAbs
|
||||
|
||||
@@ -31,4 +28,3 @@ assert is_unit(v3)
|
||||
assert is_unit(v4)
|
||||
|
||||
print('OK')
|
||||
# end::ABS_DEMO[]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
# tag::MYMAX_TYPES[]
|
||||
from typing import Protocol, Any, TypeVar, overload, Callable, Iterable, Union
|
||||
from collections.abc import Callable, Iterable
|
||||
from typing import Protocol, Any, TypeVar, overload, Union
|
||||
|
||||
class SupportsLessThan(Protocol):
|
||||
def __lt__(self, other: Any) -> bool: ...
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import json
|
||||
# tag::BOOKDICT[]
|
||||
from typing import TypedDict
|
||||
import json
|
||||
|
||||
class BookDict(TypedDict):
|
||||
isbn: str
|
||||
@@ -10,14 +10,14 @@ class BookDict(TypedDict):
|
||||
# end::BOOKDICT[]
|
||||
|
||||
# tag::TOXML[]
|
||||
AUTHOR_EL = '<AUTHOR>{}</AUTHOR>'
|
||||
AUTHOR_ELEMENT = '<AUTHOR>{}</AUTHOR>'
|
||||
|
||||
def to_xml(book: BookDict) -> str: # <1>
|
||||
elements: list[str] = [] # <2>
|
||||
for key, value in book.items():
|
||||
if isinstance(value, list): # <3>
|
||||
elements.extend(
|
||||
AUTHOR_EL.format(n) for n in value) # <4>
|
||||
AUTHOR_ELEMENT.format(n) for n in value) # <4>
|
||||
else:
|
||||
tag = key.upper()
|
||||
elements.append(f'<{tag}>{value}</{tag}>')
|
||||
|
||||
@@ -10,13 +10,13 @@ class BookDict(TypedDict):
|
||||
# end::BOOKDICT[]
|
||||
|
||||
# tag::TOXML[]
|
||||
AUTHOR_EL = '<AUTHOR>{}</AUTHOR>'
|
||||
AUTHOR_ELEMENT = '<AUTHOR>{}</AUTHOR>'
|
||||
|
||||
def to_xml(book: BookDict) -> str: # <1>
|
||||
elements: List[str] = [] # <2>
|
||||
for key, value in book.items():
|
||||
if isinstance(value, list): # <3>
|
||||
elements.extend(AUTHOR_EL.format(n)
|
||||
elements.extend(AUTHOR_ELEMENT.format(n)
|
||||
for n in value)
|
||||
else:
|
||||
tag = key.upper()
|
||||
|
||||
Reference in New Issue
Block a user