Код: Выделить всё
import requests
from pydantic import BaseModel, Field
from typing import List
url = "https://statsapi.mlb.com/api/v1.1/game/745707/feed/live/diffPatch"
response = requests.get(url).json()
class AtBat(BaseModel):
ab_number: int = Field(None) # Ideally I don't want this here, but it's there to bypass the missing error
_counter: int = 1
def __init__(self, **data):
super().__init__(**data)
self.ab_number = AtBat._counter
AtBat._counter += 1
print(self.ab_number) # Can use this as a debugging check
class Plays(BaseModel):
allPlays: List[AtBat]
data = Plays(**response['liveData']['plays'])
Код: Выделить всё
TypeError: unsupported operand type(s) for +=: 'ModelPrivateAttr' and 'int'
Код: Выделить всё
allPlays = [AtBat(ab_number=1), AtBat(ab_number=2), AtBat(ab_number=3), ..., ]
Подробнее здесь: https://stackoverflow.com/questions/793 ... n-pydantic
Мобильная версия