Browsing API from the Python console
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Browsing API from the Python console
In https://www.freecadweb.org/wiki/Power_users_hub section API Functions, the reader is informed that "For more accurate information, browse the modules directly from FreeCAD's Python console."
How does one browse the modules directly from FreeCAD's Python console?
How does one browse the modules directly from FreeCAD's Python console?
Re: Browsing API from the Python console
When you type the 'dot' after a module, you get an autocompletion list, that displays doc on mouse-hover :
Re: Browsing API from the Python console
Don't know why, some cases it does not return all available functions/attribute (not a programmer, may not be accurate about the correct term)
Re: Browsing API from the Python console
One of the attributes/properties as it's adjusted that I see that's not getting generated on the command line is line weight. I don't know if it's in the Python object model, but that's a minor quibble. If I get a proper OpenGL driver the line transparency will be enabled.
Re: Browsing API from the Python console
Can you post what instances ?
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
'good first issues' | Open TODOs and FIXMEs | How to Help FreeCAD | How to report Bugs
- DeepSOIC
- Veteran
- Posts: 7896
- Joined: Fri Aug 29, 2014 12:45 am
- Location: used to be Saint-Petersburg, Russia
Re: Browsing API from the Python console
Some attributes are special in how they are implemented. The objects that define them should provide __dir__() method to list them, and probably they just don't.
https://docs.python.org/2/library/functions.html#dir
- DeepSOIC
- Veteran
- Posts: 7896
- Joined: Fri Aug 29, 2014 12:45 am
- Location: used to be Saint-Petersburg, Russia
Re: Browsing API from the Python console
Update. Here's another example where FC console doesn't provide autocompletion.
That is, my_attr does not show up when browsing with console, but it is included into dir(). So I don't know, how does FC fill up the autocomplete.
If I add my_attr as class variable (which is a convenient way to define and document attributes, but has some traps for young players):then it does show up.
Code: Select all
>>> class MyTest(object):
... def __init__(self):
... self.my_attr = 9
...
>>> i = MyTest()
>>> i.my_attr
9
>>> 'my_attr' in dir(i)
True
If I add my_attr as class variable (which is a convenient way to define and document attributes, but has some traps for young players):
Code: Select all
class MyTest(object):
my_attr = 9
i = MyTest()
Re: Browsing API from the Python console
Yes FreeCAD does the auto-completion in a different way. We had a discussion about this, but came to the conclusion that work should start on this topic once the py3-porting is done: https://forum.freecadweb.org/viewtopic. ... 10#p206336
ps.: there are also problems with autocompletion of properties (@property). In python3 some autocomplete attempts will also crash FreeCAD.
- DeepSOIC
- Veteran
- Posts: 7896
- Joined: Fri Aug 29, 2014 12:45 am
- Location: used to be Saint-Petersburg, Russia
Re: Browsing API from the Python console
A bit of debugging reveals that this line:
https://github.com/FreeCAD/FreeCAD/blob ... s.cpp#L294
is executed when I press "." after instance. So it converts instance into class object, and calls dir() on class object. I think it shouldn't be happening, it's probably a bug.
Also it defines quite a few behavior exclusions for some FC objects. I'm not sure why is it done so instead of implementing __dir__ method in the relevant objects. There is a comment that tries to explain it, but I don't quite understand it.
https://github.com/FreeCAD/FreeCAD/blob ... s.cpp#L294
Code: Select all
Py::Object obj(eval, true);
// Checks whether the type is a subclass of PyObjectBase because to get the doc string
// of a member we must get it by its type instead of its instance otherwise we get the
// wrong string, namely that of the type of the member.
// Note: 3rd party libraries may use their own type object classes so that we cannot
// reliably use Py::Type. To be on the safe side we should use Py::Object to assign
// the used type object to.
//Py::Object type = obj.type();
Py::Object type(PyObject_Type(obj.ptr()), true);
if (...) {
...
} else if (PyObject_IsInstance(obj.ptr(), basetype.o) == 1) {
// New style class which can be a module, type, list, tuple, int, float, ...
// Make sure it's not a type objec
union PyType_Object typetype = {&PyType_Type};
if (PyObject_IsInstance(obj.ptr(), typetype.o) != 1) {
// this should be now a user-defined Python class
// http://stackoverflow.com/questions/12233103/in-python-at-runtime-determine-if-an-object-is-a-class-old-and-new-type-instan
if (Py_TYPE(obj.ptr())->tp_flags & Py_TPFLAGS_HEAPTYPE) {
obj = type; // <------------- this one is executed!
}
}
}
Also it defines quite a few behavior exclusions for some FC objects. I'm not sure why is it done so instead of implementing __dir__ method in the relevant objects. There is a comment that tries to explain it, but I don't quite understand it.