Skip to content

Commit

Permalink
Merge pull request #199 from pittcsc/mypy-gym
Browse files Browse the repository at this point in the history
Fix `mypy` errors and rename `NamedTuple` fields in `gym.py`
  • Loading branch information
tianyizheng02 authored Aug 25, 2024
2 parents 39f1fbd + f28fdc4 commit 40c9a14
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
15 changes: 7 additions & 8 deletions pittapi/gym.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@

class Gym(NamedTuple):
name: str
date: str
count: int
percentage: int
last_updated: str | None = None
current_count: int | None = None
percent_full: int | None = None

@classmethod
def from_text(cls, text: str) -> Gym:
info = text.split("|")
name = info[0]
if len(info) < 4:
return cls(name=name, date=None, count=None, percentage=None)
return cls(name=name)
count = int(info[2][12:])
date = info[3][9:]
date_time = info[3][9:]
try:
percentage = int(info[4].rstrip("%"))
except ValueError:
percentage = 0

return cls(name=name, date=date, count=count, percentage=percentage)
return cls(name=name, last_updated=date_time, current_count=count, percent_full=percentage)


def get_all_gyms_info() -> list[Gym]:
Expand All @@ -71,7 +71,6 @@ def get_all_gyms_info() -> list[Gym]:
soup = BeautifulSoup(page.text, "html.parser")
gym_info_list = soup.find_all("div", class_="barChart")

# Iterate through list and add to dictionary
gyms = [Gym.from_text(gym.get_text("|", strip=True)) for gym in gym_info_list]
return gyms

Expand All @@ -81,6 +80,6 @@ def get_gym_info(gym_name: str) -> Gym | None:
info = get_all_gyms_info()
if gym_name in GYM_NAMES:
for gym in info:
if gym.name == gym_name and gym.date and gym.count and gym.percentage:
if gym.name == gym_name and gym.last_updated and gym.current_count and gym.percent_full:
return gym
return None
29 changes: 18 additions & 11 deletions tests/gym_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@ def test_fetch_gym_info(self):

gym_info = gym.get_all_gyms_info()
expected_info = [
gym.Gym(name="Baierl Rec Center", date="07/09/2024 09:05 AM", count=100, percentage=50),
gym.Gym(name="Bellefield Hall: Fitness Center & Weight Room", date="07/09/2024 09:05 AM", count=50, percentage=0),
gym.Gym(name="Bellefield Hall: Court & Dance Studio", date=None, count=None, percentage=None),
gym.Gym(name="Trees Hall: Fitness Center", date="07/09/2024 09:05 AM", count=70, percentage=58),
gym.Gym(name="Trees Hall: Courts", date="07/09/2024 09:05 AM", count=20, percentage=33),
gym.Gym(name="Baierl Rec Center", last_updated="07/09/2024 09:05 AM", current_count=100, percent_full=50),
gym.Gym(
name="Bellefield Hall: Fitness Center & Weight Room",
last_updated="07/09/2024 09:05 AM",
current_count=50,
percent_full=0,
),
gym.Gym(name="Bellefield Hall: Court & Dance Studio"),
gym.Gym(name="Trees Hall: Fitness Center", last_updated="07/09/2024 09:05 AM", current_count=70, percent_full=58),
gym.Gym(name="Trees Hall: Courts", last_updated="07/09/2024 09:05 AM", current_count=20, percent_full=33),
gym.Gym(
name="Trees Hall: Racquetball Courts & Multipurpose Room",
date="07/09/2024 09:05 AM",
count=10,
percentage=25,
last_updated="07/09/2024 09:05 AM",
current_count=10,
percent_full=25,
),
gym.Gym(name="William Pitt Union", date="07/09/2024 09:05 AM", count=25, percentage=25),
gym.Gym(name="Pitt Sports Dome", date="07/09/2024 09:05 AM", count=15, percentage=20),
gym.Gym(name="William Pitt Union", last_updated="07/09/2024 09:05 AM", current_count=25, percent_full=25),
gym.Gym(name="Pitt Sports Dome", last_updated="07/09/2024 09:05 AM", current_count=15, percent_full=20),
]

self.assertEqual(gym_info, expected_info)
Expand All @@ -37,7 +42,9 @@ def test_get_gym_info(self):
responses.add(responses.GET, gym.GYM_URL, body=mock_gym_html, status=200)

gym_info = gym.get_gym_info("Baierl Rec Center")
expected_info = gym.Gym(name="Baierl Rec Center", date="07/09/2024 09:05 AM", count=100, percentage=50)
expected_info = gym.Gym(
name="Baierl Rec Center", last_updated="07/09/2024 09:05 AM", current_count=100, percent_full=50
)
self.assertEqual(gym_info, expected_info)

@responses.activate
Expand Down

0 comments on commit 40c9a14

Please sign in to comment.