Database Reference
In-Depth Information
Understanding the Code
Now that we've seen the code itself as well as an overview of how it works, we'll analyze the code to understand what
is going on behind the scenes. Understanding the code will not only help you exploit all of its functionality, it will also
allow you to customize it to your tastes and needs.
The first thing to notice is that everything after the class declaration is indented by at least four spaces. This
means that all of the code after the class declaration is part of the class. Within the class are function declarations,
and beneath each function is the code that belongs to that function. Let's break this down into smaller pieces so as to
understand what exactly the code is doing and why using a class offers significant advantages.
The first function shown in Listing 6-15 is called __init__() . This is a reserved function name that will
automatically be executed when the class is called. The word self is a reference to the instance created from the class.
When an instance variable or object (preceded by self. ) is created, it is a variable or object assigned to the instance
for the life of that instance. When the myinst instance is created from updateProps() , the list object targs and
boolean variable reloadtargs are created as well. Instance variables and objects can be viewed as part of the instance
in most cases.
Listing 6-15. _init_( ) is the initial function of the updateProps( ) class
def __init__(self, agentfilter='.*', typefilter='.*',
namefilter='.*', propdict={}):
self.targs = []
self.reloadtargs = True
self.props(propdict)
self.__loadtargobjects()
self.filt(agentfilter=agentfilter, typefilter=typefilter,
namefilter=namefilter)
In Listing 6-16, we create an instance object of the updateProps() class by creating an instance of (executing) the
class and assigning that execution to a variable. Then we can print out the reloadtargs instance variable. Notice that
the word without parentheses is a class and the same word with parentheses is an instance.
Listing 6-16. Create an instance from the class
emcli>import updateProps
emcli>myinst = updateProps.updateProps()
emcli>print(myinst.reloadtargs)
False
emcli>type(updateProps.updateProps)
<type 'classobj'>
emcli>type(updateProps.updateProps())
<type 'instance'>
emcli>type(myinst)
<type 'instance'>
The last four lines of the __init__ function call the props, __loadtargobjects , and filt functions. The
props function, which sets the property keys and values, the __loadtargobjects function, which caches the target
information, and the filt function, which allows us to filter the targets assigned to the instance, are defined further
down in the class. Creating an instance parses the entire class before processing commands, so the ordering of
functions within a class is not important.
The querying of the target information is a fairly expensive process and can take between one and twenty
seconds to run. The __loadtargobjects function, as shown in Listing 6-17, is a performance-tuning function that
makes the execution of the class more efficient. It only queries and loads the target information on the creation of the
instance by being called within the __init__ function, and then only when self.reloadtargs is set to true .
 
Search WWH ::




Custom Search