New python library for deferred loading

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

New python library for deferred loading

Post by etrombly »

I recently added lazyloader from tensorflow to freecad. The pull was merged so it's available on master. If your workbench is importing a module that is a little slow, you can defer loading until first usage. This really helps with workbench loading times. Here's how you use it:

Code: Select all

from lazy_loader.lazy_loader import LazyLoader
numpy = LazyLoader("numpy", globals(), "numpy")

or

whatever = LazyLoader("module", globals(), "module.whatever")

or to replicate import module as something

something = LazyLoader("module", globals(), "module")
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: New python library for deferred loading

Post by vocx »

etrombly wrote: Wed Apr 15, 2020 4:15 pm

Code: Select all

numpy = LazyLoader("numpy", globals(), "numpy")
whatever = LazyLoader("module", globals(), "module.whatever")
I'm not sure of these two calls.

Can you do this as well?

Code: Select all

whatever = LazyLoader("module.whatever", globals(), "module.whatever")
The first argument must be a module, is this right? The third argument seems to be whatever object you are importing, that is, the module itself, a class, or variable, or what?
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: New python library for deferred loading

Post by etrombly »

The first variable is the package or module name, the second variable is the parents globals, the third variable is the package or package and submodule. You can't pull in functions though, it has to be a package or module. I actually haven't tried it the way you called it, with module.whatever as the first variable, the examples had it how I showed it. You'd have to try to see if it works that way.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: New python library for deferred loading

Post by Kunda1 »

etrombly wrote: Wed Apr 15, 2020 5:37 pm The first variable is the package or module name, the second variable is the parents globals, the third variable is the package or package and submodule. You can't pull in functions though, it has to be a package or module. I actually haven't tried it the way you called it, with module.whatever as the first variable, the examples had it how I showed it. You'd have to try to see if it works that way.
Can we get some documentation on this neat new functionality ?
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
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: New python library for deferred loading

Post by etrombly »

Sure, where do you think it should go? The wiki?
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: New python library for deferred loading

Post by Kunda1 »

etrombly wrote: Sun Apr 26, 2020 7:08 pm Sure, where do you think it should go? The wiki?
I think so. Do you have wiki access ?
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
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: New python library for deferred loading

Post by etrombly »

No, I just started contributing recently.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: New python library for deferred loading

Post by vocx »

Kunda1 wrote: Sun Apr 26, 2020 4:32 pm Can we get some documentation on this neat new functionality ?
I'm not sure what sort of additional documentation you need. The usage is pretty much what is described in the first post. And this is already documented in the source code, in src/3rdParty/lazy_loader/__init__.py

Essentially we just need to change the first and third arguments and that's all there is to it to defer the import of that particular module.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
Kunda1
Veteran
Posts: 13434
Joined: Thu Jan 05, 2017 9:03 pm

Re: New python library for deferred loading

Post by Kunda1 »

vocx wrote: Tue Apr 28, 2020 4:37 am I'm not sure what sort of additional documentation you need. The usage is pretty much what is described in the first post. And this is already documented in the source code, in src/3rdParty/lazy_loader/__init__.py
Instead of devs digging through the code to learn that we're experimenting with this third party lib, we can mention it's existence and a short synopsis of what it does and how to use it. Perhaps link to the code itself? (ideally we could link to the generated API documentation).
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
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: New python library for deferred loading

Post by etrombly »

Added it to the extra python modules page of the wiki, it's at the bottom. https://wiki.freecadweb.org/Extra_python_modules
Post Reply