blpapi.test.MessageFormatter¶
- class blpapi.test.MessageFormatter¶
MessageFormatter
is used to populate/format a message. It supports writing only once to each field. Attempting to set an already set element will fail.Note that the behavior is undefined if:
A message is formatted with
formatMessageJson()
orformatMessageXml()
orformatMessageDict()
is further formatted.A message formatted with set*() or append*() is further formatted using
formatMessageJson()
orformatMessageXml()
orformatMessageDict()
.
Currently, the JSON and XML formatting methods have some known limitations:
The parsers can not differentiate complex field types (sequences, choices, arrays) that are empty with complex field types that are missing / null. These fields are dropped and absent in the message contents.
Enumerations of type “Datetime”, or any “Datetime” element with timezone offset or sub-microsecond precision (e.g. nanoseconds) are not supported.
- appendElement()¶
Create an array
blpapi.Element
and append it to theblpapi.Element
on which thisMessageFormatter
is operating.- Raises
Exception – If the schema of the
blpapi.Message
is flat or the currentblpapi.Element
to which this newblpapi.Element
is being appended is not an array, a sequence or a choice.
- appendValue(value)¶
Append the specified
value
to the element on which thisMessageFormatter
is operating.- Parameters
value (bool or str or int or float or datetime.date or datetime.time or datetime.datetime or Name) – The value to append.
- Raises
Exception – If the schema of the message is flat or the current element to which
value
is appended is not an array.
- formatMessageDict(dictMessage)¶
Format the
blpapi.Message
from itsdict
representation.- Parameters
dictMessage (dict) – A dictionary representing the content used to format the message.
- Raises
Exception – If the method fails to format the
blpapi.Message
.
The behavior is undefined if further formatting is done using this
MessageFormatter
.
- formatMessageJson(jsonMessage)¶
Format the
blpapi.Message
from itsJSON
representation.- Parameters
jsonMessage (str) – A
JSON
-formattedstr
representing the content used to format the message.- Raises
Exception – If the method fails to format the
blpapi.Message
.
The behavior is undefined if further formatting is done using this
MessageFormatter
.
- formatMessageXml(xmlMessage)¶
Format the
blpapi.Message
from itsXML
representation.- Parameters
xmlMessage (str) – An
XML
-formattedstr
representing the content used to format the message.- Raises
Exception – If the method fails to format the
blpapi.Message
.
The behavior is undefined if further formatting is done using this
MessageFormatter
.
- popElement()¶
Undo the most recent call to
pushElement()
on thisMessageFormatter
and return the context of theMessageFormatter
to where it was before the call topushElement()
. OncepopElement()
has been called, it is invalid to attempt to re-visit the same context.
- pushElement(name)¶
Change the level at which this
MessageFormatter
is operating to the element specified byname
.After this returns, the context of the
MessageFormatter
is set to the element specified byname
in the schema and any calls tosetElement()
or meth:pushElement are applied at that level.If
name
represents an array of scalars thenappendValue()
must be used to add values.If
name
represents an array of complex types thenappendElement()
must be used.- Parameters
name (Name or str) – Specifies the
blpapi.Element
on which to operate. The element must identify either a choice, a sequence or an array at the current level of the schema or the behavior is undefined.- Raises
Exception – If the
name
is invalid for the current message or if the element identified byname
has already been set.
- setElement(name, value)¶
Set the element with the specified
name
to the specifiedvalue
in the currentblpapi.Message
referenced by thisMessageFormatter
.- Parameters
name (Name or str) – Identifies the element which will be set to
value
.value (bool or str or int or float or datetime.date or datetime.time or datetime.datetime or Name or None) – The value to assign to the specified element.
- Raises
Exception – An exception is raised if any of the following are true: - The
name
is invalid for the current message - The element identified byname
has already been set - Thevalue
cannot be assigned to the element identified byname