Youre already using library code

Look at the first line of the original code:

we are

I« LSe Code h A Ue urllî^uest code ■the ^uet" library

The import line tells Python that you intend to use some library code called urllib . request. A library is a bunch of prewritten code that you can use in your own programs. In this case, the urllib . request code accesses data on the Web. This is a library that comes as standard with Python.

To see how the code is used, look at this line:

Library name.

£.ery library tonla.ns

Wtio*-that r* tan use in your own ^am

The code that follows the = sign is calling a function in urllib . request called urlopen () . Notice how we say we want the code: urllib . request, followed by a ".", then the name of the function.

we are

But how will the time library help us? Let's see...

find the time

ExidâSe

These are some of the functions provided by Python's built-in time library:

Python Library Documentation

: time time.clock^ ^ seMnds, given as a floating point number.

time.daTyhl^s9hrte(turns 0 if you are not currently in Daylight Savings Time.

time.9mtime() utc date and time (not affected

Tells you current UlC date a by the timezone).

time.localtime() .ime (is affected by

Tells you the current local time (is af your timezone).

time.SlDeoenP't edCS ) anything for the specified. number of seconds.

^Ïlls you the number of seconds since January 1st 1970.

time.timezone() hnnrs difference between ls you the number of hours differe Tells you t e the utc timezone (London).

your timezone and the UTC

You need to use one of these functions to help you fix your code.

But which one? Draw a circle around the function you think you might need.

With the appropriate function identified, amend the code to control how often the request for the web page is sent to the server. The Beans'R'Us webmaster has been in touch to say that their web-based pricing information is updated every 15 minutes. Fill in the blanks in the code as indicated by the dashed lines.

Hints: 15 minutes equates to 15 multiplied by 60 seconds, which is 900 seconds. Also: to use the functionality provided by a library, remember to import it first.

import urllib.request price = 99.99 while price > 4.74:

page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html") text = page.read().decode("utf8") where = text.find('>$') start_of_price = where + 2 end_of_price = start_of_price + 4 price = float(text[start_of_price:end_of_price]) print ("Buy!")

time is on our side m

ExcRciSe %0Lvt\0 H

These are some of the functions provided by Python's built-in time library:

Python Library Documentation:

time. clock time in seconds, given as a floating point number.

time.daTyhlis9hrte(^Urns 0 if you are not currently in Daylight Savings Time.

} you current UTC date and time (not affected by the timezone).

time.localtime() time (is affected by

Tells you the current local time (is af your timezone). time.sleep(secs)

Don't do anything for the specified number of time.time () oeconds since January 1st,

This looks ^ Tells you the number of seconds si

1970.

time.timezone() ,ours difference between

T^lls you the number °f hours dife Tells you e .nH the UTC timezone (London). your timezone and the UTC

You need to use one of these functions to help you fix your code.

But which one? You were to draw a circle around the function you thought you might need.

With the appropriate function identified, you were to amend the code to control how often the request for the web page is sent to the server. The Beans'R'Us webmaster has been in touch to say that their web-based pricing information is updated every 15 minutes.You were to fill in the blanks in the code as indicated by the dashed lines.

Hints: 15 minutes equates to 15 multiplied by 60 seconds, which is 900 seconds. Also: to use the functionality provided by a library, remember to import it first.

page = urllib.request.urlopen("http://www.beans-r-us.biz/prices.html") text = page.read().decode("utf8") where = text.find('>$') start_of_price = where + 2 end_of_price = start_of_price + 4 price = float(text[start_of_price:end_of_price]) print ("Buy!")

I „-J. 4-u, I.wav-V at the top import urllib.request import time import urllib.request import time price = qq.qq whil

coffee all around

Was this article helpful?

0 0

Post a comment