mirror of
https://github.com/macaodha/batdetect2.git
synced 2025-06-29 22:51:58 +02:00
Better read config nested field
This commit is contained in:
parent
c2c4ac53fd
commit
1338ae7431
@ -1,4 +1,4 @@
|
|||||||
from typing import Optional, Type, TypeVar
|
from typing import Any, Optional, Type, TypeVar
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
from pydantic import BaseModel, ConfigDict
|
from pydantic import BaseModel, ConfigDict
|
||||||
@ -12,6 +12,15 @@ class BaseConfig(BaseModel):
|
|||||||
T = TypeVar("T", bound=BaseModel)
|
T = TypeVar("T", bound=BaseModel)
|
||||||
|
|
||||||
|
|
||||||
|
def get_object_field(obj: dict, field: str) -> Any:
|
||||||
|
if "." not in field:
|
||||||
|
return obj[field]
|
||||||
|
|
||||||
|
field, rest = field.split(".", 1)
|
||||||
|
subobj = obj[field]
|
||||||
|
return get_object_field(subobj, rest)
|
||||||
|
|
||||||
|
|
||||||
def load_config(
|
def load_config(
|
||||||
path: PathLike,
|
path: PathLike,
|
||||||
schema: Type[T],
|
schema: Type[T],
|
||||||
@ -21,6 +30,6 @@ def load_config(
|
|||||||
config = yaml.safe_load(file)
|
config = yaml.safe_load(file)
|
||||||
|
|
||||||
if field:
|
if field:
|
||||||
config = config[field]
|
config = get_object_field(config, field)
|
||||||
|
|
||||||
return schema.model_validate(config)
|
return schema.model_validate(config)
|
||||||
|
Loading…
Reference in New Issue
Block a user