blpapi.EventFormatter
- class blpapi.EventFormatter(event)
EventFormatteris used to populateEvents for publishing.An
EventFormatteris created from anEventobtained fromcreatePublishEvent()onService. Once theMessageorMessages have been appended to theEventusing theEventFormattertheEventcan be published usingpublish()on theProviderSession.EventFormatterobjects cannot be copied to ensure that there is no ambiguity about what happens if twoEventFormatters are both formatting the sameEvent.The
EventFormattersupports appending message of the same type multiple times in the sameEvent. However theEventFormattersupports write once only to each field. It is an error to callsetElement()orpushElement()for the same name more than once at a particular level of the schema when creating a message.- __init__(event)
Create an
EventFormatterto createMessages in the specifiedevent.- Parameters
event – Event to be formatted
An
Eventmay only be referenced by oneEventFormatterat any time. Attempting to create a secondEventFormatterreferencing the sameEventwill result in an exception being raised.
- appendMessage(messageType, topic, sequenceNumber=None)
Append an (empty) message to the
Eventreferenced by thisEventFormatter- Parameters
messageType – Type of the message
topic – Topic to publish the message under
sequenceNumber – Sequence number of the message
After a message has been appended its elements can be set using the various
setElement()methods.Note
It is expected that
sequenceNumberis greater (unless the value wrapped orNoneis specified) than the last value used in any previous message on thistopic, otherwise the behavior is undefined.
- appendRecapMessage(topic, correlationId=None, sequenceNumber=None, fragmentType=0)
Append a (empty) recap message to the
Eventreferenced by thisEventFormatter.- Parameters
topic – Topic to publish under
correlationId – Specify if recap message added in response to a
TOPIC_RECAPmessagesequenceNumber – Sequence number of the message
fragmentType – Type of the message fragment
Specify the optional
correlationIdif this recap message is added in response to aTOPIC_RECAPmessage.After a message has been appended its elements can be set using the various
setElement()methods. It is an error to create append a recap message to an Admin event.Single-tick recap messages should have
fragmentTypeset toMessage.FRAGMENT_NONE. Multi-tick recaps can have eitherMessage.FRAGMENT_START,Message.FRAGMENT_INTERMEDIATE, orMessage.FRAGMENT_ENDas thefragmentType.Note
It is expected that
sequenceNumberis greater (unless the value wrapped orNoneis specified) than the last value used in any previous message on thistopic, otherwise the behavior is undefined.
- appendResponse(operationName)
Append an (empty) response message for the specified
operationName.- Parameters
operationName (
Union[str,Name,bytes]) – Name of the operation whose response type to use
Append an (empty) response message for the specified
operationName(e.g.ReferenceDataRequest) that will be sent in response to the previously received operation request. After a message for this operation has been appended its elements can be set using thesetElement()method. Only one response can be appended.Note
The behavior is undefined unless the
Eventis currently empty.Note
For
PermissionRequestmessages, use thePermissionResponseoperation name.- Return type
- appendValue(value)
- fromPy(value)
Format this
EventFormatter‘s underlyingEventusingvalue.- Parameters
value (collections.abc.Mapping) – the object used for formatting
- Raises
The
valueused to format theEventis always acollections.abc.Mappinginstance. The keys areNameorstrinstances, and the values vary depending on theElementbeing formatted.If the
Elementidentified by the key isa complex type, it is formatted using a
collections.abc.Mappingwhose keys are the names of its sub-Elements.an array, it is formatted using a
collections.abc.Sequenceof theElement’s values (see note below for more details).
Otherwise, the
Elementis formatted using its associated scalar value (e.g.strorint).Note
Although
str,bytes,bytearray, andmemoryvieware sub-types ofcollections.abc.Sequence,fromPy()treats them as scalars of type string and will use them to format scalarElements. If you wish to format an arrayElementwith instances of the aforementioned types, put them in a differentcollections.abc.Sequence, likelist.For null
Elements:A null complex
Elementis formatted using an emptycollections.abc.Mapping.A null scalar
Elementis formatted usingNone.An empty array
Elementis formatted using an emptycollections.abc.Sequence.
Note
The behavior is undefined if
fromPy()is used to format anEventthat has already been formatted. Further formatting afterfromPy()is also undefined.For example, the following
SampleOperationhas the following BLPAPI representation:SampleOperation = { complexElement = { nullElement = { } } scalarArray[] = { "value1", "value2" } complexArray[] = { complexArray = { value = 1 message = "msg" } } valueElement = "value" nullValueElement = }SampleOperationcan be created with the following code:response = service.createResponseEvent(CorrelationId(0)) ef = EventFormatter(response) ef.appendResponse("SampleOperation") ef.pushElement("complexElement") ef.setElementNull("nullElement") ef.popElement() ef.pushElement("scalarArray") ef.appendValue("value1") ef.appendValue("value2") ef.popElement() ef.pushElement("complexArray") ef.appendElement() ef.setElement("value", 1) ef.setElement("message", "msg") ef.popElement() ef.popElement() ef.setElement("valueElement", "value") ef.setElementNull("nullValueElement")
fromPy()can be used to formatSampleOperationthe same way:response = service.createResponseEvent(CorrelationId(0)) ef = EventFormatter(response) ef.appendResponse("SampleOperation") sampleResponseAsDict = { "complexElement": { "nullElement": { } }, "scalarArray": [ "value1", "value2" ], "complexArray": [ { "value": 1 "message": "msg" } ], "valueElement": "value", "nullValueElement": None } ef.fromPy(sampleResponseAsDict)
- Return type
- popElement()
Undo the most recent call to
pushElement()orappendElement()on thisEventFormatterand return the context of theEventFormatterto where it was before the call topushElement()orappendElement(). OncepopElement()has been called it is invalid to attempt to re-visit the same context.- Return type
- pushElement(name)
Change the level at which this
EventFormatteris operating.After this returns the context of the
EventFormatteris set to the elementnamein the schema and any calls tosetElement()orpushElement()are applied at that level.If
namerepresents an array of scalars thenappendValue()must be used to add values.If
namerepresents an array of complex types thenappendElement()creates the first entry and sets the context of theEventFormatterto that element.Calling
appendElement()again will create another entry.If the
nameis invalid for the current message, ifappendMessage()has never been called or if the element identified bynamehas already been set an exception is raised.Note
The element
namemust identify either a choice, a sequence or an array at the current level of the schema or the behavior is undefined.- Return type
- setElement(name, value)
Set an element in the
Eventreferenced by thisEventFormatter.- Parameters
If the
nameis invalid for the current message, or ifappendMessage()has never been called, or if the element identified bynamehas already been set, an exception will be raised.Note
Clients wishing to format and publish null values (e.g. for the purpose of cache management) should not use this function; use
setElementNull()instead.- Return type