tuomasvaltanen

Untitled

Nov 24th, 2021 (edited)
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. # More complex datasets
  2.  
  3. # THIS IS WHERE WE LEFT OFF LAST TIME...
  4.  
  5. import urllib.request
  6. import json
  7.  
  8. # this module needs to be installed separately
  9. # in PyCharm you can install the package if its not found!
  10. import var_dump as vd
  11.  
  12. # get internet data
  13. url = 'https://edu.frostbit.fi/api/events/en'
  14. req = urllib.request.Request(url)
  15. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  16.  
  17. # the needed data from internet is now in the "data" -variable!
  18. data = json.loads(raw_data)
  19. # vd.var_dump(data)
  20.  
  21. # loop through each event we happen to have this time
  22. for event in data:
  23.     print(event['name'])
  24.     categories = ", ".join(event['categories'])
  25.     print(categories)
  26.     print()
  27.  
  28. # NEW VERSION
  29.  
  30. import urllib.request
  31. import json
  32.  
  33. # this module needs to be installed separately
  34. # in PyCharm you can install the package if its not found!
  35. import var_dump as vd
  36.  
  37. # get internet data
  38. url = 'https://edu.frostbit.fi/api/events/en'
  39. req = urllib.request.Request(url)
  40. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  41.  
  42. # the needed data from internet is now in the "data" -variable!
  43. data = json.loads(raw_data)
  44. # vd.var_dump(data)
  45.  
  46. # ask the user for a search word (a simple version of Google in this case)
  47. search_word = input("What kind of events you are looking for?\n")
  48.  
  49. for event in data:
  50.     address_text = event['address']['street_address']
  51.     postal_code = event['address']['postal_code']
  52.  
  53.     categories = ", ".join(event['categories'])
  54.  
  55.     # if the user's search word is not included in the event
  56.     # skip this event => continue
  57.     if search_word not in categories:
  58.         continue
  59.  
  60.     # if the search word is found, print normally
  61.     print(event['name'])
  62.     print(f"{address_text} {postal_code}")
  63.     print(categories)
  64.     print()
  65.  
  66. # the weather data example, NEW FILE
  67.  
  68. import json
  69. import urllib.request
  70. import var_dump as vd
  71.  
  72. url = "https://edu.frostbit.fi/api/weather/"
  73. req = urllib.request.Request(url)
  74. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  75. weather = json.loads(raw_data)
  76. vd.var_dump(weather)
  77.  
  78. # helper variables, MODIFY THESE IN THE LOOP, IF-statements etc.
  79. strongest_wind = 0
  80. weakest_wind = 0
  81. strongest_wind_city = ""
  82. weakest_wind_city = ""
  83.  
  84. # go through each city
  85. for city in weather:
  86.     print(city['location'])
  87.     print(city['wind'])
  88.     print()
  89.  
  90. # finally print the results as needed (check problem description for actual format)
  91. print(strongest_wind_city)
  92. print(strongest_wind)
  93.  
  94. # OUR FIRST FUNCTION
  95.  
  96. # functions.py -file:
  97.  
  98. # our functions file
  99. def print_title():
  100.     print("Welcome to our app!")
  101.     print("-------------------")
  102.     print("Please follow the instructions.")
  103.     print()
  104.  
  105.  
  106. def combine_text(first, last, age):
  107.     print(f"{first} {last}, {age} years old.")
  108.  
  109. # the actual application, another file
  110. from functions import *
  111.  
  112. print("START")
  113.  
  114. print_title()
  115.  
  116. print("END")
  117.  
  118. combine_text("Test", "Developer", 31)
  119.  
Add Comment
Please, Sign In to add comment