Python introspection... 2005-06-10 - By Bernard Lebel
Back Hello,
Is there an easier way to check if an object has, for example, the "LocalProperties" attribute, other than by going through such a chunk of code?
Right now I have this:
xsi = Application
oObject = xsi.selection(0)
# Iterate object attributes for i in xrange( 0, oObject._oleobj_.GetTypeInfoCount() ):
typeInfo = xsi.selection(0)._oleobj_.GetTypeInfo( i ) typeAttr = typeInfo.GetTypeAttr() # Iterate "functions" of object for iFunction in xrange( 0, typeAttr.cFuncs ): functionDescription = typeInfo.GetFuncDesc( iFunction ) # Get name of function (even if function is actually a property) sName = typeInfo.GetNames( functionDescription.memid )[0] if sName == 'LocalProperties': xsi.logmessage( 'yeah!' )
I have tried simpler approches, like
if not oObject.localproperties: xsi.logmessage( 'no' )
But I get an attribute error (<unknown>.localproperties).
Thanks Bernard
On 4/6/05, Jerry Gamache <jerryg@(protected)> wrote: > Once you know the function name, you sometimes want to get some very terse documentation: > > def FormatDocumentation(typeInfo, funDesc): > nameAndParms = typeInfo.GetNames(funDesc.memid) > docum = typeInfo.GetDocumentation(funDesc.memid) > import pythoncom > # This one differentiates between "functions" and "accessors" > if funDesc.invkind == pythoncom.INVOKE_FUNC: > docStr = nameAndParms[0] + "(" + ",".join(nameAndParms[1:]) + ")\n" > else: > docStr = nameAndParms[0] + "\n" > if docum[1]: > docStr += "\t" + docum[1] > return docStr > > def GetDocumentation( dynDisp, funcName = None ): > allTypeInfoDoc = [] > for iTI in xrange(0,dynDisp._oleobj_.GetTypeInfoCount()): > typeInfo = dynDisp._oleobj_.GetTypeInfo(iTI) > typeAttr = typeInfo.GetTypeAttr() > if funcName: > for iFun in xrange(0,typeAttr.cFuncs): > funDesc = typeInfo.GetFuncDesc(iFun) > name = typeInfo.GetNames(funDesc.memid)[0] > if name.upper() == funcName.upper(): > return FormatDocumentation(typeInfo, funDesc) > else: > dict = {} > className = dynDisp._oleobj_.GetTypeInfo(0) .GetDocumentation(-1) > classDoc = "class %s \n\t%s\n\n"%(className[0] ,className[1]) > allFuncDoc = [] > for iFun in xrange(0,typeAttr.cFuncs): > funDesc = typeInfo.GetFuncDesc(iFun) > name = typeInfo.GetNames(funDesc.memid)[0] > if not dict.has_key(name): > dict[name] = 1 > allFuncDoc.append(FormatDocumentation (typeInfo, funDesc)) > allFuncDoc.sort() > allTypeInfoDoc.append(classDoc + "\n".join(allFuncDoc )) > if funcName: > return "Documentation not found for %s"%(funcName,) > else: > return "\n".join(allTypeInfoDoc) > > # 2 ways to call this one: without a function name it retrieves doc for all functions > Application.LogMessage(GetDocumentation(Application)) > # And with a name, it tries to find the doc for that function: > Application.LogMessage(GetDocumentation(Application, "StatusBar")) > Application.LogMessage(GetDocumentation(Application, "NotThere"))
--- Unsubscribe? Mail Majordomo@(protected) with the following text in body: unsubscribe xsi
|
|