This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| def elide(text, max_len, ellipsis='…'): | |
| if len(text) <= max_len: | |
| return text | |
| cut = max_len | |
| while cut > 0 and text[cut - 1].isalnum(): | |
| cut -= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from random import shuffle | |
| from typing import Sequence, List, Any | |
| def sample(population: Sequence, size: int) -> List: | |
| if size < 1: | |
| raise ValueError('size must be >= 1') | |
| result = list(population) | |
| shuffle(result) | |
| return result[:size] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| ``char_index`` builds an inverted index mapping words to sets of Unicode | |
| characters which contain that word in their names. For example:: | |
| >>> index = char_index(32, 65) | |
| >>> sorted(index['SIGN']) | |
| ['#', '$', '%', '+', '<', '=', '>'] | |
| >>> sorted(index['DIGIT']) | |
| ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] | |
| >>> index['DIGIT'] & index['EIGHT'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Protocol, Any, TypeVar, overload, Callable, Iterable, Union | |
| class Comparable(Protocol): | |
| def __lt__(self, other: Any) -> bool: | |
| ... | |
| MISSING = object() | |
| EMPTY_MSG = 'max() arg is an empty sequence' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| builtins numpy builtins numpy decimal fractions builtins numpy | |
| complex complex64 float float16 Decimal Fraction int uint8 | |
| ---------------------------------------------------------------------------------------------- | |
| numbers.Number • • • • • • • • | |
| ---------------------------------------------------------------------------------------------- | |
| numbers.Complex • • • • • • • | |
| SupportsComplex • • • | |
| complex(x) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) (1+0j) | |
| ---------------------------------------------------------------------------------------------- | |
| numbers.Real • • • • • |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pessoas = [ | |
| {'nome': 'Ana', 'peso': 65.2}, | |
| {'nome': 'Juca', 'peso': 89.1}, | |
| {'nome': 'Carla', 'peso': 58.3} | |
| ] | |
| for pessoa in pessoas: | |
| nome = pessoa['nome'] | |
| peso = pessoa['peso'] | |
| print(f'{nome:10} {peso:.1f}') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| C:\Users\luciano>python | |
| Python 3.9.0 (tags/v3.9.0:9cf6752, Oct 5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32 | |
| Type "help", "copyright", "credits" or "license" for more information. | |
| >>> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import TypeVar, TYPE_CHECKING | |
| BT = TypeVar('BT', bound=float) | |
| def triple2(a: BT) -> BT: | |
| return a * 3 | |
| res2 = triple2(2) | |
| if TYPE_CHECKING: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ python3 pi_series.py | |
| 1 3.000000000000000000000000000000 -0.14159265358979312 | |
| 2 3.122498999199199154475081741111 -0.01909365439059396 | |
| 3 3.138470965295042880427445197711 -0.0031216882947502356 | |
| 4 3.141030313220715797228876908775 -0.0005623403690773188 | |
| 5 3.141485090117183798241740078083 -0.00010756347260931776 | |
| 6 3.141571214690141999881234369241 -2.1438899651116117e-05 | |
| 7 3.141588250040259211459670041222 -4.403549533904538e-06 | |
| 8 3.141591728079553114127975277370 -9.2551024000187e-07 | |
| 9 3.141592455512121073724074449274 -1.980776720422739e-07 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Could this be valid Python? | |
| if now >= T[4:20:PM]: sextou() | |
| >>> t = T[4:20] | |
| >>> t | |
| T[4:20] |