Skip to content

Commit dd535ab

Browse files
committed
sync from Atlas
1 parent b429f21 commit dd535ab

File tree

16 files changed

+51
-62
lines changed

16 files changed

+51
-62
lines changed

08-def-type-hints/columnize.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
# tag::COLUMNIZE[]
22
from collections.abc import Sequence
33

4-
def columnize(sequence: Sequence[str], num_columns: int = 0) -> list[tuple[str, ...]]:
4+
def columnize(
5+
sequence: Sequence[str], num_columns: int = 0
6+
) -> list[tuple[str, ...]]:
57
if num_columns == 0:
6-
num_columns = round(len(sequence) ** .5)
8+
num_columns = round(len(sequence) ** 0.5)
79
num_rows, reminder = divmod(len(sequence), num_columns)
810
num_rows += bool(reminder)
911
return [tuple(sequence[i::num_rows]) for i in range(num_rows)]
1012
# end::COLUMNIZE[]
1113

1214

1315
def demo() -> None:
14-
nato = ('Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India'
15-
' Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo'
16-
' Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu'
17-
).split()
16+
nato = (
17+
'Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India'
18+
' Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo'
19+
' Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu'
20+
).split()
1821

1922
for row in columnize(nato, 4):
2023
for word in row:

15-more-types/cast/find.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
def find_first_str(a: list[object]) -> str:
55
index = next(i for i, x in enumerate(a) if isinstance(x, str))
6-
# We only get here if there's at least one string in a
6+
# We only get here if there's at least one string
77
return cast(str, a[index])
88
# end::CAST[]
99

15-more-types/protocol/abs_demo.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#!/usr/bin/env python3
2-
3-
# tag::ABS_DEMO[]
41
import math
52
from typing import NamedTuple, SupportsAbs
63

@@ -31,4 +28,3 @@ def is_unit(v: SupportsAbs[float]) -> bool: # <2>
3128
assert is_unit(v4)
3229

3330
print('OK')
34-
# end::ABS_DEMO[]

15-more-types/protocol/mymax/mymax.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# tag::MYMAX_TYPES[]
2-
from typing import Protocol, Any, TypeVar, overload, Callable, Iterable, Union
2+
from collections.abc import Callable, Iterable
3+
from typing import Protocol, Any, TypeVar, overload, Union
34

45
class SupportsLessThan(Protocol):
56
def __lt__(self, other: Any) -> bool: ...

15-more-types/typeddict/books.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import json
12
# tag::BOOKDICT[]
23
from typing import TypedDict
3-
import json
44

55
class BookDict(TypedDict):
66
isbn: str
@@ -10,14 +10,14 @@ class BookDict(TypedDict):
1010
# end::BOOKDICT[]
1111

1212
# tag::TOXML[]
13-
AUTHOR_EL = '<AUTHOR>{}</AUTHOR>'
13+
AUTHOR_ELEMENT = '<AUTHOR>{}</AUTHOR>'
1414

1515
def to_xml(book: BookDict) -> str: # <1>
1616
elements: list[str] = [] # <2>
1717
for key, value in book.items():
1818
if isinstance(value, list): # <3>
1919
elements.extend(
20-
AUTHOR_EL.format(n) for n in value) # <4>
20+
AUTHOR_ELEMENT.format(n) for n in value) # <4>
2121
else:
2222
tag = key.upper()
2323
elements.append(f'<{tag}>{value}</{tag}>')

15-more-types/typeddict/books_any.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ class BookDict(TypedDict):
1010
# end::BOOKDICT[]
1111

1212
# tag::TOXML[]
13-
AUTHOR_EL = '<AUTHOR>{}</AUTHOR>'
13+
AUTHOR_ELEMENT = '<AUTHOR>{}</AUTHOR>'
1414

1515
def to_xml(book: BookDict) -> str: # <1>
1616
elements: List[str] = [] # <2>
1717
for key, value in book.items():
1818
if isinstance(value, list): # <3>
19-
elements.extend(AUTHOR_EL.format(n)
19+
elements.extend(AUTHOR_ELEMENT.format(n)
2020
for n in value)
2121
else:
2222
tag = key.upper()

16-op-overloading/bingoaddable.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
>>> globe += 1 # <6>
4747
Traceback (most recent call last):
4848
...
49-
TypeError: right operand in += must be 'AddableBingoCage' or an iterable
49+
TypeError: right operand in += must be 'Tombola' or an iterable
5050
5151
# end::ADDABLE_BINGO_IADD_DEMO[]
5252
@@ -72,9 +72,9 @@ def __iadd__(self, other):
7272
try:
7373
other_iterable = iter(other) # <4>
7474
except TypeError: # <5>
75-
self_cls = type(self).__name__
76-
msg = "right operand in += must be {!r} or an iterable"
77-
raise TypeError(msg.format(self_cls))
75+
msg = ('right operand in += must be '
76+
"'Tombola' or an iterable")
77+
raise TypeError(msg)
7878
self.load(other_iterable) # <6>
7979
return self # <7>
8080

17-it-generator/aritprog_v1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
>>> from decimal import Decimal
2020
>>> ap = ArithmeticProgression(0, Decimal('.1'), .3)
2121
>>> list(ap)
22-
[Decimal('0.0'), Decimal('0.1'), Decimal('0.2')]
22+
[Decimal('0'), Decimal('0.1'), Decimal('0.2')]
2323
2424
# end::ARITPROG_CLASS_DEMO[]
2525
"""

17-it-generator/aritprog_v2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
>>> from decimal import Decimal
1515
>>> ap = aritprog_gen(0, Decimal('.1'), .3)
1616
>>> list(ap)
17-
[Decimal('0.0'), Decimal('0.1'), Decimal('0.2')]
17+
[Decimal('0'), Decimal('0.1'), Decimal('0.2')]
1818
1919
"""
2020

17-it-generator/columnize_iter.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
# tag::COLUMNIZE[]
2-
from typing import Sequence, Tuple, Iterator
2+
from collections.abc import Sequence, Iterator
33

4-
def columnize(sequence: Sequence[str], num_columns: int = 0) -> Iterator[Tuple[str, ...]]:
4+
def columnize(
5+
sequence: Sequence[str], num_columns: int = 0
6+
) -> Iterator[tuple[str, ...]]: # <1>
57
if num_columns == 0:
6-
num_columns = round(len(sequence) ** .5)
8+
num_columns = round(len(sequence) ** 0.5)
79
num_rows, reminder = divmod(len(sequence), num_columns)
810
num_rows += bool(reminder)
9-
return (tuple(sequence[i::num_rows]) for i in range(num_rows))
11+
return (tuple(sequence[i::num_rows]) for i in range(num_rows)) # <2>
1012
# end::COLUMNIZE[]
1113

12-
1314
def demo() -> None:
14-
nato = ('Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India'
15-
' Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo'
16-
' Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu'
17-
).split()
15+
nato = (
16+
'Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India'
17+
' Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo'
18+
' Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu'
19+
).split()
1820

1921
for row in columnize(nato, 4):
2022
for word in row:
2123
print(f'{word:15}', end='')
2224
print()
2325

24-
2526
if __name__ == '__main__':
2627
demo()

0 commit comments

Comments
 (0)