Automate the Boring Stuff with Python

Home > Other > Automate the Boring Stuff with Python > Page 46
Automate the Boring Stuff with Python Page 46

by Al Sweigart


  You can combine all of these PyAutoGUI features to automate any mindlessly repetitive task on your computer. In fact, it can be downright hypnotic to watch the mouse cursor move on its own and see text appear on the screen automatically. Why not spend the time you saved by sitting back and watching your program do all your work for you? There’s a certain satisfaction that comes from seeing how your cleverness has saved you from the boring stuff.

  Practice Questions

  Q:

  1. How can you trigger PyAutoGUI’s fail safe to stop a program?

  Q:

  2. What function returns the current resolution()?

  Q:

  3. What function returns the coordinates for the mouse cursor’s current position?

  Q:

  4. What is the difference between pyautogui.moveTo() and pyautogui.moveRel()?

  Q:

  5. What functions can be used to drag the mouse?

  Q:

  6. What function call will type out the characters of "Hello world!"?

  Q:

  7. How can you do keypresses for special keys such as the keyboard’s left arrow key?

  Q:

  8. How can you save the current contents of the screen to an image file named screenshot.png?

  Q:

  9. What code would set a two second pause after every PyAutoGUI function call?

  Practice Projects

  For practice, write programs that do the following.

  Looking Busy

  Many instant messaging programs determine whether you are idle, or away from your computer, by detecting a lack of mouse movement over some period of time—say, ten minutes. Maybe you’d like to sneak away from your desk for a while but don’t want others to see your instant messenger status go into idle mode. Write a script to nudge your mouse cursor slightly every ten seconds. The nudge should be small enough so that it won’t get in the way if you do happen to need to use your computer while the script is running.

  Instant Messenger Bot

  Google Talk, Skype, Yahoo Messenger, AIM, and other instant messaging applications often use proprietary protocols that make it difficult for others to write Python modules that can interact with these programs. But even these proprietary protocols can’t stop you from writing a GUI automation tool.

  The Google Talk application has a search bar that lets you enter a username on your friend list and open a messaging window when you press ENTER. The keyboard focus automatically moves to the new window. Other instant messenger applications have similar ways to open new message windows. Write a program that will automatically send out a notification message to a select group of people on your friend list. Your program may have to deal with exceptional cases, such as friends being offline, the chat window appearing at different coordinates on the screen, or confirmation boxes that interrupt your messaging. Your program will have to take screen-shots to guide its GUI interaction and adopt ways of detecting when its virtual keystrokes aren’t being sent.

  Note

  You may want to set up some fake test accounts so that you don’t accidentally spam your real friends while writing this program.

  Game-Playing Bot Tutorial

  There is a great tutorial titled “How to Build a Python Bot That Can Play Web Games” that you can find at http://nostarch.com/automatestuff/. This tutorial explains how to create a GUI automation program in Python that plays a Flash game called Sushi Go Round. The game involves clicking the correct ingredient buttons to fill customers’ sushi orders. The faster you fill orders without mistakes, the more points you get. This is a perfectly suited task for a GUI automation program—and a way to cheat to a high score! The tutorial covers many of the same topics that this chapter covers but also includes descriptions of PyAutoGUI’s basic image recognition features.

  Appendix A. Installing Third-Party Modules

  Beyond the standard library of modules packaged with Python, other developers have written their own modules to extend Python’s capabilities even further. The primary way to install third-party modules is to use Python’s pip tool. This tool securely downloads and installs Python modules onto your computer from https://pypi.python.org/, the website of the Python Software Foundation. PyPI, or the Python Package Index, is a sort of free app store for Python modules.

  The pip Tool

  The executable file for the pip tool is called pip on Windows and pip3 on OS X and Linux. On Windows, you can find pip at C:Python34Scriptspip.exe. On OS X, it is in /Library/Frameworks/Python.framework/Versions/3.4/bin/pip3. On Linux, it is in /usr/bin/pip3.

  While pip comes automatically installed with Python 3.4 on Windows and OS X, you must install it separately on Linux. To install pip3 on Ubuntu or Debian Linux, open a new Terminal window and enter sudo apt-get install python3-pip. To install pip3 on Fedora Linux, enter sudo yum install python3 -pip into a Terminal window. You will need to enter the administrator password for your computer in order to install this software.

  Installing Third-Party Modules

  The pip tool is meant to be run from the command line: You pass it the command install followed by the name of the module you want to install. For example, on Windows you would enter pip install ModuleName, where ModuleName is the name of the module. On OS X and Linux, you’ll have to run pip3 with the sudo prefix to grant administrative privileges to install the module. You would need to type sudo pip3 install ModuleName.

  If you already have the module installed but would like to upgrade it to the latest version available on PyPI, run pip install –U ModuleName (or pip3 install –U ModuleName on OS X and Linux).

  After installing the module, you can test that it installed successfully by running import ModuleName in the interactive shell. If no error messages are displayed, you can assume the module was installed successfully.

  You can install all of the modules covered in this book by running the commands listed next. (Remember to replace pip with pip3 if you’re on OS X or Linux.)

  pip install send2trash

  pip install requests

  pip install beautifulsoup4

  pip install selenium

  pip install openpyxl

  pip install PyPDF2

  pip install python-docx (install python-docx, not docx)

  pip install imapclient

  pip install pyzmail

  pip install twilio

  pip install pillow

  pip install pyobjc-core (on OS X only)

  pip install pyobjc (on OS X only)

  pip install python3-xlib (on Linux only)

  pip install pyautogui

  Note

  For OS X users: The pyobjc module can take 20 minutes or longer to install, so don’t be alarmed if it takes a while. You should also install the pyobjc-core module first, which will reduce the overall installation time.

  Appendix B. Running Programs

  If you have a program open in IDLE’s file editor, running it is a simple matter of pressing F5 or selecting the Run▸Run Module menu item. This is an easy way to run programs while writing them, but opening IDLE to run your finished programs can be a burden. There are more convenient ways to execute Python scripts.

  Shebang Line

  The first line of all your Python programs should be a shebang line, which tells your computer that you want Python to execute this program. The shebang line begins with #!, but the rest depends on your operating system.

  On Windows, the shebang line is #! python3.

  On OS X, the shebang line is #! /usr/bin/env python3.

  On Linux, the shebang line is #! /usr/bin/python3.

  You will be able to run Python scripts from IDLE without the shebang line, but the line is needed to run them from the command line.

  Running Python Programs on Windows

  On Windows, the Python 3.4 interpreter is located at C:Python34python.exe. Alternatively, the convenient py.exe program will read the shebang line at the top of the .py file’s source code and run the appropriate version of Pyth
on for that script. The py.exe program will make sure to run the Python program with the correct version of Python if multiple versions are installed on your computer.

  To make it convenient to run your Python program, create a .bat batch file for running the Python program with py.exe. To make a batch file, make a new text file containing a single line like the following:

  @py.exe C:pathtoyourpythonScript.py %*

  Replace this path with the absolute path to your own program, and save this file with a .bat file extension (for example, pythonScript.bat). This batch file will keep you from having to type the full absolute path for the Python program every time you want to run it. I recommend you place all your batch and .py files in a single folder, such as C:MyPythonScripts or C:UsersYourNamePythonScripts.

  The C:MyPythonScripts folder should be added to the system path on Windows so that you can run the batch files in it from the Run dialog. To do this, modify the PATH environment variable. Click the Start button and type Edit environment variables for your account. This option should auto-complete after you’ve begun to type it. The Environment Variables window that appears will look like Figure B-1.

  From System variables, select the Path variable and click Edit. In the Value text field, append a semicolon, type C:MyPythonScripts, and then click OK. Now you can run any Python script in the C:MyPythonScripts folder by simply pressing WIN-R and entering the script’s name. Running pythonScript, for instance, will run pythonScript.bat, which in turn will save you from having to run the whole command py.exe C: MyPythonScriptspythonScript.py from the Run dialog.

  Figure B-1. The Environment Variables window on Windows

  Running Python Programs on OS X and Linux

  On OS X, selecting Applications▸Utilities▸Terminal will bring up a Terminal window. A Terminal window is a way to enter commands on your computer using only text, rather than clicking through a graphic interface. To bring up the Terminal window on Ubuntu Linux, press the WIN (or SUPER) key to bring up Dash and type in Terminal.

  The Terminal window will begin in the home folder of your user account. If my username is asweigart, the home folder will be /Users/asweigart on OS X and /home/asweigart on Linux. The tilde (~) character is a shortcut for your home folder, so you can enter cd ~ to change to your home folder. You can also use the cd command to change the current working directory to any other directory. On both OS X and Linux, the pwd command will print the current working directory.

  To run your Python programs, save your .py file to your home folder. Then, change the .py file’s permissions to make it executable by running chmod +x pythonScript.py. File permissions are beyond the scope of this book, but you will need to run this command on your Python file if you want to run the program from the Terminal window. Once you do so, you will be able to run your script whenever you want by opening a Terminal window and entering ./pythonScript.py. The shebang line at the top of the script will tell the operating system where to locate the Python interpreter.

  Running Python Programs with Assertions Disabled

  You can disable the assert statements in your Python programs for a slight performance improvement. When running Python from the terminal, include the -O switch after python or python3 and before the name of the .py file. This will run an optimized version of your program that skips the assertion checks.

  Appendix C. Answers to the Practice Questions

  This appendix contains the answers to the practice problems at the end of each chapter. I highly recommend that you take the time to work through these problems. Programming is more than memorizing syntax and a list of function names. As when learning a foreign language, the more practice you put into it, the more you will get out of it. There are many websites with practice programming problems as well. You can find a list of these at http://nostarch.com/automatestuff/.

  Chapter 1

  The operators are +, -, *, and /. The values are 'hello', -88.8, and 5.

  The string is 'spam'; the variable is spam. Strings always start and end with quotes.

  The three data types introduced in this chapter are integers, floating-point numbers, and strings.

  An expression is a combination of values and operators. All expressions evaluate (that is, reduce) to a single value.

  An expression evaluates to a single value. A statement does not.

  The bacon variable is set to 20. The bacon + 1 expression does not reassign the value in bacon (that would need an assignment statement: bacon = bacon + 1).

  Both expressions evaluate to the string 'spamspamspam'.

  Variable names cannot begin with a number.

  The int(), float(), and str() functions will evaluate to the integer, floating-point number, and string versions of the value passed to them.

  The expression causes an error because 99 is an integer, and only strings can be concatenated to other strings with the + operator. The correct way is I have eaten ' + str(99) + ' burritos.'.

  Chapter 2

  True and False, using capital T and F, with the rest of the word in lowercase

  and, or, and not

  True and True is True.

  True and False is False.

  False and True is False.

  False and False is False.

  True or True is True.

  True or False is True.

  False or True is True.

  False or False is False.

  not True is False.

  not False is True.

  False False True False False True

  ==, !=, <, >, <=, and >=.

  == is the equal to operator that compares two values and evaluates to a Boolean, while = is the assignment operator that stores a value in a variable.

  A condition is an expression used in a flow control statement that evaluates to a Boolean value.

  The three blocks are everything inside the if statement and the lines print('bacon') and print('ham').

  print('eggs') if spam > 5: print('bacon') else: print('ham') print('spam')

  The code:

  if spam == 1: print('Hello') elif spam == 2: print('Howdy') else: print('Greetings!')

  Press CTRL-C to stop a program stuck in an infinite loop.

  The break statement will move the execution outside and just after a loop. The continue statement will move the execution to the start of the loop.

  They all do the same thing. The range(10) call ranges from 0 up to (but not including) 10, range(0, 10) explicitly tells the loop to start at 0, and range(0, 10, 1) explicitly tells the loop to increase the variable by 1 on each iteration.

  The code:

  for i in range(1, 11): print(i)

  and:

  i = 1 while i <= 10: print(i) i = i + 1

  This function can be called with spam.bacon().

  Chapter 3

  Functions reduce the need for duplicate code. This makes programs shorter, easier to read, and easier to update.

  The code in a function executes when the function is called, not when the function is defined.

  The def statement defines (that is, creates) a function.

  A function consists of the def statement and the code in its def clause.

  A function call is what moves the program execution into the function, and the function call evaluates to the function’s return value.

  There is one global scope, and a local scope is created whenever a function is called.

  When a function returns, the local scope is destroyed, and all the variables in it are forgotten.

  A return value is the value that a function call evaluates to. Like any value, a return value can be used as part of an expression.

  If there is no return statement for a function, its return value is None.

  A global statement will force a variable in a function to refer to the global variable.

  The data type of None is NoneType.

  That import statement imports a module named areallyourpetsnamederic. (This isn’t a real Python module, by the way.)

&nb
sp; This function can be called with spam.bacon().

 

‹ Prev