본문 바로가기

Python345

Pytest _ client 에서 parmas 값 넣는 방법 테스트 코드를 작성하다보면 client를 사용해 uri요청을 보낼 때가 있습니다. 그리고 쿼리 파라미터(query string)는 패스 파라미터와 마찬가지로 uri를 텍스트로 작성해 보낼 수 있습니다. uri를 만드는 방법 세 가지를 함께 보겠습니다. 방법1. 일반 텍스트 response = client.get( "http://0.0.0.0:8000/users?limit=10&offset=0" ) 하지만 텍스트로만 작성할 경우, 하나의 케이스만 테스트 할 수 있는 정적 값이 되고 맙니다. 방법2. f-string 다음처럼 f-string을 통해 동적 값을 넣어줄 수 있습니다. limit = 10 offset = 0 response = client.get( f"http://0.0.0.0:8000/users.. 2022. 4. 27.
Two to One Description: Take 2 strings s1 and s2 including only letters from ato z. Return a new sorted string, the longest possible, containing distinct letters - each taken only once - coming from s1 or s2. Examples: a = "xyaabbbccccdefww" b = "xxxxyyyyabklmopq" longest(a, b) -> "abcdefklmopqwxy" a = "abcdefghijklmnopqrstuvwxyz" longest(a, a) -> "abcdefghijklmnopqrstuvwxyz" Solution: 1. Concatenate strin.. 2022. 4. 26.
컬럼 속성 변경 후 alembic migration할 때 ‘None’ 생기는 현상 테이블 컬럼의 속성을 변경하고 alembic migration 할 때 version 파일에 None이 생기는 현상을 발견했습니다. 의도 일단 저는 stock 테이블에서 code와 name컬럼의 속성을 unique로 변경하고자 했습니다. # models.py class Stock(Base): __tablename__ = "stock" id = Column(Integer, primary_key=True, autoincrement=True) code = Column(String(50), unique=True) # unique=True 속성 추가 name = Column(String(50), unique=True) # unique=True 속성 추가 market = Column(ENUM("KOSPI", "KOS.. 2022. 4. 26.
Apparently-Modifying Strings Description: For every string, after every occurrence of 'and' and/or 'but', insert the substring 'apparently' directly after the occurrence(s). If input does not contain 'and' or 'but', return the same string. If a blank string, return ''. If substring 'apparently' is already directly after an 'and' and/or 'but', do not add another. (Do not add duplicates). Examples: Input 1 'It was great and I.. 2022. 4. 26.
SQLAlchemy 1.x 와 2.0의 Query 스타일 비교 SQLAlchemy 2.0 SQLAlchemy 2.0에서는 query를 생성할 때 2.0 스타일을 사용할 수 있습니다. 2.0 스타일은 기존의 Session.query()가 아닌 select() 등의 메소드로 생성한 쿼리를 Session.execute()로 전달하여 DB와 통신할 수 있습니다. 현재 공식문서에서는 기존 스타일의 ORM을 제거할 계획은 없다고 하며, 어떤 스타일이든 함께 사용할 수 있다고 합니다. 다만, SQLAlchemy를 비동기로 구현할 경우 AsyncSession 클래스는 Session.query()를 지원하지 않으므로 2.0 스타일을 사용해야합니다. Within the ORM, 2.0 style query execution is supported, using select() cons.. 2022. 4. 25.
SQLAlchemy에서의 비동기 쿼리 (feat. 2.0 Style) AsyncSession을 통한 비동기 DB 통신 SQLAlchemy 에서는 DB와 비동기로 통신하기 위해서 AsyncSession 을 사용합니다. 그런데 AsyncSession을 사용하게 되면 ORM방식도 바뀌는데요. SQLAlchemy 공식문서에서는 2.0 스타일 쿼리를 사용한다고 합니다. Synopsis - ORM Using 2.0 style querying, the [AsyncSession] class provides full ORM functionality. Within the default mode of use, special care must be taken to avoid lazy loading or other expired-attribute access involving ORM relati.. 2022. 4. 24.