當前位置: 首頁>>代碼示例>>Python>>正文


Python pydantic.BaseSettings方法代碼示例

本文整理匯總了Python中pydantic.BaseSettings方法的典型用法代碼示例。如果您正苦於以下問題:Python pydantic.BaseSettings方法的具體用法?Python pydantic.BaseSettings怎麽用?Python pydantic.BaseSettings使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pydantic的用法示例。


在下文中一共展示了pydantic.BaseSettings方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_env_inheritance

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_inheritance(env):
    class SettingsParent(BaseSettings):
        foobar: str = 'parent default'

        class Config:
            fields = {'foobar': {'env': 'different'}}

    class SettingsChild(SettingsParent):
        foobar: str = 'child default'

    assert SettingsParent().foobar == 'parent default'
    assert SettingsParent(foobar='abc').foobar == 'abc'

    assert SettingsChild().foobar == 'child default'
    assert SettingsChild(foobar='abc').foobar == 'abc'
    env.set('different', 'env value')
    assert SettingsParent().foobar == 'env value'
    assert SettingsParent(foobar='abc').foobar == 'abc'
    assert SettingsChild().foobar == 'env value'
    assert SettingsChild(foobar='abc').foobar == 'abc' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:22,代碼來源:test_settings.py

示例2: test_env_inheritance_field

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_inheritance_field(env):
    class SettingsParent(BaseSettings):
        foobar: str = Field('parent default', env='foobar_env')

    class SettingsChild(SettingsParent):
        foobar: str = 'child default'

    assert SettingsParent().foobar == 'parent default'
    assert SettingsParent(foobar='abc').foobar == 'abc'

    assert SettingsChild().foobar == 'child default'
    assert SettingsChild(foobar='abc').foobar == 'abc'
    env.set('foobar_env', 'env value')
    assert SettingsParent().foobar == 'env value'
    assert SettingsParent(foobar='abc').foobar == 'abc'
    assert SettingsChild().foobar == 'child default'
    assert SettingsChild(foobar='abc').foobar == 'abc' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:19,代碼來源:test_settings.py

示例3: test_config_file_settings_nornir

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_config_file_settings_nornir(env):
    """
    See https://github.com/samuelcolvin/pydantic/pull/341#issuecomment-450378771
    """

    class Settings(BaseSettings):
        a: str
        b: str
        c: str

        def _build_values(self, init_kwargs, _env_file, _env_file_encoding):
            config_settings = init_kwargs.pop('__config_settings__')
            return {**config_settings, **init_kwargs, **self._build_environ()}

    env.set('C', 'env setting c')

    config = {'a': 'config a', 'b': 'config b', 'c': 'config c'}
    s = Settings(__config_settings__=config, b='argument b', c='argument c')
    assert s.a == 'config a'
    assert s.b == 'argument b'
    assert s.c == 'env setting c' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:23,代碼來源:test_settings.py

示例4: test_env_file_config

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_file_config(env, tmp_path):
    p = tmp_path / '.env'
    p.write_text(test_env_file)

    class Settings(BaseSettings):
        a: str
        b: str
        c: str

        class Config:
            env_file = p

    env.set('A', 'overridden var')

    s = Settings()
    assert s.a == 'overridden var'
    assert s.b == 'better string'
    assert s.c == 'best string' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:20,代碼來源:test_settings.py

示例5: test_env_file_config_case_sensitive

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_file_config_case_sensitive(tmp_path):
    p = tmp_path / '.env'
    p.write_text(test_env_file)

    class Settings(BaseSettings):
        a: str
        b: str
        c: str

        class Config:
            env_file = p
            case_sensitive = True

    with pytest.raises(ValidationError) as exc_info:
        Settings()
    assert exc_info.value.errors() == [{'loc': ('a',), 'msg': 'field required', 'type': 'value_error.missing'}] 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:18,代碼來源:test_settings.py

示例6: test_env_file_export

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_file_export(env, tmp_path):
    p = tmp_path / '.env'
    p.write_text(
        """\
export A='good string'
export B=better-string
export C="best string"
"""
    )

    class Settings(BaseSettings):
        a: str
        b: str
        c: str

        class Config:
            env_file = p

    env.set('A', 'overridden var')

    s = Settings()
    assert s.a == 'overridden var'
    assert s.b == 'better-string'
    assert s.c == 'best string' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:26,代碼來源:test_settings.py

示例7: test_env_file_example

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_file_example(tmp_path):
    p = tmp_path / '.env'
    p.write_text(
        """\
# ignore comment
ENVIRONMENT="production"
REDIS_ADDRESS=localhost:6379
MEANING_OF_LIFE=42
MY_VAR='Hello world'
"""
    )

    class Settings(BaseSettings):
        environment: str
        redis_address: str
        meaning_of_life: int
        my_var: str

    s = Settings(_env_file=str(p))
    assert s.dict() == {
        'environment': 'production',
        'redis_address': 'localhost:6379',
        'meaning_of_life': 42,
        'my_var': 'Hello world',
    } 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:27,代碼來源:test_settings.py

示例8: is_pydantic

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def is_pydantic(test_object):
    try:
        instance = isinstance(test_object, BaseSettings) or isinstance(test_object, BaseModel)
    except TypeError:
        instance = False
    try:
        subclass = issubclass(test_object, BaseSettings) or issubclass(test_object, BaseModel)
    except TypeError:
        subclass = False
    return instance or subclass 
開發者ID:MolSSI,項目名稱:QCElemental,代碼行數:12,代碼來源:autodocs.py

示例9: test_with_prefix

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_with_prefix(env):
    class Settings(BaseSettings):
        apple: str

        class Config:
            env_prefix = 'foobar_'

    with pytest.raises(ValidationError):
        Settings()
    env.set('foobar_apple', 'has_prefix')
    s = Settings()
    assert s.apple == 'has_prefix' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:14,代碼來源:test_settings.py

示例10: test_nested_env_with_basemodel

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_nested_env_with_basemodel(env):
    class TopValue(BaseModel):
        apple: str
        banana: str

    class Settings(BaseSettings):
        top: TopValue

    with pytest.raises(ValidationError):
        Settings()
    env.set('top', '{"banana": "secret_value"}')
    s = Settings(top={'apple': 'value'})
    assert s.top == {'apple': 'value', 'banana': 'secret_value'} 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:15,代碼來源:test_settings.py

示例11: test_nested_env_with_dict

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_nested_env_with_dict(env):
    class Settings(BaseSettings):
        top: Dict[str, str]

    with pytest.raises(ValidationError):
        Settings()
    env.set('top', '{"banana": "secret_value"}')
    s = Settings(top={'apple': 'value'})
    assert s.top == {'apple': 'value', 'banana': 'secret_value'} 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:11,代碼來源:test_settings.py

示例12: test_required_sub_model

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_required_sub_model(env):
    class Settings(BaseSettings):
        foobar: DateModel

    with pytest.raises(ValidationError):
        Settings()
    env.set('FOOBAR', '{"pips": "TRUE"}')
    s = Settings()
    assert s.foobar.pips is True 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:11,代碼來源:test_settings.py

示例13: test_non_class

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_non_class(env):
    class Settings(BaseSettings):
        foobar: NoneStr

    env.set('FOOBAR', 'xxx')
    s = Settings()
    assert s.foobar == 'xxx' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:9,代碼來源:test_settings.py

示例14: test_env_list

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_list(env):
    class Settings(BaseSettings):
        foobar: str

        class Config:
            fields = {'foobar': {'env': ['different1', 'different2']}}

    env.set('different1', 'value 1')
    env.set('different2', 'value 2')
    s = Settings()
    assert s.foobar == 'value 1' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:13,代碼來源:test_settings.py

示例15: test_env_list_field

# 需要導入模塊: import pydantic [as 別名]
# 或者: from pydantic import BaseSettings [as 別名]
def test_env_list_field(env):
    class Settings(BaseSettings):
        foobar: str = Field(..., env='foobar_env_name')

    env.set('FOOBAR_ENV_NAME', 'env value')
    s = Settings()
    assert s.foobar == 'env value' 
開發者ID:samuelcolvin,項目名稱:pydantic,代碼行數:9,代碼來源:test_settings.py


注:本文中的pydantic.BaseSettings方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。