Chapter 5. Business-oriented programming

 

This chapter covers

  • Writing programs for the real world
  • Interacting with existing systems
  • How to handle errors in your program

In this chapter, we’re going to take a look at how Python can be used in the real world to help you to do your job better and faster. As a sample project, you’ll take some stock data from a web page on the internet, extract the figures you’re interested in, and then see how you can make use of that data, report on it, and send those reports to interested parties. To make your life easier, all of this will be written so it’s easy to automate.

One of the critical tasks facing many programmers and system administrators is to make many different systems talk to each other. You might need to do the following:

  • Read some data from one system
  • Compare it with the results from a second
  • Check that both of them make sense (often referred to as a sanity check)
  • Save the results for later use
  • Email relevant people with a report about what you found or any problems you encountered

People are depending on the information from these systems, so whatever you write has to be robust. You can’t try something and hope for the best. Sound daunting? Don’t worry—Python is at its heart a practical language, and has a number of features and library modules to make interfacing with the real world and all of its quirks much easier.

Making programs talk to each other

Getting started

Downloading the page with Python

Writing out to a CSV file

Emailing the CSV file

A simple script—what could possibly go wrong?

How to deal with breaking scripts

Exceptions

Where to from here?

Summary