Python schematics.models.Model() Examples

The following are 30 code examples of schematics.models.Model(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module schematics.models , or try the search function .
Example #1
Source File: test_schema_types.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def test_complex_type():

    class Model(models.Model):
        x = schema_types.ComplexNumberType()

    model = Model({"x": 1 + 2j})
    model.validate()
    assert model.to_primitive() == {"x": [1, 2]}
    assert model.to_native() == {"x": 1 + 2j}

    model = Model({"x": [1, 2]})
    model.validate()
    assert model.to_primitive() == {"x": [1, 2]}
    assert model.to_native() == {"x": 1 + 2j}

    model = Model({"x": 1})
    model.validate()
    assert model.to_primitive() == {"x": [1, 0]}
    assert model.to_native() == {"x": 1 + 0j}

    with pytest.raises(ValueError, match="not convert to complex"):
        model = Model({"x": "hi"}) 
Example #2
Source File: context.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def get_node_model_dict(self,
                            node_meta_type: str) -> Dict[str, models.Model]:
        """Returns a mapping from node type to model.

        Args:
            node_meta_type: Node metatype to use.

        Returns:
            A dictionary for the given node metatype that maps node types
            to models. If the node metatype is not used in this context, an
            empty dictionary is returned.
        """
        if node_meta_type not in self._optplan_node_map:
            return {}

        return {
            node_type: vals[0] for node_type, vals in self._optplan_node_map[
                node_meta_type].items()
        } 
Example #3
Source File: context.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def register_node_type(self, node_meta_type: str, node_type: str,
                           model: models.Model, fun: CreatorFunction) -> None:
        """Registers a optplan node.

        Args:
            node_meta_type: Metatype of the node.
            node_type: Node type.
            model: Schematics model used to (de)serialize the node.
            fun: Callable used to instantiate the node from the model.

        Raises:
            ValueError: If a model with the same node metatype and node type
                was already registered for this context.
        """
        node_map = self._optplan_node_map.get(node_meta_type, {})
        if node_type in node_map:
            raise ValueError(
                "Node type '{}' with metatype '{}' was registered twice.".
                format(node_type, node_meta_type))
        node_map[node_type] = (model, fun)
        self._optplan_node_map[node_meta_type] = node_map 
Example #4
Source File: context.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def get_node_model_dict(self,
                            node_meta_type: str) -> Dict[str, models.Model]:
        """Retrieves a mapping from node type to corresponding model.

        Note this returns a mapping for a given a metatype.

        Args:
            node_meta_type: Node metatype.

        Returns:
            Dictionary.
        """
        model_dict = {}
        for context in self._stack:
            model_dict.update(context.get_node_model_dict(node_meta_type))

        return model_dict 
Example #5
Source File: context.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def get_node_model(self, node_meta_type: str,
                       node_type: str) -> Optional[models.Model]:
        """Retrieves the schematics model corresponding to a given type.

        Args:
            node_meta_type: Node metatype.
            node_type: Node type.

        Returns:
            Model corresponding to node metatype and type. Returns `None`
            if no such model can be found.
        """
        if node_meta_type not in self._optplan_node_map:
            return None

        if node_type not in self._optplan_node_map[node_meta_type]:
            return None
        return self._optplan_node_map[node_meta_type][node_type][0] 
Example #6
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_boolean():
    """
    messages:
        - INVALID_BOOLEAN
        - NUMBER_TOO_LOW
        - NUMBER_TOO_HIGH
    """

    class Data(Model):
        a = BooleanType()

    INVALID = ["", "a", "2" "TRUE", "FALSE", "TruE", "FalsE"]
    VALID = [0, 1, "0", "1", "True", "False", "true", "false", True, False]
    _test_valid_invalid(
        model=Data,
        valid=VALID,
        invalid=INVALID,
        expected_error=messages.INVALID_BOOLEAN,
    ) 
Example #7
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_multiple_errors_per_field():
    """
    messages:
        - FIELD_TOO_SHORT
        - REGEX_NOT_MATCHED
    """

    class Data(Model):
        a = StringType(min_length=3, regex=r"foo")

    data = {"a": "z"}
    v = SchematicsValidator(Data)
    result = v.validate(data, strict=True)
    assert result[0] is False
    error_messages = result[1]
    assert "a" in error_messages
    expected = [messages.FIELD_TOO_SHORT, messages.REGEX_NOT_MATCHED]
    assert sorted(error_messages["a"]) == expected 
Example #8
Source File: validate.py    From slim with zlib License 6 votes vote down vote up
def get_pv_model_info(model: Union[peewee.Model, Type[peewee.Model]]):
    new_model_cls: Type[Model] = type(model.__class__.__name__ + 'Validator', (Model,), {})
    foreign_keys = {}
    peewee_fields = {}

    ret = {
        'table_name': get_pv_table_name(model),
        'primary_key': get_pv_pk_name(model),
        'foreign_keys': foreign_keys,
        'data_model': new_model_cls,
        '_peewee_fields': peewee_fields
    }

    for name, field in model._meta.fields.items():
        if isinstance(field, peewee.ForeignKeyField):
            rm = field.rel_model
            name = '%s_id' % name
            # TODO: 这里可能会出问题
            foreign_keys[name] = [SQLForeignKey(get_pv_table_name(rm), get_pv_pk_name(rm))]

        peewee_fields[name] = field
        new_model_cls._append_field(name, field_class_to_schematics_field(field))

    return ret 
Example #9
Source File: io.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def _replace_ref_nodes_with_names(
        model: models.Model,
        model_list: List[optplan.ProblemGraphNode]) -> None:
    """Replaces `optplan.ProblemGraphNode` references with names.

    This is used by `dumps` to replace all `optplan.ProblemGraphNode` instances
    with their names for serialization.

    Args:
        model: Model to recurse on.
        model_list: List of `optplan.ProblemGraphNode` models.
    """

    # Replace model reference with a name.
    def process_field(model: models.Model, child_model: models.Model) -> str:
        if isinstance(child_model, str):
            return child_model

        ind = model_list.index(child_model)
        return model_list[ind].name

    _iter_optplan_fields(model, set(), process_field) 
Example #10
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_string_regex():
    """
    messages:
        - REGEX_NOT_MATCHED
    """

    class Data(Model):
        a = StringType(regex=".*def.*")

    _test_data(
        model=Data,
        data={"a": "abc"},
        expected=(False, {"a": [messages.REGEX_NOT_MATCHED]}),
    )
    _test_data(model=Data, data={"a": "abcdefghi"}, expected=(True, {}))
    _test_data(model=Data, data={"a": "def"}, expected=(True, {})) 
Example #11
Source File: test_schematics.py    From transmute-core with MIT License 6 votes vote down vote up
def test_schematics_uses_cached_entries():
    """
    Regression test. Validating that SchematicsSerializer uses the
    same json schema it used previously, as before erroneous
    use of the instantiated model as a key was causing memory leaks.
    """
    serializer = SchematicsSerializer()
    # A nested schema type is required as primitives have
    # hard-coded dictionaries representing the json.
    class SchematicsBody(Model):
        name = StringType(max_length=5)

    # ensure that instances have the same key as well.
    instance = ModelType(SchematicsBody)
    original_payload = serializer.to_json_schema(instance)
    second_payload = serializer.to_json_schema(instance)
    assert original_payload is second_payload

    # classes are also valid output to recieve that the json schema
    original_payload = serializer.to_json_schema(SchematicsBody)
    second_payload = serializer.to_json_schema(SchematicsBody)
    assert original_payload is second_payload 
Example #12
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_choices():
    """
    messages:
        - VALUE_NOT_IN_CHOICES
    """

    class Data(Model):
        a = StringType(choices=["a", "b"])
        b = IntType(choices=[1, 2, 3])

    _test_data(model=Data, data={}, expected=(True, {}))
    _test_data(model=Data, data={"a": "b", "b": 3}, expected=(True, {}))
    _test_data(
        model=Data,
        data={"a": "c", "b": 4},
        expected=(
            False,
            {
                "a": [messages.VALUE_NOT_IN_CHOICES],
                "b": [messages.VALUE_NOT_IN_CHOICES],
            },
        ),
    ) 
Example #13
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_required():
    """
    messages:
        - MISSING_REQUIRED_FIELD
    """

    class DataRequired(Model):
        a = StringType(required=True)

    class DataNotRequired(Model):
        a = StringType(required=False)

    _test_data(
        model=DataRequired,
        data={},
        expected=(False, {"a": [messages.MISSING_REQUIRED_FIELD]}),
    )
    _test_data(model=DataNotRequired, data={}, expected=(True, {})) 
Example #14
Source File: io.py    From spins-b with GNU General Public License v3.0 6 votes vote down vote up
def validate_references(model: models.Model) -> None:
    """Validates that all `optplan.ReferenceType` are valid.

    This function recursively checks to see if every `ReferenceType` field
    is actually holding a reference to the appropriate type.

    Args:
        model: The model to check.

    Raises:
        ValueError: If a bad type is encountered.
    """

    def process_field(parent: models.Model,
                      child: Union[str, optplan.ProblemGraphNode],
                      field_type: optplan.ReferenceType) -> None:
        if not child:
            return

        if not isinstance(child, (str, field_type.reference_type)):
            raise ValueError("Expected type {} for node {}, got {}".format(
                field_type.reference_type, child, type(child)))

    _iter_optplan_fields(model, set(), process_field, pass_field_info=True) 
Example #15
Source File: schema.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def validate_references(model: models.Model) -> None:
    """Validates that all `optplan.ReferenceType` are valid.

    This function recursively checks to see if every `ReferenceType` field
    is actually holding a reference to the appropriate type.

    Args:
        model: The model to check.

    Raises:
        ValueError: If a bad type is encountered.
    """

    def process_field(parent: models.Model,
                      child: Union[str, optplan.ProblemGraphNodeSchema],
                      field_type: optplan.ReferenceType) -> None:
        if not child:
            return

        if (not isinstance(child, (str, field_type.reference_type)) and
                not isinstance(child, optplan.WildcardSchema)):
            raise ValueError("Expected type {} for node {}, got {}.".format(
                field_type.reference_type,
                child,
                type(child),
            ))

    _iter_optplan_fields(model, set(), process_field, pass_field_info=True) 
Example #16
Source File: io.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def loads(serialized_plan: str) -> optplan.OptimizationPlan:
    """Loads a serialized optimization plan.

    Args:
        serialized_plan: Serialized plan.

    Returns:
        Stored `optplan.OptimizationPlan`.
    """
    plan = optplan.OptimizationPlan(json.loads(serialized_plan))
    validate(plan)
    _validate_optplan_version(plan.version)

    # Unpack the optimization plan.
    # First create a dictionary mapping strings to the node.
    node_map = {}
    for node in plan.nodes:
        node_map[node.name] = node

    # Now, recursively fill any node names with the actual node.
    def process_field(model: models.Model, child_model: str) -> models.Model:
        return node_map[child_model]

    visited = set()
    if plan.transformations:
        for transformation in plan.transformations:
            _iter_optplan_fields(transformation, visited, process_field)
    for node in plan.nodes:
        _iter_optplan_fields(node, visited, process_field)

    validate_references(plan)
    return plan 
Example #17
Source File: context.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self) -> None:
        """Creates a new context."""
        # This is of type Dict[str, Dict[str, (models.Model, Callable)]].
        self._optplan_node_map = {} 
Example #18
Source File: optplan.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def load(self, save_folder: Optional[str] = None) -> None:
        """Loads the optimization plan.

        Args:
            save_folder: Folder in which to save the optimization plan.
                If `None`, the optimization plan save path is used.
        """
        if not save_folder:
            save_folder = self._save_path

        with open(os.path.join(save_folder, "optplan.json"), "r") as fp:
            plan = optplan.schema.loads(fp.read())

        from schematics import models

        def process_field(model: models.Model,
                          child_model: models.Model) -> ProblemGraphNode:
            return _create_node(child_model)

        visited = set()

        def _create_node(model: models.Model) -> ProblemGraphNode:
            if model.name in self._node_map:
                return self._node_map[model.name]

            optplan.schema._iter_optplan_fields(model, visited, process_field)
            params = {}
            for key, value in model.items():
                if key == "type":
                    continue
                params[key] = value
            return optplan.GLOBAL_CONTEXT_STACK.get_node(
                model.type).creator(**params)

        for node in plan.nodes:
            # TODO(logansu): Check for clashes in names.
            self.add_node(_create_node(node))

        for model in plan.actions:
            # TODO(logansu): Check for clashes in names.
            self.add_action(_create_node(model)) 
Example #19
Source File: converter.py    From transmute-core with MIT License 5 votes vote down vote up
def create_cattrs_converter():
    converter = Converter()
    converter.register_structure_hook(bool, _structure_bool)
    converter.register_structure_hook(string_type, _structure_string)
    converter.register_structure_hook(Model, _structure_schematics)
    converter.register_structure_hook(BaseType, _structure_basetype)
    converter.register_structure_hook(datetime, _structure_datetime)
    converter.register_unstructure_hook(Model, _unstructure_schematics)
    converter.register_unstructure_hook(datetime, _unstructure_datetime)
    converter.register_unstructure_hook(BaseType, _unstructure_basetype)
    return converter 
Example #20
Source File: schema.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def loads(serialized_plan: str) -> optplan.OptimizationPlanSchema:
    """Loads a serialized optimization plan.

    Args:
        serialized_plan: Serialized plan.

    Returns:
        Stored `optplan.OptimizationPlanSchema`.
    """
    plan = optplan.OptimizationPlanSchema(json.loads(serialized_plan))
    validate(plan)
    _validate_optplan_version(plan.version)

    # Unpack the optimization plan.
    # First create a dictionary mapping strings to the node.
    node_map = {}
    for node in plan.nodes:
        node_map[node.name] = node

    # Now, recursively fill any node names with the actual node.
    def process_field(model: models.Model, child_model: str) -> models.Model:
        return node_map[child_model]

    visited = set()
    if plan.actions:
        for action in plan.actions:
            _iter_optplan_fields(action, visited, process_field)
    for node in plan.nodes:
        _iter_optplan_fields(node, visited, process_field)

    validate_references(plan)
    return plan 
Example #21
Source File: schema.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def _extract_nodes(value: Any,
                   model_list: List[optplan.ProblemGraphNodeSchema]) -> None:
    """Finds any nodes recursively in `value` and appends them to `model_list`.

    This is used by `dumps` to find all relevant `optplan.ProblemGraphNodeSchema`
    instances.

    Args:
        value: Value to recurse on.
        model_list: List of `optplan.ProblemGraphNodeSchema` models found so far.
    """
    if isinstance(value, optplan.ProblemGraphNodeSchema):
        if value not in model_list:
            model_list.append(value)
        else:
            # This node has been processed before so quit.
            return

    if isinstance(value, models.Model):
        for item in value.keys():
            _extract_nodes(value[item], model_list)
    elif isinstance(value, list):
        for item in value:
            _extract_nodes(item, model_list)
    elif isinstance(value, dict):
        for _, item in value.items():
            _extract_nodes(item, model_list) 
Example #22
Source File: test_schema_types.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def test_numpy_array_type():

    class Model(models.Model):
        x = schema_types.NumpyArrayType()

    model = Model({"x": 1})
    model.validate()
    assert model.to_primitive() == {"x": 1}
    assert model.to_native() == {"x": 1}

    model = Model({"x": [1]})
    model.validate()
    assert model.to_primitive() == {"x": [1]}
    assert model.to_native() == {"x": [1]}

    model = Model({"x": np.array([1, 2])})
    model.validate()
    assert model.to_primitive() == {"x": [1, 2]}
    model_native = model.to_native()
    assert list(model_native.keys()) == ["x"]
    np.testing.assert_array_equal(model_native["x"], [1, 2])

    model = Model({"x": np.array([[1], [2]])})
    model.validate()
    assert model.to_primitive() == {"x": [[1], [2]]}
    model_native = model.to_native()
    assert list(model_native.keys()) == ["x"]
    np.testing.assert_array_equal(model_native["x"], [[1], [2]])

    with pytest.raises(models.DataError, match="not support complex"):
        model = Model({"x": 1 + 2j})
        model.validate() 
Example #23
Source File: test_schematics.py    From transmute-core with MIT License 5 votes vote down vote up
def test_schematics_to_json_schema_required_value(object_serializer_set):
    class CardNameRequired(Model):
        name = StringType(required=True)
        price = IntType()

    assert object_serializer_set.to_json_schema(CardNameRequired) == {
        "properties": {"name": {"type": "string"}, "price": {"type": "integer"}},
        "title": "CardNameRequired",
        "required": ["name"],
        "type": "object",
    } 
Example #24
Source File: pipelines.py    From spidermon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _load_schematics_validator(cls, model_path):
        model_class = load_object(model_path)
        if not issubclass(model_class, Model):
            raise NotConfigured(
                "Invalid model, models must subclass schematics.models.Model"
            )
        return SchematicsValidator(model_class) 
Example #25
Source File: io.py    From spins-b with GNU General Public License v3.0 5 votes vote down vote up
def autoname(model: models.Model) -> None:
    """Autonamtically names `optplan.ProblemGraphNode` recursively.

    This function recursively searches through fields to find any
    `optplan.ProblemGraphNode` instances. Any unnamed `optplan.ProblemGraphNode` instances are
    given a name generated by `generate_name`.

    Args:
        model: Model to recursively search.
    """
    _autoname_helper(model, set()) 
Example #26
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_rogue_fields():
    """
    messages:
        - UNEXPECTED_FIELD
    """
    _test_data(
        model=Model, data={"a": 1}, expected=(False, {"a": [messages.UNEXPECTED_FIELD]})
    )
    _test_data(model=Model, data={"a": 1}, expected=(True, {}), strict=False) 
Example #27
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_string_valid():
    """
    messages:
        - INVALID_STRING
    """

    class Data(Model):
        a = StringType()

    _test_data(model=Data, data={"a": "hello there!"}, expected=(True, {}))
    _test_data(
        model=Data, data={"a": []}, expected=(False, {"a": [messages.INVALID_STRING]})
    ) 
Example #28
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_datetime():
    """
    messages:
        - INVALID_DATETIME
    """

    class Data(Model):
        a = DateTimeType()

    class DataWithFormats(Model):
        a = DateTimeType(formats=("%Y-%m-%d %H:%M:%S.%f", "%Y-%m-%d %H:%M:%S"))

    if SCHEMATICS1:
        INVALID = ["2015-05-13 13:35:15.718978", "2015-05-13 13:35:15"]
    else:
        INVALID = ["foo", "1-2-3"]
        CUSTOM_FORMAT = ["2015-05-13 13:35:15.718978", "2015-05-13 13:35:15"]
    VALID = ["2015-05-13T13:35:15.718978", "2015-05-13T13:35:15"]
    _test_valid_invalid(
        model=Data,
        valid=VALID,
        invalid=INVALID,
        expected_error=messages.INVALID_DATETIME,
    )
    if SCHEMATICS1:
        for dt in INVALID:
            _test_data(model=DataWithFormats, data={"a": dt}, expected=(True, {}))
    else:
        for dt in CUSTOM_FORMAT:
            _test_data(model=DataWithFormats, data={"a": dt}, expected=(True, {})) 
Example #29
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_date():
    """
    messages:
        - INVALID_DATE
    """

    class Data(Model):
        a = DateType()

    INVALID = ["2015-05-13 13:35:15", "13-05-2013", "2015-20-13", "2015-01-40"]
    VALID = ["2015-05-13", "2050-01-01"]
    _test_valid_invalid(
        model=Data, valid=VALID, invalid=INVALID, expected_error=messages.INVALID_DATE
    ) 
Example #30
Source File: test_validators_schematics.py    From spidermon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_int():
    """
    messages:
        - INVALID_INT
        - NUMBER_TOO_LOW
        - NUMBER_TOO_HIGH
    """

    class Data(Model):
        a = IntType(min_value=-10, max_value=10)
        b = IntType()

    INVALID = ["", "a", "2a", "2015-05-13 13:35:15", "7.2"]
    VALID = ["1", "8", "-2", "-7", 1, 8, -2, -7]
    if SCHEMATICS1:
        VALID.append(7.2)
    else:
        INVALID.append(7.2)
    _test_valid_invalid(
        model=Data, valid=VALID, invalid=INVALID, expected_error=messages.INVALID_INT
    )
    _test_data(
        model=Data, data={"a": -20}, expected=(False, {"a": [messages.NUMBER_TOO_LOW]})
    )
    _test_data(
        model=Data, data={"a": 11}, expected=(False, {"a": [messages.NUMBER_TOO_HIGH]})
    )