Skip to content

Commit 1283115

Browse files
committed
sync with Atlas
1 parent a77e6d1 commit 1283115

File tree

8 files changed

+108
-70
lines changed

8 files changed

+108
-70
lines changed

02-array-seq/match_lat_lon.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
def main():
2424
print(f'{"":15} | {"latitude":>9} | {"longitude":>9}')
2525
for record in metro_areas:
26-
match record:
27-
case [name, _, _, (lat, lon)] if lon <= 0:
26+
match record: # <1>
27+
case [name, _, _, (lat, lon)] if lon <= 0: # <2>
2828
print(f'{name:15} | {lat:9.4f} | {lon:9.4f}')
2929
# end::MAIN[]
3030

04-text-byte/charfinder/README.rst

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
========================
2+
Character Finder Utility
3+
========================
4+
5+
Usage tips
6+
==========
7+
8+
`cf.py` works as an executable on Unix-like systems,
9+
if you have `python3` on your `$PATH`::
10+
11+
$ chmod +x cf.py
12+
$ ./cf.py cat eyes
13+
U+1F638 😸 GRINNING CAT FACE WITH SMILING EYES
14+
U+1F63B 😻 SMILING CAT FACE WITH HEART-SHAPED EYES
15+
U+1F63D 😽 KISSING CAT FACE WITH CLOSED EYES
16+
17+
Use `wc -l` to count the number of hits::
18+
19+
$ ./cf.py hieroglyph | wc -l
20+
1663
21+
22+
With `tee` you can get the output and the count::
23+
24+
$ ./cf.py trigram | tee >(wc -l)
25+
U+2630 ☰ TRIGRAM FOR HEAVEN
26+
U+2631 ☱ TRIGRAM FOR LAKE
27+
U+2632 ☲ TRIGRAM FOR FIRE
28+
U+2633 ☳ TRIGRAM FOR THUNDER
29+
U+2634 ☴ TRIGRAM FOR WIND
30+
U+2635 ☵ TRIGRAM FOR WATER
31+
U+2636 ☶ TRIGRAM FOR MOUNTAIN
32+
U+2637 ☷ TRIGRAM FOR EARTH
33+
8
34+
35+
36+
Running the tests
37+
=================
38+
39+
Run the ``doctest`` module from the command line on
40+
this README.rst file (using ``-v`` to make tests visible)::
41+
42+
$ python3 -m doctest README.rst -v
43+
44+
That's what the ``test.sh`` script does.
45+
46+
47+
Tests
48+
-----
49+
50+
Import functions for testing::
51+
52+
>>> from cf import find, main
53+
54+
Test ``find`` with single result::
55+
56+
>>> find('sign', 'registered') # doctest:+NORMALIZE_WHITESPACE
57+
U+00AE ® REGISTERED SIGN
58+
59+
Test ``find`` with two results::
60+
61+
>>> find('chess', 'queen', last=0xFFFF) # doctest:+NORMALIZE_WHITESPACE
62+
U+2655 ♕ WHITE CHESS QUEEN
63+
U+265B ♛ BLACK CHESS QUEEN
64+
65+
Test ``find`` with no results::
66+
67+
>>> find('no_such_character')
68+
69+
Test ``main`` with no words::
70+
71+
>>> main([])
72+
Please provide words to find.

04-text-byte/charfinder/cf.py

-6
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@
44

55
FIRST, LAST = ord(' '), sys.maxunicode # <1>
66

7-
87
def find(*query_words, first=FIRST, last=LAST): # <2>
98
query = {w.upper() for w in query_words} # <3>
10-
count = 0
119
for code in range(first, last + 1):
1210
char = chr(code) # <4>
1311
name = unicodedata.name(char, None) # <5>
1412
if name and query.issubset(name.split()): # <6>
1513
print(f'U+{code:04X}\t{char}\t{name}') # <7>
16-
count += 1
17-
print(f'({count} found)')
18-
1914

2015
def main(words):
2116
if words:
2217
find(*words)
2318
else:
2419
print('Please provide words to find.')
2520

26-
2721
if __name__ == '__main__':
2822
main(sys.argv[1:])

04-text-byte/charfinder/cf_tests.rst

-36
This file was deleted.

04-text-byte/charfinder/test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#!/bin/bash
2-
python3 -m doctest cf_tests.rst $1
2+
python3 -m doctest README.rst $1

07-1class-func/clip.py

+28-20
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,48 @@
11
"""
2-
>>> clip('banana ', 6)
3-
'banana'
4-
>>> clip('banana ', 7)
5-
'banana'
6-
>>> clip('banana ', 5)
2+
>>> clip('banana split', 5)
73
'banana'
84
>>> clip('banana split', 6)
95
'banana'
106
>>> clip('banana split', 7)
117
'banana'
12-
>>> clip('banana split', 10)
8+
>>> clip('banana split', 8)
139
'banana'
1410
>>> clip('banana split', 11)
1511
'banana'
1612
>>> clip('banana split', 12)
1713
'banana split'
18-
>>> clip('bananasplit', 5)
19-
'bananasplit'
20-
>>> clip('banana split', 8)
21-
'banana'
14+
>>> clip('banana-split', 3)
15+
'banana-split'
16+
17+
Jess' tests:
18+
19+
>>> text = 'The quick brown fox jumps over the lazy dog.'
20+
>>> clip14 = clip(text, max_len=14)
21+
>>> clip14
22+
'The quick'
23+
>>> len(clip14)
24+
9
25+
>>> clip15 = clip(text, max_len=15)
26+
>>> clip15
27+
'The quick brown'
28+
>>> len(clip15)
29+
15
30+
2231
"""
2332

2433
# tag::CLIP[]
2534
def clip(text, max_len=80):
26-
"""Return text clipped at the last space before or after max_len"""
35+
"""Return max_len characters clipped at space if possible"""
2736
text = text.rstrip()
28-
end = len(text)
29-
if end <= max_len:
37+
if len(text) <= max_len or ' ' not in text:
3038
return text
31-
space_before = text.rfind(' ', 0, max_len)
32-
if space_before >= 0:
33-
end = space_before
39+
end = len(text)
40+
space_at = text.rfind(' ', 0, max_len + 1)
41+
if space_at >= 0:
42+
end = space_at
3443
else:
35-
space_after = text.find(' ', max_len)
36-
if space_after >= 0:
37-
end = space_after
44+
space_at = text.find(' ', max_len)
45+
if space_at >= 0:
46+
end = space_at
3847
return text[:end].rstrip()
3948
# end::CLIP[]
40-

07-1class-func/clip_introspection.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
>>> clip.__code__ # doctest: +ELLIPSIS
55
<code object clip at 0x...>
66
>>> clip.__code__.co_varnames
7-
('text', 'max_len', 'end', 'space_before', 'space_after')
7+
('text', 'max_len', 'end', 'space_at')
88
>>> clip.__code__.co_argcount
99
2

15-more-types/typeddict/books.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
# tag::BOOKDICT[]
2-
from typing import TypedDict, List
2+
from typing import TypedDict
33
import json
44

55
class BookDict(TypedDict):
66
isbn: str
77
title: str
8-
authors: List[str]
8+
authors: list[str]
99
pagecount: int
1010
# end::BOOKDICT[]
1111

1212
# tag::TOXML[]
1313
AUTHOR_EL = '<AUTHOR>{}</AUTHOR>'
1414

1515
def to_xml(book: BookDict) -> str: # <1>
16-
elements: List[str] = [] # <2>
16+
elements: list[str] = [] # <2>
1717
for key, value in book.items():
1818
if isinstance(value, list): # <3>
1919
elements.extend(
@@ -29,4 +29,4 @@ def to_xml(book: BookDict) -> str: # <1>
2929
def from_json(data: str) -> BookDict:
3030
whatever: BookDict = json.loads(data) # <1>
3131
return whatever # <2>
32-
# end::FROMJSON[]
32+
# end::FROMJSON[]

0 commit comments

Comments
 (0)