Private API¶
These are internal modules
_func_parsing.py¶
Functions for parsing leetcode code snippet
- generate_function_ast(code: str) FunctionDef | None [source]¶
Generates abstract syntax tree (ast) from code
- Parameters:
code (str) – class source code
- Returns:
function ast for the leetcode problem
- Return type:
ast.FunctionDef
- get_params(function_ast: FunctionDef) tuple [source]¶
Gets function parameters and types
- Parameters:
function_ast (ast.FunctionDef) – function ast
- Returns:
list of parameters and ist of types
- Return type:
tuple
- get_rtype(function_ast: FunctionDef) str | None [source]¶
Gets function return type
- Parameters:
function_ast (ast.FunctionDef) – function ast
- Returns:
the return type of the function
- Return type:
str
_leetcode_api¶
Functions to fetch data from LeetCode API
- fetch_problems() list [source]¶
Fetch all problems from LeetCode API
- Returns:
tuples with num, title, slug, and question id for each problem
- Return type:
list
- _fetch_graphql(payload: dict) dict [source]¶
Fetch data from LeetCode GraphQL API
- Parameters:
payload (dict) – json payload to send
- Returns:
json response
- Return type:
dict
_parsing.py¶
Functions for parsing leetcode problem info
- extract_fields(problem_info: dict, synced_code: dict) dict [source]¶
Extracts fields from problem info and synced code
- Parameters:
problem_info (dict) – problem info dict
synced_code (dict) – synced user python code
- Returns:
all the problem info fields
- Return type:
dict
- _format_and_split_content(content: str) namedtuple [source]¶
Formats html content and splits into sections
- Parameters:
content (str) – leetcode problem content
- Returns:
description, examples, and constraints from content
- Return type:
tuple
- _html_to_rst(text: str) str [source]¶
Replaces html tags with rst syntax
- Parameters:
text (str) – html text
- Returns:
rst formatted text
- Return type:
str
- _extract_python_snippet(snippets: dict) str | None [source]¶
Extracts python code snippet from list of snippets
- Parameters:
snippets (dict) – leetcode problem code snippets
- Returns:
the python3 snippet if present
- Return type:
str
- _extract_examples(examples_section: str) list [source]¶
Extract and parse each example from examples section
- Parameters:
examples_section (dict) – leetcode content section containing examples
- Returns:
dict with input, output, img, and explanation for each example
- Return type:
list
_problem_index.py¶
Database for storing leetcode problem num, title, slug, and question id
Implements context manager protocol with sqlite3
- class ProblemIndex[source]¶
Manages a database connection to store and retrieve LeetCode problems info
GraphQL queries require a title-slug and question-id separate from the frontend id/number. Leetcode has a public listing of all problems, but fetching + scanning that is slow. Instead, cache the results so that operation is not repeated.
- _query(stmt: str, params: tuple) Row [source]¶
Executes a query.
- Parameters:
stmt (str) – SQL statement to execute
params (tuple) – associated parameters
want_value (bool) – if True, return query result
- Returns:
query result (if want_value)
- Return type:
sqlite3.Row
- update_problems(problems: list)[source]¶
Updates the problems table with the given problems.
- Parameters:
problems – list of problems to insert
- insert_problem(num: str, title: str, slug: str, question_id: str)[source]¶
Inserts a problem into the problems table.
- Parameters:
num (str) – problem number
title (str) – problem title
slug (str) – problem title slug
question_id (str) – problem internal id
_util.py¶
Common utility functions