Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

the necessary modifications to your script.


TABLE 23.2 Functions Used in Loaded Modules

To load only the functions you use from each module, you modify your import statements, using the
chart you’ve created as a guide. The import statements will now look similar to the following:


Click here to view code image


##### Import Functions & Variables #######
#
from os import listdir, system #Import from OS module
#
#Import from PyGame Library
from pygame import event, font, display, image, init, transform
#
from sys import exit #Import from System module

After you make this change, you need to change all the calls to the various functions. For example, you
change pygame.init() to init(), and you change sys.exit() to exit(). Python no
longer recognizes the entire pygame module because you no longer load it. Instead, it only
recognizes the functions you load from that module.


Use the chart you created and step through the Python script, making all the necessary changes to the
function calls. When you have completed these changes, you should test the Python image presentation
script. You will be amazed at how much faster it starts up! This is a good activity for any Python
script you write: Load up whole modules; tweak the script until you are happy with it; chart your
modules and their functions; modify the script to load only the functions; and modify the function
calls.


Removing Any Implemented Delays


This optimization is an easy one! For smaller, non-HD images, you needed to include the
pygame.time.delay operation to allow the images to “pause” on the screen. When loading the
large HD images, this pause is not needed, so you simply remove the line below from the script:


pygame.time.delay(500)

You also need to be sure to remove the loading of the time function in the pygame module’s
import statement.


Adding Buffering to the Screen


This speed fix will gain you only a millisecond or two, but implementing it is still worthwhile—and
it’s easy. You simply add an additional flag, DOUBLEBUF, to the flags for your presentation screen,
as shown here:


Click here to view code image


ScreenFlag = FULLSCREEN | NOFRAME | DOUBLEBUF
PrezScreen = display.set_mode((0,0),ScreenFlag)
Free download pdf