Skip to content

Commit 6dc047c

Browse files
committed
chore: cleanup
1 parent 0fcecf2 commit 6dc047c

File tree

4 files changed

+294
-128
lines changed

4 files changed

+294
-128
lines changed

openproficiency/ProficiencyScore.py

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
class ProficiencyScoreName(Enum):
99
"""Enum for proficiency score names."""
10+
1011
UNAWARE = 0.0
1112
AWARE = 0.1
1213
FAMILIAR = 0.5
@@ -22,11 +23,11 @@ def __init__(
2223
self,
2324
# Required
2425
topic_id: str,
25-
score: Union[float, ProficiencyScoreName]
26+
score: Union[float, ProficiencyScoreName],
2627
):
2728
# Required
2829
self.topic_id = topic_id
29-
self._set_score(score)
30+
self.score = score
3031

3132
# Properties - Score
3233
@property
@@ -37,67 +38,60 @@ def score(self) -> float:
3738
@score.setter
3839
def score(self, value: Union[float, ProficiencyScoreName]) -> None:
3940
"""Set the score numerically or using a ProficiencyScoreName enum."""
40-
self._set_score(value)
41+
# If numeric, directly store it.
42+
if isinstance(value, (int, float)):
43+
if not (0.0 <= value <= 1.0):
44+
raise ValueError(f"Score must be between 0.0 and 1.0, got {value}")
45+
self._score = float(value)
46+
47+
# If enum, store as numeric value.
48+
elif isinstance(value, ProficiencyScoreName):
49+
self._score = value.value
50+
51+
else:
52+
raise ValueError(f"Score must be numeric or ProficiencyScoreName enum. Got type: '{type(value)}'")
4153

4254
# Properties - Score
4355
@property
4456
def score_name(self) -> ProficiencyScoreName:
4557
"""Get the proficiency name as an enum value."""
46-
return self._get_name_from_score(self._score)
47-
48-
@score_name.setter
49-
def score_name(self, value: ProficiencyScoreName) -> None:
50-
"""Set the proficiency name using a ProficiencyScoreName enum."""
51-
if not isinstance(value, ProficiencyScoreName):
52-
raise ValueError(
53-
f"Name must be a ProficiencyScoreName enum, got {type(value)}")
54-
self._score = value.value
58+
return ProficiencyScore.get_score_name(self._score)
5559

5660
# Methods
57-
def _set_score(self, value: Union[float, ProficiencyScoreName]) -> None:
58-
"""Internal method to set score from numeric or enum value."""
59-
if isinstance(value, ProficiencyScoreName):
60-
self._score = value.value
61-
62-
elif isinstance(value, (int, float)):
63-
# Validate score is between 0.0 and 1.0
64-
if not (0.0 <= value <= 1.0):
65-
raise ValueError(
66-
f"Score must be between 0.0 and 1.0, got {value}")
67-
self._score = float(value)
68-
69-
else:
70-
raise ValueError(
71-
f"Score must be numeric or ProficiencyScoreName enum. Got type: '{type(value)}'")
72-
73-
def _get_name_from_score(self, score: float) -> ProficiencyScoreName:
74-
"""Internal method to determine proficiency name from numeric score."""
75-
if score <= 0.0:
76-
return ProficiencyScoreName.UNAWARE
77-
elif score <= 0.1:
78-
return ProficiencyScoreName.AWARE
79-
elif score <= 0.5:
80-
return ProficiencyScoreName.FAMILIAR
81-
elif score <= 0.8:
82-
return ProficiencyScoreName.PROFICIENT
83-
elif score <= 1.0:
84-
return ProficiencyScoreName.PROFICIENT_WITH_EVIDENCE
85-
else:
86-
raise ValueError(f"Invalid score value: {score}")
87-
88-
def to_dict(self) -> dict:
61+
def to_dict(self) -> dict[str, float]:
8962
"""Convert to a JSON-serializable dictionary."""
9063
return {
9164
"topic_id": self.topic_id,
92-
"score": self._score
65+
"score": self._score,
9366
}
9467

9568
def to_json(self) -> str:
9669
"""Convert to a JSON string."""
9770
return json.dumps(self.to_dict())
9871

99-
# Debugging
72+
# Static Methods
73+
@staticmethod
74+
def get_score_name(score: float) -> ProficiencyScoreName:
75+
"""Internal method to determine proficiency name from numeric score."""
76+
if score == 1.0:
77+
return ProficiencyScoreName.PROFICIENT_WITH_EVIDENCE
10078

79+
elif score >= 0.8:
80+
return ProficiencyScoreName.PROFICIENT
81+
82+
elif score >= 0.5:
83+
return ProficiencyScoreName.FAMILIAR
84+
85+
elif score >= 0.1:
86+
return ProficiencyScoreName.AWARE
87+
88+
elif score >= 0.0:
89+
return ProficiencyScoreName.UNAWARE
90+
91+
else:
92+
raise ValueError(f"Invalid score value: {score}")
93+
94+
# Debugging
10195
def __repr__(self) -> str:
10296
"""String representation of ProficiencyScore."""
10397
return f"ProficiencyScore(topic_id='{self.topic_id}', score={self._score}, name={self.score_name.name})"

openproficiency/TranscriptEntry.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,22 @@ def to_json(self) -> str:
5252
"""Convert TranscriptEntry to JSON string."""
5353
return json.dumps(self.to_dict())
5454

55-
# Methods - Class
56-
@classmethod
57-
def from_dict(cls, data: dict[str, Union[str, int, float]]) -> "TranscriptEntry":
55+
# Methods - Static
56+
@staticmethod
57+
def from_dict(data: dict[str, Union[str, int, float]]) -> "TranscriptEntry":
5858
"""Create a TranscriptEntry from a dictionary."""
59-
return cls(
59+
return TranscriptEntry(
6060
user_id=data["user_id"],
6161
topic_id=data["topic_id"],
6262
score=data["score"],
6363
issuer=data["issuer"],
6464
timestamp=datetime.fromisoformat(data["timestamp"]),
6565
)
6666

67-
@classmethod
68-
def from_json(cls, json_str: str) -> "TranscriptEntry":
67+
@staticmethod
68+
def from_json(json_str: str) -> "TranscriptEntry":
6969
"""Create a TranscriptEntry from a JSON string."""
70-
return cls.from_dict(json.loads(json_str))
70+
return TranscriptEntry.from_dict(json.loads(json_str))
7171

7272
# Debugging
7373
def __repr__(self) -> str:

0 commit comments

Comments
 (0)