나는 이렇게 학습한다 575

Python _ zoneinfo 사용법, ZoneInfo 와 pytz 차이

ZoneInfo 을 사용하면 기존 pytz를 사용하지 않고도 타임존을 사용할 수 있다! (*ZoneInfo 는 Python3.9 버전 이상부터 사용가능) 'ZoneInfo' vs 'pytz' 그렇다면 ZoneInfo 와 pytz 는 어떤 차이가 있을까? 우선 ZoneInfo 는 따로 설치할 필요가 없는 빌트인 클래스이므로 다음처럼 간단하게 불러올 수 있다. from zoneinfo import ZoneInfo ZoneInfo 와 pytz 를 각각 사용해 현재시간을 가져와보자. pytztz = timezone("Asia/Seoul") zonetz = ZoneInfo("Asia/Seoul") print(datetime.datetime.now(tz=pytztz).tzname()) print(datetime.d..

FastAPI _ BaseSettings 을 lru_cache 할 때, unhashable type 에러 해결방법

BaseSettings을 사용한 객체를 lru_cache 할 때, TypeError: unhashable type: 에러가 발생할 때가 있다. 정리하면 다음과 같은 상황이다. given FastAPI 에서 lru_cache로 반환하는 BaseSettings 객체를, when 다시 lru_cache를 사용하는 함수가 인수로 받을 때, then TypeError: unhashable type: 에러가 발생함. e.g class Config(BaseSettings): URL: str = Field(..., env="URL") class Config: env_file = "secrets/.env" env_file_encoding = "utf-8" @lru_cache def get_config() -> Confi..

pytest _ 비동기 테스트 실행하기 (feat. auto 모드)

pytest 에서 async 테스트 함수를 실행하는 방법에는 'strict 모드' 와 'auto 모드' 가 있다. 오늘은 손쉽게 비동기 테스트를 할 수 있는 'auto 모드' 에 대해 알아보자. 먼저 pytest-asyncio 를 설치한다. pip install pytest-asyncio 아래 1번과 2번 중에 하나를 선택하여 파일과 코드를 추가한다. 1번 # pytest.ini [pytest] asyncio_mode = auto 2번 # pyproject.toml [tool.pytest.ini_options] asyncio_mode = "auto" 이것으로 'auto 모드' 설정이 끝났다. auto 모드는 pytest.ini 혹은 pyproject.toml 파일에 옵션 코드를 추가하여 async 테스트..

1025. Exclusive "or" (xor) Logical Operator

Exclusive "or" (xor) Logical Operator Overview In some scripting languages like PHP, there exists a logical operator (e.g. &&, ||, and, or, etc.) called the "Exclusive Or" (hence the name of this Kata). The exclusive or evaluates two booleans. It then returns true if exactly one of the two expressions are true, false otherwise. For example: false xor false == false // since both are false true x..