Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
wiki2:python3 [2020/06/20 13:21] alfred [Notes on co-routines] |
wiki2:python3 [2022/10/12 19:03] (actual) |
||
|---|---|---|---|
| Línea 88: | Línea 88: | ||
| Co-routines are functions that in a way similar to green threads are executed in a single thread and process. However, they are not so costly as are not managed by the operating system but by the **event loop**. | Co-routines are functions that in a way similar to green threads are executed in a single thread and process. However, they are not so costly as are not managed by the operating system but by the **event loop**. | ||
| + | For an advance use go to [[wiki2:python:notes#advanced_asyncio|Advanced asyncio]]. | ||
| ==== Basic points ==== | ==== Basic points ==== | ||
| Línea 424: | Línea 425: | ||
| asyncio.run(main()) | asyncio.run(main()) | ||
| + | </code> | ||
| + | |||
| + | ===== Enums ===== | ||
| + | |||
| + | <code python> | ||
| + | from enum import Enum | ||
| + | |||
| + | class Numbers(Enum): | ||
| + | ONE = 1 | ||
| + | TWO = 2 | ||
| + | |||
| + | number = Numbers(2) # <Numbers.TWO: 2> | ||
| + | number1 = Numbers["ONE"] # <Numbers.TWO: 2> | ||
| + | members = [n for n in Numbers] # [<Numbers.ONE: 1>, <Numbers.TWO: 2>] | ||
| + | values = [n.value for n in Numbers] # [1, 2] | ||
| + | </code> | ||
| + | |||
| + | ==== Links ==== | ||
| + | |||
| + | * https://realpython.com/python-enum/ | ||
| + | |||
| + | ===== Python type checking ===== | ||
| + | |||
| + | <code> | ||
| + | pi: float = 3.142 | ||
| + | |||
| + | def headline(text: str, align: bool = True) -> str: | ||
| + | pass | ||
| + | | ||
| + | | ||
| + | </code> | ||
| + | |||
| + | The ''MyPy'' library allows to check code files. | ||
| + | |||
| + | <code> | ||
| + | >>> names: list = ["Guido", "Jukka", "Ivan"] | ||
| + | >>> version: tuple = (3, 7, 1) | ||
| + | >>> options: dict = {"centered": False, "capitalize": True} | ||
| + | >>> from typing import Dict, List, Tuple | ||
| + | >>> names: List[str] = ["Guido", "Jukka", "Ivan"] | ||
| + | >>> version: Tuple[int, int, int] = (3, 7, 1) | ||
| + | >>> options: Dict[str, bool] = {"centered": False, "capitalize": True} | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | import random | ||
| + | from typing import Any, Sequence | ||
| + | |||
| + | def choose(items: Sequence[Any]) -> Any: | ||
| + | return random.choice(items) | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | class Animal: | ||
| + | def __init__(self, name: str, birthday: date) -> None: | ||
| + | self.name = name | ||
| + | self.birthday = birthday | ||
| + | |||
| + | @classmethod | ||
| + | def newborn(cls, name: str) -> "Animal": | ||
| + | return cls(name, date.today()) | ||
| + | |||
| + | def twin(self, name: str) -> "Animal": | ||
| + | cls = self.__class__ | ||
| + | return cls(name, self.birthday) | ||
| + | </code> | ||
| + | |||
| + | <code> | ||
| + | def headline(text, width=80, fill_char="-"): | ||
| + | # type: (str, int, str) -> str | ||
| + | return f" {text.title()} ".center(width, fill_char) | ||
| + | |||
| + | def headline( | ||
| + | text, # type: str | ||
| + | width=80, # type: int | ||
| + | fill_char="-", # type: str | ||
| + | ): # type: (...) -> str | ||
| + | return f" {text.title()} ".center(width, fill_char) | ||
| </code> | </code> | ||
| ===== Notes ===== | ===== Notes ===== | ||