Python introspection to enlightenment?
Brendan Scott
disposableemail at apps.opensourcelaw.biz
Wed Jun 14 12:20:09 UTC 2017
On 06/14/2017 06:36 PM, Boudewijn Rempt wrote:
> On Tue, 13 Jun 2017, Brendan Scott wrote:
>
>> I'm trying to query the attributes of the krita module, krita.Krita and
>> krita.Krita.instance() objects to get them to tell me a bit about themselves
>> (actually, it looks like I'm inspecting krita.krita.Krita?!).
>
> Well, we do need to discuss how we should name the various components of
> the python scripting plugin.
>
> We've got:
>
> libkis: the wrapper library that exposes Krita to scripting languages, which has a class called Krita,
> that is aliased as Krita, Application or Scripter and forms the main entry for scripting.
>
> Krita.sip: generates a python module called pykrita, which wraps libkis, and provides pykrita and PyKrita.krita
> krita: the python module created in plugins/extensions/pykrita/plugin/krita... This provides
> the actual built-ins Scripter, Application and Krita.
>
> I am getting confused myself, and would like to make this a bit clearer.
>
[snip]
> If I do in the Scripter:
>
> import krita
> print(dir(krita.Krita))
>
> I get the full list of methods, though:
>
[snip]
Yes, my introspection code was wrong. Have fixed. Some of the docstrings work, most of them are terse (eg "QObject.destroyed[QObject] [signal]") and a fair few of them are None. Callables are identified as callable.
Here is my code:
[code]
#import sys
#from PyQt5.QtGui import *
#from PyQt5.QtWidgets import *
#from krita import *
import krita
def dumper(obj):
""" look at each attribute of object
print details
"""
template = "\n\n%s: %s callable: %s, type: %s, \ndocstring: \n%s\nattributes: \n%s"
print("object type = %s\n attributes:"%type(obj))
#print(obj.__name__)
for i, attr_name in enumerate(dir(obj)):
if attr_name[:2] == "__":
continue # skip private attributes
attr = getattr(obj, attr_name)
attributes = [a for a in dir(attr) if a[:2] != "__"]
deets = template%(i, attr_name, callable(attr), type(attr), attr.__doc__, attributes)
print(deets)
print("\n**************Dict***********\n%s\n**************\n"%obj.__dict__)
#dumper(krita)
#dumper(krita.Krita)
dumper(krita.Extension)
[end code]
Gives this output (ie attributes of krita.Extension):
object type = <class 'PyQt5.QtCore.pyqtWrapperType'>
attributes:
26: blockSignals callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.blockSignals(bool) -> bool
attributes:
[]
27: childEvent callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
28: children callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.children() -> list-of-QObject
attributes:
[]
29: connectNotify callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
30: customEvent callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
31: deleteLater callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.deleteLater()
attributes:
[]
32: destroyed callable: True, type: <class 'PyQt5.QtCore.pyqtSignal'>,
docstring:
QObject.destroyed[QObject] [signal]
attributes:
[]
33: disconnect callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.disconnect()
attributes:
[]
34: disconnectNotify callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
35: dumpObjectInfo callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.dumpObjectInfo()
attributes:
[]
36: dumpObjectTree callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.dumpObjectTree()
attributes:
[]
37: dynamicPropertyNames callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.dynamicPropertyNames() -> list-of-QByteArray
attributes:
[]
38: event callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.event(QEvent) -> bool
attributes:
[]
39: eventFilter callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.eventFilter(QObject, QEvent) -> bool
attributes:
[]
40: findChild callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.findChild(type, str name='', Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> QObject
QObject.findChild(tuple, str name='', Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> QObject
attributes:
[]
41: findChildren callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.findChildren(type, str name='', Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> list-of-QObject
QObject.findChildren(tuple, str name='', Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> list-of-QObject
QObject.findChildren(type, QRegExp, Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> list-of-QObject
QObject.findChildren(tuple, QRegExp, Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> list-of-QObject
QObject.findChildren(type, QRegularExpression, Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> list-of-QObject
QObject.findChildren(tuple, QRegularExpression, Qt.FindChildOptions options=Qt.FindChildrenRecursively) -> list-of-QObject
attributes:
[]
42: inherits callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.inherits(str) -> bool
attributes:
[]
43: installEventFilter callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.installEventFilter(QObject)
attributes:
[]
44: isSignalConnected callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
45: isWidgetType callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.isWidgetType() -> bool
attributes:
[]
46: isWindowType callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.isWindowType() -> bool
attributes:
[]
47: killTimer callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.killTimer(int)
attributes:
[]
48: metaObject callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.metaObject() -> QMetaObject
attributes:
[]
49: moveToThread callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.moveToThread(QThread)
attributes:
[]
50: objectName callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.objectName() -> str
attributes:
[]
51: objectNameChanged callable: True, type: <class 'PyQt5.QtCore.pyqtSignal'>,
docstring:
QObject.objectNameChanged[str] [signal]
attributes:
[]
52: parent callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.parent() -> QObject
attributes:
[]
53: property callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.property(str) -> QVariant
attributes:
[]
54: pyqtConfigure callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.pyqtConfigure(...)
Each keyword argument is either the name of a Qt property or a Qt signal.
For properties the property is set to the given value which should be of an
appropriate type.
For signals the signal is connected to the given value which should be a
callable.
attributes:
[]
55: receivers callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
56: removeEventFilter callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.removeEventFilter(QObject)
attributes:
[]
57: sender callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
58: senderSignalIndex callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
59: setObjectName callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.setObjectName(str)
attributes:
[]
60: setParent callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.setParent(QObject)
attributes:
[]
61: setProperty callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.setProperty(str, QVariant) -> bool
attributes:
[]
62: setup callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
63: signalsBlocked callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.signalsBlocked() -> bool
attributes:
[]
64: startTimer callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.startTimer(int, Qt.TimerType timerType=Qt.CoarseTimer) -> int
attributes:
[]
65: staticMetaObject callable: False, type: <class 'PyQt5.QtCore.QMetaObject'>,
docstring:
QMetaObject()
QMetaObject(QMetaObject)
attributes:
['checkConnectArgs', 'classInfo', 'classInfoCount', 'classInfoOffset', 'className', 'connectSlotsByName', 'constructor', 'constructorCount', 'enumerator', 'enumeratorCount', 'enumeratorOffset', 'indexOfClassInfo', 'indexOfConstructor', 'indexOfEnumerator', 'indexOfMethod', 'indexOfProperty', 'indexOfSignal', 'indexOfSlot', 'invokeMethod', 'method', 'methodCount', 'methodOffset', 'normalizedSignature', 'normalizedType', 'property', 'propertyCount', 'propertyOffset', 'superClass', 'userProperty']
66: thread callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.thread() -> QThread
attributes:
[]
67: timerEvent callable: True, type: <class 'builtin_function_or_method'>,
docstring:
None
attributes:
[]
68: tr callable: True, type: <class 'builtin_function_or_method'>,
docstring:
QObject.tr(str, str disambiguation=None, int n=-1) -> str
attributes:
[]
**************Dict***********
{'disconnectNotify': <built-in method disconnectNotify>, '__doc__': None, 'timerEvent': <built-in method timerEvent>, 'sender': <built-in method sender>, 'senderSignalIndex': <built-in method senderSignalIndex>, 'customEvent': <built-in method customEvent>, '__module__': 'PyKrita.krita', 'connectNotify': <built-in method connectNotify>, 'childEvent': <built-in method childEvent>, 'receivers': <built-in method receivers>, 'isSignalConnected': <built-in method isSignalConnected>, 'setup': <built-in method setup>}
**************
> I'm really glad that you're posing all these questions, before we really
> release the Python scripting -- it is a big help in getting things right!
Oh, good. I was worried I was being bothersome.
More information about the kimageshop
mailing list