forked from AbhishekMali21/PYTHON-FOR-EVERYBODY
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
85d6323
commit e3eac3b
Showing
1 changed file
with
233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# CHAPTER 13 exercises" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"**Exercise 1: Change either the www.py4e.com/code3/geojson.py or www.py4e.com/code3/geoxml.py\n", | ||
"to print out the two-character country code from the retrieved data. Add error\n", | ||
"checking so your program does not traceback if the country code is not there.\n", | ||
"Once you have it working, search for “Atlantic Ocean” and make sure it can\n", | ||
"handle locations that are not in any country.**" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Enter location: Atlantic Ocean\n", | ||
"Retrieving http://py4e-data.dr-chuck.net/json?address=Atlantic+Ocean&key=42\n", | ||
"Retrieved 1228 characters\n", | ||
"{\n", | ||
" \"results\": [\n", | ||
" {\n", | ||
" \"address_components\": [\n", | ||
" {\n", | ||
" \"long_name\": \"Atlantic Ocean\",\n", | ||
" \"short_name\": \"Atlantic Ocean\",\n", | ||
" \"types\": [\n", | ||
" \"establishment\",\n", | ||
" \"natural_feature\"\n", | ||
" ]\n", | ||
" }\n", | ||
" ],\n", | ||
" \"formatted_address\": \"Atlantic Ocean\",\n", | ||
" \"geometry\": {\n", | ||
" \"bounds\": {\n", | ||
" \"northeast\": {\n", | ||
" \"lat\": 68.1962901,\n", | ||
" \"lng\": 19.9970779\n", | ||
" },\n", | ||
" \"southwest\": {\n", | ||
" \"lat\": -75.9725902,\n", | ||
" \"lng\": -82.9931606\n", | ||
" }\n", | ||
" },\n", | ||
" \"location\": {\n", | ||
" \"lat\": -14.5994134,\n", | ||
" \"lng\": -28.6731465\n", | ||
" },\n", | ||
" \"location_type\": \"APPROXIMATE\",\n", | ||
" \"viewport\": {\n", | ||
" \"northeast\": {\n", | ||
" \"lat\": 68.1962901,\n", | ||
" \"lng\": 19.9970779\n", | ||
" },\n", | ||
" \"southwest\": {\n", | ||
" \"lat\": -75.9725902,\n", | ||
" \"lng\": -82.9931606\n", | ||
" }\n", | ||
" }\n", | ||
" },\n", | ||
" \"place_id\": \"ChIJ_7hu48qBWgYRT1MQ81ciNKY\",\n", | ||
" \"types\": [\n", | ||
" \"establishment\",\n", | ||
" \"natural_feature\"\n", | ||
" ]\n", | ||
" }\n", | ||
" ],\n", | ||
" \"status\": \"OK\"\n", | ||
"}\n", | ||
"lat -14.5994134 lng -28.6731465 code N/A\n", | ||
"Atlantic Ocean\n", | ||
"Enter location: Antartica\n", | ||
"Retrieving http://py4e-data.dr-chuck.net/json?address=Antartica&key=42\n", | ||
"Retrieved 1381 characters\n", | ||
"{\n", | ||
" \"results\": [\n", | ||
" {\n", | ||
" \"address_components\": [\n", | ||
" {\n", | ||
" \"long_name\": \"Antarctica\",\n", | ||
" \"short_name\": \"Antarctica\",\n", | ||
" \"types\": [\n", | ||
" \"continent\",\n", | ||
" \"establishment\",\n", | ||
" \"natural_feature\"\n", | ||
" ]\n", | ||
" },\n", | ||
" {\n", | ||
" \"long_name\": \"Antarctica\",\n", | ||
" \"short_name\": \"AQ\",\n", | ||
" \"types\": [\n", | ||
" \"country\",\n", | ||
" \"political\"\n", | ||
" ]\n", | ||
" }\n", | ||
" ],\n", | ||
" \"formatted_address\": \"Antarctica\",\n", | ||
" \"geometry\": {\n", | ||
" \"bounds\": {\n", | ||
" \"northeast\": {\n", | ||
" \"lat\": -60.1086999,\n", | ||
" \"lng\": 180\n", | ||
" },\n", | ||
" \"southwest\": {\n", | ||
" \"lat\": -90,\n", | ||
" \"lng\": -180\n", | ||
" }\n", | ||
" },\n", | ||
" \"location\": {\n", | ||
" \"lat\": -82.862752,\n", | ||
" \"lng\": 135\n", | ||
" },\n", | ||
" \"location_type\": \"APPROXIMATE\",\n", | ||
" \"viewport\": {\n", | ||
" \"northeast\": {\n", | ||
" \"lat\": -28.2602827,\n", | ||
" \"lng\": 130.581566\n", | ||
" },\n", | ||
" \"southwest\": {\n", | ||
" \"lat\": -89.34771769999999,\n", | ||
" \"lng\": -155.2387406\n", | ||
" }\n", | ||
" }\n", | ||
" },\n", | ||
" \"place_id\": \"ChIJ4Ql4Koj_nbARx8fIXTgKjbA\",\n", | ||
" \"types\": [\n", | ||
" \"continent\",\n", | ||
" \"establishment\",\n", | ||
" \"natural_feature\"\n", | ||
" ]\n", | ||
" }\n", | ||
" ],\n", | ||
" \"status\": \"OK\"\n", | ||
"}\n", | ||
"lat -82.862752 lng 135 code N/A\n", | ||
"Antarctica\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import urllib.request, urllib.parse, urllib.error\n", | ||
"import json\n", | ||
"import ssl\n", | ||
"api_key = False\n", | ||
"\n", | ||
"# If you have a Google Places API key, enter it here\n", | ||
"# api_key = 'AIzaSy___IDByT70'\n", | ||
"# https://developers.google.com/maps/documentation/geocoding/intro\n", | ||
"\n", | ||
"if api_key is False:\n", | ||
" api_key = 42\n", | ||
" serviceurl = 'http://py4e-data.dr-chuck.net/json?'\n", | ||
"else :\n", | ||
" serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'\n", | ||
"\n", | ||
"# Ignore SSL certificate errors\n", | ||
"ctx = ssl.create_default_context()\n", | ||
"ctx.check_hostname = False\n", | ||
"ctx.verify_mode = ssl.CERT_NONE\n", | ||
"\n", | ||
"while True:\n", | ||
" address = input('Enter location: ')\n", | ||
" if len(address) < 1: break\n", | ||
"\n", | ||
" parms = dict()\n", | ||
" parms['address'] = address\n", | ||
" if api_key is not False: parms['key'] = api_key\n", | ||
" url = serviceurl + urllib.parse.urlencode(parms)\n", | ||
"\n", | ||
" print('Retrieving', url)\n", | ||
" uh = urllib.request.urlopen(url, context=ctx)\n", | ||
" data = uh.read().decode()\n", | ||
" print('Retrieved', len(data), 'characters')\n", | ||
"\n", | ||
" try:\n", | ||
" js = json.loads(data)\n", | ||
" except:\n", | ||
" js = None\n", | ||
" \n", | ||
" if not js or 'status' not in js or js['status'] != 'OK':\n", | ||
" print('==== Failure To Retrieve ====')\n", | ||
" print(data)\n", | ||
" continue\n", | ||
"\n", | ||
" print(json.dumps(js, indent=4))\n", | ||
"\n", | ||
" lat = js['results'][0]['geometry']['location']['lat']\n", | ||
" lng = js['results'][0]['geometry']['location']['lng']\n", | ||
" try:\n", | ||
" shname = js['results'][0]['address_components'][3]['short_name']\n", | ||
" except:\n", | ||
" shname = 'N/A'\n", | ||
" print('lat', lat, 'lng', lng, 'code', shname)\n", | ||
" location = js['results'][0]['formatted_address']\n", | ||
" print(location)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |