-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbase.py
More file actions
27 lines (21 loc) · 859 Bytes
/
base.py
File metadata and controls
27 lines (21 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from pydantic import BaseModel, ConfigDict
def to_camel_case(value: str) -> str:
parts = value.split("_")
if not parts:
return value
return parts[0] + "".join(part[:1].upper() + part[1:] for part in parts[1:])
class MLBBaseModel(BaseModel):
"""Common base for all MLB Stats API models.
- Pydantic v2
- Ignores unknown fields to remain resilient to API changes
- populate_by_name allows alias-based population when needed
"""
model_config = ConfigDict(
extra="ignore",
alias_generator=to_camel_case,
populate_by_name=True,
# MLB's API occasionally returns numbers for fields that are logically strings
# (e.g. liveData.plays.*.playEvents.*.base can be 1/2/3).
# Enable coercion to be resilient to these inconsistencies.
coerce_numbers_to_str=True,
)