Skip to content

Commit

Permalink
update to 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
clydewtt committed Dec 1, 2024
1 parent 68dff0d commit 44dc44c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 21 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,23 @@ for section in mack_sections: # List[CourseSection]

---

### Get Courses by Instructor

Retrieve all courses and sections taught by a specific instructor. Format the instructor name as `'LastName, FirstName'`:

#### Returns:
- **`courses`**: A list of all [`Course`](#course) objects with the specified instructor

#### Example Usage
```python
mack_courses = morgan_data.get_courses_by_instructor("Mack, Naja")

for course in mack_courses: # List[Course]
print(course)
```

---

### Get All Instructors

Fetch a list of all instructors and the courses they teach:
Expand Down Expand Up @@ -193,6 +210,7 @@ class Course:
signature: str
subject_abbreviation: str
subject: str
description: str
credit_hours: int
name: str
number: str
Expand Down
108 changes: 88 additions & 20 deletions morgan_course_data/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, term: str):
raise ValueError(f"Invalid term '{term}'. Allowed terms are: {', '.join(self.ALLOWED_TERMS)}")

self.course_db = get_db_connection(term)['COURSES']
self.other_db = get_db_connection("SPRING_2025")['OTHER_DATA']
self.other_db = get_db_connection(term)['OTHER_DATA']

def get_courses_paginated(
self, cursor: Optional[str] = None, page_size: int = 20
Expand Down Expand Up @@ -84,6 +84,7 @@ def get_courses_paginated(
signature=raw_course.get("signature", ""),
subject_abbreviation=raw_course.get("subject_abbreviation", ""),
subject=raw_course.get("subject", ""),
description=raw_course.get("description", ""),
credit_hours=raw_course.get("credit_hours", 0),
name=raw_course.get("name", ""),
number=raw_course.get("number", ""),
Expand Down Expand Up @@ -152,6 +153,7 @@ def get_all_courses(self) -> List[Course]:
signature=raw_course.get("signature", ""),
subject_abbreviation=raw_course.get("subject_abbreviation", ""),
subject=raw_course.get("subject", ""),
description=raw_course.get("description", ""),
credit_hours=raw_course.get("credit_hours", 0),
name=raw_course.get("name", ""),
number=raw_course.get("number", ""),
Expand Down Expand Up @@ -188,16 +190,17 @@ def get_course_by_signature(self, course_signature: str) -> Union[Course, None]:

# Create and return the `Course` dataclass object
return Course(
signature=doc["signature"],
subject_abbreviation=doc["subject_abbreviation"],
subject=doc["subject"],
credit_hours=doc["credit_hours"],
name=doc["name"],
number=doc["number"],
full_name=doc["full_name"],
prerequisites=doc["prerequisites"],
sections=sections
)
signature=doc.get("signature", ""),
subject_abbreviation=doc.get("subject_abbreviation", ""),
subject=doc.get("subject", ""),
description=doc.get("description", ""),
credit_hours=doc.get("credit_hours", 0),
name=doc.get("name", ""),
number=doc.get("number", ""),
full_name=doc.get("full_name", ""),
prerequisites=doc.get("prerequisites", {}),
sections=sections
)

def get_courses_by_subject_abbreviation(self, subject_abbreviation: str) -> List[Course]:
"""
Expand All @@ -221,14 +224,15 @@ def get_courses_by_subject_abbreviation(self, subject_abbreviation: str) -> List
CourseSection(**{k: v for k, v in section.items() if k != '_id'})
for section in doc.get("sections", [])]
course = Course(
signature=doc["signature"],
subject_abbreviation=doc["subject_abbreviation"],
subject=doc["subject"],
credit_hours=doc["credit_hours"],
name=doc["name"],
number=doc["number"],
full_name=doc["full_name"],
prerequisites=doc["prerequisites"],
signature=doc.get("signature", ""),
subject_abbreviation=doc.get("subject_abbreviation", ""),
subject=doc.get("subject", ""),
description=doc.get("description", ""),
credit_hours=doc.get("credit_hours", 0),
name=doc.get("name", ""),
number=doc.get("number", ""),
full_name=doc.get("full_name", ""),
prerequisites=doc.get("prerequisites", {}),
sections=sections
)
courses.append(course)
Expand All @@ -255,7 +259,7 @@ def get_course_sections_by_instructor(self, instructor_name: str) -> List[Course
# Filter sections for the specified instructor
filtered_sections = [
CourseSection(**{k: v for k, v in section.items() if k != "_id"})
for section in doc["sections"]
for section in doc.get("sections", [])
if section["instructor"] == instructor_name
]

Expand All @@ -264,6 +268,70 @@ def get_course_sections_by_instructor(self, instructor_name: str) -> List[Course

return sections

def get_courses_by_instructor(self, instructor_name: str) -> List[Course]:
"""
Fetch courses taught by the specified instructor, including only the sections they teach.
Args:
instructor_name (str): The instructor name for which the course data should be retrieved. Follows `lastName, firstName`
Example: 'Naja, Mack'
Returns:
A list of `Course` objects with filtered `CourseSection` instances taught by the given instructor.
"""
# Query for documents where at least one section has the specified instructor
data = self.course_db.find({"sections.instructor": instructor_name})

result = []
for doc in data:
# Filter sections for the specified instructor
filtered_sections = [
CourseSection(
title=section.get("title", ""),
section=section.get("section", ""),
type=section.get("type", ""),
crn=section.get("crn", -1),
instructional_method=section.get("instructional_method", ""),
instructor=section.get("instructor", ""),
enrollment_actual=section.get("enrollment_actual", ""),
enrollment_max=section.get("enrollment_max", ""),
enrollment_available=section.get("enrollment_available", ""),
meetings=[
Meeting(
start_time=meeting.get("start_time", ""),
end_time=meeting.get("end_time", ""),
days=meeting.get("days", []),
building=meeting.get("building", ""),
campus=meeting.get("campus", ""),
room=meeting.get("room", ""),
start_date=meeting.get("start_date", ""),
end_date=meeting.get("end_date", ""),
)
for meeting in section.get("meetings", [])
],
)
for section in doc.get("sections", [])
if section.get("instructor") == instructor_name
]

if filtered_sections:
# Create a Course object with filtered sections
course = Course(
signature=doc.get("signature", ""),
subject_abbreviation=doc.get("subject_abbreviation", ""),
subject=doc.get("subject", ""),
description=doc.get("description", ""),
credit_hours=doc.get("credit_hours", 0),
name=doc.get("name", ""),
number=doc.get("number", ""),
full_name=doc.get("full_name", ""),
prerequisites=doc.get("prerequisites", {}),
sections=filtered_sections,
)
result.append(course)

return result

def get_all_instructors(self) -> list[Instructor]:
"""
Expand Down
1 change: 1 addition & 0 deletions morgan_course_data/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Course:
signature: str
subject_abbreviation: str
subject: str
description: str
credit_hours: int
name: str
number: str
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name="morgan_course_data",
version="0.1.1",
version="0.1.2",
packages=find_packages(),
description="Python package for querying Morgan State University course data.",
long_description=README,
Expand Down

0 comments on commit 44dc44c

Please sign in to comment.