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

+9-6
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

+1-1
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

-4
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

+2-1
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

+3-3
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

+2-2
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

+4-4
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

+1-1
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

+1-1
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

+11-10
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()

17-it-generator/iter_gen_type.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from collections.abc import Iterator
2+
from keyword import kwlist
3+
from typing import TYPE_CHECKING
4+
5+
short_kw = (k for k in kwlist if len(k) < 5) # <1>
6+
7+
if TYPE_CHECKING:
8+
reveal_type(short_kw) # <2>
9+
10+
long_kw: Iterator[str] = (k for k in kwlist if len(k) >= 4) # <3>
11+
12+
if TYPE_CHECKING: # <4>
13+
reveal_type(long_kw)

17-it-generator/tree/step2/tree.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def sub_tree(cls):
99

1010

1111
def display(cls):
12-
for cls_name, level in tree(cls):
12+
for cls_name, level in tree(cls): # <3>
1313
indent = ' ' * 4 * level
1414
print(f'{indent}{cls_name}')
1515

23-dyn-attr-prop/oscon/data/osconfeed.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@
12431243
"time_start": "2014-07-21 13:30:00",
12441244
"time_stop": "2014-07-21 17:00:00",
12451245
"venue_serial": 1451,
1246-
"description": "Metaprograming in Python is fun and profitable thanks to its rich Data Model \u2013 APIs that let you handle functions, modules and even classes as objects that you can create, inspect and modify at runtime. The Data Model also enables your own objects to support infix operators, become iterable and emulate collections. This workshop shows how, through a diverse selection of examples and exercises.",
1246+
"description": "Metaprogramming in Python is fun and profitable thanks to its rich Data Model \u2013 APIs that let you handle functions, modules and even classes as objects that you can create, inspect and modify at runtime. The Data Model also enables your own objects to support infix operators, become iterable and emulate collections. This workshop shows how, through a diverse selection of examples and exercises.",
12471247
"website_url": "http://oscon.com/oscon2014/public/schedule/detail/34107",
12481248
"speakers": [150170],
12491249
"categories": [

23-dyn-attr-prop/oscon/demo_schedule2.py

-25
This file was deleted.

23-dyn-attr-prop/oscon/test_schedule_v3.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ def test_event_speakers():
5656

5757
def test_event_no_speakers():
5858
event = schedule.Record.fetch('event.36848')
59-
assert event.speakers == []
59+
assert event.speakers == []

23-dyn-attr-prop/pseudo_construction.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# pseudo-code for object construction
1+
# pseudocode for object construction
22
def make(the_class, some_arg):
33
new_object = the_class.__new__(some_arg)
44
if isinstance(new_object, the_class):

0 commit comments

Comments
 (0)