Skip to content

Commit

Permalink
Merge branch 'dev' into update-py-support
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyizheng02 authored Jun 13, 2024
2 parents 4059644 + a9d8aac commit e5531f3
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 68 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/autotest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python }}
# https://github.com/actions/cache/blob/main/examples.md#python---pip
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
7 changes: 7 additions & 0 deletions .github/workflows/tests-on-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ jobs:
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python }}
# https://github.com/actions/cache/blob/main/examples.md#python---pip
- uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
14 changes: 7 additions & 7 deletions pittapi/cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from typing import List, NamedTuple
from typing import NamedTuple

import requests

Expand All @@ -36,7 +36,7 @@ class Event(NamedTuple):
GRADUATION_CALENDAR_URL: str = "https://25livepub.collegenet.com/calendars/pitt-graduation-calendar.json"


def _fetch_calendar_events(url: str) -> List[Event]:
def _fetch_calendar_events(url: str) -> list[Event]:
""""""
data = requests.get(url).json()
events = []
Expand All @@ -52,27 +52,27 @@ def _fetch_calendar_events(url: str) -> List[Event]:
return events


def get_academic_calendar() -> List[Event]:
def get_academic_calendar() -> list[Event]:
""""""
return _fetch_calendar_events(ACADEMIC_CALENDAR_URL)


def get_grades_calendar() -> List[Event]:
def get_grades_calendar() -> list[Event]:
""""""
return _fetch_calendar_events(GRADES_CALENDAR_URL)


def get_enrollment_calendar() -> List[Event]:
def get_enrollment_calendar() -> list[Event]:
""""""
return _fetch_calendar_events(ENROLLMENT_CALENDAR_URL)


def get_course_calendar() -> List[Event]:
def get_course_calendar() -> list[Event]:
"""This is not a calendar about course schedule but rather
when courses/class are being determined for the next semester"""
return _fetch_calendar_events(COURSE_CALENDAR_URL)


def get_graduation_calendar() -> List[Event]:
def get_graduation_calendar() -> list[Event]:
""""""
return _fetch_calendar_events(GRADUATION_CALENDAR_URL)
41 changes: 20 additions & 21 deletions pittapi/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import datetime
import re
import requests
from typing import Dict, List, NamedTuple, Optional, Tuple, Union
from typing import NamedTuple

# https://pitcsprd.csps.pitt.edu/psc/pitcsprd/EMPLOYEE/SA/s/WEBLIB_HCX_CM.H_CLASS_SEARCH.FieldFormula.IScript_ClassSearch?institution=UPITT&term=2244&date_from=&date_thru=&subject=CS&subject_like=&catalog_nbr=&time_range=&days=&campus=PIT&location=&x_acad_career=UGRD&acad_group=&rqmnt_designtn=&instruction_mode=&keyword=&class_nbr=&acad_org=&enrl_stat=O&crse_attr=&crse_attr_value=&instructor_name=&instr_first_name=&session_code=&units=&trigger_search=&page=1
SUBJECTS_API = "https://pitcsprd.csps.pitt.edu/psc/pitcsprd/EMPLOYEE/SA/s/WEBLIB_HCX_CM.H_COURSE_CATALOG.FieldFormula.IScript_CatalogSubjects?institution=UPITT"
Expand All @@ -31,13 +30,13 @@
# id -> unique course ID, not to be confused with course code (for instance, CS 0007 has code 105611)
# career -> for example, UGRD (undergraduate)

TERM_REGEX = "2\d\d[147]"
TERM_REGEX = r"2\d\d[147]"
VALID_TERMS = re.compile(TERM_REGEX)


class Instructor(NamedTuple):
name: str
email: Optional[str] = None
email: str | None = None


class Meeting(NamedTuple):
Expand All @@ -46,7 +45,7 @@ class Meeting(NamedTuple):
end_time: str
start_date: str
end_date: str
instructors: Optional[List[Instructor]] = None
instructors: list[Instructor] | None = None


class Attribute(NamedTuple):
Expand All @@ -71,7 +70,7 @@ class SectionDetails(NamedTuple):
wait_list_total: str
valid_to_enroll: str

combined_section_numbers: Optional[List[str]] = None
combined_section_numbers: list[str] | None = None


class Section(NamedTuple):
Expand All @@ -81,9 +80,9 @@ class Section(NamedTuple):
class_number: str
section_type: str
status: str
instructors: Optional[List[Instructor]] = None
meetings: Optional[List[Meeting]] = None
details: Optional[SectionDetails] = None
instructors: list[Instructor] | None = None
meetings: list[Meeting] | None = None
details: SectionDetails | None = None


class Course(NamedTuple):
Expand All @@ -95,17 +94,17 @@ class Course(NamedTuple):

class CourseDetails(NamedTuple):
course: Course
course_description: Optional[str] = None
credit_range: Optional[Tuple[int]] = None
requisites: Optional[str] = None
components: List[Component] = None
attributes: List[Attribute] = None
sections: Optional[List[Section]] = None
course_description: str | None = None
credit_range: tuple[int, int] | None = None
requisites: str | None = None
components: list[Component] | None = None
attributes: list[Attribute] | None = None
sections: list[Section] | None = None


class Subject(NamedTuple):
subject_code: str
courses: Dict[str, Course]
courses: dict[str, Course]


def get_subject_courses(subject: str) -> Subject:
Expand All @@ -131,7 +130,7 @@ def get_subject_courses(subject: str) -> Subject:
return Subject(subject_code=subject, courses=courses)


def get_course_details(term: Union[str, int], subject: str, course: Union[str, int]) -> CourseDetails:
def get_course_details(term: str | int, subject: str, course: str | int) -> CourseDetails:
term = _validate_term(term)
subject = _validate_subject(subject)
course = _validate_course(course)
Expand Down Expand Up @@ -225,7 +224,7 @@ def get_course_details(term: Union[str, int], subject: str, course: Union[str, i
)


def get_section_details(term: Union[str, int], class_number: Union[str, int]) -> Section:
def get_section_details(term: str | int, class_number: str | int) -> Section:
term = _validate_term(term)

json_response = _get_section_details(term, class_number)
Expand Down Expand Up @@ -307,7 +306,7 @@ def get_section_details(term: Union[str, int], class_number: Union[str, int]) ->


# validation for method inputs
def _validate_term(term: Union[str, int]) -> str:
def _validate_term(term: str | int) -> str:
"""Validates that the term entered follows the pattern that Pitt does for term codes."""
if VALID_TERMS.match(str(term)):
return str(term)
Expand All @@ -321,7 +320,7 @@ def _validate_subject(subject: str) -> str:
raise ValueError("Subject code entered isn't a valid Pitt subject code.")


def _validate_course(course: Union[str, int]) -> str:
def _validate_course(course: str | int) -> str:
"""Validates that the course name entered is 4 characters long and in string form."""
if course == "":
raise ValueError("Invalid course number, please enter a non-empty string.")
Expand Down Expand Up @@ -368,7 +367,7 @@ def _get_section_details(term: str, section_id: str) -> dict:


# operations from api calls
def _get_subject_codes() -> List[str]:
def _get_subject_codes() -> list[str]:
response = _get_subjects()
codes = []
for subject in response["subjects"]:
Expand Down
4 changes: 0 additions & 4 deletions pittapi/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

from typing import List, NamedTuple

from requests_html import HTMLSession
from parse import compile
import requests
import urllib3

Expand Down
8 changes: 3 additions & 5 deletions pittapi/laundry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
"""

import requests
import re
from typing import Any, Dict, List, Union
from typing import Any

from bs4 import BeautifulSoup

BASE_URL = "https://www.laundryview.com/api/currentRoomData?school_desc_key=197&location={}"

Expand All @@ -46,7 +44,7 @@ def _get_laundry_info(building_name: str) -> Any:
return info


def get_status_simple(building_name: str) -> Dict[str, str]:
def get_status_simple(building_name: str) -> dict[str, str]:
"""
:returns: a dictionary with free washers and dryers as well as total washers
and dryers for given building
Expand Down Expand Up @@ -90,7 +88,7 @@ def get_status_simple(building_name: str) -> Dict[str, str]:
}


def get_status_detailed(building_name: str) -> List[Dict[str, Union[str, int]]]:
def get_status_detailed(building_name: str) -> list[dict[str, str | int]]:
"""
:returns: A list of washers and dryers for the passed
building location with their statuses
Expand Down
14 changes: 7 additions & 7 deletions pittapi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import requests
from html.parser import HTMLParser
from typing import Any, Dict, List
from typing import Any

LIBRARY_URL = (
"https://pitt.primo.exlibrisgroup.com/primaws/rest/pub/pnxs"
Expand Down Expand Up @@ -50,7 +50,7 @@ def get_data(self) -> str:
return "".join(self.data)


def get_documents(query: str, page: int = 1) -> Dict[str, Any]:
def get_documents(query: str, page: int = 1) -> dict[str, Any]:
"""Return ten resource results from the specified page"""
parsed_query = query.replace(" ", "+")
full_query = LIBRARY_URL + QUERY_START + parsed_query
Expand All @@ -61,7 +61,7 @@ def get_documents(query: str, page: int = 1) -> Dict[str, Any]:
return results


def get_document_by_bookmark(bookmark: str) -> Dict[str, Any]:
def get_document_by_bookmark(bookmark: str) -> dict[str, Any]:
"""Return resource referenced by bookmark"""
payload = {"bookMark": bookmark}
resp = sess.get(LIBRARY_URL, params=payload)
Expand All @@ -81,7 +81,7 @@ def _strip_html(html: str) -> str:
return strip.get_data()


def _extract_results(json: Dict[str, Any]) -> Dict[str, Any]:
def _extract_results(json: dict[str, Any]) -> dict[str, Any]:
results = {
"total_results": json["info"]["total"],
"pages": json["info"]["last"],
Expand All @@ -90,7 +90,7 @@ def _extract_results(json: Dict[str, Any]) -> Dict[str, Any]:
return results


def _extract_documents(documents: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
def _extract_documents(documents: list[dict[str, Any]]) -> list[dict[str, Any]]:
new_docs = []
keep_keys = {
"title",
Expand Down Expand Up @@ -119,8 +119,8 @@ def _extract_documents(documents: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
return new_docs


def _extract_facets(facet_fields: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
facets = {} # type: Dict[str,List[Dict[str,Any]]]
def _extract_facets(facet_fields: list[dict[str, Any]]) -> dict[str, list[dict[str, Any]]]:
facets: dict[str, list[dict[str, Any]]] = {}
for facet in facet_fields:
facets[facet["display_name"]] = []
for count in facet["counts"]:
Expand Down
5 changes: 2 additions & 3 deletions pittapi/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import re
import math
import requests
import grequests
from typing import Dict, List, Any
from typing import Any

sess = requests.session()

Expand All @@ -44,7 +43,7 @@ def _load_n_items(feed: str, max_news_items: int):
return responses


def get_news(feed: str = "main_news", max_news_items: int = 10) -> List[Dict[str, Any]]:
def get_news(feed: str = "main_news", max_news_items: int = 10) -> list[dict[str, Any]]:
# feed indicates the desired news feed
# 'main_news' - main news
# 'cssd' - student announcements, on my pitt
Expand Down
6 changes: 2 additions & 4 deletions pittapi/people.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
"""

from requests_html import HTMLSession
from typing import List, Dict
from parse import compile

# Please note that find.pitt.edu will not accept more than 10 requests within a few minutes
# It will time out if that happens
Expand All @@ -43,7 +41,7 @@
}


def _parse_segments(person: dict, segments: List[str]) -> None:
def _parse_segments(person: dict, segments: list[str]) -> None:
label = None
for segment in segments:
if "class" in segment.attrs and "row-label" in segment.attrs["class"]:
Expand All @@ -62,7 +60,7 @@ def _parse_segments(person: dict, segments: List[str]) -> None:
person[label] = segment.text


def get_person(query: str) -> List[Dict[str, str]]:
def get_person(query: str) -> list[dict[str, str]]:
payload = {"search": query}
session = HTMLSession()
resp = session.post(PEOPLE_SEARCH_URL, data=payload)
Expand Down
10 changes: 5 additions & 5 deletions pittapi/shuttle.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"""

import requests
from typing import List, Dict, Any
from typing import Any

sess = requests.session()


def get_map_vehicle_points(api_key: str = "8882812681") -> Dict[str, Any]:
def get_map_vehicle_points(api_key: str = "8882812681") -> dict[str, Any]:
"""Return the map location for all active vehicles."""
payload = {"ApiKey": api_key}
response = sess.get(
Expand All @@ -33,7 +33,7 @@ def get_map_vehicle_points(api_key: str = "8882812681") -> Dict[str, Any]:
return response.json()


def get_route_stop_arrivals(api_key: str = "8882812681", times_per_stop: int = 1) -> Dict[str, Any]:
def get_route_stop_arrivals(api_key: str = "8882812681", times_per_stop: int = 1) -> dict[str, Any]:
"""Return stop arrival times for all vehicles."""
payload = {"ApiKey": api_key, "TimesPerStopString": times_per_stop}
response = sess.get(
Expand All @@ -43,7 +43,7 @@ def get_route_stop_arrivals(api_key: str = "8882812681", times_per_stop: int = 1
return response.json()


def get_vehicle_route_stop_estimates(vehicle_id: str, quantity: int = 2) -> Dict[str, Any]:
def get_vehicle_route_stop_estimates(vehicle_id: str, quantity: int = 2) -> dict[str, Any]:
"""Return {quantity} stop estimates for all active vehicles."""
payload = {"vehicleIdStrings": vehicle_id, "quantity": str(quantity)}
response = sess.get(
Expand All @@ -53,7 +53,7 @@ def get_vehicle_route_stop_estimates(vehicle_id: str, quantity: int = 2) -> Dict
return response.json()


def get_routes(api_key: str = "8882812681") -> Dict[str, Any]:
def get_routes(api_key: str = "8882812681") -> dict[str, Any]:
"""Return the routes with Vehicle Route Name, Vehicle ID, and all stops, etc."""
payload = {"ApiKey": api_key}
response = sess.get(
Expand Down
Loading

0 comments on commit e5531f3

Please sign in to comment.