Database Reference
In-Depth Information
Dictionaries
An equally important object type to understand in Python, and especially in EM CLI, is the “dictionary.” Similar to
Java's “hashtable,” Perl's “hash” objects, and PL/SQL's “associative arrays,” dictionaries are made up of a collection
of key/value pairs. In other words, each element of the dictionary will be made up of one key and one value.
The value can then be extracted by specifying its corresponding key. The syntax for calling a value is similar to that
of calling a value in a list, but a key is specified instead of an index value. Listing 6-5 calls the value belonging to the
key labeled 'second' .
Listing 6-5. Call a value from a dictionary by specifying the key to which it belongs
emcli>mydic = {'first': 'one', 'second': 'two', 'third': 'three', 'fourth': 'four', 'fifth': 'five'}
emcli>mydic['second']
'two'
Dictionaries use keys instead of a numerical index. JSON is represented as a dictionary object in Python since
JSON is little more than an efficient key/value object. This can be shown clearly in EM CLI, as seen in Listing 6-6.
Listing 6-6. Use the list( ) function in EM CLI to show a JSON dictionary
emcli>set_client_property('EMCLI_OUTPUT_TYPE', 'JSON')
emcli>list(resource='Targets').isJson()
True
emcli>type(list(resource='Targets').out())
<type 'dict'>
emcli>list(resource='Targets').out()
{'exceedsMaxRows': False, 'columnHeaders': ['TARGET_NAM...
Most EM CLI functions include an isJson function, which returns a boolean result indicating whether the result
set will be JSON or not. This command indicates that the result set will return JSON instead of text.
By default, the interactive mode of EM CLI is text mode. The first command changes that behavior so that all
result sets are returned as JSON.
The fourth line calls the out sub-function of the list function. This out function either prints to the screen or feeds
to another object the output of the function that is calling it. In this case, the list function is returning information
about the EM targets, and the type function is telling us that this output is being returned as a dictionary object.
The sixth line shows a very small part of the actual output, showing that it is contained within curly braces. The
curly braces are what indicate that this object is a dictionary. Notice that one of the values is a list rather than a string.
A dictionary value can be any other object type, including another dictionary.
Logon Script
It is necessary when using either interactive or scripting modes of EM CLI to establish a connection between EM CLI
and the OMS. This is a repeatable procedure, so it is a good practice to have a script in place that can be called each
time one calls a script or logs into EM CLI interactive mode.
The logon script is described below with comments regarding the various parts of it. The complete script is listed
after the analysis.
The EM CLI classes and functions need to be imported first:
from emcli import *
 
Search WWH ::




Custom Search