OIM3640 - Problem Solving and Software Design

2025 Spring

Session 16 (3/25)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Class Review
  • Lectures
  • Introduction to API (if time permits)

Welcome/News/Announcements

  • Next Class:
    • API and parsing JSON data
    • Please register on OpenWeather and get an API Key (free tier for Current weather and forecasts collection, not One Call API 3.0) before next class.
    • Do not share your API key with others or post it on GitHub.
  • Communications
    • Meet with me in person during office hours at least once this semester.
    • Email - specify course # in subject title, e.g., "OIM3640: GitHub settings"
    • Use Slack/GitHub when asking code-related questions
  • Questions?

What we have learned so far...

  • Variables, Expressions, Statements
  • Types: int, float, string, boolean, Nonetype, string, list, dict
  • Functions
  • Conditional Statements: if...elif...else
  • Iterations: for, while
  • Pseudo-code
  • Debugging

Quick Quiz - Dictionaries

  • Are dictionaries mutable?
  • Is it true that dict keys must be unique, but their values can be duplicated?
  • What is the output?
    person = {'name': 'Guido', 'age': 69 }
    print('Guido' in person)
    
  • How do you access the value associated with the key 'age' in person?
  • How do you retrieve the value associated with a key, say 'country', in person safely, without raising an error if the key doesn't exist?
  • How do you add a new key-value pair 'city': 'Belmont, CA' to person?
  • How can you create a dictionary to count each letter's frequency in a string?

Data Structures in Python

Choosing Data Structures

  • Programs often have to work with many objects of various types. For example:
    • A portfolio of stocks
    • A table of stock prices
    • A collection of customer orders
  • Main choices:
    • list: ordered data, access by index
    • tuple: ordered data, immutable, access by index
    • dict: (normally treated as) unordered data, actually insertion-ordered (since Python3.7), access by key
    • set: unordered collection of unique items, cannot access by index or key

Considerations when Choosing Data Structures

  • Data order:
    • Do you need to maintain insertion order or allow arbitrary order?
  • Mutability:
    • Do you need it to be mutable (changeable) or immutable (fixed)?
  • Fast access:
    • Do you need fast access to elements by key or index?
  • Uniqueness:
    • Do you need to ensure that elements are unique within the collection?

Session 16

  • Case - Text Analysis
  • Download following file(s) from OIM3640/resources/code/data to data folder
    • Pride and Prejudice.txt
  • Download following file(s) from OIM3640/resources/code/ to session16 folder
    • analyze_book.py

API

API (Application Programming Interface)

  • An API is a set of protocols, tools, and standards for building software applications, that allows different applications to communicate with each other.

  • Example: Let's say you want to know the current price of Bitcoin

API Example: OpenWeather

  • Create an API key from https://openweathermap.org/api
  • Download the sample code, weather.py, from OIM3640/resources/code
  • Use the urllib.request library to send HTTP requests to the OpenWeather API and retrieve data in JSON format
  • Remember to protect sensitive data (e.g., API keys, API tokens, passwords)
    • Create .env file and store your API key, e.g., OPENWEATHER_API_KEY=...
    • Install python-dotenv in terminal: python -m pip install python-dotenv
    • import os
      from dotenv import load_dotenv
      
      load_dotenv()
      APIKEY = os.getenv("OPENWEATHER_API_KEY")
      
    • Add .env to .gitignore file to prevent committing it to a public repository.

Recommendations on API