[Python] Load nested settings from yaml file using pydantic

Discussão em 'Python' iniciado por Stack, Março 20, 2025.

  1. Stack

    Stack Membro Participativo

    Consider the following yaml config file.

    # config.yaml

    key1:
    value1: x
    level1:
    level2_1:
    key1: y
    level3:
    key1: z
    key2: zz
    level2_2:
    key1: a
    level3: null


    I am looking for a pydantic model that loads the configuration from the config.yaml file. I am especially interested in using the dot-notation for all levels of this nested configuration. For example, I would like to use the settings as follows (assuming I assign the model to a variable called settings):

    settings.key1.value1 # x
    settings.level1.level2_1.key1 # y
    settings.level1.level2_2.level3.key2 # zz

    Continue reading...

Compartilhe esta Página