python/응용
-
ProcessPoolExecutorpython/응용 2022. 10. 29. 18:39
https://medium.com/@mecha-mind/think-twice-before-using-asyncio-in-python-7683472cb7a3 import math import random import time from concurrent.futures import ProcessPoolExecutor def square_array(arr: list[int]) -> list[int]: return [x ** 2 for x in arr] def square_array_in_parallel(arr: list[int], n: int = 4, m: int = 1000) -> list[int]: """ n: number of processes m: number of sub-arrays the array..
-
Raw SQL & SQLAlchemy 예제python/응용 2022. 7. 4. 22:31
Raw SQL pip install pymysql Single Connection import pymysql conn = pymysql.connect( host='127.0.0.1', port=3306, user='admin', passwd='admin', db='test', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor, autocommit=False ) # if the connection was lost, then it reconnects conn.ping(reconnect=True) with conn.cursor() as cursor: cursor.execute("""select name, age from person where age < %..
-
requestspython/응용 2022. 5. 31. 16:46
import requests payload = {'key1': 'value1', 'key2': 'value2'} r = requests.get('http://httpbin.org/get', params=payload, timeout=60) # r = requests.post('http://httpbin.org/post', data=payload) # r = requests.post('http://httpbin.org/put', data=payload) print(r.status_code) print(r.text) print(r.json())
-
json 읽기 / 쓰기python/응용 2022. 5. 31. 16:31
import json s = { "employ": [ {"id": 1, "name": "Mike"}, {"id": 2, "name": "Nancy"} ] } print(s) # json.dumps: returns a string representing a json object from an object json_dumps = json.dumps(s) print(json_dumps) # json.loads: returns an object from a string representing a json object print(json.loads(json_dumps)) # write to file with open('test.json', 'w') as f: json.dump(s, f) # read from fi..
-
pytest - fixturepython/응용 2022. 5. 23. 17:20
기본 사용 import pytest @pytest.fixture() def setup(): print("\nSetup ===============") def test1(setup): print("Execute test1") assert True test_cal.py::test1 Setup =============== PASSED [100%]Execute test1 yield 사용 import pytest @pytest.fixture() def setup2(): print("\nBefore ====================") yield print("\nAfter =====================") def test2(setup2): print("Execute test2") assert True ..
-
pytest - basicpython/응용 2022. 5. 23. 14:41
cal.py class Cal(object): def add(self, x, y): if type(x) is not int or type(y) is not int: raise ValueError('Invalid Int') return x + y test_cal.py import pytest import cal # scope 영역마다 1 번씩만 수행됨 @pytest.fixture(scope="function") def calculator(): c = cal.Cal() print("Before") yield c print("After") def test_cal(calculator): assert calculator.add(1, 2) == 3 @pytest.mark.parametrize(["x", "y", "..
-
Thread - queue & PoisonPillpython/응용 2022. 5. 23. 10:51
import logging import time from queue import Queue from threading import Thread logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) logger.setLevel(logging.INFO) queue = Queue(3) consumer_count = 2 class Consumer(Thread): def run(self) -> None: while True: value = queue.get(block=True) if value is None: logger.info(f"Recv poison pill, terminate Consumer-{self.name}") bre..
-
loggingpython/응용 2022. 5. 22. 00:57
import logging # https://docs.python.org/3/library/logging.html#logrecord-attributes formatter = '%(name)s %(asctime)s %(levelname)s %(message)s' logging.basicConfig(level=logging.INFO, format=formatter) # level 을 지정해 줘야 아래 logger.setLevel 이 적용되는듯? # logging.basicConfig(level=logging.INFO, filename='test.log') logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) h = logging.FileHa..