blpapi.EventFormatter
- class blpapi.EventFormatter
EventFormatter
is used to populateEvent
s for publishing.An
EventFormatter
is created from anEvent
obtained fromcreatePublishEvent()
onService
. Once theMessage
orMessage
s have been appended to theEvent
using theEventFormatter
theEvent
can be published usingpublish()
on theProviderSession
.EventFormatter
objects cannot be copied to ensure that there is no ambiguity about what happens if twoEventFormatter
s are both formatting the sameEvent
.The
EventFormatter
supports appending message of the same type multiple times in the sameEvent
. However theEventFormatter
supports 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
EventFormatter
to createMessage
s in the specifiedevent
.- Parameters
event (Event) – Event to be formatted
An
Event
may only be referenced by oneEventFormatter
at any time. Attempting to create a secondEventFormatter
referencing the sameEvent
will result in an exception being raised.
- appendElement()
- appendMessage(messageType, topic, sequenceNumber=None)
Append an (empty) message to the
Event
referenced by thisEventFormatter
- Parameters
After a message has been appended its elements can be set using the various
setElement()
methods.Note
It is expected that
sequenceNumber
is greater (unless the value wrapped orNone
is 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
Event
referenced by thisEventFormatter
.- Parameters
topic (Topic) – Topic to publish under
correlationId (CorrelationId) – Specify if recap message added in response to a
TOPIC_RECAP
messagesequenceNumber (int) – Sequence number of the message
fragmentType (int) – Type of the message fragment
Specify the optional
correlationId
if this recap message is added in response to aTOPIC_RECAP
message.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
fragmentType
set toMessage.FRAGMENT_NONE
. Multi-tick recaps can have eitherMessage.FRAGMENT_START
,Message.FRAGMENT_INTERMEDIATE
, orMessage.FRAGMENT_END
as thefragmentType
.Note
It is expected that
sequenceNumber
is greater (unless the value wrapped orNone
is 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
.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
Event
is currently empty.Note
For
PermissionRequest
messages, use thePermissionResponse
operation name.
- appendValue(value)
- destroy()
Destroy the handle using stored dtor
- fromPy(value)
Format this
EventFormatter
‘s underlyingEvent
usingvalue
.- Parameters
value (collections.abc.Mapping) – the object used for formatting
- Raises
The
value
used to format theEvent
is always acollections.abc.Mapping
instance. The keys areName
orstr
instances, and the values vary depending on theElement
being formatted.If the
Element
identified by the key isa complex type, it is formatted using a
collections.abc.Mapping
whose keys are the names of its sub-Element
s.an array, it is formatted using a
collections.abc.Sequence
of theElement
’s values (see note below for more details).
Otherwise, the
Element
is formatted using its associated scalar value (e.g.str
orint
).Note
Although
str
,bytes
,bytearray
, andmemoryview
are sub-types ofcollections.abc.Sequence
,fromPy()
treats them as scalars of type string and will use them to format scalarElement
s. If you wish to format an arrayElement
with instances of the aforementioned types, put them in a differentcollections.abc.Sequence
, likelist
.For null
Element
s:A null complex
Element
is formatted using an emptycollections.abc.Mapping
.A null scalar
Element
is formatted usingNone
.An empty array
Element
is formatted using an emptycollections.abc.Sequence
.
Note
The behavior is undefined if
fromPy()
is used to format anEvent
that has already been formatted. Further formatting afterfromPy()
is also undefined.For example, the following
SampleOperation
has the following BLPAPI representation:SampleOperation = { complexElement = { nullElement = { } } scalarArray[] = { "value1", "value2" } complexArray[] = { complexArray = { value = 1 message = "msg" } } valueElement = "value" nullValueElement = }
SampleOperation
can 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 formatSampleOperation
the 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)
- popElement()
Undo the most recent call to
pushElement()
orappendElement()
on thisEventFormatter
and return the context of theEventFormatter
to where it was before the call topushElement()
orappendElement()
. OncepopElement()
has been called it is invalid to attempt to re-visit the same context.
- pushElement(name)
Change the level at which this
EventFormatter
is operating.After this returns the context of the
EventFormatter
is set to the elementname
in the schema and any calls tosetElement()
orpushElement()
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()
creates the first entry and sets the context of theEventFormatter
to that element.Calling
appendElement()
again will create another entry.If the
name
is invalid for the current message, ifappendMessage()
has never been called or if the element identified byname
has already been set an exception is raised.Note
The element
name
must identify either a choice, a sequence or an array at the current level of the schema or the behavior is undefined.
- setElement(name, value)
Set an element in the
Event
referenced by thisEventFormatter
.- Parameters
If the
name
is invalid for the current message, or ifappendMessage()
has never been called, or if the element identified byname
has 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.