sync from Atlas
This commit is contained in:
@@ -19,7 +19,7 @@ Arithmetic progression class
|
||||
>>> from decimal import Decimal
|
||||
>>> ap = ArithmeticProgression(0, Decimal('.1'), .3)
|
||||
>>> list(ap)
|
||||
[Decimal('0.0'), Decimal('0.1'), Decimal('0.2')]
|
||||
[Decimal('0'), Decimal('0.1'), Decimal('0.2')]
|
||||
|
||||
# end::ARITPROG_CLASS_DEMO[]
|
||||
"""
|
||||
|
||||
@@ -14,7 +14,7 @@ Arithmetic progression generator function::
|
||||
>>> from decimal import Decimal
|
||||
>>> ap = aritprog_gen(0, Decimal('.1'), .3)
|
||||
>>> list(ap)
|
||||
[Decimal('0.0'), Decimal('0.1'), Decimal('0.2')]
|
||||
[Decimal('0'), Decimal('0.1'), Decimal('0.2')]
|
||||
|
||||
"""
|
||||
|
||||
|
||||
@@ -1,26 +1,27 @@
|
||||
# tag::COLUMNIZE[]
|
||||
from typing import Sequence, Tuple, Iterator
|
||||
from collections.abc import Sequence, Iterator
|
||||
|
||||
def columnize(sequence: Sequence[str], num_columns: int = 0) -> Iterator[Tuple[str, ...]]:
|
||||
def columnize(
|
||||
sequence: Sequence[str], num_columns: int = 0
|
||||
) -> Iterator[tuple[str, ...]]: # <1>
|
||||
if num_columns == 0:
|
||||
num_columns = round(len(sequence) ** .5)
|
||||
num_columns = round(len(sequence) ** 0.5)
|
||||
num_rows, reminder = divmod(len(sequence), num_columns)
|
||||
num_rows += bool(reminder)
|
||||
return (tuple(sequence[i::num_rows]) for i in range(num_rows))
|
||||
return (tuple(sequence[i::num_rows]) for i in range(num_rows)) # <2>
|
||||
# end::COLUMNIZE[]
|
||||
|
||||
|
||||
def demo() -> None:
|
||||
nato = ('Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India'
|
||||
' Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo'
|
||||
' Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu'
|
||||
).split()
|
||||
nato = (
|
||||
'Alfa Bravo Charlie Delta Echo Foxtrot Golf Hotel India'
|
||||
' Juliett Kilo Lima Mike November Oscar Papa Quebec Romeo'
|
||||
' Sierra Tango Uniform Victor Whiskey X-ray Yankee Zulu'
|
||||
).split()
|
||||
|
||||
for row in columnize(nato, 4):
|
||||
for word in row:
|
||||
print(f'{word:15}', end='')
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
demo()
|
||||
|
||||
13
17-it-generator/iter_gen_type.py
Normal file
13
17-it-generator/iter_gen_type.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from collections.abc import Iterator
|
||||
from keyword import kwlist
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
short_kw = (k for k in kwlist if len(k) < 5) # <1>
|
||||
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(short_kw) # <2>
|
||||
|
||||
long_kw: Iterator[str] = (k for k in kwlist if len(k) >= 4) # <3>
|
||||
|
||||
if TYPE_CHECKING: # <4>
|
||||
reveal_type(long_kw)
|
||||
@@ -9,7 +9,7 @@ def sub_tree(cls):
|
||||
|
||||
|
||||
def display(cls):
|
||||
for cls_name, level in tree(cls):
|
||||
for cls_name, level in tree(cls): # <3>
|
||||
indent = ' ' * 4 * level
|
||||
print(f'{indent}{cls_name}')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user