Skip to content

Nov 22: Prac6, Help with Project

Learning Objectives

After today's class, you should be able to:

  • Write a list comprehension that includes an expression and a condition.
  • Explore the contents of a relatively complex object stored in a JSON file.
  • Read and write data in JSON files using json.load() and json.dump().

Practice Quiz

  • List comprehensions

    • See zyBook and Real Python

    • Example 1: Squaring Numbers

      # Before
      squares = []
      for x in range(10):
          squares.append(x**2)
      
      # After
      squares = [x**2 for x in range(10)]
      

    • Example 2: All Caps Words

      text = "JMU is the BEST"
      
      # Before
      caps = []
      for word in text.split():
          if word.isupper():
              caps.append(word)
      
      # After
      caps = [word for word in text.split() if word.isupper()]
      

  • Exploring skyscrapers.json

    • Loading the data from the JSON file
      Note: This was done before calling your function.

      import json
      from pprint import pprint
      
      with open("skyscrapers.json") as file:
          data = json.load(file)
      

    • Lists use indexes. Dictionaries use keys.

      >>> data[0]["name"]
      'The Illinois'
      
      >>> data[0]["statistics"]["height"]
      1609.3599853516
      

Hints on PA3

  • Part B: Nested Data

    • Command-line arguments
      • sys.argv
      • sys.exit()
    • F-string field width
      x = 149
      y = 159
      print(f"|{x:10d}|{y:10d}|")
      s = "Hi!"
      t = "Ho!"
      print(f"|{s:10s}|{t:10s}|")
      
  • Part C: Recursion

    • Influence of a user depends on their friends' influence
    • Need to recursively call the function on each friend
  • Part D: API Usage

    • Walk through the provided fulltracks.py module
    • How to call another API method (based on the docs)