Skip to content

Commit

Permalink
commiting for retesting refactor_testing_query_courseid.py with data8…
Browse files Browse the repository at this point in the history
… and data100 for classes
  • Loading branch information
jlucarga committed Jul 11, 2024
1 parent 173fb2e commit a13fb28
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 117 deletions.
114 changes: 114 additions & 0 deletions scripts/implement_edit_rev.txt
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):
1 change: 1 addition & 0 deletions scripts/query_courseid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def getCourseInformation(term_id, class_name, page_number=1, page_size=100):
return_json = json.dumps(extracted_info, indent=4)
print(return_json)
return return_json

elif check_pattern(class_name) == True:
desired_display_name = extract_pattern(class_name)
subject_area_code = desired_display_name.split()[0].upper()
Expand Down
11 changes: 8 additions & 3 deletions scripts/refactor_testing_query_courseid.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ def extract_pattern(s):
return f"{remaining} {char}{numbers}"

def getCourseInformation(term_id, class_name, page_number=1, page_size=100):

if class_name == "unknown":
error_string = "Class name is unknown. Please enter a valid class name."
return json.dumps(error_string, indent=4)

elif not has_digits(class_name):
error_string = f"Class name {class_name} is invalid. Please enter a valid class name."
return json.dumps(error_string, indent=4)
Expand Down Expand Up @@ -61,12 +63,15 @@ def getCourseInformation(term_id, class_name, page_number=1, page_size=100):
"department": classes[0]['course']['subjectArea']['description'],
"enrollment_count": sum(section['aggregateEnrollmentStatus']['enrolledCount'] for section in classes)
}
return json.dumps(extracted_info, indent=4)
# Creating a new JSON object with the extracted information
return_json = json.dumps(extracted_info, indent=4)
print(return_json)
return return_json

elif check_pattern(class_name):
desired_display_name = extract_pattern(class_name)
subject_area_code, number = desired_display_name.split()
full_url = base_url + 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 + f"term-id={term_id}&subject-area-code={subject_area_code}&catalog-number={number}&page-number {page_number}&page-size={page_size}"

response = requests.get(full_url, headers=headers)
if response.status_code == 200:
Expand Down Expand Up @@ -114,7 +119,7 @@ def main():

else:
term_id = args.term_id
class_names = args.class_name
class_names = args.class_names

for class_name in args.class_names:
result = getCourseInformation(args.term_id, class_name)
Expand Down
114 changes: 0 additions & 114 deletions scripts/testing_refactored.ipynb

This file was deleted.

0 comments on commit a13fb28

Please sign in to comment.