New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A suggestion for a more robust and modular import system #88
Comments
Yes, I'm also not very happy with fact that importing all classes causes I think you idea is very nice and a something like a good compromise. We Best, On 31.07.2016 16:47, Sebastiaan Mathot wrote:
Dr. Oliver Lindemann |
Yet another solution would be to add the docstrings to the factory function (and not to the class, to avoid duplication), and copy them to the instantiated objects at runtime. Like so:
Edit: This actually doesn't make sense, as it copies the docstring of the constructor to the object itself. Copying is probably not even really necessary, or, if implemented, it should be |
Yes that one option. But I would do it only for some critical classes Definitively, you are right, video and parallel port should not case As an alternative solution, we could still import everything and Oliver On 01.08.2016 09:44, Sebastiaan Mathot wrote:
Dr. Oliver Lindemann |
Hi Sebastiaan, Thanks for your suggestion. Oliver and me discussed this in the past actually. Back then we decided to do this for the extras, but not for the core modules. Since 0.8.0 the extras have hence to be imported manually, in addition to Expyriment itself. To me, this already was a compromise as it means that the extras will not be visible any longer to someone who uses Expyriment interactively. Since the extras are, well, extra features, their non-visibility is somehow justifiable. For the rest of the Expyriment, however, I don't think it is. To me, proper introspection is absolute key for a library. Your suggestion, while being a great one technically, unfortunately will also break proper introspection, as you have mentioned yourself. I therefore am with Oliver on this, and I think we should rather catch critical imports and show a warning if it goes wrong (this was actually the intention for For your two specific cases the fixes seem easy. In So to sum up, I think we should of course just make sure that for a release everything in core works and Expyriment does not crash when imported. To achieve this, we will create alpha/beta releases for you and others to test, once we have implemented all the features for the next release. Until then, the development branches might be broken from time to time, while we work on these features. |
@florian, your Oliver On 01.08.2016 10:23, Florian Krause wrote:
Dr. Oliver Lindemann |
Well, not if you can disable/ enable it through an environment variable. And the docstring-copying method also largely preserves introspection, at least at the level of docstrings.
I haven't installed either. But that shouldn't be necessary, should it? |
@lindemann09 Your fix would not check for dlportio if inpout32 failed. I fixed that now. @smathot could you check if this runs on your system? |
@smathot Yes, the environment variable might be an option to think about. However, it would still break introspection if it is set to |
Right. What aspects of introspection should be preserved? My understanding is that it's basically all about the docstrings, right? So if the factory function has the same docstring as the constructor of the class that it produces, then it would be fully transparent docstring-wise. Even more transparent is to use a type-morphing class that is essentially an undefined class that morphs into another class once it's instantiated. Believe it or not, the following works, and will give you a regular
|
Yes, that might be an option. We will look into this. But help me again with what the benefits are that justify this overhead. I see mainly one advantage, and that is memory usage. However, on my machine the Python process uses 38.2MB memory after I imported expyriment. Considering that 13MB of this are Python itself, Pygame and OpenGL, that leaves 25MB for expyriment itself. It does not seem that much to me to be honest. Initializing a screen uses more memory even. The downside I see is that we use the sanity check that all modules actually load properly. For instance, without this, you might have never stumbled upon the parallel port bug and we might not know of it (as it worked on all machines we tested). |
Well, that's true, of course. It mostly just goes against my sense of elegance. And the fragility thing, speaking of which:
But that would be better handled with an automated unittest. In OpenSesame I have unittests to check (among many other thing) the syntax (and in some cases importability) of all modules. But I don't import them all when OpenSesame starts! |
I guess our sense of elegance differs here :-) I agree with you that proper unit tests are very much needed, independent of the suggestion discussed here. I hope we will eventually have some time to implement them. |
@smathot I will keep this in mind for the 1.0.0 release. For now, we would like to focus on getting the 0.9.0 (with Python 3 support) out quickly (expect a beta very soon). |
We are considering this now for the extras and maybe in future for all classes. |
Right now, expyriment always imports everything. This is not only uneccesary, but also problematic for two reasons:
stimuli._video
andio._parallel_port
modules were broken, and--even though I didn't even use those--this prevented me from using expyriment alltogether.I've been thinking about the best way to improve this without changing the import scheme from the perspective of the user. For example,
expyriment.stimui.__init__.py
has this line:This allows users to do:
This way of importing is convenient for users, but the implementation is suboptimal, for the two reasons I described above. One way to improve this would be to change
expyriment.stimui.__init__.py
so that it contains:In other words, instead of directly importing the
Canvas
class from_canvas
, you define a factory function that only imports when necessary. This works identical in every way, except:Canvas
class itself, because it is now a function; andCanvas
. So this is actually a side-effect of 1. Docstrings cannot be copied without importing_canvas
, thus defeating the purpose of the factory function.Point 2 may be problematic in some cases, for example for users who use an editor that automatically shows these docstrings. So you could even imagine making this optional. For example, say that there is an environment variable called
EXPYRIMENT_LAZY_IMPORT
, then you could do:What do you think?
The text was updated successfully, but these errors were encountered: