OIM3640 - Problem Solving and Software Design

2024 Fall

Session 15 (10/21)

contain

Today's Agenda

  • Welcome/News/Announcements
  • Introduction to API
  • Lectures

Welcome/News/Announcements

  • Next class:
    • Introducing Project
    • Quiz 4 on Data Structures/APIs
  • 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, dictionary
  • Functions
  • Conditional Statements: if...elif...else
  • Iterations: for, while
  • Pseudo-code

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 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 the .gitignore to prevent committing it to a public repository.

Recommendations on API

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, access by key
    • set: unordered collection of unique items

Considerations when Choosing Data Structures

  • Data order:
    • Do you need to maintain the order of elements?
  • 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 15

  • 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 session15 folder
    • analyze_book.py

Assignment 2 - Text Analysis Project

  • Due 10/30 Wednesday
  • To get started:
    • Fork the base repository to your GitHub account.
    • Clone the forked repository to your computer.
  • To submit:
    • Push all the code and update the README.md file in your GitHub repository.
    • Create a pull request from your forked repository to the upstream repository.
    • Submit the URL of your project's GitHub repository to Canvas.
  • Please Start early!