Last active
November 22, 2020 13:45
-
-
Save daniel-schroeder-dev/cc4c7c7dac06ab952739ce237c9c3d7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python3 | |
| print("Content-Type: text/html \n") | |
| import sqlite3 | |
| import requests | |
| from random import randint | |
| def create_gradebook(): | |
| """ | |
| Creates a list of tuples representing a gradebook in the form: | |
| gradebook = [ | |
| ("Daniel", 87, "--"), | |
| ("Steve", 65, "--"), | |
| ("Mary", 95, "--"), | |
| etc... | |
| ] | |
| """ | |
| url = "https://randomuser.me/api/" | |
| url_query = { | |
| "results": 50, | |
| "nat": "us", | |
| } | |
| response = requests.get(url, url_query) | |
| json_data = response.json() | |
| users = json_data["results"] | |
| gradebook = [] | |
| for user in users: | |
| name = user["name"]["first"] | |
| grade = randint(40, 100) | |
| gradebook.append((name, grade, "--")) | |
| return gradebook | |
| def create_gradebook_db(): | |
| gradebook = create_gradebook() | |
| connection = sqlite3.connect("database/gradebook.db") | |
| sql = connection.cursor() | |
| query = "DROP TABLE IF EXISTS grades" | |
| sql.execute(query) | |
| query = """ | |
| CREATE TABLE IF NOT EXISTS grades( | |
| name TEXT, | |
| score INTEGER, | |
| grade TEXT | |
| ) | |
| """ | |
| sql.execute(query) | |
| query = "INSERT INTO grades VALUES(?, ?, ?)" | |
| for grade in gradebook: | |
| sql.execute(query, grade) | |
| connection.commit() | |
| if __name__ == "__main__": | |
| create_gradebook_db() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment