Project on Python Programs

Added on - Sep 2019

Trusted by 2+ million users,
1000+ happy students everyday
Showing pages 1 to 4 of 12 pages
Project #3: Ride Across the RiverBackgroundWe saw in the previous project that our Python programs are capable ofconnecting to the "outside world" around them — to other programs runningon the same machine, or even to other programs running on different machinesin faraway places. This is a powerful thing for a program to be able to do,because it is no longer limited to taking its input from a user or from a filestored locally; its input is now potentially anything that's accessible via theInternet, making it possible to solve a vast array of new problems and processa much broader collection of information. Once you have the ability to connectyour programs to others, a whole new world opens up: suddenly, the idea thatyou should be able to write a program that combines, say, Google searchqueries, the Internet Movie Database, and your favorite social network to findpeople who like movies similar to the ones you like doesn't seem so far-fetched.But we also saw that getting programs to share information is tricky, for (atleast) two reasons. Firstly, there's a software engineering problem: A protocolhas to be designed that both programs can use to have their conversation.Secondly, there's a social problem: If the same person (or group of people)isn't writing both programs, it's necessary for them to agree on the protocolahead of time, then to implement it. This second problem has a potentiallycatastrophic effect on our ability to make things work — how could you everconvince Google to agree to use your protocol just to communicate with you?In practice, both of these problems are largely solved by the presenceofstandards, such as those defined by theWorld Wide Web Consortium.Standards help by providing detailed communication protocols whose detailshave already been hammered out, with the intention of handling the mostcommon set of needs that will arise in programs. This eliminates the need todesign one's own protocol (where the standard protocols will suffice, which ismore often than you might think) and allows programs to be combined inarbitrary ways; as long as they support the protocol, they've taken a big steptoward being able to interoperate with each other. What's more, standardprotocols often have standard implementations, so that you won't have to codeup the details yourself as you did in the previous project. For example, Pythonhas built-in support for a number of standard Internet protocols, includingHTTP (HyperText Transfer Protocol, the protocol that your browser uses todownload web pages) among others.At first blush, HTTP doesn't seem all that important. It appears to be aprotocol that will allow you to write programs that download web pages (i.e.,that allow you to write programs that play the same role that web browsersdo). But it turns out that HTTP is a lot more important thanthat,since it is the
protocol that underlies a much wider variety of traffic on the Internet than youmight first imagine. This is not limited only to the conversation that yourbrowser haswitha web server in order to download a web page, though thatconversation most often uses HTTP (or its more secure variant, HTTPS).HTTP also underlies a growing variety of program-to-programcommunications using web protocols, whereweb sitesor other softwaresystems communicate directly with what are broadly calledweb services,fetching data and affecting change. This is why you can post tweets to Twitterusing either theirweb site, a client application on your laptop, or a smartphoneapp; all of these applications use the same protocol to communicate with theTwitter service, differing only in the form of user interface they provide.Fortunately, since HTTP support is built directly into Python, we can writeprograms that use these web services without having to handle low-leveldetails of the protocol, though there are some details that you'll need to befamiliar with if you want to use the provided implementation effectively. We'llbe discussing some of these details in lecture soon, and these will beaccompanied by acode example, which will give you some background in thetools you'll need to solve these kinds of problems in Python.This project gives you the opportunity to explore a small part of the vast sea ofpossibilities presented by web APIs and web services. You'll likely find thatyou spend a lot of your time in this project understanding the web API you'llneed — being able to navigate technical documentation and gradually build anunderstanding of another system is a vital skill in building real software — andthat the amount of code you need isn't nearly what you might expect when youfirst read the project write-up. As always, work incrementally rather thantrying to work on the entire project all at once. When you're done, you'll havetaken a valuable step toward being able to build Python programs that interactwith web services, which opens up your ability to write programs for yourselfthat are real and useful.Additionally, you'll get what might be your first experience with writingclasses in Python, which will broaden your ability to write clean, expressivePython programs, a topic we'll continue revisiting and refining throughout therest of this course.The problem, in generalIn your work on this project, you'll write a program that is capable ofdisplaying information about a trip from one location to another (e.g., drivingdirections between two street addresses). For example, you might display turn-by-turn directions, an estimate of how long it might take to get from onelocation to another, and so on. You'll use real-world map data — real cities,real streets — to solve your problem. So, when you're done, your program willbe a very simple navigation system, not entirely unlike the ones you see on
some smartphones or in some cars. If you want to know how to drive fromBren Hall at UCI to Staples Center in Los Angeles, your program will be ableto tell you.That may sound like something that is well beyond your current skill leveland/or time you have available, that you're being asked to build a complete,professional system that would ordinarily be written by a large team of peopleover a period of months. And, indeed, if you had to write the entire systemfrom scratch, that would certainly be true; it would take even a seasonedprofessional a lot longer than the time allotted to complete a task like that, andwould require skills well beyond what's been taught in your coursework todate.But there is good news here: We operate in an interconnected world, whereinformation of all kinds is available to us in web browsers, smartphoneapplications, and so on. And we're fortunate that a lot of that information isavailable for free, not just in a way that lets us view it in a web browser, but ina way that makes it available to the programs we write. Provided that we canfind an online service that provides the information we need, and provided thatwe're licensed to use it — either because it's free and we meet the terms of use,or because we're willing to pay for the privilege — we can use it to solve ourproblem.As it turns out, the information we need to solve this particular problem isavailable online, in a form that can be consumed by a Python program, andlicensed in a way that lets us use it (because it is free for non-commercial use).The only trick is figuring out how to get the information into our program.Luckily for us, the service that provides this information uses standardprotocols and formats that are common on the Internet; even better, all of theseprotocols and formats are implemented already in Python's standard library, sothe low-level details will not be our concern, and we can focus on the moreinteresting parts of the problem.The MapQuest Open Data APIsMapQuest is a company that is in the business of providing online services fordisplaying maps, providing directions, reporting on current traffic conditions,and other related services. While they're a for-profit company, some of theirservices are provided free of charge for non-commercial use. As long as you'renot building a product from which you'll be trying to make money, you canuse MapQuest's free services, with the one additional caveat that you have tofollow the rules laid out in their license. These kinds of license restrictions areno joke, so we'll spend a little time to be sure we're taking them seriously.For our work on this project, you'll be concerned with two parts of the OpenMapQuest API, provided by a company called MapQuest; we'll need both theOpen Directions Service and the Open Elevation Service. Both of these are
web-based APIs, similar to the one we used in thecode examplein which wedownloaded and displayed information about YouTube videos. LikeYouTube's API, the Open MapQuest API uses HTTP, with queries describedusing a URL, and with responses returned in JSON format. Fortunately, all ofthese technologies are supported in Python's standard library, so most of thedetails are things that will be handled for us automatically, but, as we saw inthe YouTube example, there are some things we need to get right, and not allof the detailswilbe the same in the MapQuest example as they were in theYouTube example, so it'll be vital for you to understandwhywe did the thingswe did in thecode example, so you can know whether and where the sametechniques apply.The two APIs you'll need are described in detail at the links below. Youcertainly won't need to read all of the documentation, but you'll want to take alook around and familiarize yourself with what the API cando,because part ofyour goal in this project is to decide what parts of the API you'll need to solveyour problem.MapQuest Open Directions Service documentationMapQuest Open Elevation Service documentationCreating an account and getting an AppKeyLike YouTube's API that we saw in class, MapQuest's API requires anAPIKey, which links your usage of the API to an account and authorizes you touse the API. Before you can make use of the API, you'll need to obtain yourown API Key — and due to usage restrictions, we won't all be able to sharethe same key, so each of you will need to take this step.Do not share yourAPI Key with other students!You'll only need to do this once, and you'll beable to use your API Key for all of your work on this project once it's beencreated. And, don't worry, obtaining the API Key is free for non-commercialuses like ours.Visit theMapQuest Developersite in your browser.Part of the way down the page, you'll see a blue button that saysGet YourFree API Key. Click that button.A form will be displayed, in which you can choose a username, a password,and so on. Fill in the necessary information, and be sure to use an emailaddress that you have access to; you'll need to receive emails fromMapQuest along the way.Once you've created your account, you'll be logged in and presented withsome choices, one of which isKeys & Reporting. Click that.You'll then see a green button that saysCreate a New App. Click that,because you're going to be writing an application that uses the OpenMapQuest APIs.When asked, supply anApp Name(maybeICS 32 Project 3would be agood choice). TheCallback URLis not important forus,since we're not
Desklib Logo
You are reading a preview
Upload your documents to download or

Become a Desklib member to get access