77
88class 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 } )"
0 commit comments