Skip to content

Commit

Permalink
[Refactor] Prepare for easy change
Browse files Browse the repository at this point in the history
  • Loading branch information
diligejy committed Jan 20, 2024
1 parent fa70e04 commit 32af2ed
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions PyCon_PL/Test_Driven_Pandas/tests/test_gradebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,32 @@
from typing import cast

def _create_group(raw_students_group:pd.DataFrame) -> pd.DataFrame:
result = pd.DataFrame()
result = pd.DataFrame(index=raw_students_group.index)
result = result.assign(
net_id = raw_students_group["NetID"].str.lower(),
net_id = raw_students_group.index,
email_address = raw_students_group['Email Address'].str.lower(),
)
result[['last_name', 'first_name']] = raw_students_group['Name'].str.split(", ", expand=True)
return result

def generate_gradebook(students_df:pd.DataFrame) -> dict[int, pd.DataFrame]:
print(
'This is index', students_df.index)
students_df.index = students_df.index.str.lower()
return {
cast(int, group): _create_group(raw_students_group=table)
for group, table in students_df.groupby("Group")
}

def test_results_are_grouped_by_student_group():
def test_results_are_grouped_by_student_group_for_students_in_one_group():
students = [{
"ID" : 1,
"Name" : "Doe, John",
"NetID" : "JXD12345",
"Email Address" : "[email protected]",
"Group" : 1,
}]
students_df = pd.DataFrame(data=students).set_index("ID")
students_df = pd.DataFrame(data=students).set_index("NetID")

result = generate_gradebook(students_df=students_df)
assert list(result.keys()) == [1]
Expand All @@ -50,7 +53,7 @@ def two_students_in_the_same_group() -> pd.DataFrame():
"Group": 1,
}
]
return pd.DataFrame(data=students).set_index("ID")
return pd.DataFrame(data=students).set_index("NetID")

def test_results_are_grouped_by_student_group_for_students_in_multiple_group():
students = [{
Expand All @@ -66,7 +69,7 @@ def test_results_are_grouped_by_student_group_for_students_in_multiple_group():
"Email Address" : "[email protected]",
"Group" : 2,
}]
students_df = pd.DataFrame(data=students).set_index("ID")
students_df = pd.DataFrame(data=students).set_index("NetID")
result = generate_gradebook(students_df=students_df)
assert list(result.keys()) == [1, 2]

Expand Down

0 comments on commit 32af2ed

Please sign in to comment.