-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commiting for retesting refactor_testing_query_courseid.py with data8…
… and data100 for classes
- Loading branch information
Showing
4 changed files
with
123 additions
and
117 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> + extracted_info = { | ||
+ "display_name": classes[0]['displayName'], | ||
+ "department": classes[0]['course']['subjectArea']['description'], | ||
+ "enrollment_count": sum(section['aggregateEnrollmentStatus']['enrolledCount'] for section in classes) | ||
+ } | ||
+ return json.dumps(extracted_info, indent=4) | ||
+ | ||
+ error_string = f"Failed to retrieve data for {class_name}. Status code: {response.status_code}" | ||
+ return json.dumps(error_string, indent=4) | ||
+ | ||
+def main(): | ||
+ """ | ||
+ Main function to handle command-line arguments or interactive input. | ||
+ """ | ||
+ parser = argparse.ArgumentParser(description="Fetch course information based on term ID and class name.") | ||
+ parser.add_argument('term_id', type=str, nargs='?', help="The term ID (e.g., '2232').") | ||
with nargs='?' you would typically also have a default=<whatever> clause. i would suggest just using an int for term_id and making it optional. | ||
|
||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> @@ -0,0 +1,176 @@ | ||
+import requests | ||
please add #!/usr/bin/env python3 as the first line in the script, followed by a blank like. this will allow us to run the script as ./whatever.py instead of requiring python3 whatever.py | ||
|
||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> @@ -0,0 +1,176 @@ | ||
+import requests | ||
+import json | ||
+import re | ||
+import os | ||
+import argparse | ||
please alphabetize the imports | ||
|
||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> + } | ||
+ return json.dumps(extracted_info, indent=4) | ||
+ | ||
+ error_string = f"Failed to retrieve data for {class_name}. Status code: {response.status_code}" | ||
+ return json.dumps(error_string, indent=4) | ||
+ | ||
+def main(): | ||
+ """ | ||
+ Main function to handle command-line arguments or interactive input. | ||
+ """ | ||
+ parser = argparse.ArgumentParser(description="Fetch course information based on term ID and class name.") | ||
+ parser.add_argument('term_id', type=str, nargs='?', help="The term ID (e.g., '2232').") | ||
+ parser.add_argument('class_names', type=str, nargs='*', help="One or more class names (e.g., 'data8', 'compsci189').") | ||
+ | ||
+ args = parser.parse_args() | ||
+ | ||
i would also add some logic to check if the proper env vars are set before continuing script execution. | ||
|
||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> + Args: | ||
+ term_id (str): The term ID. | ||
+ class_name (str): The class name. | ||
+ page_number (int, optional): The page number. Defaults to 1. | ||
+ page_size (int, optional): The page size. Defaults to 100. | ||
+ | ||
+ Returns: | ||
+ str: JSON string with extracted information or error message. | ||
+ """ | ||
+ class_info, error_message = validate_class_name(class_name) | ||
+ if error_message: | ||
+ return json.dumps(error_message, indent=4) | ||
+ | ||
+ subject_area_code, number = class_info | ||
+ | ||
+ base_url = "https://gateway.api.berkeley.edu/uat/sis/v1/classes?" | ||
i'd make this a global variable... upper-case the variable name, stick it at the top of the script below the imports. | ||
|
||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> + return json.dumps(error_message, indent=4) | ||
+ | ||
+ subject_area_code, number = class_info | ||
+ | ||
+ base_url = "https://gateway.api.berkeley.edu/uat/sis/v1/classes?" | ||
+ url_params = f"term-id={term_id}&subject-area-code={subject_area_code}&catalog-number={number}&page-number={page_number}&page-size={page_size}" | ||
+ full_url = base_url + url_params | ||
+ | ||
+ headers = { | ||
+ "app_id": os.getenv('APP_ID'), | ||
+ "app_key": os.getenv('APP_KEY'), | ||
+ } | ||
+ | ||
+ response = requests.get(full_url, headers=headers) | ||
+ | ||
+ if response.status_code == 200: | ||
i would recommend a try/except block here, or an else clause in case we don't get a 200 code returned. | ||
|
||
In scripts/refactor_testing_query_courseid.py: | ||
|
||
> + "app_key": os.getenv('APP_KEY'), | ||
+ } | ||
+ | ||
+ response = requests.get(full_url, headers=headers) | ||
+ | ||
+ if response.status_code == 200: | ||
+ data = response.json()['apiResponse']['response'] | ||
+ classes = data['classes'] | ||
+ extracted_info = { | ||
+ "display_name": classes[0]['displayName'], | ||
+ "department": classes[0]['course']['subjectArea']['description'], | ||
+ "enrollment_count": sum(section['aggregateEnrollmentStatus']['enrolledCount'] for section in classes) | ||
+ } | ||
+ return json.dumps(extracted_info, indent=4) | ||
+ | ||
+ if check_pattern(class_name): |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.