Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a1f6bfc

Browse files
Add files via upload
1 parent 7834902 commit a1f6bfc

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

‎api.py‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Working with API in Python and web resources
2+
3+
import requests # Importing library for doing requests
4+
import json # Importing library to work with json' data format
5+
6+
# Web resource openweathermap.org to get weather forecast
7+
# Url for sending requests to
8+
api_url = 'http://api.openweathermap.org/data/2.5/weather'
9+
10+
# Asking the city name to be found and represented with weather forecast
11+
city = input('City name, please ')
12+
13+
# Setting parameters for the request
14+
params = {
15+
'q': city,
16+
'appid': '11c0d3dc6093f7442898ee49d2430d20', # It's needed to sing up to get personal one
17+
'units': 'metric' # Saying that we need to get data in 'metric' standard and show temperature in Celsius
18+
}
19+
20+
# Creating a request
21+
respond = requests.get(api_url, params=params)
22+
23+
# Asking for the status
24+
print(respond.status_code)
25+
26+
# Asking for the content type
27+
print(respond.headers['Content-Type'])
28+
29+
# Getting the Python object as dictionary from json data in two ways
30+
# Option 1 - Getting the dictionary from json data with 'requests' library
31+
dic_from_json_1 = respond.json()
32+
print(dic_from_json_1)
33+
34+
# Option 2 - Getting the dictionary from json data with 'json' library
35+
dic_from_json_2 = json.loads(respond.text)
36+
print(dic_from_json_2)
37+
38+
# All data that is received above contains key and the description can be found here
39+
# https://openweathermap.org/current#current_JSON
40+
41+
# Showing some results from dictionary
42+
data = dic_from_json_1
43+
template = 'Current temperature in {} is {}'
44+
print(template.format(city, data['main']['temp']))
45+
46+
47+
48+
49+
# Implementing the task
50+
# Finding information about numbers from web resource numbersapi.com
51+
# Reading numbers from the file and writing them into the list
52+
n = []
53+
with open('test_numbers.txt') as f:
54+
for line in f:
55+
n += [line.rstrip()]
56+
57+
# Url for sending requests to
58+
api_url = 'http://numbersapi.com' # Don't forget to add the number itself '/n/' to the url when doing requests
59+
60+
# Setting parameters for the request
61+
params = {
62+
'type': 'math',
63+
'json': 'true'
64+
}
65+
66+
# Creating a requests and writing results in the dictionary
67+
d = {}
68+
for i in range(len(n)):
69+
respond = requests.get(api_url+'/'+n[i]+'/', params=params)
70+
71+
# Getting the dictionary from json data with 'json' library
72+
data = json.loads(respond.text)
73+
74+
# All data that is received above contains key and the description can be found here
75+
# http://numbersapi.com
76+
77+
# Writing results in the dictionary by the key 'found'
78+
if data['found'] == False:
79+
d[n[i]] = 'Boring'
80+
if data['found'] == True:
81+
d[n[i]] = 'Interesting'
82+
83+
# Writing results from dictionary in the new list in the same order they were found in the file
84+
results = []
85+
for i in range(len(n)):
86+
results += [d[n[i]]]
87+
88+
# Joining all the elements from the list with the symbol \n
89+
content = '\n'.join(results)
90+
91+
# Writing content in the new file
92+
with open('test_numbers_results.txt', 'w') as w:
93+
w.write(content)

‎test_numbers.txt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
994
2+
996
3+
965
4+
904
5+
969
6+
941
7+
913
8+
945
9+
916
10+
982
11+
951
12+
927
13+
924
14+
989
15+
959

‎test_numbers_results.txt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Interesting
2+
Boring
3+
Boring
4+
Boring
5+
Interesting
6+
Interesting
7+
Boring
8+
Interesting
9+
Interesting
10+
Interesting
11+
Interesting
12+
Interesting
13+
Interesting
14+
Interesting
15+
Boring

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /