Preliminary Software Engineering
Unit 1 - Programming Fundamentals
Unit 1 - Programming Fundamentals
  • 1 - Programming Fundamentals
    • Programming Fundamentals Content
  • 2 - Python
    • Expected Python Knowledge
    • GitHub
    • Learning Python
    • Data Structures and File Management
      • Data Structures
        • Lists
        • Arrays
          • Single and Multi-Dimensional Arrays
        • Lists vs Arrays
          • Activities
        • Tuples
        • Sets
        • Dictionaries
          • Activities
      • File Handling
        • Loops, Lists, Dictionaries
        • Activities
  • 3 - Theory Content
    • Theory Content Explained
      • NESA Directional Verbs
      • Responding to Directional Verbs
  • 4 - Software Development and Management
    • Approaches to Software Development
      • Waterfall Model
      • Agile Model
      • Summary: Waterfall vs Agile
      • Activities
    • Software Development Steps
      • Sample Exam Question
      • Requirements Definition
        • Understanding the Need
        • Key Questions to Ask
        • Examples
        • Activities
        • Sample Exam Question
      • Determining Specifications
        • Functional Specifications
        • Non-Functional Specifications
        • Activities
      • Design
        • Top-Down Design Approach
        • Bottom-Up Design Approach
      • Development
        • Optimising Code
      • Integration
        • Example: Payment Program
        • Activity
        • Application Programming Interface (API)
          • Example: OpenWeather API
          • Example: WeatherAPI
          • Activity: Prepare Spells
      • Testing and Debugging
        • Test Data
          • Activities
        • Testing the System
          • Activities
        • Debugging
          • Types of Errors
            • Activities
          • Python Debugger
            • Activities
          • VS Code Debugger
            • Activities
      • Installation
        • Activities
        • Sample Exam Question
      • Maintenance
  • Charts and Algorithms
    • IPO Charts and Pseudocode
      • Activities
    • Algorithms, Flowcharts, Pseudocode
      • Pseudocode Activities
      • Flowchart Activities
      • Sample Exam Questions
    • Structure Charts
      • Activities
      • Sample Exam Questions
    • Data Flow Diagrams
      • Activities
    • Data Dictionaries
      • Activities
    • Decision Trees
      • Activities
      • Sample Exam Questions
    • Gantt Chart
    • Class Diagrams
      • Sample Exam Question
    • Storyboards
      • Sample Exam Question
  • Testing and Debugging
    • Test Data
      • Activities
    • Testing the System
      • Activities
    • Debugging
      • Types of Errors
        • Activities
      • Python Debugger
        • Activities
      • VS Code Debugger
        • Activities
  • Computational Thinking
    • Decomposition
    • Abstraction
    • Activities
  • Version Control
    • Git
    • GitHub
    • Activities
  • Number Systems
    • Binary Systems
    • Hexadecimal Numbers
    • Using Two's Complement
    • Activities
  • 5 - Assessment Task 1
    • Data Science Project
      • Before we Start
        • Setting up GitHub Repository
        • Setting Up Markdown Documentation
      • Examples of API Usage
        • Starter Code: NASA Scenario
        • Starter Code: Spell Book
        • Starter: Pokédex Explorer
        • Starter Code: Weather App
        • Example: OpenWeather API
        • Example: WeatherAPI
        • Example: Prepare Spells
    • Task Guide
      • Requirements Definition
      • Determining Specifications
        • Use Cases
      • Design
        • Gantt Chart
        • Structure Chart
        • Algorithms
        • Data Dictionary
      • Development
        • Comments vs DocStrings
        • UI - main.py
        • Create Python Module
          • Example: NASA Module
          • Example: WeatherFetch Module
          • Example: SpellBook Module
      • Integration
        • Example: Pokedex
      • Testing and Debugging
        • Commit Changes
      • Installation
      • Maintenance
    • Submitting Your Task
Powered by GitBook
On this page
  1. 5 - Assessment Task 1
  2. Task Guide
  3. Development
  4. Create Python Module

Example: NASA Module

PreviousCreate Python ModuleNextExample: WeatherFetch Module

Last updated 5 months ago

CtrlK

This is some starter code which you could use if you want to use the NASA API. You will need to sign up at https://api.nasa.gov/ to get your API Key.

API Key

Some APIs do not keep their API public and you will need to go on their website and sign up to receive a unique API key.

Make sure you replace "DEMO_KEY" with a real key from the NASA API in the API_KEY variable.

nasa.py
import requests

# Base URL for NASA's Astronomy Picture of the Day (APOD) API
API_URL = "https://api.nasa.gov/planetary/apod"
API_KEY = "YOUR_KEY"  # Replace with your NASA API key

def fetch_apod(date=None):

Testing the Above

"""Functions to interact with the NASA APOD API."""
date = input("Enter a date (YYYY-MM-DD) or press Enter for today's APOD: ")
apod_data = fetch_apod(date if date else None)
display_apod(apod_data)

"""Fetch NASA's Astronomy Picture of the Day (APOD)."""
params = {"api_key": API_KEY}
if date:
params["date"] = date # Format: YYYY-MM-DD
response = requests.get(API_URL, params=params)
if response.status_code == 200:
data = response.json()
return {
"title": data.get("title", "Unknown"),
"date": data.get("date", "Unknown"),
"explanation": data.get("explanation", "No description available."),
"image_url": data.get("url", "No image available")
}
else:
print("Error fetching data from NASA API.")
return None
def display_apod(apod_data):
"""Display APOD details."""
if apod_data:
print(f"Title: {apod_data['title']}")
print(f"Date: {apod_data['date']}")
print(f"Description: {apod_data['explanation'][:200]}...")
print(f"Image URL: {apod_data['image_url']}")
else:
print("No data to display.")