python
-
Curryingpython/응용 2023. 6. 11. 19:53
https://medium.com/data-engineer-things/functional-programming-in-data-engineering-with-python-part-1-c2c4f677f749 Without Currying def make_breakfast(toasts, sausages, eggs): return f"Breakfast made with {toasts} pieces of toasts, {sausages} sausages and {eggs} scrambled eggs" my_breakfast = make_breakfast(3, 4, 3) print(my_breakfast) With Currying def make_breakfast(toasts): def add_sausages(s..
-
pydantic: Genericspython/응용 2023. 5. 28. 22:29
https://mypy.readthedocs.io/en/stable/generics.html https://docs.pydantic.dev/latest/usage/models/#generic-models from typing import TypeVar, List, Generic from pydantic import BaseModel from pydantic.generics import GenericModel T = TypeVar('T', bound=BaseModel, covariant=True) class Dog(BaseModel): name: str age: int class Paging(GenericModel, Generic[T]): total: int page: int size: int items:..
-
jinja2 template 으로 Html 본문작성 후 이메일 보내기python/응용 2023. 5. 27. 20:29
templates/result.html {% for item in items %} {{ item.name }} {{ item.value }} {% endfor %} main.py import smtplib from datetime import datetime from email.mime.text import MIMEText from typing import List import jinja2 template_loader = jinja2.FileSystemLoader(searchpath="/home/user/app/templates", encoding="utf-8") template_env = jinja2.Environment(loader=template_loader, autoescape=True) def ..
-
자주하는 실수python/응용 2023. 5. 7. 21:03
How to remove elements in a Python List while looping a = [1, 2, 2, 3, 4] def even(x): return x % 2 == 0 for item in a: if even(item): a.remove(item) print(a) 결과: [1, 2, 3] 참고: https://www.python-engineer.com/posts/remove-elements-in-list-while-iterating/ Mutable Default Arguments def append_to(e, to=[]): to.append(e) return to my_list = append_to(12) print(my_list) my_other_list = append_to(42)..
-
__slot__, __call__python/응용 2023. 4. 10. 23:42
https://medium.com/@zlliu/36-things-i-didnt-know-about-python-until-recently-compilation-abdd425f2ce2 __slot__ python 에서는 runtime 에 class attribute 를 추가할 수 있다. 그런데 아무거나 마구 추가할 수 없도록 __slots__ 으로 제한할 수 있다. __slot__ 에 정의된 이름의 attribute 만 정의(or 추가) 될 수 있다. (__slot__ 은 class attibute 목록 명세?) class Dog: __slots__ = ['name', 'age'] dog = Dog() dog.name = 'apple' # ok dog.age = 1 # ok dog.gender = 'm' ..
-
yield frompython/응용 2023. 4. 10. 20:46
https://medium.com/better-programming/a-hands-on-guide-to-concurrency-in-python-with-asyncio-af33a795d808 참고 def get_all_numbers(): yield 1 yield from get_some_numbers() yield 5 def get_some_numbers(): yield 2 yield 3 yield 4 for i in get_all_numbers(): print(i) >> 1 >> 2 >> 3 >> 4 >> 5 Coroutines are defined using the async def keyword and can use the await syntax inside. The await keyword is v..
-
ABC vs Protocolpython/응용 2023. 4. 1. 20:36
ABC import abc # abc.ABC 를 상속받아 생성: Person 객체를 직접 생성 불가능하게 함 class Person(abc.ABC): def __init__(self, age=1): self.age = age @abc.abstractmethod def drive(self): pass class Adult(Person): def drive(self): print("ok") person = Adult() person.drive() Protocol https://www.pythontutorial.net/python-oop/python-protocol/ 를 참고함 from typing import Protocol ##############################################..
-
ThreadPoolExecutorpython/응용 2023. 2. 9. 18:13
https://codethief.io/ko/sending-simultaneous-requests-using-python/ import requests from concurrent.futures import ThreadPoolExecutor def get_url(url): return requests.get(url) urls = ["https://postman-echo.com/get?foo1=bar1&foo2=bar2"] * 10 with ThreadPoolExecutor(max_workers=1) as pool: response_list = list(pool.map(get_url, urls)) for res in response_list: print(res) ThreadPool vs ThreadPoolE..