9. Library time
¶
Current time in seconds
time.clock()
Am I in daylight saving time?
time.daylight()
Sleep for a few seconds,
time.sleep(seconds)
Timezone
time.timezone()
9.1. 10 seconds between each access¶
from urllib.request import urlopen
import time
# This is used to increase the program duration to 3 minutes.
import sys
sys.setExecutionLimit(180000)
LOYALTY_PRICES_URL = "https://api.allorigins.win/raw?url=http://beans.itcarlow.ie/prices-loyalty.html"
price = 99.99
limit = 0
availability = True
while price >= 4.74 or limit <=15:
try:
page = urlopen(LOYALTY_PRICES_URL)
except:
print("URL not available at this time.")
availability = False
break
page = urlopen(LOYALTY_PRICES_URL)
text = page.read()
location = text.find(">$")
start = location + 2
end = start + 4
price = float(text[start:end])
Activity: 9.1.1 ActiveCode (ac_l33_9_en)
9.2. Summary¶
Strings are character strings.
We access individual characters by their index, starting at zero.
Methods are functions built into variables.
There are programming libraries with ready-made code.
Data has a type, such as
int
orstring
.
9.3. Python Tools¶
text[4]
accesses the fifth character.text[4:9]
accesses from the fifth to the ninth character.The method
text.find()
searches for a substring.float()
converts something to a floating point number.Libraries:
urllib.request
andtime
.
Activity: 9.3.1 Poll (TWP33E)