# How to Use the Anima Python Module for Creating LeetCode Solution Templates

> Learn to use the anima Python module to quickly create LeetCode solution templates. Scaffold directories with Markdown articles and placeholder media files effortlessly.

- Repository: [吴师兄学算法/LeetCodeAnimation](https://github.com/MisterBooo/LeetCodeAnimation)
- Tags: how-to-guide
- Published: 2026-03-01

---

**The `anima` Python module scaffolds complete LeetCode solution directories with Markdown article templates and placeholder media files via the `Anima.new()` method or command-line interface.**

The `anima` package in the MisterBooo/LeetCodeAnimation repository automates the boilerplate setup for LeetCode animation tutorials. It generates a standardized folder structure, copies a Markdown template, and creates empty media placeholders, allowing creators to focus on content rather than directory management.

## Architecture of the Anima Module

The implementation is split across four specialized modules to separate concerns:

- **[`anima/base.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/base.py)** – Locates the repository root and template paths using `get_project_path()` and `get_md_template_path()`
- **[`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py)** – Defines `ProblemInfo` (holding ID, title, and slug generation) and `Solution` (handling directory creation via `Solution.create()`)
- **[`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py)** – Orchestrates the scaffolding workflow through the `create_solution()` function
- **[`anima/__init__.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/__init__.py)** – Exposes the public `Anima` class with the `new()` entry point

The CLI entry point at [`anima.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima.py) uses the **fire** library to expose `Anima.new` as a terminal command.

## Creating Solution Templates with the Anima Python Module

You can scaffold new LeetCode solutions using three different approaches depending on your workflow needs.

### Method 1: Using the Anima Class API

The simplest approach invokes the `Anima` wrapper class, which delegates to the underlying creation logic:

```python
from anima import Anima

# Create a solution scaffold for LeetCode problem #15 "Three Sum"

Anima().new(15, "Three Sum")

```

When executed from the repository root, this generates a directory named `0015-Three-Sum` containing the complete folder hierarchy.

### Method 2: Command-Line Interface

For terminal-based workflows, invoke the CLI directly via [`anima.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima.py):

```bash

# From the repository root

python anima.py new 42 "Trapping Rain Water"

```

This prints a confirmation message indicating the scaffold location and creates the same directory structure as the Python API.

### Method 3: Direct Function Calls

For automation pipelines or custom scripts, bypass the wrapper and call the core function directly from [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py):

```python
from anima.create import create_solution

# Create solution for problem #100

create_solution(100, "Same Tree")

```

This approach provides identical functionality to `Anima.new()` while offering more flexibility for batch processing.

## Directory Structure Generated by Anima

Each invocation creates a slugged directory (format: `{id:04d}-{Title-Case}`) containing three subdirectories:

- **`Animation/`** – Contains empty placeholder files `Animation.m4v` and `Animation.gif` ready for media assets
- **`Article/`** – Contains `{slug}.md` copied from [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) for the written explanation
- **`Code/`** – Empty directory reserved for solution source code

For example, running `Anima().new(15, "Three Sum")` produces:

```

0015-Three-Sum/
├─ Animation/
│  ├─ Animation.m4v
│  └─ Animation.gif
├─ Article/
│  └─ 0015-Three-Sum.md
└─ Code/

```

## Core Implementation Details

The module uses a pipeline architecture to ensure idempotent directory creation:

1. **`ProblemInfo` instantiation** – In [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py), this dataclass stores the numeric ID and title, generating the URL-safe `title_slug()` used for folder names
2. **Path resolution** – [`anima/base.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/base.py) determines the repository root using `Path` resolution and locates [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) via `get_md_template_path()`
3. **Directory creation** – `Solution.create()` in [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py) builds the three-folder hierarchy and touches empty media files
4. **Template copying** – `create_solution()` in [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py) copies the Markdown template into the Article folder and checks for existing directories to prevent overwrites

The `Anima` class in [`anima/__init__.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/__init__.py) simply forwards arguments to `create_solution()`, maintaining a minimal public API while keeping filesystem logic encapsulated in helper modules.

## Summary

- The `anima` Python module automates scaffolding for LeetCode animation tutorials in the MisterBooo/LeetCodeAnimation repository
- **Three usage patterns** exist: the `Anima` class API, CLI via `python anima.py`, and direct `create_solution()` calls
- Generated directories follow the pattern `{id:04d}-{Title-Case}` and contain `Animation/`, `Article/`, and `Code/` subfolders
- The Markdown template at [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) is copied into each new solution's Article folder
- Core logic resides in [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py) (data structures), [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py) (orchestration), and [`anima/base.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/base.py) (path utilities)

## Frequently Asked Questions

### What is the anima Python module used for?

The `anima` module scaffolds standardized directory structures for LeetCode solution tutorials. It creates the necessary folders, copies article templates, and generates placeholder media files so creators can immediately start writing explanations and recording animations.

### How does anima generate the folder names?

In [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py), the `ProblemInfo` dataclass converts the problem title into a URL-safe slug using the `title_slug()` method. The final directory name combines the zero-padded problem ID with this slug (e.g., `0042-Trapping-Rain-Water`).

### Can I customize the Markdown template used by anima?

Yes. The module copies [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) from the repository root into each new solution's Article folder. Modifying this file changes the starting template for all future solutions created with `Anima.new()` or the CLI.

### Is anima suitable for automation scripts?

Absolutely. While the CLI and `Anima` class provide convenient interfaces, you can import `create_solution` directly from `anima.create` to integrate LeetCode scaffolding into larger automation pipelines without invoking the command line.