Herramientas de usuario

Herramientas del sitio


wiki2:python3

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
wiki2:python3 [2020/06/20 13:13]
alfred [Other tools]
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 392: Línea 393:
  
   * [[https://​news.ycombinator.com/​item?​id=23498742|Discussion on Hacker News]]   * [[https://​news.ycombinator.com/​item?​id=23498742|Discussion on Hacker News]]
 +
 +=== functools.partial for passing arguments ===
 +''​functools.partial''​ allows to create a function that not requires arguments from one that requires them.
 +<code python>
 +loop.call_soon(partial(print,​ "​Hello",​ flush=True))
 +</​code>​
 +
 +=== Stop co-routines when signals ===
 +<code python>
 +import asyncio
 +import functools
 +import os
 +import signal
 +
 +def ask_exit(signame,​ loop):
 +    print("​got signal %s: exit" % signame)
 +    loop.stop()
 +
 +async def main():
 +    loop = asyncio.get_running_loop()
 +
 +    for signame in {'​SIGINT',​ '​SIGTERM'​}:​
 +        loop.add_signal_handler(
 +            getattr(signal,​ signame),
 +            functools.partial(ask_exit,​ signame, loop))
 +
 +    await asyncio.sleep(3600)
 +
 +print("​Event loop running for 1 hour, press Ctrl+C to interrupt."​)
 +print(f"​pid {os.getpid()}:​ send SIGINT or SIGTERM to exit."​)
 +
 +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>​
 ===== Notes ===== ===== Notes =====
  
wiki2/python3.1592658791.txt.gz · Última modificación: 2020/06/20 14:13 (editor externo)