Skip to content

Commit a1ca02b

Browse files
Merge pull request #298 from dvonthenen/implement-dict-for-response
Fix Accessing Object via Dictionary
2 parents 9784ce0 + 1a00262 commit a1ca02b

File tree

5 files changed

+555
-194
lines changed

5 files changed

+555
-194
lines changed

deepgram/clients/analyze/v1/response.py

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def __getitem__(self, key):
2020
_dict = self.to_dict()
2121
return _dict[key]
2222

23+
def __str__(self) -> str:
24+
return self.to_json(indent=4)
25+
2326

2427
# Analyze Response Types:
2528

@@ -35,6 +38,9 @@ def __getitem__(self, key):
3538
_dict = self.to_dict()
3639
return _dict[key]
3740

41+
def __str__(self) -> str:
42+
return self.to_json(indent=4)
43+
3844

3945
@dataclass_json
4046
@dataclass
@@ -47,6 +53,9 @@ def __getitem__(self, key):
4753
_dict = self.to_dict()
4854
return _dict[key]
4955

56+
def __str__(self) -> str:
57+
return self.to_json(indent=4)
58+
5059

5160
@dataclass_json
5261
@dataclass
@@ -59,6 +68,9 @@ def __getitem__(self, key):
5968
_dict = self.to_dict()
6069
return _dict[key]
6170

71+
def __str__(self) -> str:
72+
return self.to_json(indent=4)
73+
6274

6375
@dataclass_json
6476
@dataclass
@@ -71,6 +83,9 @@ def __getitem__(self, key):
7183
_dict = self.to_dict()
7284
return _dict[key]
7385

86+
def __str__(self) -> str:
87+
return self.to_json(indent=4)
88+
7489

7590
@dataclass_json
7691
@dataclass
@@ -93,27 +108,37 @@ class Metadata:
93108

94109
def __getitem__(self, key):
95110
_dict = self.to_dict()
96-
if _dict["intents_info"] is not None:
111+
if "intents_info" in _dict:
97112
_dict["intents_info"] = IntentsInfo.from_dict(_dict["intents_info"])
98-
if _dict["sentiment_info"] is not None:
113+
if "sentiment_info" in _dict:
99114
_dict["sentiment_info"] = SentimentInfo.from_dict(_dict["sentiment_info"])
100-
if _dict["summary_info"] is not None:
115+
if "summary_info" in _dict:
101116
_dict["summary_info"] = SummaryInfo.from_dict(_dict["summary_info"])
102-
if _dict["topics_info"] is not None:
117+
if "topics_info" in _dict:
103118
_dict["topics_info"] = TopicsInfo.from_dict(_dict["topics_info"])
104119
return _dict[key]
105120

121+
def __str__(self) -> str:
122+
return self.to_json(indent=4)
123+
106124

107125
@dataclass_json
108126
@dataclass
109127
class Average:
110-
sentiment: Optional[Sentiment] = ""
128+
sentiment: Optional[Sentiment] = field(
129+
default=None, metadata=config(exclude=lambda f: f is None)
130+
)
111131
sentiment_score: Optional[float] = 0
112132

113133
def __getitem__(self, key):
114134
_dict = self.to_dict()
135+
if "sentiment" in _dict:
136+
_dict["sentiment"] = Sentiment.from_dict(_dict["sentiment"])
115137
return _dict[key]
116138

139+
def __str__(self) -> str:
140+
return self.to_json(indent=4)
141+
117142

118143
@dataclass_json
119144
@dataclass
@@ -124,6 +149,9 @@ def __getitem__(self, key):
124149
_dict = self.to_dict()
125150
return _dict[key]
126151

152+
def __str__(self) -> str:
153+
return self.to_json(indent=4)
154+
127155

128156
@dataclass_json
129157
@dataclass
@@ -135,6 +163,9 @@ def __getitem__(self, key):
135163
_dict = self.to_dict()
136164
return _dict[key]
137165

166+
def __str__(self) -> str:
167+
return self.to_json(indent=4)
168+
138169

139170
@dataclass_json
140171
@dataclass
@@ -146,14 +177,19 @@ def __getitem__(self, key):
146177
_dict = self.to_dict()
147178
return _dict[key]
148179

180+
def __str__(self) -> str:
181+
return self.to_json(indent=4)
182+
149183

150184
@dataclass_json
151185
@dataclass
152186
class Segment:
153187
text: Optional[str] = ""
154188
start_word: Optional[int] = 0
155189
end_word: Optional[int] = 0
156-
sentiment: Optional[Sentiment] = ""
190+
sentiment: Optional[Sentiment] = field(
191+
default=None, metadata=config(exclude=lambda f: f is None)
192+
)
157193
sentiment_score: Optional[float] = 0
158194
intents: Optional[List[Intent]] = field(
159195
default=None, metadata=config(exclude=lambda f: f is None)
@@ -164,57 +200,79 @@ class Segment:
164200

165201
def __getitem__(self, key):
166202
_dict = self.to_dict()
167-
if _dict["intents"] is not None:
203+
if "sentiment" in _dict:
204+
_dict["sentiment"] = Sentiment.from_dict(_dict["sentiment"])
205+
if "intents" in _dict:
168206
_dict["intents"] = Intent.from_dict(_dict["intents"])
169-
if _dict["topics"] is not None:
207+
if "topics" in _dict:
170208
_dict["topics"] = Topic.from_dict(_dict["topics"])
171209
return _dict[key]
172210

211+
def __str__(self) -> str:
212+
return self.to_json(indent=4)
213+
173214

174215
@dataclass_json
175216
@dataclass
176217
class Sentiments:
177-
segments: Optional[List[Segment]] = None
178-
average: Optional[Average] = None
218+
segments: Optional[List[Segment]] = field(
219+
default=None, metadata=config(exclude=lambda f: f is None)
220+
)
221+
average: Optional[Average] = field(
222+
default=None, metadata=config(exclude=lambda f: f is None)
223+
)
179224

180225
def __getitem__(self, key):
181226
_dict = self.to_dict()
182-
if _dict["segments"] is not None:
227+
if "segments" in _dict:
183228
_dict["segments"] = [
184-
Segment.from_dict(segments) for _, segments in _dict["segments"].items()
229+
Segment.from_dict(segments) for segments in _dict["segments"]
185230
]
186-
if _dict["average"] is not None:
231+
if "average" in _dict:
187232
_dict["average"] = Average.from_dict(_dict["average"])
188233
return _dict[key]
189234

235+
def __str__(self) -> str:
236+
return self.to_json(indent=4)
237+
190238

191239
@dataclass_json
192240
@dataclass
193241
class Topics:
194-
segments: Optional[List[Segment]] = None
242+
segments: Optional[List[Segment]] = field(
243+
default=None, metadata=config(exclude=lambda f: f is None)
244+
)
195245

196246
def __getitem__(self, key):
197247
_dict = self.to_dict()
198-
if _dict["segments"] is not None:
248+
if "segments" in _dict:
199249
_dict["segments"] = [
200-
Segment.from_dict(segments) for _, segments in _dict["segments"].items()
250+
Segment.from_dict(segments) for segments in _dict["segments"]
201251
]
202252
return _dict[key]
203253

254+
def __str__(self) -> str:
255+
return self.to_json(indent=4)
256+
204257

205258
@dataclass_json
206259
@dataclass
207260
class Intents:
208-
segments: Optional[List[Segment]] = None
261+
segments: Optional[List[Segment]] = field(
262+
default=None, metadata=config(exclude=lambda f: f is None)
263+
)
209264

210265
def __getitem__(self, key):
211266
_dict = self.to_dict()
212-
if _dict["segments"] is not None:
267+
if "segments" in _dict:
213268
_dict["segments"] = [
214-
Segment.from_dict(segments) for _, segments in _dict["segments"].items()
269+
Segment.from_dict(segments) for segments in _dict["segments"]
215270
]
216271
return _dict[key]
217272

273+
def __str__(self) -> str:
274+
return self.to_json(indent=4)
275+
218276

219277
@dataclass_json
220278
@dataclass
@@ -234,34 +292,44 @@ class Results:
234292

235293
def __getitem__(self, key):
236294
_dict = self.to_dict()
237-
if _dict["summary"] is not None:
295+
if "summary" in _dict:
238296
_dict["summary"] = Summary.from_dict(_dict["summary"])
239-
if _dict["sentiments"] is not None:
297+
if "sentiments" in _dict:
240298
_dict["sentiments"] = Sentiments.from_dict(_dict["sentiments"])
241-
if _dict["topics"] is not None:
299+
if "topics" in _dict:
242300
_dict["topics"] = Topics.from_dict(_dict["topics"])
243-
if _dict["intents"] is not None:
301+
if "intents" in _dict:
244302
_dict["intents"] = Intents.from_dict(_dict["intents"])
245303
return _dict[key]
246304

305+
def __str__(self) -> str:
306+
return self.to_json(indent=4)
307+
247308

248309
# Analyze Response Result:
249310

250311

251312
@dataclass_json
252313
@dataclass
253314
class AnalyzeResponse:
254-
metadata: Optional[Metadata] = None
255-
results: Optional[Results] = None
315+
metadata: Optional[Metadata] = field(
316+
default=None, metadata=config(exclude=lambda f: f is None)
317+
)
318+
results: Optional[Results] = field(
319+
default=None, metadata=config(exclude=lambda f: f is None)
320+
)
256321

257322
def __getitem__(self, key):
258323
_dict = self.to_dict()
259-
if _dict["metadata"] is not None:
324+
if "metadata" in _dict:
260325
_dict["metadata"] = Metadata.from_dict(_dict["metadata"])
261-
if _dict["results"] is not None:
326+
if "results" in _dict:
262327
_dict["results"] = Results.from_dict(_dict["results"])
263328
return _dict[key]
264329

330+
def __str__(self) -> str:
331+
return self.to_json(indent=4)
332+
265333

266334
@dataclass_json
267335
@dataclass

0 commit comments

Comments
 (0)