공부하는삶/Python

Use HTTPException FAstAPI 에서 오류는 HTTPException 클래스를 사용해 예외를 발생시켜 처리한다. HPPTException 클래스는 아래 세 개의 인수를 받는다 status_code : 예외 처리시 반환할 상태코드 detail : 클라이언트에게 전달한 메시지 headers : 헤더를 요구하거나 응답을 위한 선택적 인수 from fastapi import FastAPI, HTTPException app = FastAPI() items = {"foo": "The Foo Wrestlers"} @app.get("/items-header/{item_id}") async def read_item_header(item_id: str): if item_id not in items: rais..
array : data type = homogeneous / sequence data type. 같은 종류의 순서가 있는 데이터. 프로그래밍 용어 => indexing, slicing, sequence operatoration 가능함 sequence operatoration? x = [1, 2, 3] x + x >> [1, 2, 3, 1, 2, 3] 'abc' + 'bcd' >> 'abcbcd' 3*'abc' >> 'abcabcabc' Tensorflow에서 Operator overloading ? 원래 array + array = 뒤에서 붙어주는 연산인데 tensorflow에서는 vectorization으로 지원해줌 vectorization연산? elementwise방식으로 loop 없이 동시에 여러번..
Focus not on constructing a data collection but rather on describing "what" that data collection consists of. Encapsulation One obvious way of focusing more on "what" than "how" is simply to refactor code, and to put the data construction in a more isolated place-i.e., in a function or method #configure the data to start with collection = get_initial_state() state_var = None for datum in data_se..
Composite pattern의 활용 composite pattern 중 하나의 속성은 inheritance를 대신 할 수 있음 class A: class C: class B: def __init__(self): self.a = A() self.c = B() def multi(self): return self.a * self.b class AA: a = 1 def aaaa(slef, x): self.b = x def aaa(self): return self.b class BB(AA): pass 상속의 문제점 : AA와 coupling. 밀접하게 연결이 되어 있어서, class AA가 바뀌면 class BB의 기능도 바뀌어짐. class CC: ## AA가 변할 때 같이 안 변함 a = 1 # overi..
파이썬 표준 라이브러리는 C로 구현된 시퀀스를 제공한다 1) 자료형에 따른 분류 컨테이너 시퀀스(Container Sequence): 객체에 대한 참조. 서로 다른 자료형의 항목을 담을 수 있음. list, tuple, collections.deque 규일 시퀀스(Flat Sequence): 자신의 메모리 공간에 각 항목의 값. 단 하나의 자료형만 담을 수 있음. str, bytes, bytearray, memoryview, array.array 2) 가변성에 따른 불류 가변 시퀀스 : list, bytearray, array.array, collections.deque, memoryview 불변 시퀀스 : tuple, str, bytes 지능형 리스트(list comprehension) colors =..
1. 문자열 표현 __repr__() 특별 메서드는 객체를 문자열로 표현하기 위해 repr() 내장 메서드에 의해 호출된다. __repr__() 메서드가 반환한 문자열은 명확해야 하며, 가능하면 표현된 객체를 재생성하는 데 필요한 소스 코드와 일치해야 한다. __repr__()와 __str__()을 비교하면, __str__() 메서드는 str() 생성자에 의해 호출되며 print()에 의해 암묵적으로 사용된다. __str__()은 사용자에게 보여주기 적당한 형태의 문자열을 반환해야 한다. 이 두 메서드 중 하나만 구현해야 하면 __repr__() 메서드를 구현하는 것을 추천한다. 파이썬 인터프리터는 __str__() 메서드가 구현되어 있지 않을 때의 대책으로 __repr__() 메서드를 호출하기 때문이다..
Hanna 한나
'공부하는삶/Python' 카테고리의 글 목록