sync from Atlas

This commit is contained in:
Luciano Ramalho
2021-08-25 14:46:57 -03:00
parent b429f217d3
commit dd535abcf7
16 changed files with 51 additions and 62 deletions

View File

@@ -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}>')

View File

@@ -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()