MCPcopy Create free account
hub / github.com/MITK/MITK / InitProperty

Function InitProperty

Wrapping/Python/mitk/Property.cpp:35–184  ·  view source on GitHub ↗

* \brief Initializes Python bindings for MITK property types. * * Registers BaseProperty and its concrete subclasses (StringProperty, * BoolProperty, IntProperty, FloatProperty, DoubleProperty, ColorProperty), * plus the PropertyNotOwnedError exception. */

Source from the content-addressed store, hash-verified

33 * plus the PropertyNotOwnedError exception.
34 */
35void InitProperty(py::module_ &m)
36{
37 auto propertyNotOwnedError =
38 py::register_exception<PropertyNotOwnedError>(m, "PropertyNotOwnedError", PyExc_AttributeError);
39 propertyNotOwnedError.attr("__doc__") =
40 R"(Raised when ``set_property()`` or ``remove_property()`` is called on a
41property that is provided read-only (not owned) by this object.
42
43Subclass of ``AttributeError``. Use :py:meth:`property_is_owned` to check
44before writing.
45)";
46
47 py::class_<mitk::BaseProperty, mitk::BaseProperty::Pointer>(m, "BaseProperty",
48 R"(Abstract base class for typed property values.
49
50Every property attached to an MITK object is a ``BaseProperty`` subclass:
51:py:class:`StringProperty`, :py:class:`BoolProperty`,
52:py:class:`IntProperty`, :py:class:`FloatProperty`,
53:py:class:`DoubleProperty`, :py:class:`ColorProperty`,
54:py:class:`TemporoSpatialStringProperty`.
55
56When you read properties through the :py:attr:`Image.properties` mapping
57(or via :py:meth:`get_property`), values are auto-coerced to Python-native
58types. The raw ``BaseProperty`` is reachable with ``raw=True``.
59)")
60 .def("__str__", &mitk::BaseProperty::GetValueAsString,
61 "Return the property value as a string.")
62 .def("__repr__",
63 [](const mitk::BaseProperty &p)
64 { return "<mitk." + std::string(p.GetNameOfClass()) + ": " + p.GetValueAsString() + ">"; })
65 .def("__eq__", [](const mitk::BaseProperty &a, const mitk::BaseProperty &b) { return a == b; })
66 .def("to_json",
67 [](const mitk::BaseProperty &p) { return mitk::ConvertPropertyToSelfContainedJson(&p).dump(); },
68 R"(Serialize this property to a self-contained JSON string.
69
70The string carries enough type information that :py:meth:`from_json` can
71reconstruct the correct subclass.
72
73Returns:
74 JSON string representation of the property.
75)")
76 .def_static(
77 "from_json",
78 [](const std::string &json) { return mitk::ConvertPropertyFromSelfContainedJson(nlohmann::json::parse(json)); },
79 py::arg("json"),
80 R"(Reconstruct a ``BaseProperty`` subclass from JSON.
81
82Args:
83 json: JSON string produced by :py:meth:`to_json`.
84
85Returns:
86 A new ``BaseProperty`` of the appropriate subclass.
87)")
88 .def_property_readonly("value", nullptr,
89 "Property value (overridden by subclasses with type-specific accessors).")
90 .def("clone", [](const mitk::BaseProperty &p) { return p.Clone(); },
91 "Return a deep copy of this property.");
92

Callers 1

PYBIND11_MODULEFunction · 0.85

Calls 7

dictToPropertyFunction · 0.85
containsMethod · 0.80
NewFunction · 0.50
GetNameOfClassMethod · 0.45
GetValueAsStringMethod · 0.45
CloneMethod · 0.45

Tested by

no test coverage detected