Private API

These are internal modules

_cookies.py

Get cookie for LeetCode API

get_cookies(cookie_file: str) dict[source]

Load all cookies from a Netscape HTTP Cookie File

Parameters:

cookie_file (str) – path to Netscape HTTP Cookie File

Returns:

dictionary of all cookie keys and values

Return type:

dict

_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

_add_pass_to_functions(class_src: str) str[source]

Adds pass to each function in class source code to allow for ast parsing

Parameters:

class_src – source code of class

Returns: source code with pass added to each function

_parse_annotation(node: None) str[source]

Converts the annotation binary tree to a string

Travels down recursively, don’t expect many levels…

Parameters:

ast.arg.annotation – the annotation object

Returns:

the annotation string

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

fetch_synced_code(question_id: int) dict[source]

Fetch synced code from LeetCode GraphQL API

Parameters:

question_id (int) – problem question id (not frontend id)

Returns:

json response with synced code

Return type:

dict

fetch_problem_info(slug: str) dict[source]

Fetch problem info

Parameters:

slug (str) – problem title slug

Returns:

json response with problem info

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

_extract_constraints(constraints_section: str) list[source]

Extracts constraints from constraints section

Parameters:

constraints_section (str) – leetcode content section containing constraints

Returns:

all the constraints

Return type:

list

_extract_function_info(code: str) dict | None[source]

Extracts function info from python code

Parameters:

code (str) – python class

Returns:

function info with name, params, param_types, and rtype

Return type:

dict

_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.

__init__()[source]

Initialize database connection and creates database/table if it doesn’t exist.

_init_db()[source]

Creates database and table.

_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

select_problem(num: int) Row[source]

Selects a problem from the problems table.

Parameters:

num (int) – problem number

Returns:

row containing the problem’s num, title, slug, and question id

Return type:

sqlite3.Row

count_problems() Row[source]

Counts the number of problems in the problems table.

Returns:

count of problems

Return type:

int

_util.py

Common utility functions

absolute_path(relative_path: str) str[source]

Get absolute path to file in fetch_leetcode_problem directory

Parameters:

relative_path (str) – relative path to file

Returns:

absolute path to file

Return type:

str