Browsing API from the Python console

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
a3bksll47
Posts: 197
Joined: Sat Mar 17, 2018 3:42 am

Browsing API from the Python console

Post by a3bksll47 »

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?
User avatar
Chris_G
Veteran
Posts: 2598
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Browsing API from the Python console

Post by Chris_G »

When you type the 'dot' after a module, you get an autocompletion list, that displays doc on mouse-hover :
python_console2.jpeg
python_console2.jpeg (38.2 KiB) Viewed 1715 times
a3bksll47
Posts: 197
Joined: Sat Mar 17, 2018 3:42 am

Re: Browsing API from the Python console

Post by a3bksll47 »

Thanks.
paullee
Veteran
Posts: 5118
Joined: Wed May 04, 2016 3:58 pm

Re: Browsing API from the Python console

Post by paullee »

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)
a3bksll47
Posts: 197
Joined: Sat Mar 17, 2018 3:42 am

Re: Browsing API from the Python console

Post by a3bksll47 »

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.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: Browsing API from the Python console

Post by Kunda1 »

paullee wrote: Thu Dec 06, 2018 5:23 am 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)
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
User avatar
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

Post by DeepSOIC »

paullee wrote: Thu Dec 06, 2018 5:23 am Don't know why, some cases it does not return all available functions/attribute
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
User avatar
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

Post by DeepSOIC »

Update. Here's another example where FC console doesn't provide autocompletion.

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
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):

Code: Select all

class MyTest(object):
  my_attr = 9

i = MyTest()
then it does show up.
User avatar
looo
Veteran
Posts: 3941
Joined: Mon Nov 11, 2013 5:29 pm

Re: Browsing API from the Python console

Post by looo »

DeepSOIC wrote: Thu Dec 06, 2018 1:31 pm So I don't know, how does FC fill up the autocomplete.
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.
User avatar
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

Post by DeepSOIC »

A bit of debugging reveals that this line:
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!
        }
    }
}
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.
Post Reply