Hardware Reference
In-Depth Information
search_term = sys.argv[1]
else:
search_term = DEFAULT_SEARCH_TERM
Command-Line Options and Arguments
When you run a Python program from the command line, you can follow it with a list of
arguments. This provides an easy way to pass data into a program at startup and is typically
used to pass names of iles or coniguration options. For many programs, if you run it with
just the -h option, it will display simple help and summarise the options and arguments
available. To try this with espeak, type the following from a terminal to display a summary
of command-line options:
espeak -h
In the twitter_tag_listen.py example, because your program only takes one argu-
ment, you read it from the list held by sys.argv . However, as you begin to master Python
and your programs get more complicated, you may wish to use the argparse module that
splits up the arguments and can automate generating usage information.
Finally, you enter the main loop - a block of code that will keep going around in a loop, run-
ning again and again. his is a while loop, which is discussed in Chapter 2.
In this program you use the condition True , to make it loop indeinitely. To stop the pro-
gram, press Ctrl + C. In Linux, this keyboard combination sends a message from the operat-
ing system to interrupt the program, which in most cases will terminate it (cause it to stop
running).
With all the components written and tested, it's the moment of truth: Will they work
together?
Putting It All Together
Connect the wires from your toy to the common and normally open relay contact terminals
on PiFace Digital as shown in Figure 10-5. he code example in this chapter uses relay 0,
which is the bottom two terminals nearest to JP3 and the Raspberry Pi expansion pins. You
can use the manual override button in the emulator as described in Chapter 9 to check that
the toy moves when the relay is turned on.
 
Search WWH ::




Custom Search