Skip to content

Commit a41f49e

Browse files
author
Lily Ng
committed
Py4e final
0 parents  commit a41f49e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+152248
-0
lines changed

.gitignore

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Sample code
2+
hidden.py
3+
/pagerank
4+
5+
# Environments
6+
.env
7+
.venv
8+
env/
9+
venv/
10+
ENV/
11+
env.bak/
12+
venv.bak/
13+
14+
# Byte-compiled / optimized / DLL files
15+
__pycache__/
16+
*.py[cod]
17+
*$py.class
18+
*.pyc
19+
20+
# Docs
21+
pythonlearn.pdf
22+
Regex-cheatsheet.txt

13-1-geojson.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
print out the two-character country code from the retrieved data.
3+
Add error checking so your program does not traceback if
4+
the country code is not there. Once you have it working,
5+
search for “Atlantic Ocean” and make sure it can handle locations
6+
that are not in any country.
7+
"""
8+
9+
10+
import urllib.request, urllib.parse, urllib.error
11+
import json
12+
import ssl
13+
14+
api_key = False
15+
# If you have a Google Places API key, enter it here
16+
# api_key = 'AIzaSy___IDByT70'
17+
# https://developers.google.com/maps/documentation/geocoding/intro
18+
19+
if api_key is False:
20+
api_key = 42
21+
serviceurl = 'http://py4e-data.dr-chuck.net/json?'
22+
else :
23+
serviceurl = 'https://maps.googleapis.com/maps/api/geocode/json?'
24+
25+
# Ignore SSL certificate errors
26+
ctx = ssl.create_default_context()
27+
ctx.check_hostname = False
28+
ctx.verify_mode = ssl.CERT_NONE
29+
30+
while True:
31+
address = input('Enter location: ')
32+
if len(address) < 1: break
33+
34+
parms = dict()
35+
parms['address'] = address
36+
if api_key is not False: parms['key'] = api_key
37+
url = serviceurl + urllib.parse.urlencode(parms)
38+
39+
print('Retrieving', url)
40+
uh = urllib.request.urlopen(url, context=ctx)
41+
data = uh.read().decode()
42+
print('Retrieved', len(data), 'characters')
43+
44+
try:
45+
js = json.loads(data)
46+
except:
47+
js = None
48+
49+
if not js or 'status' not in js or js['status'] != 'OK':
50+
print('==== Failure To Retrieve ====')
51+
print(data)
52+
continue
53+
54+
print(json.dumps(js, indent=4))
55+
56+
counter = -1
57+
location = js['results'][0]['address_components']
58+
for item in location:
59+
counter += 1
60+
if js["results"][0]["address_components"][counter]["types"] == ['country', 'political']:
61+
print("Country code:", js["results"][0]["address_components"][counter]["short_name"])
62+
else:
63+
continue
64+

auth.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""
2+
Create auth() to retrieve token from .env
3+
"""
4+
5+
import os
6+
7+
def auth():
8+
return os.getenv('TOKEN')
9+
10+
def create_headers(bearer_token):
11+
headers = {"Authorization": "Bearer {}".format(bearer_token)}
12+
return headers
13+
14+
bearer_token = auth()
15+
headers = create_headers(bearer_token)

avgspamconf.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
fname = input("Enter file name: ")
2+
try:
3+
fhand = open(fname)
4+
except:
5+
print('File cannot be opened:', fname)
6+
exit()
7+
count = 0
8+
total_float_str = 0
9+
for line in fhand:
10+
line = line.rstrip()
11+
if not line.startswith("X-DSPAM-Confidence:"):
12+
continue
13+
else:
14+
count = count + 1
15+
split_str = line.split()
16+
line_str = split_str[1]
17+
float_str = float(line_str)
18+
total_float_str = total_float_str + float_str
19+
if count:
20+
avg_spam = total_float_str / count
21+
print("Average spam confidence: ", avg_spam)

data/cover3.jpg

225 KB
Loading

0 commit comments

Comments
 (0)