Skip to content

Commit 0fcecf2

Browse files
committed
feat: add from_dict, to_json, and general cleanup
1 parent ad1186b commit 0fcecf2

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

openproficiency/TranscriptEntry.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import json
44
from datetime import datetime
5-
from typing import Optional
5+
from typing import Optional, Union
66
from .ProficiencyScore import ProficiencyScore
77

88

@@ -17,15 +17,14 @@ def __init__(
1717
topic_id: str,
1818
score: float,
1919
issuer: str,
20-
2120
# Optional
2221
timestamp: Optional[datetime] = None,
2322
):
2423
# Required
2524
self.user_id = user_id
2625
self._proficiency_score = ProficiencyScore(
2726
topic_id=topic_id,
28-
score=score
27+
score=score,
2928
)
3029
self.issuer = issuer
3130

@@ -39,21 +38,43 @@ def proficiency_score(self) -> ProficiencyScore:
3938
return self._proficiency_score
4039

4140
# Methods
42-
def to_dict(self) -> dict:
43-
"""Convert Topic to JSON-serializable dictionary."""
41+
def to_dict(self) -> dict[str, Union[str, int, float]]:
42+
"""Convert TranscriptEntry to JSON-serializable dictionary."""
4443
return {
4544
"user_id": self.user_id,
4645
"topic_id": self._proficiency_score.topic_id,
4746
"score": self._proficiency_score.score,
4847
"issuer": self.issuer,
49-
"timestamp": self.timestamp.isoformat()
48+
"timestamp": self.timestamp.isoformat(),
5049
}
5150

5251
def to_json(self) -> str:
53-
"""Convert Topic to JSON string."""
52+
"""Convert TranscriptEntry to JSON string."""
5453
return json.dumps(self.to_dict())
5554

55+
# Methods - Class
56+
@classmethod
57+
def from_dict(cls, data: dict[str, Union[str, int, float]]) -> "TranscriptEntry":
58+
"""Create a TranscriptEntry from a dictionary."""
59+
return cls(
60+
user_id=data["user_id"],
61+
topic_id=data["topic_id"],
62+
score=data["score"],
63+
issuer=data["issuer"],
64+
timestamp=datetime.fromisoformat(data["timestamp"]),
65+
)
66+
67+
@classmethod
68+
def from_json(cls, json_str: str) -> "TranscriptEntry":
69+
"""Create a TranscriptEntry from a JSON string."""
70+
return cls.from_dict(json.loads(json_str))
71+
5672
# Debugging
5773
def __repr__(self) -> str:
5874
"""String representation of TranscriptEntry."""
59-
return f"TranscriptEntry(user_id='{self.user_id}', topic_id='{self._proficiency_score.topic_id}', score={self._proficiency_score.score}, issuer='{self.issuer}')"
75+
return (
76+
f"TranscriptEntry(user_id='{self.user_id}', "
77+
f"topic_id='{self._proficiency_score.topic_id}', "
78+
f"score={self._proficiency_score.score}, "
79+
f"issuer='{self.issuer}'"
80+
)

tests/TranscriptEntry_test.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Tests for the TranscriptEntry class."""
22

33
from datetime import datetime
4-
from openproficiency import TranscriptEntry, ProficiencyScore, ProficiencyScoreName
4+
from openproficiency import TranscriptEntry
55

66

77
class TestTranscriptEntry:
@@ -21,13 +21,13 @@ def test_init_required_params(self):
2121
user_id=user_id,
2222
topic_id=topic_id,
2323
score=score,
24-
issuer=issuer
24+
issuer=issuer,
2525
)
2626

2727
# Assert
2828
assert entry.user_id == user_id
29-
assert entry._proficiency_score.topic_id == topic_id
30-
assert entry._proficiency_score.score == score
29+
assert entry.proficiency_score.topic_id == topic_id
30+
assert entry.proficiency_score.score == score
3131
assert entry.issuer == issuer
3232
assert entry.timestamp is not None
3333

@@ -47,7 +47,7 @@ def test_init_with_optional_params(self):
4747
topic_id=topic_id,
4848
score=score,
4949
issuer=issuer,
50-
timestamp=timestamp
50+
timestamp=timestamp,
5151
)
5252

5353
# Assert
@@ -68,7 +68,7 @@ def test_init_default_timestamp(self):
6868
user_id=user_id,
6969
topic_id=topic_id,
7070
score=score,
71-
issuer=issuer
71+
issuer=issuer,
7272
)
7373
after = datetime.now()
7474

@@ -85,7 +85,7 @@ def test_proficiency_score(self):
8585
user_id="user-123",
8686
topic_id="git-commit",
8787
score=0.8,
88-
issuer="github-learn"
88+
issuer="github-learn",
8989
)
9090

9191
# Act
@@ -106,7 +106,7 @@ def test_to_dict(self):
106106
topic_id="git-commit",
107107
score=0.8,
108108
issuer="github-learn",
109-
timestamp=datetime(2024, 1, 15, 10, 30, 0)
109+
timestamp=datetime(2024, 1, 15, 10, 30, 0),
110110
)
111111

112112
# Act
@@ -118,7 +118,7 @@ def test_to_dict(self):
118118
"topic_id": "git-commit",
119119
"score": 0.8,
120120
"issuer": "github-learn",
121-
"timestamp": "2024-01-15T10:30:00"
121+
"timestamp": "2024-01-15T10:30:00",
122122
}
123123

124124
def test_to_json(self):
@@ -130,7 +130,7 @@ def test_to_json(self):
130130
topic_id="git-commit",
131131
score=0.8,
132132
issuer="github-learn",
133-
timestamp=datetime(2024, 1, 15, 10, 30, 0)
133+
timestamp=datetime(2024, 1, 15, 10, 30, 0),
134134
)
135135

136136
# Act
@@ -153,7 +153,7 @@ def test_repr(self):
153153
user_id="user-123",
154154
topic_id="git-commit",
155155
score=0.8,
156-
issuer="github-learn"
156+
issuer="github-learn",
157157
)
158158

159159
# Act

0 commit comments

Comments
 (0)