本日の学習 Python/FastAPI(9/4)

2025-09-04
  • 本日の学習 Python/FastAPI
    • FakeRedis
    • AsyncGenerator
  • Send
    • ABC
  • Abstract Base Class
  • 抽象クラスを作成、abstractmethod(子で必ず実装が必要)を定義するため
    • LRU Cache
  • LRU = Least Recently Used(最近最も使われていない)
  • キャッシュが使われていないときに一番使われていないものを削除する
from functools import lru_cache

@lru_cache(maxsize=3)
def heavy_calce(x):
  print(x)
  return x * x
- Pydantic
  • computed_field
  • ただのpropertyだとjsonなどの結果やOpenAPIのドキュメントに含まれない。それを含めるようにするため。
class Person2(BaseModel):
    first_name: str
    last_name: str
    
    @computed_field
    @property
    def full_name(self) -> str:
        return f"{self.first_name} {self.last_name}"