blpapi.Element¶
-
class
blpapi.Element¶ Represents an item in a message.
An
Elementcan represent:A single value of any data type supported by the Bloomberg API
An array of values
A sequence or a choice
The value(s) in an
Elementcan be queried in a number of ways. For anElementwhich represents a single value or an array of values use thegetValueAsBool()etc. functions. For anElementwhich represents a sequence or choice usegetElementAsBool()etc. functions. In addition, for choices and sequences,hasElement()andgetElement()are useful.This example shows how to access the value of a scalar element
sas a floating point number:f = s.getValueAsFloat()
Similarly, this example shows how to retrieve the third value in an array element
a, as a floating point number:f = a.getValueAsFloat(2)
Use
numValues()to determine the number of values available. For single values, it will return either0or1. For arrays it will return the actual number of values in the array.To retrieve values from a complex element types (sequences and choices) use the
getElementAs...()family of methods. This example shows how to get the value of the elementcityin the sequence elementaddress:city = address.getElementAsString("city")
Note
getElementAsXYZ(name)method is a shortcut togetElement(name).getValueAsXYZ().The value(s) of an
Elementcan be set in a number of ways. For anElementwhich represents a single value or an array of values use thesetValue()orappendValue()functions. For an element which represents a sequence or a choice use thesetElement()functions.This example shows how to set the value of an
Elements:value=5 s.setValue(value)
This example shows how to append a value to an array element
a:value=5 a.appendValue(value)
To set values in a complex element (a sequence or a choice) use the
setElement()family of functions. This example shows how to set the value of the elementcityin the sequence elementaddressto a string:address.setElement("city", "New York")
Methods which specify an
Elementname accept name in two forms:Nameor a string. PassingNameis more efficient. However, it requires theNameto have been created in the global name table.The form which takes a string is less efficient but will not cause a new
Nameto be created in the global name table. Because all validElementnames will have already been placed in the global name table by the API if the supplied string cannot be found in the global name table the appropriate error or exception can be returned.The API will convert data types as long as there is no loss of precision involved.
Elementobjects are always created by the API, never directly by the application.-
appendValue(value)¶ Append the specified
valueto thisElements entries at the end.- Parameters
value – Value to append
- Raises
Exception – If this
Element’s datatype can’t be initialized from the type of the specifiedvalue, or if the current size of thisElement(numValues()) is equal to the maximum defined byelementDefinition().maxValues().
This method can process the following types of
valuewithout conversion:boolean
integers
float
string
datetypes (
datetime.time,datetime.dateordatetime.datetime)
Any other
valuewill be converted to a string withstrfunction and then processed in the same way as stringvalue.
-
elementDefinition()¶ - Returns
Reference to the read-only element definition object that defines the properties of this elements value.
- Return type
-
elements()¶ - Returns
Iterator over elements contained in this
Element.- Raises
UnsupportedOperationException – If this
Elementis not a sequence.
-
getChoice()¶
-
getElement(nameOrIndex)¶
-
getElementAsBool(name)¶ - Parameters
- Returns
This element’s sub-element with
nameas a boolean- Return type
- Raises
Exception – If
nameis neither aNamenor a string, or if thisElementis neither a sequence nor a choice, or in case it has no sub-element with the specifiedname, or in case the element’s value can’t be returned as a boolean.
-
getElementAsDatetime(name)¶ - Parameters
- Returns
This element’s sub-element with
nameas one of the datetime types- Return type
- Raises
Exception – If
nameis neither aNamenor a string, or if thisElementis neither a sequence nor a choice, or in case it has no sub-element with the specifiedname, or in case the element’s value can’t be returned as a datetime.
-
getElementAsFloat(name)¶ - Parameters
- Returns
This element’s sub-element with
nameas a float- Return type
- Raises
Exception – If
nameis neither aNamenor a string, or if thisElementis neither a sequence nor a choice, or in case it has no sub-element with the specifiedname, or in case the element’s value can’t be returned as a float.
-
getElementAsInteger(name)¶ - Parameters
- Returns
This element’s sub-element with
nameas an integer- Return type
- Raises
Exception – If
nameis neither aNamenor a string, or if thisElementis neither a sequence nor a choice, or in case it has no sub-element with the specifiedname, or in case the element’s value can’t be returned as an integer.
-
getElementAsName(name)¶ - Parameters
- Returns
This element’s sub-element with
nameas aName- Return type
- Raises
Exception – If
nameis neither aNamenor a string, or if thisElementis neither a sequence nor a choice, or in case it has no sub-element with the specifiedname, or in case the element’s value can’t be returned as aName.
-
getElementAsString(name)¶ - Parameters
- Returns
This element’s sub-element with
nameas a string- Return type
- Raises
Exception – If
nameis neither aNamenor a string, or if thisElementis neither a sequence nor a choice, or in case it has no sub-element with the specifiedname, or in case the element’s value can’t be returned as a string.
-
getElementValue(name)¶
-
getValue(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementdefined by this element’s datatype.- Raises
InvalidConversionException – If the data type of this
Elementis either a sequence or a choice.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsBool(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas a boolean.- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to a boolean.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsDatetime(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas one of the datetime types.- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to a datetime.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsElement(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas a Element.- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to anElement.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsFloat(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas a float.- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to an float.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsInteger(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas a integer- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to an integer.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsName(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas a Name.- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to aName.IndexOutOfRangeException – If
index >= numValues().
-
getValueAsString(index=0)¶ - Parameters
index (int) – Index of the value in the element
- Returns
indexth entry in theElementas a string.- Return type
- Raises
InvalidConversionException – If the data type of this
Elementcannot be converted to a string.IndexOutOfRangeException – If
index >= numValues().
-
hasElement(name, excludeNullElements=False)¶
-
isArray()¶ - Returns
Trueif this element is an array.- Return type
This element is an array if
elementDefinition().maxValues()>1or ifelementDefinition().maxValues()==UNBOUNDED.
-
isComplexType()¶ - Returns
Trueifdatatype()==DataType.SEQUENCEordatatype()==DataType.CHOICEandFalseotherwise.- Return type
-
isNullValue(position=0)¶
-
name()¶ - Returns
If this
Elementis part of a sequence or choiceElement, then return theNameof thisElementwithin the sequence or choiceElementthat owns it. If thisElementis not part of a sequenceElement(that is it is an entireRequestorMessage) then return theNameof theRequestorMessage.- Return type
-
numElements()¶ - Returns
Number of elements in this element.
- Return type
The number of elements is
0ifisComplexType()returnsFalse, and no greater than1if theDataTypeisCHOICE; if theDataTypeisSEQUENCEthis may return any number (including0).
-
numValues()¶ - Returns
Number of values contained by this element.
- Return type
The number of values is
0ifisNull()returnsTrue, and no greater than1ifisComplexType()returnsTrue. The value returned will always be in the range defined byelementDefinition().minValues()andelementDefinition().maxValues().
-
setElement(name, value)¶ Set this Element’s sub-element with ‘name’ to the specified ‘value’.
- Parameters
- Raises
Exception – If
nameis neither aNamenor a string, or if this element has no sub-element with the specifiedname, or if theElementidentified by the specifiednamecannot be initialized from the type of the specifiedvalue.
This method can process the following types of
valuewithout conversion:boolean
integers
float
string
datetypes (
datetime.time,datetime.dateordatetime.datetime)
Any other
valuewill be converted to a string withstrfunction and then processed in the same way as stringvalue.
-
setValue(value, index=0)¶ Set the specified
indexth entry in thisElementto thevalue.- Parameters
index (int) – Index of the sub-element
value – Value to set the sub-element to
- Raises
Exception – If this
Element’s datatype can’t be initialized with the type of the specifiedvalue, or ifindex >= numValues().
This method can process the following types of
valuewithout conversion:boolean
integers
float
string
datetypes (
datetime.time,datetime.dateordatetime.datetime)
Any other
valuewill be converted to a string withstrfunction and then processed in the same way as stringvalue.
-
toString(level=0, spacesPerLevel=4)¶ Format this
Elementto the string at the specified indentation level.- Parameters
- Returns
This element formatted as a string
- Return type
If
levelis negative, suppress indentation of the first line. IfspacesPerLevelis negative, format the entire output on one line, suppressing all but the initial indentation (as governed bylevel).
-
values()¶ - Returns
Iterator over values contained in this
Element.
If
isComplexType()returnsTruefor thisElement, the empty iterator is returned.