python

· python
Q1) 파이썬에는 반복문이 for in 밖에 없나요?A1) while 도 있습니다.아니요, 파이썬에는 for in 외에도 while 반복문이 있습니다.파이썬의 주요 반복문:for 반복문# 리스트 순회fruits = ['apple', 'banana', 'orange']for fruit in fruits: print(fruit)# range 사용for i in range(5): print(i)# 딕셔너리 순회person = {'name': '오이', 'job': '개발자'}for key, value in person.items(): print(f"{key}: {value}")while 반복문# 조건이 참인 동안 반복count = 0while count 반복문 제어문:break: 반복문 즉시 ..
· python
Q1) schemas.py == dto?파이썬 자체에 대한 질문이라기 보다는 nest.js 와의 비교 일것 같은데, schemas.py 에서 정의한 class 들은 nest.js 의 dto 같은 거라고 봐야 할까요?A1)정확합니다! 💯 schemas.py의 클래스들은 NestJS의 DTO와 거의 동일한 역할을 합니다!NestJS DTO vs FastAPI Pydantic SchemasNestJS DTO 예시// create-book.dto.tsimport { IsString, IsOptional, IsInt, Min, Max, Matches } from 'class-validator';export class CreateBookDto { @IsString() @Length(1, 200) title:..
· python
Q1) 파이썬 모듈 시스템?아무래도 파이썬에 아직 익숙하지 않고 자바스크립트 베이스다보니 export import 가 어떻게 이루어지는지 잘 모르겠어요. 예를들어 main.py 에서는from fastapi import FastAPI, HTTPException, Depends, statusfrom sqlalchemy.orm import Sessionfrom typing import List from . import crud, models, schemasfrom .database import SessionLocal, engine, get_dbfrom .config import settings이렇게 되어 있는데, config 는 .config 로 가져오고, crud, models, schemas 는 왜 . 으..
· python
Q1) get_db 에서 제너레이터를 반환하는 이유가 궁금해요A1) 제너레이터 설명훌륭한 질문입니다! get_db 함수와 제너레이터 패턴에 대해 자세히 설명드리겠습니다.1. get_db 함수의 역할기본 구조def get_db(): db = SessionLocal() # 1. DB 세션 생성 try: yield db # 2. 세션을 "빌려줌" finally: db.close() # 3. 반드시 세션 종료함수의 생명주기요청 시작: 새로운 DB 세션 생성요청 처리: 라우터 함수에 세션 전달요청 완료: 자동으로 세션 종료2. 제너레이터를 사용하는 이유일반 함수 vs 제너레이터 비교❌ 일반 함수로 하면 어떻게 될까?def get_db_wrong(..
· python
uv를 사용한 FastAPI 도서 관리 API 만들기1단계: uv 설치 및 프로젝트 초기화uv 설치# uv 설치brew install uv프로젝트 초기화uv init fastapi-practicecd fastapi-practice2단계: 필요한 의존성 추가# FastAPI 관련 패키지들 (따옴표 필수!)uv add fastapi "uvicorn[standard]"# 데이터베이스 관련uv add sqlalchemy "psycopg[binary]" alembic# 환경변수 관리uv add python-dotenv# 개발용 도구uv add --dev pytest httpx주의: zsh 쉘에서는 []가 포함된 패키지명을 반드시 따옴표로 감싸야 합니다!3단계: DB - Docker 설정 (PostgreSQL)ve..
adminisme
'python' 카테고리의 글 목록