Data Structure: J-Son?

Jed Rembold & Fred Agbo

April 8, 2024

Announcements

  • Project ImageShop is due today!

  • Guidelines for Personal Project is posted

    • Personal project is 15% of the course grade component
    • Due date is Monday 15 April at 10:00pm
  • On a general note, be concrete and creative about the project.

  • Polling: https://www.polleverywhere.com/agbofred203

Project guidelines

Sample projects

Review Question

Consider the program on the right hand side and the structure of the defined data. What will be printed to the terminal?

  1. course1[CS151]["Jake"]: {'A', 'B', 'C'}
  2. ['Jake': {'A', 'B', 'C'}]
  3. Jake {'A', 'C', 'B'}
  4. Sally {'A', 'B', 'B'}
data ={
    "CS151": {
        "Sally": {"A", "B", "B"},
        "Jake": {"B", "A", "B", "C"},
        "Ben": {"A", "B", "A", "B"}
    },
    "DATA152": {
        "James": ["B", "C", "A"],
        "Lily": ["A", "A", "B"]
    }
}
course1 = data["CS151"]
course2 = data["DATA152"]
for std, grade in course1.items():
    if len(grade) == len(course2["Lily"]):
        print(std, grade)

Compound Structure Storage

  • Structures representing complicated data can often be large enough that you don’t want to store them within your program itself
  • We can put them in their own file, but reading them in with our current tools would be complicated
    • Current methods read in text, so we would need to parse the text to identify what data structures we needed to create and what elements we needed to add
    • This is certainly possible, but potentially more overhead than what we would like for some structures
  • Useful then to store the data structure in file in such a format that it can be easily read into Python

File I/O

  • A variety of ways this can be done
    • XML, YAML, JSON
  • JSON is particularly interesting to us, because its syntax almost exactly matches Python’s (even though it was made for Javascript)
  • Python has a built-in library to read and write JSON files, just called json
    • json.load(file_handle)
      • Loads the JSON data structure from the specified file into its Python equivalent
    • json.dump(data_object, file_handle)
      • Writes a JSON text representation of the data object to the given file
    • Both methods are used inside our normal with open() as fhandle: syntax

Using JSON

  • To read a JSON file into a variable data:

    import json
    with open('file.json') as fh:
        data = json.load(fh)
  • To write a variable with complex structure out to a JSON file:

    import json
    with open('file.json', 'w') as fh:
        json.dump(data, fh)

The Power of JSON

  • One very nice aspect of JSON is that it is often the defacto way that information is passed around the internet
  • This means it can be easy to find data providers where you can access or download information already in a JSON format
  • DND Fireball Spell info here
  • We could download this information to a file, which we could then read in and use within our Python program
  • It is possible to also process the information straight from the internet as well

JSON Gotchas

  • If you are writing JSON files from within Python or using files gotten elsewhere, they should already be properly formatted
  • If you need/want to edit a JSON file directly though, you should be aware of a few “gotchas” where the JSON syntax varies slightly from Python’s syntax
    • You can not have trailing commas at the end of a JSON structure
      • Something like [1, 2, 3,] is perfectly fine in Python, but illegal in JSON
    • JSON strings require double quotes
      • In Python you can use either double or single quotes, but JSON requires double
    • Booleans are all lowercase in JSON
      • Vs starting with a capital letter in Python
// reveal.js plugins