Closed MartinHeroux closed 4 years ago
If you want to limit imports what first comes to mind is using __all__
: https://twitter.com/pybites/status/1261659830495870984
Where (which branch) is the code so far so I can debug?
Thanks Bob
I have pushed the interface
branch to github; you should not be able to see it.
I will look into __all__
and get back to you. Thanks!
I don't think all will do it because it will limit things for the user as well as the test...at least that is what I think. Will try over the weekend.
Debugging, inspiring to look at how request
does this: https://github.com/psf/requests/blob/master/requests/__init__.py
This is pretty sweet: the init imports make that I can just import the classes directly in conftest.py - I will open a PR later ...
Check it out: https://github.com/MartinHeroux/spike2py/pull/37
The issue with the above is the end user will also have access to all the classes and namped tuples.
The only thing the end user really needs is Trial_Info
and Trial
from trial.py
. The stuff in signal_processing.py, plot.py and channels.py are there to keep related code together. The end user will never need to call the other stuff; for example channel
from the call from .plot import channel
.
Maybe I am asking for too much. But I found a few examples of __init__.py
files similar to mine. I just don't know if/how they get their tests to run.
If you only include the line from .trial import Trial_Info, Trial
in the __init__.py
file, then the interface is what I want, but most of the tests won't run.
I went looking at Brian Okken's Pytest book and the main tasks
package that he used as the example throughout the book has the following code in the __init__.py
file:
from .api import ( # noqa: F401
Task,
TasksException,
add,
get,
list_tasks,
count,
update,
delete,
delete_all,
unique_id,
start_tasks_db,
stop_tasks_db
)
This means that when the user does import tasks
, they will be able to type tasks.
and the hit tab
and have those (and only those) classes and functions available.
But this only means others put code like what I had in mind in the __init__.py
. It does not help me figure out how to get pytest to run test on functions and classes that are not imported in __init__py
.
Let me know if you find something that deals with this specifically.
I will continue looking, but I will also move on to other stuff because this one seems a bit too hard for me to figure out at the moment.
Where do you have the latest __init__.py and do you have it as you want it at this point?
Then I can look at the test code from there ...
Cheers Bob
Hi bob,
I am having trouble finding a solution to my problem.
I have modified my
__init__.py
in the main package folder.The reason I want to do this is to limit the user interface when using the package. Before this change, importing
spike2py
meant thatspike2py
was full of functions that I did not want to user to necessarily have access to (e.g.spike2py.mixin
).The changed
__init__.py
With the new version of the
__init__.py
file, the user is not distracted by lots of functions and classes that are not meant for the end user. See this screenshot of what it looks like:But doing this means that I can't run my tests. I guess this is because I previously had all the modules imported into to
__init__.py
spike2py file. I tried a few hacks that I found on stack overflow but none of them worked.Have you ever come across this? Do you know how to fix it so that I can run the tests but not have the user see/access all the methods and classes?