python类Schema()的实例源码

test_config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_x10_address():
    """Test x10 addr validator."""
    schema = vol.Schema(cv.x10_address)
    with pytest.raises(vol.Invalid):
        schema('Q1')
        schema('q55')
        schema('garbage_addr')

    schema('a1')
    schema('C11')
test_config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def test_time_zone():
    """Test time zone validation."""
    schema = vol.Schema(cv.time_zone)

    with pytest.raises(vol.MultipleInvalid):
        schema('America/Do_Not_Exist')

    schema('America/Los_Angeles')
    schema('UTC')
test_config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def test_has_at_least_one_key():
    """Test has_at_least_one_key validator."""
    schema = vol.Schema(cv.has_at_least_one_key('beer', 'soda'))

    for value in (None, [], {}, {'wine': None}):
        with pytest.raises(vol.MultipleInvalid):
            schema(value)

    for value in ({'beer': None}, {'soda': None}):
        schema(value)
test_config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def test_ordered_dict_key_validator():
    """Test ordered_dict key validator."""
    schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.string))

    with pytest.raises(vol.Invalid):
        schema({None: 1})

    schema({'hello': 'world'})

    schema = vol.Schema(cv.ordered_dict(cv.match_all, int))

    with pytest.raises(vol.Invalid):
        schema({'hello': 1})

    schema({1: 'works'})
test_config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def test_ordered_dict_value_validator():
    """Test ordered_dict validator."""
    schema = vol.Schema(cv.ordered_dict(cv.string))

    with pytest.raises(vol.Invalid):
        schema({'hello': None})

    schema({'hello': 'world'})

    schema = vol.Schema(cv.ordered_dict(int))

    with pytest.raises(vol.Invalid):
        schema({'hello': 'world'})

    schema({'hello': 5})
test_config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def test_enum():
    """Test enum validator."""
    class TestEnum(enum.Enum):
        """Test enum."""

        value1 = "Value 1"
        value2 = "Value 2"

    schema = vol.Schema(cv.enum(TestEnum))

    with pytest.raises(vol.Invalid):
        schema('value3')

    TestEnum['value1']
config_validation.py 文件源码 项目:scarlett_os 作者: bossjones 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def url(value: Any) -> str:
    """Validate an URL."""
    url_in = str(value)

    if urlparse(url_in).scheme in ['http', 'https']:
        return vol.Schema(vol.Url())(url_in)

    raise vol.Invalid('invalid url')
upnp_client.py 文件源码 项目:home-assistant-dlna-dmr 作者: StevenLooman 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def _state_variable_create_schema(self, type_info):
        # construct validators
        validators = []

        data_type = type_info['data_type_python']
        validators.append(data_type)

        if 'allowed_values' in type_info:
            allowed_values = type_info['allowed_values']
            in_ = vol.In(allowed_values)  # coerce allowed values? assume always string for now
            validators.append(in_)

        if 'allowed_value_range' in type_info:
            min_ = type_info['allowed_value_range'].get('min', None)
            max_ = type_info['allowed_value_range'].get('max', None)
            min_ = data_type(min_)
            max_ = data_type(max_)
            range_ = vol.Range(min=min_, max=max_)
            validators.append(range_)

        # construct key
        key = vol.Required('value')

        if 'default_value' in type_info:
            default_value = type_info['default_value']
            if data_type == bool:
                default_value = default_value == '1'
            else:
                default_value = data_type(default_value)
            key.default = default_value

        return vol.Schema({key: vol.All(*validators)})
test_puppet.py 文件源码 项目:lxdock 作者: lxdock 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_can_set_default_manifest_file(self):
        with tempfile.TemporaryDirectory() as d:
            with (Path(d) / 'default.pp').open('w') as f:
                f.write('dummy pp file')
            config = {'manifests_path': d}
            try:
                final_config = Schema(PuppetProvisioner.schema)(config)
            except Invalid as e:
                pytest.fail("schema validation didn't pass: {}".format(e))
            assert final_config == {
                'manifest_file': 'default.pp',
                'manifests_path': d}
test_puppet.py 文件源码 项目:lxdock 作者: lxdock 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_can_set_default_environment(self):
        with tempfile.TemporaryDirectory() as d:
            os.mkdir(str(Path(d) / 'production'))
            config = {'environment_path': d}
            try:
                final_config = Schema(PuppetProvisioner.schema)(config)
            except Invalid as e:
                pytest.fail("schema validation didn't pass: {}".format(e))
            assert final_config == {
                'environment': 'production',
                'environment_path': d}
test_puppet.py 文件源码 项目:lxdock 作者: lxdock 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_can_set_default_mode_and_manifest_file(self, mock_isfile, mock_isdir):
        config = {}
        try:
            final_config = Schema(PuppetProvisioner.schema)(config)
        except Invalid as e:
            pytest.fail("schema validation didn't pass: {}".format(e))
        assert final_config == {
            'manifests_path': 'manifests',
            'manifest_file': 'default.pp'}
web_service_model.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 15 收藏 0 点赞 0 评论 0
def action_model(value):
    """Validates the data against action_model schema."""
    action_model_schema = voluptuous.Schema({
        "action": voluptuous.And(basestring, lambda o: not o.startswith("_"))
    }, required=True)

    return action_model_schema(value)
web_service_model.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def banana_model(value):
    """Validates the data against the banana_model schema."""
    banana_model_schema = voluptuous.Schema({
        "content": basestring
    }, required=True)

    return banana_model_schema(value)
lingam.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def validate_config(_config):
        lingam_schema = voluptuous.Schema({
            "module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
            "threshold": float
        }, required=True)
        return lingam_schema(_config)
svm_one_class.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def validate_config(_config):
        svm_schema = voluptuous.Schema({
            "module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
            "nb_samples": voluptuous.Or(float, int)
        }, required=True)
        return svm_schema(_config)
logistic_regression.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def validate_config(_config):
        log_reg_schema = voluptuous.Schema({
            'module': voluptuous.And(
                basestring, NoSpaceCharacter()),
            'nb_samples': voluptuous.Or(float, int)
        }, required=True)
        return log_reg_schema(_config)
svc.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def validate_config(_config):
        svc_schema = voluptuous.Schema({
            'module': voluptuous.And(
                basestring, NoSpaceCharacter()),
            'nb_samples': voluptuous.Or(float, int)
        }, required=True)
        return svc_schema(_config)
elliptic_envelope.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def validate_config(_config):
        elliptic_schema = voluptuous.Schema({
            'module': voluptuous.And(
                basestring, NoSpaceCharacter()),
            'nb_samples': voluptuous.Or(float, int)
        }, required=True)
        return elliptic_schema(_config)
decision_tree.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def validate_config(_config):
        decisiontree_schema = voluptuous.Schema({
            'module': voluptuous.And(
                basestring, NoSpaceCharacter()),
            'nb_samples': voluptuous.Or(float, int)
        }, required=True)
        return decisiontree_schema(_config)
random_forest_classifier.py 文件源码 项目:monasca-analytics 作者: openstack 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def validate_config(_config):
        randomforest_schema = voluptuous.Schema({
            'module': voluptuous.And(
                basestring, NoSpaceCharacter()),
            'nb_samples': voluptuous.Or(float, int)
        }, required=True)
        return randomforest_schema(_config)


问题


面经


文章

微信
公众号

扫码关注公众号