Re: [Tutor] loading modules only when needed and PEP 008
Hey Timmie, Tim Michelsen wrote: >> When I do this sort of thing I like >> to move my imports into my functions/methods. > Would this then be compliant with the style recommendations? Hm, I'm not sure. I guess that if you consider that somewhere near the top of the style guide it says something about 'foolish consistency' than I suppose it does . I think it's ok to deviate from the pep when it makes sense to do so. Define 'makes sense'. I'm assuming that importing the matplotlib at the top of your file is making your app unusably slow; to me that is a great reason to break from the style guide. >> And as we are talking about style, note that your else: pass isn't >> really necessary but it does make it explicitly clear that you are >> choosing not to do anything if the plot_data isn't 'yes'. > Ok, so I already got some style here, at least... I wouldn't include the 'else' bits, but that's my style. >> Are those >> tabs you're using? Four spaces are preferred, according to the >> style guide. > I always use 4 spaces. This sometimes leads to confusions when working > with the same editor on text files that rely tabs but this is another > issue... Sorry. In my mail reader the indentation looked like tabs. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] loading modules only when needed and PEP 008
> When I do this sort of thing I like > to move my imports into my functions/methods. Would this then be compliant with the style recommendations? > The 'main' code then > conditionally calls the function/method. All the code in the > function safely assumes that the import has been done but code in the > calling function assumes the import hasn't been done. Yes, this should a the solution. > And as we are talking about style, note that your else: pass isn't > really necessary but it does make it explicitly clear that you are > choosing not to do anything if the plot_data isn't 'yes'. Ok, so I already got some style here, at least... > Are those > tabs you're using? Four spaces are preferred, according to the > style guide. I always use 4 spaces. This sometimes leads to confusions when working with the same editor on text files that rely tabs but this is another issue... Thanks very much for your fast response! Kind regards, Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] loading modules only when needed and PEP 008
Hi Tim, Tim Michelsen wrote: > Hello fellow Pythonistas, > I have a question concerning import statements. ... > it takes a lot > of time for a TKinter-GUI to start up. And this start-up time is even > longer when matplotlib is imported > a optimized version would be: > import sys > > plot_data = 'yes' # option: yes/no > > if plot_data == 'yes': > import matplotlib > else: > pass ... > How would you handle such a case? > What is recommended in such a case? > Does anyone have experience with this? Beware that code later in your module that calls matplotlib.foo() may fail if plot_data is not 'yes'. When I do this sort of thing I like to move my imports into my functions/methods. The 'main' code then conditionally calls the function/method. All the code in the function safely assumes that the import has been done but code in the calling function assumes the import hasn't been done. - if plot_data: show_plot_data(mydata) - def show_plot_data(data): "load matplotlib and show the user the data" import matplotlib ...do stuff with matplotlib... - And as we are talking about style, note that your else: pass isn't really necessary but it does make it explicitly clear that you are choosing not to do anything if the plot_data isn't 'yes'. Are those tabs you're using? Four spaces are preferred, according to the style guide. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] loading modules only when needed and PEP 008
Hello fellow Pythonistas, I have a question concerning import statements. My code uses matplotlib to plot the results of my calculations. Since I want to make this plotting functionality a optional feature I would like to import matplotlib only when needed because it takes a lot of time for a TKinter-GUI to start up. And this start-up time is even longer when matplotlib is imported. I am still in doubt because PEP 008 [1] states that all imports should come right at the beginning of the code. My current code goes something like: import sys import matplotlib ## ## do some calcs ## ... matplotlib.plot(mydata) a optimized version would be: import sys plot_data = 'yes' # option: yes/no if plot_data == 'yes': import matplotlib else: pass ## ## do some calcs ## ... if plot_data == 'yes': matplotlib.plot(mydata) else: pass How would you handle such a case? What is recommended in such a case? Does anyone have experience with this? Thanks for your suggestions in advance! Kind regards, Tim [1] PEP 8 -- Style Guide for Python Code - http://www.python.org/dev/peps/pep-0008/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor