-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphql_question_content.py
127 lines (99 loc) · 4.23 KB
/
graphql_question_content.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from leetcode.models import *
class QuestionContent(QueryTemplate):
""" A class to represent a LeetCode problem content.
Args:
title_slug (str, optional): The title slug of the problem. If provided the data is fetched when the object is created. Defaults to None."""
def __init__(self, title_slug: str = None):
super().__init__()
# Instance-specific variables
self._title_slug = title_slug
self._data = None
self._params = {'titleSlug': title_slug}
self._data_fetched: bool = False
self.question_panels: List[rich.panel.Panel] = []
if title_slug is not None:
self.fetch_data(self.title_slug)
def fetch_data(self, title_slug) -> Dict:
""" Fetches the content data for the problem.
Args:
title_slug (str, optional): The title slug of the problem. Defaults to None.
Returns:
Dict: The content data for the problem.
"""
try:
with Loader('Fetching question details...', ''):
# If provided title slug does not change anything, return the data
if title_slug is not None and title_slug != self.title_slug:
self.title_slug = title_slug
if self.data_fetched:
return self.data
graphql_query = GraphQLQuery(self.query, self.params)
response = self.leet_API.post_query(graphql_query)
if response['data']['question'] is None:
raise Exception('There is no question with title slug: ' + title_slug)
self.data = response['data']['question']['content']
self.data_fetched = True
return self.data
except Exception as e:
console.print(f"{e.__class__.__name__}: {e}", style=ALERT)
sys.exit(1)
def show(self):
""" Displays the question panels for the current LeetCode question.
If the data has not been fetched yet, an exception is raised.
"""
if self.data_fetched:
self.question_panels = LeetQuestionToSections(self.data)
for x in self.question_panels:
console.print(x)
else:
raise Exception("Data is not fetched yet.")
def __rich_console__(self, console: Console, options):
""" Renders the question content in a rich console.
If the data has been fetched, the question panels are generated using the LeetQuestionToSections function and yielded.
If the data has not been fetched, an exception is raised.
Args:
console (Console): The console to render the content in.
options: Additional options for rendering the content.
Raises:
Exception: If the data has not been fetched yet.
"""
if self.data_fetched:
self.question_panels = LeetQuestionToSections(self.data)
for x in self.question_panels:
yield x
else:
# raise exception that data is not fetched
raise Exception("Data is not fetched yet.")
@property
def data(self):
return self._data
@data.setter
def data(self, data: dict):
if data is None:
raise ValueError(f"Data for question with title slug '{self.title_slug}' is None.")
self._data = data
@property
def params(self):
return self._params
@params.setter
def params(self, params: dict):
self._params = params
@property
def title_slug(self):
return self._title_slug
@title_slug.setter
def title_slug(self, title_slug: str):
self._title_slug = title_slug
self._data_fetched = False
self.params = {'titleSlug': title_slug}
@property
def data_fetched(self):
return self._data_fetched
@data_fetched.setter
def data_fetched(self, data_fetched: bool):
self._data_fetched = data_fetched
if __name__ == '__main__':
content = QuestionContent('two-sum')
print(content)
content.fetch_data('add-two-integers')
print(content)