• Civilization 7 has been announced. For more info please check the forum here .

Python API

TheLopez

Deity
Joined
Jan 16, 2006
Messages
2,525
Location
Oregon
I don't know, I noticed it was down this morning when I was linking it in to Valchas' thread. I've been working on a reflection tool to inspect Python objects, it's not very far along, but it is occasionally helpful...

Code:
def getInfoOnObject(pObj):
    sObj = str(pObj.__class__)
    sFields = str(pObj.__dict__)
    pPlayer = gc.getPlayer(0)
    iX = pPlayer.getStartingPlot().getX()
    iY = pPlayer.getStartingPlot().getY()
    #sMethods = str(pObj.__methods__)
    #sMembers = str(pObj.__members__)
    sDir = str(dir(pObj))
    count = 0
    for sField in dir(pObj):
        sField = str(sField)
        if(sField[0] != '_'):
            count = count + 1
            if(count > 2):
                message = sField
                CyInterface().addMessage(0,True,len(message),message,'AS2D_BOMBARDED',0,'Art/Interface/Buttons/Units/Fighter.dds',ColorTypes(6),iX,iY,True,True)
    
                sAttrDir = str(dir(getattr(pObj, sField)))
                break
                
    methodList = [method for method in dir(pObj) if callable(getattr(pObj, method))]
    fieldList = [field for field in dir(pObj) if not callable(getattr(pObj, field))]
    processFunc = 1 and (lambda s: " ".join(s.split())) or (lambda s: s)
    message =  "\n".join(["%s %s" %(method.ljust(30), processFunc(str(getattr(pObj, method).__doc__))) for method in methodList])
    CyInterface().addMessage(0,True,len(message),message,'AS2D_BOMBARDED',0,'Art/Interface/Buttons/Units/Fighter.dds',ColorTypes(7),iX,iY,True,True)
    #message = message + "\n".join(["%s %s" %(field.ljust(30), str(type(getattr(pObj, field)))) for field in fieldList])
    message = "\n".join(["%s %s" %(field.ljust(30), str(type(getattr(pObj, field)))) for field in fieldList])
    CyInterface().addMessage(0,True,len(message),message,'AS2D_BOMBARDED',0,'Art/Interface/Buttons/Units/Fighter.dds',ColorTypes(7),iX,iY,True,True)
    #message = "Object:" + sObj + "\nFields:" + sFields + "\nMethods:" + sMethods +"\nMembers" + sMembers
    #message = "Object:" + sObj + "\nFields:" + sDir + "\nTest: " + sAttrDir
    #message = "Object:" + sObj + "\nFields:" + sDir
    #message = "Object:" + sObj + "\n\n" + message
    message = "Object:" + sObj + "\n\n"
    CyInterface().addMessage(0,True,len(message),message,'AS2D_BOMBARDED',0,'Art/Interface/Buttons/Units/Fighter.dds',ColorTypes(7),iX,iY,True,True)
 
I use them both. Plus that function I pasted. Haven't figured out how to get the args of a method. I'm not a Python guy. Only learned Python about a month ago (30 days as of three days ago!!! Yay me!.. er.. whatever), and I haven't found a really juicy, accessible presentation of the language in a manner that makes me smile basking in the glow of sudden comprehension 8). I have written enough scripting language compilers to believe that it must be possible. Even though it seems apparent that Python is implemented in C/C++, it has the *appearance* of a design developed in itself. Or most likely in a much (*cough*, *cough*) better language, like Smalltalk. As such presumably all the pieces you would need to make a function call should be accessible from within the language, even if it lacks formal Reflection. Sadly, even having the arg information will only get me so far, as Python, being weakly typed, won't have much information about the args other then default value, and it's name (given that it uses direct string keys in a dictionary for its namespaces). Crazy.
 
Top Bottom