tuomasvaltanen

Untitled

Nov 15th, 2022 (edited)
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.06 KB | None | 0 0
  1. # TRY THIS IN THE BEGINNING OF LECTURE AND SEE IF IT WORKS:
  2. # If you get an SSL-error, see Moodle for the "SSL-errors with internet data, HOW TO FIX"
  3.  
  4. # If it works okay, you'll see data in raw format in console window
  5. # If you get an SSL-error, the code is not working as expected
  6.  
  7. import urllib.request
  8.  
  9. # get internet data
  10. url = 'https://edu.frostbit.fi/api/events/en'
  11. req = urllib.request.Request(url)
  12. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  13.  
  14. print(raw_data)
  15.  
  16. # NEW FILE
  17.  
  18. # collections inside a collection
  19. temp_day_1 = [13.5, 16.4, 11.6, 11.3]
  20. temp_day_2 = [12.1, 15.2, 11.9, 10.4]
  21. temp_day_3 = [15.3, 17.6, 12.5, 11.6]
  22.  
  23. # temperatures has measurements from 3 days
  24. # each day has 4 temperature values
  25. temperatures = [temp_day_1, temp_day_2, temp_day_3]
  26.  
  27. # process each day, one at a time
  28. for day in temperatures:
  29.     print("Processing new day!")
  30.  
  31.     # process each temperature in ONE DAY at a time
  32.     for t in day:
  33.         print(t)
  34.  
  35. # NEW FILE
  36.  
  37. movies = ["Interstellar", "Forrest Gump", "Jurassic Park"]
  38. books = ["Lord of the Rings", "Da Vinci Code", "Robinson Crusoe"]
  39.  
  40. # all data is in list of lists, each sublist is a category of products
  41. products = [movies, books]
  42.  
  43. # process all data, each category at a time
  44. for category in products:
  45.    
  46.     # process each item in each category
  47.     for item in category:
  48.         print(item)
  49.  
  50. # NEW FILE
  51.  
  52. # dictionary inside another dictionary
  53. book = {
  54.     "name":  "My Lady Jane",
  55.     "year":  2016,
  56.     "publisher": {
  57.         "name": "HarperTeen",
  58.         "organization": "HarperCollins Publishers",
  59.         "location": "New York"
  60.     }
  61. }
  62.  
  63. # just getting name, fairly straightforward
  64. print(book['name'])
  65.  
  66. # if the dictionary is simple, you can chain the keys
  67. # publisher -> location
  68. print(book['publisher']['location'])
  69.  
  70. # using a helper variable is also okay!
  71. publisher = book['publisher']
  72. print(publisher['location'])
  73.  
  74. # NEW FILE
  75.  
  76. # example, list inside a dictionary
  77. book = {
  78.     "name":  "My Lady Jane",
  79.     "year":  2016,
  80.     "authors": ["Cynthia Hand", "Brodi Ashton", "Jodi Meadows"]
  81. }
  82.  
  83. # get second author
  84. print(book['authors'][1])
  85. print()
  86.  
  87. # you can also loop through the authors
  88. # because book['authors'] is a LIST
  89. for author in book['authors']:
  90.     print(author)
  91.  
  92. # NEW FILE
  93.  
  94. # our products, list of dicts
  95. products = [
  96.     {"name": "Coffee maker", "price": 79},
  97.     {"name": "Dishwasher", "price": 299},
  98.     {"name": "Freezer", "price": 199},
  99. ]
  100.  
  101. # process one product at a time
  102. for product in products:
  103.     name = product['name']
  104.     price = product['price']
  105.  
  106.     # print name and price of one product
  107.     print(f"{name}, {price} €")
  108.  
  109. # NEW FILE , HOTELS
  110.  
  111. # install this first!
  112. import var_dump as vd
  113.  
  114. # create first hotel
  115. hotel_1 = {
  116.     "name": "Snow Line Hotels",
  117.     "rating": 4.3,
  118.     "wifi": True,
  119.     "free_breakfast": True,
  120.     "services": ["sauna", "meetings", "restaurant", "parking", "safaris"],
  121.     "price_level": 4
  122. }
  123.  
  124. # create second hotel
  125. hotel_2 = {
  126.     "name": "North Ice Hostel",
  127.     "rating": 3.5,
  128.     "wifi": True,
  129.     "free_breakfast": False,
  130.     "services": ["sauna", "parking"],
  131.     "price_level": 2
  132. }
  133.  
  134. # place both hotels into one list
  135. hotels = [hotel_1, hotel_2]
  136.  
  137. # vd.var_dump(hotels)
  138.  
  139. # sometimes it's easier to inspect only one piece of data
  140. # at a time (one hotel)
  141. # test_hotel = hotels[0]
  142. # vd.var_dump(test_hotel)
  143. # print(test_hotel['name'])
  144. # print(test_hotel['services'])
  145.  
  146. for hotel in hotels:
  147.     print(hotel['name'])
  148.  
  149.     # traditional way: use a for loop for the services -list
  150.     # print all services in a loop one by one
  151.     # because hotel['services'] is a LIST
  152.     # for service in hotel['services']:
  153.     #    print(service)
  154.  
  155.     # another way: use join() -function
  156.     # ONLY WORKS WITH TEXT OR NUMBER LISTS, DOESN'T WORK
  157.     # WITH LIST OF DICTIONARIES!
  158.     services = "\n".join(hotel['services'])
  159.     print(services)
  160.  
  161.     print()
  162.  
  163.  
  164. # NEW FILE
  165.  
  166. # place both hotels into one list
  167. hotels = [hotel_1, hotel_2]
  168.  
  169. for hotel in hotels:
  170.     print(hotel['name'])
  171.  
  172.     if "restaurant" in hotel['services']:
  173.         print("Hotel has a restaurant!")
  174.  
  175.     print()
  176.  
  177. # NEW FILE
  178.  
  179. # place both hotels into one list
  180. hotels = [hotel_1, hotel_2]
  181.  
  182. # empty list for having sauna hotels later on
  183. sauna_hotels = []
  184.  
  185. for hotel in hotels:
  186.     print(hotel['name'])
  187.  
  188.     # if sauna in services, add hotel name to the sauna hotels list
  189.     if "sauna" in hotel['services']:
  190.         sauna_hotels.append(hotel['name'])
  191.  
  192.     print()
  193.  
  194.  
  195. print(sauna_hotels)
  196.  
  197. # NEW FILE
  198.  
  199. import urllib.request
  200. import json
  201.  
  202. # this module needs to be installed separately
  203. # in PyCharm you can install the package if its not found!
  204. import var_dump as vd
  205.  
  206. # get internet data
  207. url = 'https://edu.frostbit.fi/api/events/en'
  208. req = urllib.request.Request(url)
  209. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  210.  
  211. # the needed data from internet is now in the "data" -variable!
  212. data = json.loads(raw_data)
  213. # vd.var_dump(data)
  214.  
  215. # if you want to see only the first event at first
  216. # to understand the data better:
  217. # test_event = data[0]
  218. # vd.var_dump(test_event)
  219. # print(test_event['name'])
  220. # print(test_event['categories'])
  221.  
  222. # go through all events in data, regardless
  223. # how many events we have this time
  224. for event in data:
  225.     print(event['name'])
  226.    
  227.     # build the address from dictionary (address)
  228.     street_address = event['address']['street_address']
  229.     postal_code = event['address']['postal_code']
  230.  
  231.     print(f"{postal_code} {street_address}")
  232.  
  233.     # combine categories -list into a text separated by commas
  234.     categories = ", ".join(event['categories'])
  235.  
  236.     # only print categories if they exist
  237.     if categories != "":
  238.         print(categories)
  239.     else:
  240.         print("No categories.")
  241.  
  242.     print()
  243.  
  244. # NEW FILE
  245.  
  246. import urllib.request
  247. import json
  248.  
  249. # this module needs to be installed separately
  250. # in PyCharm you can install the package if its not found!
  251. import var_dump as vd
  252.  
  253. # get internet data
  254. url = 'https://edu.frostbit.fi/api/events/en'
  255. req = urllib.request.Request(url)
  256. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  257.  
  258. # the needed data from internet is now in the "data" -variable!
  259. data = json.loads(raw_data)
  260.  
  261.  
  262. # ask user for a search term
  263. choice = input("What are you looking for?\n")
  264.  
  265. # go through all events in data, regardless
  266. # how many events we have this time
  267. for event in data:
  268.  
  269.     # combine categories -list into a text separated by commas
  270.     categories = ", ".join(event['categories'])
  271.    
  272.     # if user's search word wasn't in the categories
  273.     # skip this event completely and continue with next one!
  274.     if choice not in categories:
  275.         continue
  276.  
  277.     print(event['name'])
  278.  
  279.     # build the address from dictionary (address)
  280.     street_address = event['address']['street_address']
  281.     postal_code = event['address']['postal_code']
  282.  
  283.     print(f"{postal_code} {street_address}")
  284.  
  285.     # only print categories if they exist
  286.     if categories != "":
  287.         print(categories)
  288.     else:
  289.         print("No categories.")
  290.  
  291.     print()
  292.  
  293. # NEW FILE
  294.  
  295. import urllib.request
  296. import json
  297.  
  298. # this module needs to be installed separately
  299. # in PyCharm you can install the package if its not found!
  300. import var_dump as vd
  301.  
  302. # get internet data
  303. url = 'https://edu.frostbit.fi/api/events/en'
  304. req = urllib.request.Request(url)
  305. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  306.  
  307. # the needed data from internet is now in the "data" -variable!
  308. data = json.loads(raw_data)
  309.  
  310. # ask user for a search term
  311. choice = input("What are you looking for?\n")
  312.  
  313. # go through all events in data, regardless
  314. # how many events we have this time
  315. for event in data:
  316.  
  317.     # combine categories -list into a text separated by commas
  318.     categories = ", ".join(event['categories'])
  319.  
  320.     # if we found a matching event
  321.     # print all details and call BREAK to
  322.     # end this loop (because no point in looking further
  323.     # because we already found what we were looking for)
  324.     if choice in categories:
  325.         print("Found a matching event!")
  326.         print(event['name'])
  327.  
  328.         # build the address from dictionary (address)
  329.         street_address = event['address']['street_address']
  330.         postal_code = event['address']['postal_code']
  331.  
  332.         print(f"{postal_code} {street_address}")
  333.  
  334.         # only print categories if they exist
  335.         if categories != "":
  336.             print(categories)
  337.         else:
  338.             print("No categories.")
  339.  
  340.         break
  341.  
  342. # NEW FILE
  343.  
  344. import json
  345. import urllib.request
  346. url = "https://edu.frostbit.fi/api/weather/"
  347. req = urllib.request.Request(url)
  348. raw_data = urllib.request.urlopen(req).read().decode("UTF-8")
  349. weather = json.loads(raw_data)
  350.  
  351. # initialize variables needed in loop
  352. strongest_wind = 0
  353. strongest_city = ""
  354. weakest_wind = 0
  355. weakest_city = ""
  356.  
  357. for city in weather:
  358.     print(city['location'])
  359.     print(city['wind'])
  360.     print()
  361.  
  362.     # if-statement, if current wind is bigger than previous
  363.     # biggest wind => this wind is now the biggest
  364.     # also savet the city name!
Add Comment
Please, Sign In to add comment