GeezerVison is showing github.com — sync with Atlas · FerMatPy/example-code-2e@1283115
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: 02-array-seq/match_lat_lon.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 23 | 23 | def main(): | 24 | 24 | print(f'{"":15} | {"latitude":>9} | {"longitude":>9}') | 25 | 25 | 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> | 28 | 28 | print(f'{name:15} | {lat:9.4f} | {lon:9.4f}') | 29 | 29 | # end::MAIN[] | 30 | 30 |
| |
Collapse file Copy file name to clipboard +72Lines changed: 72 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 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. |
Collapse file Copy file name to clipboardExpand all lines: 04-text-byte/charfinder/cf.py -6Lines changed: 0 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 |
| 5 | 5 | FIRST, LAST = ord(' '), sys.maxunicode # <1> | 6 | 6 |
| 7 | | - | 8 | 7 | def find(*query_words, first=FIRST, last=LAST): # <2> | 9 | 8 | query = {w.upper() for w in query_words} # <3> | 10 | | - count = 0 | 11 | 9 | for code in range(first, last + 1): | 12 | 10 | char = chr(code) # <4> | 13 | 11 | name = unicodedata.name(char, None) # <5> | 14 | 12 | if name and query.issubset(name.split()): # <6> | 15 | 13 | print(f'U+{code:04X}\t{char}\t{name}') # <7> | 16 | | - count += 1 | 17 | | - print(f'({count} found)') | 18 | | - | 19 | 14 |
| 20 | 15 | def main(words): | 21 | 16 | if words: | 22 | 17 | find(*words) | 23 | 18 | else: | 24 | 19 | print('Please provide words to find.') | 25 | 20 |
| 26 | | - | 27 | 21 | if __name__ == '__main__': | 28 | 22 | main(sys.argv[1:]) |
Collapse file Copy file name to clipboardExpand all lines: 04-text-byte/charfinder/cf_tests.rst -36Lines changed: 0 additions & 36 deletions Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/bin/bash | 2 | | -python3 -m doctest cf_tests.rst $1 | | 2 | +python3 -m doctest README.rst $1 |
Collapse file Copy file name to clipboardExpand all lines: 07-1class-func/clip.py +28-20Lines changed: 28 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | """ | 2 | | - >>> clip('banana ', 6) | 3 | | - 'banana' | 4 | | - >>> clip('banana ', 7) | 5 | | - 'banana' | 6 | | - >>> clip('banana ', 5) | | 2 | + >>> clip('banana split', 5) | 7 | 3 | 'banana' | 8 | 4 | >>> clip('banana split', 6) | 9 | 5 | 'banana' | 10 | 6 | >>> clip('banana split', 7) | 11 | 7 | 'banana' | 12 | | - >>> clip('banana split', 10) | | 8 | + >>> clip('banana split', 8) | 13 | 9 | 'banana' | 14 | 10 | >>> clip('banana split', 11) | 15 | 11 | 'banana' | 16 | 12 | >>> clip('banana split', 12) | 17 | 13 | '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 | +
| 22 | 31 | """ | 23 | 32 |
| 24 | 33 | # tag::CLIP[] | 25 | 34 | 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""" | 27 | 36 | text = text.rstrip() | 28 | | - end = len(text) | 29 | | - if end <= max_len: | | 37 | + if len(text) <= max_len or ' ' not in text: | 30 | 38 | 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 | 34 | 43 | 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 | 38 | 47 | return text[:end].rstrip() | 39 | 48 | # end::CLIP[] | 40 | | - | |
Collapse file Copy file name to clipboardExpand all lines: 07-1class-func/clip_introspection.rst +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | >>> clip.__code__ # doctest: +ELLIPSIS | 5 | 5 | <code object clip at 0x...> | 6 | 6 | >>> clip.__code__.co_varnames | 7 | | -('text', 'max_len', 'end', 'space_before', 'space_after') | | 7 | +('text', 'max_len', 'end', 'space_at') | 8 | 8 | >>> clip.__code__.co_argcount | 9 | 9 | 2 |
Collapse file Copy file name to clipboardExpand all lines: 15-more-types/typeddict/books.py +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | # tag::BOOKDICT[] | 2 | | -from typing import TypedDict, List | | 2 | +from typing import TypedDict | 3 | 3 | import json | 4 | 4 |
| 5 | 5 | class BookDict(TypedDict): | 6 | 6 | isbn: str | 7 | 7 | title: str | 8 | | - authors: List[str] | | 8 | + authors: list[str] | 9 | 9 | pagecount: int | 10 | 10 | # end::BOOKDICT[] | 11 | 11 |
| 12 | 12 | # tag::TOXML[] | 13 | 13 | AUTHOR_EL = '<AUTHOR>{}</AUTHOR>' | 14 | 14 |
| 15 | 15 | def to_xml(book: BookDict) -> str: # <1> | 16 | | - elements: List[str] = [] # <2> | | 16 | + elements: list[str] = [] # <2> | 17 | 17 | for key, value in book.items(): | 18 | 18 | if isinstance(value, list): # <3> | 19 | 19 | elements.extend( | @@ -29,4 +29,4 @@ def to_xml(book: BookDict) -> str: # <1> | 29 | 29 | def from_json(data: str) -> BookDict: | 30 | 30 | whatever: BookDict = json.loads(data) # <1> | 31 | 31 | return whatever # <2> | 32 | | -# end::FROMJSON[] | | 32 | +# end::FROMJSON[] |
|