ensobjlist class

Parent Previous Next

ensobjlist class

The ensobjlist class is a subclass of the list class with a couple of handy features. 

Overload index operator


items = objlist['value']
items = objlist[('value','value',...)]
 
The array index operator ([]) will allow for strings and tuples of strings to be used as index values. These essentially look to see if the description attribute of the objects in the ensobjlist match any of the values specified in the tuple. For example:

parts =(ensight.objs.core.PARTS[('wheels','engine')])

will return an ensobjlist with all of the parts that have the description 'wheels' or 'engine'. Note: in 10.2 and beyond, the value tuple can be any object that support iteration.

Find method


items = objlist.find(value, attr=ensight.objs.enums.DESCRIPTION, group=0)
items = objlist.find(('value','value',...), attr=ensight.objs.enums.DESCRIPTION, wildcard=0, group=0)

The find method is very similar to the overloaded index operator in that it searches the ensobjlist for items with attributes that match the value or tuple of values passed to the method.  The find() method also allows for the attribute to be explicitly specified.  It will default to the DESCRIPTION attribute, but it can support any attribute.  For example, to find the visible parts:

parts = ensight.objs.core.PARTS.find(True, attr=ensight.objs.enums.VISIBLE)

will return an ensobjlist with all of the visible parts. The 'wildcard' keyword may be set to an integer that allows changes the form of the value/attribute comparison. If wildcard is set to a non-zero value, both the attribute and the value will be converted into string form and the value will be used as the pattern to a 'glob' style wildcard comparison. For example:


parts = ensight.objs.core.PARTS.find("Hello*", wildcard=2)

 

will return all of the parts whose DESCRIPTION attribute begins with the word 'Hello', ignoring case.  The wildcard matching is done in the 'glob' style as implemented by fnmatch().  The default value for wildcard is 0 (do not use wildcard rules).  Other settings include:  2=use case insensitive comparison, 4=use "pathname" matching conventions for matching slash characters, 6=case insensitive comparison with pathname matching.

 

The optional keyword group=1 controls the type of object returned. If this is specified, the return value will be an ENS_GROUP object instead of an ensobjlist.  This makes it easy to do very efficient bulk attribute changes:

 

ensight.objs.core.PARTS.find(("*INLET*","*OUTLET*"), wildcard=1, group=1).setchildattr(ensight.objs.enums.VISIBLE, False)


You can set the recurse parameter as well. The recurse parameter is used to walk the children found in groups and in groups of groups and is needed if you want to make sure that you get all the available objects.  For example, after File>Open, select parts to load, you can get a list of LPARTS as follows:


core.find_objs(core.CURRENTCASE,types=[ENS_LPART],recurse=1)


Change history: 

Additional notes


Since the return values of the overloaded index operator and the find() method are also ensobjlist objects, they can be chained to create more complex filters. Combining the examples above:

parts = ensight.objs.core.PARTS[('wheels','engine')].find(True, attr=ensight.objs.enums.VISIBLE)

Also, the comparison operation is performed using the __cmp__() method on the value objects themselves, so it is possible to override the comparison operation.  An example might be to use wildcard string comparisons:


import fnmatch

class wildcard():
    def __init__(self, value):
        self._v = value
    def __cmp__(self, other):
        if fnmatch.fnmatch(other,self._v):
            return 0
        return 1

    def __eq__(self, other):

        return self.__cmp__(other) == 0

    def __ne__(self, other):

        return self.__cmp__(other) != 0

    def __gt__(self, other):

        return self.__cmp__(other) > 0

    def __lt__(self, other):

        return self.__cmp__(other) < 0

    def __ge__(self, other):

        return self.__cmp__(other) >= 0

    def __le__(self, other):

        return self.__cmp__(other) <= 0
        
print(ensight.objs.core.PARTS.find(wildcard("*gin*")))


Which returns all of the parts with 'gin' in their name using find() and an object with an overloaded comparison operator to scan the DESCRIPTION attributes.  Note this use of Python 2 and 3 comparison methods for backward compatibility.