Hardware Reference
In-Depth Information
Command-Line Options and Arguments
When you run a Python program from the command line, you can follow it with a list of argu-
ments. This provides an easy way to pass data into a program at startup and is typically used to
pass names of files or configuration 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.
You check if any command-line arguments were used by checking the length (that is, the
number of items) in sys.argv . If there's at least one, then you set the search_term to be
the first argument:
# has user passed command line argument?
if len(sys.argv) > 1:
search_term = sys.argv[1]
else:
search_term = DEFAULT_SEARCH_TERM
Finally, you enter the main loop - a block of code that will keep going round in a loop, run-
ning again and again. This is a while loop, which is discussed in Appendix B.
In this program you use the condition True , to make it loop indefinitely. To stop the program,
press Ctrl + C. In Linux, this keyboard combination sends a message from the operating 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 2-5. The 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 1 to check that the toy
moves when the relay is turned on.
 
Search WWH ::




Custom Search