Skip to content

Commit 23c0e62

Browse files
Refactor code for improved formatting consistency
Reformatted method calls and multiline statements for better readability and alignment with PEP 8 guidelines. No functionality was modified, ensuring behavior remains unchanged.
1 parent 11cacb6 commit 23c0e62

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

api/CachedLeetCodeAPI.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ def __init__(self, cache_dir: Optional[str] = None, cache_expiry: int = 3600):
1717
:param cache_expiry: Expiry time for cache in seconds (default: 1 hour).
1818
"""
1919
# Use the system's temporary directory if no cache directory is provided
20-
self.cache_dir = Path(cache_dir or tempfile.gettempdir()) / "cached_leetcode_api"
20+
self.cache_dir = (
21+
Path(cache_dir or tempfile.gettempdir()) / "cached_leetcode_api"
22+
)
2123
self.cache_expiry = cache_expiry # Expiry time in seconds
22-
self.cache_dir.mkdir(parents=True, exist_ok=True) # Create cache directory if it doesn't exist
24+
self.cache_dir.mkdir(
25+
parents=True, exist_ok=True
26+
) # Create cache directory if it doesn't exist
2327

24-
self.api = LeetCodeAPI() # Delegate actual API calls to existing LeetCodeAPI class
28+
self.api = (
29+
LeetCodeAPI()
30+
) # Delegate actual API calls to existing LeetCodeAPI class
2531

2632
def _get_cache_key(self, unique_id: str) -> str:
2733
"""
2834
Generate a cache key based on a unique identifier (e.g., query or API parameters).
2935
:param unique_id: Unique identifier for the API request (e.g., slug, query, variable JSON).
3036
:return: Cache filename based on the MD5 hash of the unique ID.
3137
"""
32-
return hashlib.md5(unique_id.encode('utf-8')).hexdigest()
38+
return hashlib.md5(unique_id.encode("utf-8")).hexdigest()
3339

3440
def _read_from_cache(self, cache_key: str) -> Any:
3541
"""
@@ -56,7 +62,9 @@ def _write_to_cache(self, cache_key: str, data: Any) -> None:
5662
with open(cache_file, "w") as f:
5763
json.dump(data, f)
5864

59-
def _fetch_with_cache(self, fetch_func: Callable, unique_id: str, *args, **kwargs) -> Any:
65+
def _fetch_with_cache(
66+
self, fetch_func: Callable, unique_id: str, *args, **kwargs
67+
) -> Any:
6068
"""
6169
Fetch data with caching.
6270
:param fetch_func: Function to fetch data if cache is not available.
@@ -87,24 +95,32 @@ def fetch_problems(self, *args, **kwargs):
8795
Cached version of fetch_problems
8896
"""
8997
unique_id = f"fetch_problems-{json.dumps([args, kwargs], sort_keys=True)}" # Unique key based on args
90-
return self._fetch_with_cache(self.api.fetch_problems, unique_id, *args, **kwargs)
98+
return self._fetch_with_cache(
99+
self.api.fetch_problems, unique_id, *args, **kwargs
100+
)
91101

92102
def fetch_daily_challenge(self, *args, **kwargs):
93103
"""
94104
Cached version of fetch_daily_challenge
95105
"""
96-
return self._fetch_with_cache(self.api.fetch_daily_challenge, "fetch_daily_challenge")
106+
return self._fetch_with_cache(
107+
self.api.fetch_daily_challenge, "fetch_daily_challenge"
108+
)
97109

98110
def fetch_problem(self, problem_slug: str, *args, **kwargs):
99111
"""
100112
Cached version of fetch_problem
101113
"""
102114
unique_id = f"fetch_problem-{problem_slug}"
103-
return self._fetch_with_cache(self.api.fetch_problem, unique_id, problem_slug, *args, **kwargs)
115+
return self._fetch_with_cache(
116+
self.api.fetch_problem, unique_id, problem_slug, *args, **kwargs
117+
)
104118

105119
def get_study_plan(self, slug: str, *args, **kwargs):
106120
"""
107121
Cached version of get_study_plan
108122
"""
109123
unique_id = f"get_study_plan-{slug}"
110-
return self._fetch_with_cache(self.api.get_study_plan, unique_id, slug, *args, **kwargs)
124+
return self._fetch_with_cache(
125+
self.api.get_study_plan, unique_id, slug, *args, **kwargs
126+
)

handlers/PracticeHandler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ def get_random_problem(
9494
:param difficulties: Difficulty levels of the problems (e.g., "Easy", "Medium", "Hard").
9595
:return: A random problem dictionary or None if no problems are found.
9696
"""
97-
problems = cached_api.fetch_problems(
98-
limit=1000, difficulties=difficulties
99-
)
97+
problems = cached_api.fetch_problems(limit=1000, difficulties=difficulties)
10098

10199
if not problems:
102100
return None

0 commit comments

Comments
 (0)