Browsing API from the Python console

Need help, or want to share a macro? Post here!
a3bksll47
Posts: 147
Joined: Sat Mar 17, 2018 3:42 am

Browsing API from the Python console

Postby a3bksll47 » Tue Dec 04, 2018 8:33 pm

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
Posts: 1250
Joined: Tue Dec 31, 2013 4:10 pm
Location: France
Contact:

Re: Browsing API from the Python console

Postby Chris_G » Wed Dec 05, 2018 2:55 pm

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

Re: Browsing API from the Python console

Postby a3bksll47 » Wed Dec 05, 2018 5:03 pm

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

Re: Browsing API from the Python console

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

Re: Browsing API from the Python console

Postby a3bksll47 » Thu Dec 06, 2018 11:56 am

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
Posts: 7065
Joined: Thu Jan 05, 2017 9:03 pm

Re: Browsing API from the Python console

Postby Kunda1 » Thu Dec 06, 2018 12:40 pm

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 ?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
User avatar
DeepSOIC
Posts: 7479
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Browsing API from the Python console

Postby DeepSOIC » Thu Dec 06, 2018 1:22 pm

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
Posts: 7479
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Browsing API from the Python console

Postby DeepSOIC » Thu Dec 06, 2018 1:31 pm

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
Posts: 3180
Joined: Mon Nov 11, 2013 5:29 pm

Re: Browsing API from the Python console

Postby looo » Thu Dec 06, 2018 1:52 pm

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.
please help with my conda-packaging efforts: https://liberapay.com/looooo/
minimalistic blog: https://looooo.github.io/mini-blog/
User avatar
DeepSOIC
Posts: 7479
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: Browsing API from the Python console

Postby DeepSOIC » Thu Dec 06, 2018 1:55 pm

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.