<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 The Iterable Operation Extensions
<--- Back
Table of Contents for this notebook
The Iterable Operation Extensions
Table of Contents for this notebook
Overview
IterEnumerateInstances
IterEnumerateInstancePaths
IterAssociatorInstances
IterAssociatorInstancePaths
IterReferenceInstances
IterReferenceInstancePaths
IterQueryInstances
Using list comprehensions with iter operations
Using iter operations and forcing the use of traditional operations
Using iter operations and forcing the use of pull operations
The iterable operation extensions (short
Step1:  In this example (and the examples below), the connection has a default namespace set. This allows us to omit the namespace from the subsequent IterEnumerateInstances() method.
This example and the following examples also shows exception handling with pywbem
Step2:  IterAssociatorInstances
The IterAssociatorInstances() method requests the instances associated with a source instance from the server. It uses either the OpenAssociatorInstances() if the server supports it or otherwise Associators().
The operation returns an iterator for instances, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the association operation
Step3:  IterAssociatorInstancePaths
The IterAssociatorInstancePaths() method requests the instance paths of the instances associated with a source instance from the server. It uses either the OpenAssociatorInstancePaths() if the server supports it or otherwise AssociatorNames().
The operation returns an iterator for instance paths, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the association operation
Step4:  IterReferenceInstances
The IterReferenceInstances() method requests the instances referencing a source instance from the server. It uses either the OpenReferenceInstances() if the server supports it or otherwise References().
The operation returns an iterator for instances, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the reference operation
Step5:  IterReferenceInstancePaths
The IterReferenceInstancePaths() method requests the instance paths of the instances referencing a source instance from the server. It uses either the OpenReferenceInstancePaths() if the server supports it or otherwise ReferenceNames().
The operation returns an iterator for instance paths, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the reference operation
Step6:  IterQueryInstances
The IterQueryInstances() method requests the instances returned by a query from the server. It uses either the OpenQueryInstances() if the server supports it or otherwise ExecQuery().
This operation returns an object with two properties
Step7:  Using list comprehensions with iter operations
As an alternative to the for-loop processing of the iterator returned by the Iter...() method, a list comprehension can be use to perform the entire processing sequence in a single statement
Step8:  Using iter operations and forcing the use of traditional operations
The following example forces the use of the traditional operations by setting the use_pull_operations flag on the connection to False
Step9:  Using iter operations and forcing the use of pull operations
The following example forces the use of the pull operations by setting the use_pull_operations flag on the connection to True. If the server does not support pull operations an exception "CIM_ERR_NOT_SUPPORTED" will be returned.
<ASSISTANT_TASK:>
Python Code:
import pywbem
# Global variables used by all examples:
server = 'http://localhost'
username = 'user'
password = 'password'
namespace = 'root/cimv2'
classname = 'CIM_ComputerSystem'
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    inst_iterator = conn.IterEnumerateInstances(classname,
                                                MaxObjectCount=max_obj_cnt)
    for inst in inst_iterator:
        print('path=%s' % inst.path)
        print(inst.tomof())
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: The Iterable Operation Extensions
<--- Back
Table of Contents for this notebook
The Iterable Operation Extensions
Table of Contents for this notebook
Overview
IterEnumerateInstances
IterEnumerateInstancePaths
IterAssociatorInstances
IterAssociatorInstancePaths
IterReferenceInstances
IterReferenceInstancePaths
IterQueryInstances
Using list comprehensions with iter operations
Using iter operations and forcing the use of traditional operations
Using iter operations and forcing the use of pull operations
The iterable operation extensions (short: iter operations) are a set of methods added to pywbem.WBEMConnection class in pywbem version 0.10.0 to simplify the use of the pull vs. traditional operations (the original enumeration operations such as EnumerateInstances).
They simplify executing instance enumerations (EnumerateInstances, Associators, and References) by merging the traditional operations and pull operations so the client developer only has to code the iter operations and the Pywbem environment uses the Pull or traditional operations based on the server characteristics.
The purpose and function and usage of these WBEMConnection methods is defined in the [Concepts section] (https://pywbem.readthedocs.io/en/latest/concepts.html#iter-operations) of the online pywbem documentation.
IterEnumerateInstances
The IterEnumerateInstances() method requests the instances of a class from the server. It uses either the OpenEnumerateInstances() if the server supports it or otherwise EnumerateInstances().
The operation returns an iterator for instances, that is being processed in a for-loop.
End of explanation
# Global variables from first example are used
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    path_iterator = conn.IterEnumerateInstancePaths(classname,
                                                    MaxObjectCount=max_obj_cnt)
    for path in path_iterator:
        print('path=%s' % path)
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: In this example (and the examples below), the connection has a default namespace set. This allows us to omit the namespace from the subsequent IterEnumerateInstances() method.
This example and the following examples also shows exception handling with pywbem: Pywbem wraps any exceptions that are considered runtime errors, and raises them as subclasses of pywbem.Error. Any other exceptions are considered programming errors. Therefore, the code above only needs to catch pywbem.Error.
Note that the creation of the pywbem.WBEMConnection object in the code above does not need to be protected by exception handling; its initialization code does not raise any pywbem runtime errors.
IterEnumerateInstancePaths
The IterEnumerateInstancePaths() method requests the instance paths of the instances of a class from the server. It uses either the OpenEnumerateInstances() if the server supports it or otherwise EnumerateInstanceNames().
The operation returns an iterator for instance paths, that is being processed in a for-loop:
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    # Find an instance path for the source end of an association.
    source_paths = conn.EnumerateInstanceNames(classname)
    if source_paths:
        inst_iterator = conn.IterAssociatorInstances(source_paths[0],
                                                     MaxObjectCount=max_obj_cnt)
        for inst in inst_iterator:
            print('path=%s' % inst.path)
            print(inst.tomof())
    else:
        print('%s class has no instances and therefore no associations', classname)
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: IterAssociatorInstances
The IterAssociatorInstances() method requests the instances associated with a source instance from the server. It uses either the OpenAssociatorInstances() if the server supports it or otherwise Associators().
The operation returns an iterator for instances, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the association operation:
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    # Find an instance path for the source end of an association.
    source_paths = conn.EnumerateInstanceNames(classname)
    if source_paths:
        path_iterator = conn.IterAssociatorInstancePaths(source_paths[0],
                                                         MaxObjectCount=max_obj_cnt)
        for path in path_iterator:
            print('path=%s' % path)
    else:
        print('%s class has no instances and therefore no instance associations', classname)
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: IterAssociatorInstancePaths
The IterAssociatorInstancePaths() method requests the instance paths of the instances associated with a source instance from the server. It uses either the OpenAssociatorInstancePaths() if the server supports it or otherwise AssociatorNames().
The operation returns an iterator for instance paths, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the association operation:
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    # Find an instance path for the source end of an association.
    source_paths = conn.EnumerateInstanceNames(classname)
    if source_paths:
        inst_iterator = conn.IterAssociatorInstances(source_paths[0],
                                                     MaxObjectCount=max_obj_cnt)
        for instance in inst_iterator:
            print('path=%s' % inst.path)
            print(inst.tomof())
    else:
        print('%s class has no instances and therefore no instance associations', classname)
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: IterReferenceInstances
The IterReferenceInstances() method requests the instances referencing a source instance from the server. It uses either the OpenReferenceInstances() if the server supports it or otherwise References().
The operation returns an iterator for instances, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the reference operation:
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    # Find an instance path for the source end of an association.
    source_paths = conn.EnumerateInstanceNames(classname)
    if source_paths:
        path_iterator = conn.IterReferenceInstancePaths(source_paths[0],
                                                        MaxObjectCount=max_obj_cnt)
        for path in path_iterator:
            print('path=%s' % path)
    else:
        print('%s class has no instances and therefore no instance associations', classname)
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: IterReferenceInstancePaths
The IterReferenceInstancePaths() method requests the instance paths of the instances referencing a source instance from the server. It uses either the OpenReferenceInstancePaths() if the server supports it or otherwise ReferenceNames().
The operation returns an iterator for instance paths, that is being processed in a for-loop.
The following code executes EnumerateInstanceNames() to enumerate instances of a class and selects the first instance to act as a source instance for the reference operation:
End of explanation
# Global variables from first example are used
query_language = "DMTF:CQL"
query = 'Select * from Pywbem_Person'
max_obj_cnt = 10
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    inst_result = conn.IterQueryInstances(query_language, query,
                                            MaxObjectCount=max_obj_cnt)
    for inst in inst_result.generator:
        print(inst.tomof())
    else:
        print('query has no result')
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: IterQueryInstances
The IterQueryInstances() method requests the instances returned by a query from the server. It uses either the OpenQueryInstances() if the server supports it or otherwise ExecQuery().
This operation returns an object with two properties: 
query_result_class (CIMClass): The query result class, if requested via the ReturnQueryResultClass parameter which is only used with the [OpenQueryInstances()]​ operation and is not available with the [ExecQuery()] operation. None, if a query result class was not requested.
generator - A generator object that allows the user to iterate the CIM instances representing the query result. These instances do not have an instance path set.
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True)
try:
    paths = [path for path in conn.IterEnumerateInstancePaths(classname,
                                                              MaxObjectCount=max_obj_cnt)]
    print(*paths, sep='\n')
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: Using list comprehensions with iter operations
As an alternative to the for-loop processing of the iterator returned by the Iter...() method, a list comprehension can be use to perform the entire processing sequence in a single statement:
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True,
                             use_pull_operations=False)
try:
    inst_iterator = conn.IterEnumerateInstances(classname,
                                                MaxObjectCount=max_obj_cnt)
    for inst in inst_iterator:
        print('path=%s' % inst.path)
        print(inst.tomof())
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: Using iter operations and forcing the use of traditional operations
The following example forces the use of the traditional operations by setting the use_pull_operations flag on the connection to False:
End of explanation
# Global variables from first example are used
max_obj_cnt = 100
conn = pywbem.WBEMConnection(server, (username, password),
                             default_namespace=namespace,
                             no_verification=True,
                             use_pull_operations=True)
try:
    inst_iterator = conn.IterEnumerateInstances(classname,
                                                MaxObjectCount=max_obj_cnt)
    for inst in inst_iterator:
        print('path=%s' % inst.path)
        print(inst.tomof())
except pywbem.Error as exc:
    print('Operation failed: %s' % exc)
Explanation: Using iter operations and forcing the use of pull operations
The following example forces the use of the pull operations by setting the use_pull_operations flag on the connection to True. If the server does not support pull operations an exception "CIM_ERR_NOT_SUPPORTED" will be returned.
End of explanation
<END_TASK>