python
-
Maybepython/returns 2022. 9. 4. 21:08
https://returns.readthedocs.io/en/latest/pages/maybe.html(scala 의 Option 과 비슷한듯)from returns.maybe import Maybe, Some, NothingMaybe.from_value(1)Maybe.from_optional(2)Maybe.from_optional(None)x = Some(2)y = Some(3)z: Maybe = Nothing# z: Maybe = Maybe.empty# mapx.map(lambda a: a + 3) # Some(5)# flatMapx.bind(lambda a: Some(a + 3)) # Some(5)# for-comprehensionMaybe.do( a + b for a in x ..
-
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..