Exemplo n.º 1
0
def test_model_property_parameter():
    class MockClass:
        pass

    p_scalar = ModelProperty(MockClass, "scalar", Parameter, "", None, True)
    assert p_scalar.glotaran_is_scalar_property
    assert not p_scalar.glotaran_is_sequence_property
    assert not p_scalar.glotaran_is_mapping_property
    assert p_scalar.glotaran_property_subtype is ParameterOrLabel
    assert p_scalar.glotaran_is_parameter_property

    p_sequence = ModelProperty(MockClass, "sequence", List[Parameter], "",
                               None, True)
    assert not p_sequence.glotaran_is_scalar_property
    assert p_sequence.glotaran_is_sequence_property
    assert not p_sequence.glotaran_is_mapping_property
    assert p_sequence.glotaran_property_subtype is ParameterOrLabel
    assert p_sequence.glotaran_is_parameter_property

    p_mapping = ModelProperty(MockClass, "mapping", Dict[str, Parameter], "",
                              None, True)
    assert not p_mapping.glotaran_is_scalar_property
    assert not p_mapping.glotaran_is_sequence_property
    assert p_mapping.glotaran_is_mapping_property
    assert p_mapping.glotaran_property_subtype is ParameterOrLabel
    assert p_mapping.glotaran_is_parameter_property
Exemplo n.º 2
0
def test_model_property_default_getter():
    class MockClass:
        _p_default = None

    p_default = ModelProperty(MockClass, "p_default", int, "", 42, True)
    assert p_default.fget(MockClass) == 42
    MockClass._p_default = 21
    assert p_default.fget(MockClass) == 21
Exemplo n.º 3
0
    def decorator(cls):

        setattr(cls, "_glotaran_has_label", not no_label)
        setattr(cls, "_glotaran_model_attribute", True)

        # store for later sanity checking
        if not hasattr(cls, "_glotaran_properties"):
            setattr(cls, "_glotaran_properties", [])
            if not no_label:
                doc = f"The label of {cls.__name__} item."
                prop = ModelProperty(cls, "label", str, doc, None, False)
                setattr(cls, "label", prop)
                getattr(cls, "_glotaran_properties").append("label")
            if has_type:
                doc = f"The type string of {cls.__name__}."
                prop = ModelProperty(cls, "type", str, doc, None, False)
                setattr(cls, "type", prop)
                getattr(cls, "_glotaran_properties").append("type")

        else:
            setattr(
                cls,
                "_glotaran_properties",
                [attr for attr in getattr(cls, "_glotaran_properties")],
            )

        for name, options in properties.items():
            if not isinstance(options, dict):
                options = {"type": options}
            prop = ModelProperty(
                cls,
                name,
                options.get("type"),
                options.get("doc", f"{name}"),
                options.get("default", None),
                options.get("allow_none", False),
            )
            setattr(cls, name, prop)
            if name not in getattr(cls, "_glotaran_properties"):
                getattr(cls, "_glotaran_properties").append(name)

        init = _create_init_func(cls)
        setattr(cls, "__init__", init)

        from_dict = _create_from_dict_func(cls)
        setattr(cls, "from_dict", from_dict)

        from_list = _create_from_list_func(cls)
        setattr(cls, "from_list", from_list)

        validate = _create_validation_func(cls)
        setattr(cls, "validate", validate)

        get_state = _create_get_state_func(cls)
        setattr(cls, "__getstate__", get_state)

        set_state = _create_set_state_func(cls)
        setattr(cls, "__setstate__", set_state)

        fill = _create_fill_func(cls)
        setattr(cls, "fill", fill)

        mprint = _create_mprint_func(cls)
        setattr(cls, "mprint", mprint)

        return cls
Exemplo n.º 4
0
    def decorator(cls):

        setattr(cls, "_glotaran_has_label", has_label)
        setattr(cls, "_glotaran_model_item", True)

        # store for later sanity checking
        if not hasattr(cls, "_glotaran_properties"):
            setattr(cls, "_glotaran_properties", [])
            if has_label:
                doc = f"The label of {cls.__name__} item."
                prop = ModelProperty(cls, "label", str, doc, None, False)
                setattr(cls, "label", prop)
                getattr(cls, "_glotaran_properties").append("label")
            if has_type:
                doc = f"The type string of {cls.__name__}."
                prop = ModelProperty(cls, "type", str, doc, None, True)
                setattr(cls, "type", prop)
                getattr(cls, "_glotaran_properties").append("type")

        else:
            setattr(
                cls,
                "_glotaran_properties",
                list(getattr(cls, "_glotaran_properties")),
            )

        for name, options in properties.items():
            if not isinstance(options, dict):
                options = {"type": options}
            prop = ModelProperty(
                cls,
                name,
                options.get("type"),
                options.get("doc", f"{name}"),
                options.get("default", None),
                options.get("allow_none", False),
            )
            setattr(cls, name, prop)
            if name not in getattr(cls, "_glotaran_properties"):
                getattr(cls, "_glotaran_properties").append(name)

        validators = _get_validators(cls)
        setattr(cls, "_glotaran_validators", validators)

        init = _init_factory(cls)
        setattr(cls, "__init__", init)

        from_dict = _from_dict_factory(cls)
        setattr(cls, "from_dict", from_dict)

        validate = _validation_factory(cls)
        setattr(cls, "validate", validate)

        as_dict = _as_dict_factory(cls)
        setattr(cls, "as_dict", as_dict)

        get_state = _get_state_factory(cls)
        setattr(cls, "__getstate__", get_state)

        set_state = _set_state_factory(cls)
        setattr(cls, "__setstate__", set_state)

        fill = _fill_factory(cls)
        setattr(cls, "fill", fill)

        markdown = _markdown_factory(cls)
        setattr(cls, "markdown", markdown)

        return cls
Exemplo n.º 5
0
def test_model_property_parameter_setter():
    class MockClass:
        pass

    p_scalar = ModelProperty(MockClass, "scalar", Parameter, "", None, True)
    p_scalar.fset(MockClass, "param.foo")
    value = p_scalar.fget(MockClass)
    assert isinstance(value, Parameter)
    assert value.full_label == "param.foo"

    p_sequence = ModelProperty(MockClass, "sequence", List[Parameter], "",
                               None, True)
    names = ["param1", "param2"]
    p_sequence.fset(MockClass, names)
    value = p_sequence.fget(MockClass)
    assert isinstance(value, list)
    assert all(map(lambda v: isinstance(v, Parameter), value))
    assert [p.full_label for p in value] == names

    p_mapping = ModelProperty(MockClass, "mapping", Dict[str, Parameter], "",
                              None, True)
    p_mapping.fset(MockClass, {f"{i}": n for i, n in enumerate(names)})
    value = p_mapping.fget(MockClass)
    assert isinstance(value, dict)
    assert all(map(lambda v: isinstance(v, Parameter), value.values()))
    assert [p.full_label for p in value.values()] == names
Exemplo n.º 6
0
def test_model_property_parameter_to_label():
    class MockClass:
        pass

    p_scalar = ModelProperty(MockClass, "scalar", Parameter, "", None, True)
    p_scalar.fset(MockClass, "param.foo")
    value = p_scalar.fget(MockClass)
    assert p_scalar.glotaran_replace_parameter_with_labels(
        value) == "param.foo"

    p_sequence = ModelProperty(MockClass, "sequence", List[Parameter], "",
                               None, True)
    names = ["param1", "param2"]
    p_sequence.fset(MockClass, names)
    value = p_sequence.fget(MockClass)
    assert p_sequence.glotaran_replace_parameter_with_labels(value) == names

    p_mapping = ModelProperty(MockClass, "mapping", Dict[str, Parameter], "",
                              None, True)
    p_mapping.fset(MockClass, {f"{i}": n for i, n in enumerate(names)})
    value = p_mapping.fget(MockClass)
    assert list(
        p_mapping.glotaran_replace_parameter_with_labels(
            value).values()) == names
Exemplo n.º 7
0
def test_model_property_non_parameter():
    class MockClass:
        pass

    p_scalar = ModelProperty(MockClass, "scalar", int, "", None, True)
    assert p_scalar.glotaran_is_scalar_property
    assert not p_scalar.glotaran_is_sequence_property
    assert not p_scalar.glotaran_is_mapping_property
    assert p_scalar.glotaran_property_subtype is int
    assert not p_scalar.glotaran_is_parameter_property
    assert p_scalar.glotaran_value_as_markdown(42) == "42"

    p_sequence = ModelProperty(MockClass, "sequence", List[int], "", None,
                               True)
    assert not p_sequence.glotaran_is_scalar_property
    assert p_sequence.glotaran_is_sequence_property
    assert not p_sequence.glotaran_is_mapping_property
    assert p_sequence.glotaran_property_subtype is int
    assert not p_sequence.glotaran_is_parameter_property
    print(p_sequence.glotaran_value_as_markdown([1, 2]))
    assert p_sequence.glotaran_value_as_markdown([1, 2]) == "\n  * 1\n  * 2"

    p_mapping = ModelProperty(MockClass, "mapping", Dict[str, int], "", None,
                              True)
    assert not p_mapping.glotaran_is_scalar_property
    assert not p_mapping.glotaran_is_sequence_property
    assert p_mapping.glotaran_is_mapping_property
    assert p_mapping.glotaran_property_subtype is int
    assert not p_mapping.glotaran_is_parameter_property
    print(p_mapping.glotaran_value_as_markdown({"a": 1, "b": 2}))
    assert p_mapping.glotaran_value_as_markdown({
        "a": 1,
        "b": 2
    }) == "\n  * a: 1\n  * b: 2"