\(\renewcommand\AA{\unicode{x212B}}\)
PythonObjectProperty¶
This is a Python binding to the C++ class Mantid::PythonInterface::PythonObjectProperty. It is meant to hold a generic python object as a property for use in algorithms defined from python.
bases: mantid.kernel.PropertyWithValue
- class mantid.kernel.PythonObjectProperty¶
- EMPTY_DBL = 8.988465674311579e+307¶
- EMPTY_INT = 2147483647¶
- EMPTY_LONG = 9223372036854775807¶
- property allowedValues¶
A list of allowed values
- property direction¶
Input, Output, InOut or Unknown. See the Direction class
- property documentation¶
The property’s doc string
- getAutoTrim((Property)arg1) bool :¶
Gets the setting of automatic trimming of whitespaces.
- property getDefault¶
Get the default value as a string
- property getGroup¶
Return the ‘group’ of the property, that is, the header in the algorithm’s list of properties.
- property isDefault¶
Is the property set at the default value
- property isValid¶
An empty string if the property is valid, otherwise it contains an error message.
- property name¶
The name of the property
- setAutoTrim((Property)arg1, (bool)setting) None :¶
Setting automatic trimming of whitespaces.
- setDisableReplaceWSButton((Property)arg1, (bool)disable) None :¶
Disable the creation of the Replace Workspace button.
- setDocumentation((Property)arg1, (str)doc) None :¶
Setting documentation for property
- property settings¶
Return the object managing this property’s settings
- property type¶
Returns a string identifier for the type
- property units¶
The units attached to this property
- property unitsAsBytes¶
The units attached to this property as a encoded bytes object. It is assumed the caller knows the correct endcoding used.
- property value¶
None( (mantid.kernel._kernel.PythonObjectPropertyWithValue)arg1) -> object
- valueAsPrettyStr((Property)arg1[, (int)maxLength=0[, (bool)collapseLists=True]]) str :¶
The value of the property as a formatted string. If maxLength is defined then the output may not contain the full contents of the property. The maxLength and collapseLists arguments do not work for all property types
- property valueAsStr¶
The value of the property as a string. For some property types, e.g. Workspaces, it is useful to be able to refer to the string value directly
Example Usage¶
The following example shows how to use this property type, both with and without a PythonObjectTypeValidator.
from mantid.kernel import PythonObjectProperty, PythonObjectTypeValidator
from mantid.api import PythonAlgorithm
class FakeClass:
pass
class FakeAlgorithm(PythonAlgorithm):
def PyInit(self):
self.declareProperty(PythonObjectProperty("UnvalidatedPyObject"))
self.declareProperty(PythonObjectProperty("ValidatedPyObject", None, PythonObjectTypeValidator(FakeClass)))
def PyExec(self):
pass
value1 = {"key1": "value one", "key2": ["one", "two", "three"]}
value2 = FakeClass()
# run the algorithm
fake = FakeAlgorithm()
fake.initialize()
fake.setProperty("UnvalidatedPyObject", value1)
fake.setProperty("ValidatedPyObject", value2)
fake.execute()
Notes¶
When string values are required, this will first attempt to write them using json.dumps.
If that does not work then serialization will recursively build a json-like object, including replacing custom classes with __dict__ property.
Note that for very complicated objects, this can result in enormous string outputs. It is recommended to stick to
simpler python types, or classes made from simple python types. This will not work on objects that use python tuples for dictionary keys.
If the recursive serialization fails, the string value will be set to "<unrepresentable object>".