phyllisstein / alp

A Python module for Alfred v2 workflows
174 stars 11 forks source link

Suggestion: Only import required submodules #10

Closed cjlucas closed 11 years ago

cjlucas commented 11 years ago

I've been looking into switching from alfred-python to alp recently for my Tower.app workflow. But I noticed that the time it took to fetch the results was about 3x slower than alfred-python.

alp currently imports all the submodules when importing the alp module. Normally this wouldn't be that big of an issue, but because Alfred executes the script every time the user types a new character, the lag becomes noticeable.

I'd suggest only importing the core and item submodules initially, then the developer utilizing this module to import optional submodules as needed.

Keep up the great work.

phyllisstein commented 11 years ago

Thanks for the suggestion! (And for your Tower workflow, too.) The way it's supposed to work---though it's possible that I bungled this---is that you should be able to delete whatever you're not using (except the core.py file) and alp should silently ignore that they're missing. That ought to bring the import time down significantly. If it doesn't, then I'll definitely look into a smarter way to handle that.

cjlucas commented 11 years ago

I haven't done extensive testing on it, but I was able to comment out all the submodules I wasn't using in __init__.py and it worked just fine.

phyllisstein commented 11 years ago

Good to know. You should also just be able to delete the .py files you're not using without actually making any changes to __init__.py; since it's written to pass on any import errors, it'll pull in what it can and ignore the rest (in my also-limited testing).

cjlucas commented 11 years ago

Exception handling in Python doesn't work that way. It will stop executing the try block as soon as one of the statements throws an exception. For instance, if I deleted mail.py, fuzzy and alp.request.request wouldn't be imported either.

phyllisstein commented 11 years ago

That is extremely good to know; thanks. I'll have to split it into individual statements.

On 27 Mar '13, at 9:02p, Chris Lucas notifications@github.com wrote:

In Python, try/except doesn't work that way. It will stop executing the try block as soon as one of the statements throws an exception. For instance, if I deleted mail.py, fuzzy and alp.request.request wouldn't be imported either.

— Reply to this email directly or view it on GitHub.

cjlucas commented 11 years ago

I'm not sure how experienced you are with Python, but a module isn't required to import all of it's submodules. The developer utilizing alp can import the submodules themselves.

Lets say __init__.py is empty, which isn't uncommon (though not recommended for this project). The user can import alp.core, which will load everything in core.py as well as __init__.py.

Then if the use user want's to take advantage of for instance, fuzzy searching, they can import alp.fuzzy themselves. Getting the benefits of a quick initialization by only loading the modules being used.

phyllisstein commented 11 years ago

Heh, not especially. (Experienced, that is.) Thanks for the help! I'll look into paring down the init file.