IS4010: AI-Enhanced Application Development

Week 7: Working with External Data

Brandon M. Greenwell

Session 1: Files & JSON

Persisting data

  • Think about every program we’ve written so far. When the script ends, all the data and variables disappear. Our programs have amnesia.
  • Persistence is the concept of saving data to a permanent location, like a file, so it can be loaded back later.
  • This allows our applications to remember their state between runs, which is essential for almost any real-world application.

Reading and writing files in python

  • The standard, safe way to work with files in python is using the with open(...) as f: syntax.
  • This ensures the file is automatically closed even if errors occur.
  • We open files in different modes:
    • 'r' is for reading from a file.
    • 'w' is for writing to a file (this will overwrite the existing content).
    • 'a' is for appending to the end of a file.
# Write to a file, overwriting any previous content
with open("my_note.txt", "w") as f:
    f.write("hello from our application!")

# Read the content back from the file
with open("my_note.txt", "r") as f:
    content = f.read()
    print(content)

The JSON data format

  • JavaScript Object Notation, or JSON, is the universal language for exchanging data on the web.
  • It’s a simple, human-readable text format that maps almost perfectly to python’s built-in data structures.
  • JSON objects are like python dictionaries ({}).
  • JSON arrays are like python lists ([]).
  • This makes it incredibly easy to work with in python.

Using python’s json library

  • Python’s built-in json library gives us two key functions for working with json data and files.
  • json.dump(): serializes a python object (like a list or dict) into a json-formatted string and writes it to a file.
  • json.load(): reads a json file and deserializes its content back into a python object.
import json

# A python dictionary
contacts = {"name": "ada lovelace", "email": "ada@example.com"}

# Serialize and write the dictionary to a file
with open("contact.json", "w") as f:
    json.dump(contacts, f, indent=4)

# Deserialize the file back into a python object
with open("contact.json", "r") as f:
    data = json.load(f)
    print(data["name"])

Introducing lab 07 (part 1)

  • The first lab activity this week will be to create a simple contact book application.
  • You will use the json library to persist your application’s data.
  • Your program must be able to save its list of contacts to a json file and load that data back when it starts up.

Session 2: Consuming APIs

What is an API?

  • An Application Programming Interface (api) is a set of rules that allows different software applications to communicate with each other.
  • Analogy: an api is like a restaurant menu.
  • The menu (the api) provides a list of dishes you can order (operations you can request) without needing to know the details of how the kitchen (the other application) actually works.
  • It defines the what, not the how.

Web APIs and HTTP

  • We will focus on web apis, which allow applications to communicate over the internet using http.
  • The standard python library for making http requests is requests. We will need to install it (pip install requests).
  • The most common http method is GET, which is used to retrieve data from a specific url.
  • When we get a response, a status code tells us if it was successful. 200 means OK, while 404 means not found.

Making a GET request

  • Using the requests library is very straightforward.
  • We call requests.get() with the url of the api endpoint.
  • We can then check the response.status_code to ensure the request was successful.
  • Most modern apis return data in json format, which the requests library can automatically parse for us using the response.json() method.
import requests

# The URL of a public API endpoint
url = "[https://pokeapi.co/api/v2/pokemon/pikachu](https://pokeapi.co/api/v2/pokemon/pikachu)"

# Make the GET request
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    # Parse the JSON response into a python dictionary
    data = response.json()
    print(f"Name: {data['name'].title()}")
    print(f"Height: {data['height']} decimetres")
else:
    print(f"Error: Received status code {response.status_code}")

Introducing lab 07 (part 2)

  • For the second part of this week’s lab, you get to explore the web!
  • Your task is to choose a free, public api that interests you (weather, movies, pokémon, etc.).
  • You will write a python script that fetches live data from your chosen api, parses the json response, and displays some interesting, formatted information to the user.
  • Full instructions are in labs/lab07/README.md.