Hướng dẫn python cli application example - ví dụ về ứng dụng python cli

Xây dựng một ứng dụng để quản lý danh sách việc cần làm của bạn có thể là một dự án thú vị khi bạn học một ngôn ngữ lập trình mới hoặc cố gắng đưa các kỹ năng của bạn lên một tầm cao mới. Trong hướng dẫn này, bạn sẽ xây dựng một ứng dụng cần làm chức năng cho dòng lệnh sử dụng Python và Typer, đây là một thư viện tương đối trẻ để tạo các ứng dụng giao diện dòng lệnh mạnh (CLI) trong hầu như không có thời gian.to-do list can be an interesting project when you’re learning a new programming language or trying to take your skills to the next level. In this tutorial, you’ll build a functional to-do application for the command line using Python and Typer, which is a relatively young library for creating powerful command-line interface (CLI) applications in almost no time.

Show

Với một dự án như thế này, bạn sẽ áp dụng một tập hợp rộng các kỹ năng lập trình cốt lõi trong khi xây dựng một ứng dụng trong thế giới thực với các tính năng và yêu cầu thực sự.

Trong hướng dẫn này, bạn sẽ học cách:

  • Xây dựng một ứng dụng cần làm chức năng với Typer CLI trong Pythonto-do application with a Typer CLI in Python
  • Sử dụng Typer để thêm các lệnh, đối số và tùy chọn vào ứng dụng việc cần làm của bạncommands, arguments, and options to your to-do app
  • Kiểm tra ứng dụng cần làm Python của bạn với Typer từ
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1 và pytest
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1
    and pytest

Ngoài ra, bạn sẽ thực hành các kỹ năng của mình liên quan đến việc xử lý các tệp JSON bằng cách sử dụng mô -đun Python, ____42 và quản lý các tệp cấu hình với mô -đun Python tựa

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
3. Với kiến ​​thức này, bạn sẽ sẵn sàng bắt đầu tạo các ứng dụng CLI ngay lập tức.

Bạn có thể tải xuống toàn bộ mã và tất cả các tài nguyên bổ sung cho ứng dụng CLI làm bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
4:

Thử nghiệm

Trong dự án từng bước này, bạn sẽ xây dựng ứng dụng giao diện dòng lệnh (CLI) để quản lý danh sách việc cần làm. Ứng dụng của bạn sẽ cung cấp CLI dựa trên Typer, thư viện hiện đại và linh hoạt để tạo các ứng dụng CLI.

Trước khi bạn bắt đầu, hãy xem bản demo này về cách ứng dụng việc cần làm của bạn sẽ trông và hoạt động khi bạn đi đến cuối hướng dẫn này. Phần đầu tiên của bản demo cho thấy cách nhận trợ giúp khi làm việc với ứng dụng. Nó cũng chỉ ra cách khởi tạo và định cấu hình ứng dụng. Phần còn lại của video trình bày cách tương tác với các tính năng thiết yếu, chẳng hạn như thêm, xóa và liệt kê các to-dos:

Tốt đẹp! Ứng dụng có CLI thân thiện với người dùng cho phép bạn thiết lập cơ sở dữ liệu việc cần làm. Khi đó, bạn có thể thêm, xóa và hoàn thành việc cần sử dụng các lệnh, đối số và tùy chọn thích hợp. Nếu bạn đã từng bị mắc kẹt, thì bạn có thể yêu cầu trợ giúp bằng cách sử dụng tùy chọn

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
5 với các đối số thích hợp.commands, arguments, and options. If you ever get stuck, then you can ask for help using the
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
5 option with proper arguments.

Bạn có cảm thấy muốn khởi động dự án ứng dụng cần làm này không? Mát mẻ! Trong phần tiếp theo, bạn sẽ lên kế hoạch cho cách cấu trúc bố cục của dự án và những công cụ nào bạn sẽ sử dụng để xây dựng nó.

Tổng quan dự án

Khi bạn muốn bắt đầu một ứng dụng mới, bạn thường bắt đầu bằng cách suy nghĩ về cách bạn muốn ứng dụng hoạt động. Trong hướng dẫn này, bạn sẽ xây dựng một ứng dụng việc cần làm cho dòng lệnh. Bạn sẽ gọi ứng dụng đó

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
6.

Bạn muốn ứng dụng của mình có giao diện dòng lệnh thân thiện với người dùng cho phép người dùng của bạn tương tác với ứng dụng và quản lý danh sách việc cần làm của họ.

Để bắt đầu, bạn muốn CLI của bạn cung cấp các tùy chọn toàn cầu sau:

  • typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    7 hoặc
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    8 hiển thị phiên bản hiện tại và thoát khỏi ứng dụng.
    or
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    8
    shows the current version and exits the application.
  • typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    5 hiển thị thông báo trợ giúp toàn cầu cho toàn bộ ứng dụng.
    shows the global help message for the entire application.

Bạn sẽ thấy những tùy chọn tương tự trong nhiều ứng dụng CLI khác ngoài kia. Đó là một ý tưởng hay để cung cấp cho họ bởi vì hầu hết người dùng làm việc với dòng lệnh đều mong muốn tìm thấy chúng trong mọi ứng dụng.

Về việc quản lý danh sách việc cần làm, ứng dụng của bạn sẽ cung cấp các lệnh để khởi tạo ứng dụng, thêm và xóa to-dos và quản lý trạng thái hoàn thành việc cần làm:

Yêu cầuSự mô tả
(venv) $ python -m pip install -r requirements.txt
0
Khởi tạo cơ sở dữ liệu cần làm ứng dụng
(venv) $ python -m pip install -r requirements.txt
1
Thêm một việc cần làm mới vào cơ sở dữ liệu với một mô tả
(venv) $ python -m pip install -r requirements.txt
2
Liệt kê tất cả các công việc trong cơ sở dữ liệu
(venv) $ python -m pip install -r requirements.txt
3
Hoàn thành việc cần làm bằng cách đặt nó như đã hoàn thành bằng cách sử dụng ID của nó
(venv) $ python -m pip install -r requirements.txt
4
Xóa một việc cần làm khỏi cơ sở dữ liệu bằng ID của nó
(venv) $ python -m pip install -r requirements.txt
5
Xóa tất cả các công việc bằng cách xóa cơ sở dữ liệu

Các lệnh này cung cấp tất cả các chức năng bạn cần để biến ứng dụng cần làm của mình thành một sản phẩm khả thi tối thiểu (MVP) để bạn có thể xuất bản nó lên PYPI hoặc nền tảng bạn chọn và bắt đầu nhận phản hồi từ người dùng.

Để cung cấp tất cả các tính năng trong ứng dụng việc cần làm của bạn, bạn sẽ cần phải hoàn thành một vài nhiệm vụ:

  1. Xây dựng giao diện dòng lệnh có khả năng thực hiện và xử lý các lệnh, tùy chọn và đối sốcommand-line interface capable of taking and processing commands, options, and arguments
  2. Chọn một loại dữ liệu thích hợp để biểu thị sự cố của bạndata type to represent your to-dos
  3. Thực hiện một cách để lưu trữ kiên trì lưu trữ danh sách việc cần làm của bạnpersistently store your to-do list
  4. Xác định cách kết nối giao diện người dùng đó với dữ liệu việc cần làmconnect that user interface with the to-do data

Các nhiệm vụ này liên quan tốt đến những gì được gọi là thiết kế điều khiển mô hình, là một mô hình kiến ​​trúc. Trong mẫu này, mô hình chăm sóc dữ liệu, chế độ xem liên quan đến giao diện người dùng và bộ điều khiển kết nối cả hai đầu để làm cho ứng dụng hoạt động.model takes care of the data, the view deals with the user interface, and the controller connects both ends to make the application work.

Lý do chính cho việc sử dụng mẫu này trong các ứng dụng và dự án của bạn là để cung cấp các mối quan tâm (SOC), làm cho các phần khác nhau của mã của bạn xử lý các khái niệm cụ thể một cách độc lập.

Quyết định tiếp theo bạn cần đưa ra là về các công cụ và thư viện mà bạn sẽ sử dụng để giải quyết từng nhiệm vụ mà bạn đã xác định thêm. Nói cách khác, bạn cần quyết định ngăn xếp phần mềm của bạn. Trong hướng dẫn này, bạn sẽ sử dụng ngăn xếp sau:

  • Typer để xây dựng ứng dụng cần làm CLI CLI
  • Được đặt tên là các bộ dữ liệu và từ điển để xử lý dữ liệu việc cần làm
  • Mô -đun Python từ
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    2 để quản lý lưu trữ dữ liệu liên tục

Bạn cũng sẽ sử dụng mô -đun

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
3 từ thư viện tiêu chuẩn Python để xử lý các cài đặt ban đầu của ứng dụng trong tệp cấu hình. Trong tệp cấu hình, bạn sẽ lưu trữ đường dẫn đến cơ sở dữ liệu việc cần làm trong hệ thống tệp của bạn. Cuối cùng, bạn sẽ sử dụng pytest như một công cụ để kiểm tra ứng dụng CLI của bạn.

Điều kiện tiên quyết

Để hoàn thành hướng dẫn này và tận dụng tối đa nó, bạn nên thoải mái với các chủ đề sau:

  • Các mẫu mô hình-vitroll
  • Giao diện dòng lệnh (CLI)
  • Gợi ý loại Python, còn được gọi là chú thích loại
  • Bài kiểm tra đơn vị với pytest
  • Lập trình hướng đối tượng trong Python
  • Tệp cấu hình với
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    3
  • Các tệp JSON với Python từ
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    2
  • Thao tác đường dẫn hệ thống tệp với
    """Top-level package for RP To-Do."""
    # rptodo/__init__.py
    
    __app_name__ = "rptodo"
    __version__ = "0.1.0"
    
    (
        SUCCESS,
        DIR_ERROR,
        FILE_ERROR,
        DB_READ_ERROR,
        DB_WRITE_ERROR,
        JSON_ERROR,
        ID_ERROR,
    ) = range(7)
    
    ERRORS = {
        DIR_ERROR: "config directory error",
        FILE_ERROR: "config file error",
        DB_READ_ERROR: "database read error",
        DB_WRITE_ERROR: "database write error",
        ID_ERROR: "to-do id error",
    }
    
    0

Đó là nó! Nếu bạn đã sẵn sàng để có được tay bẩn và bắt đầu tạo ứng dụng việc cần làm của mình, thì bạn có thể bắt đầu với việc thiết lập môi trường làm việc và bố cục dự án.

Bước 1: Thiết lập dự án việc cần làm

Để bắt đầu mã hóa ứng dụng việc cần làm của mình, bạn cần thiết lập môi trường Python hoạt động với tất cả các công cụ, thư viện và sự phụ thuộc mà bạn sẽ sử dụng trong quy trình. Sau đó, bạn cần cung cấp cho dự án một bố cục ứng dụng Python mạch lạc. Đó là những gì bạn sẽ làm trong các tiểu mục sau.

Để tải xuống tất cả các tệp và cấu trúc dự án mà bạn sẽ tạo trong phần này, nhấp vào liên kết bên dưới và chuyển đến thư mục

"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
1:

Thiết lập môi trường làm việc

Trong phần này, bạn sẽ tạo ra một môi trường ảo Python để làm việc trong dự án việc cần làm của mình. Sử dụng môi trường ảo cho mỗi dự án độc lập là một thực tiễn tốt nhất trong lập trình Python. Nó cho phép bạn cô lập các phụ thuộc dự án của bạn mà không làm lộn xộn cài đặt Python hệ thống của bạn hoặc phá vỡ các dự án khác sử dụng các phiên bản khác nhau của cùng một công cụ và thư viện.

Để tạo môi trường ảo Python, hãy chuyển sang thư mục làm việc yêu thích của bạn và tạo một thư mục gọi là

"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
2. Sau đó kích hoạt một dòng thiết bị đầu cuối hoặc dòng lệnh và chạy các lệnh sau:

$ cd rptodo_project/
$ python -m venv ./venv
$ source venv/bin/activate
(venv) $

Tại đây, trước tiên bạn nhập thư mục

"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
2 bằng
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
4. Thư mục này sẽ là thư mục gốc dự án của bạn. Sau đó, bạn tạo một môi trường ảo Python bằng cách sử dụng
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
5 từ thư viện tiêu chuẩn. Đối số của
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
5 là đường dẫn đến thư mục lưu trữ môi trường ảo của bạn. Một thực tế phổ biến là gọi thư mục đó
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
5,
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
8 hoặc
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
9, tùy thuộc vào sở thích của bạn.

Lệnh thứ ba kích hoạt môi trường ảo bạn vừa tạo. Bạn biết rằng môi trường đang hoạt động vì lời nhắc của bạn thay đổi thành một thứ như

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
0.

Bây giờ bạn có một môi trường ảo hoạt động, bạn cần cài đặt Typer để tạo ứng dụng CLI và pytest để kiểm tra mã ứng dụng của bạn. Để cài đặt Typer với tất cả các phụ thuộc tùy chọn hiện tại của nó, hãy chạy lệnh sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0

Lệnh này cài đặt Typer và tất cả các phụ thuộc được đề xuất của nó, chẳng hạn như Colorama, đảm bảo rằng màu sắc hoạt động chính xác trên cửa sổ dòng lệnh của bạn.

Để cài đặt pytest, mà bạn sẽ sử dụng sau để kiểm tra ứng dụng việc cần làm của mình, hãy chạy lệnh sau:

(venv) $ python -m pip install pytest==6.2.4

Với lệnh cuối cùng này, bạn đã cài đặt thành công tất cả các công cụ bạn cần để bắt đầu phát triển ứng dụng việc cần làm của mình. Phần còn lại của các thư viện và công cụ bạn sẽ sử dụng là một phần của thư viện tiêu chuẩn Python, vì vậy bạn không phải cài đặt bất cứ thứ gì để sử dụng chúng.

Xác định bố cục dự án

Bước cuối cùng bạn sẽ chạy để hoàn thành việc thiết lập dự án ứng dụng việc cần làm của mình là tạo các gói, mô-đun và tệp sẽ đóng khung bố cục ứng dụng. Gói lõi ứng dụng sẽ sống trong thư mục

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1 bên trong
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
2.

Tại đây, một mô tả về gói nội dung của gói:

Tập tinSự mô tả
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
3
Cho phép
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1 là gói Python
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
5
Cung cấp tập lệnh điểm nhập để chạy ứng dụng từ gói bằng lệnh
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
6
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7
Cung cấp giao diện dòng lệnh Typer cho ứng dụng
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
8
Chứa mã để xử lý tệp cấu hình ứng dụng
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
9
Chứa mã để xử lý cơ sở dữ liệu cần làm ứng dụng
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0
Cung cấp mã để kết nối CLI với cơ sở dữ liệu việc cần làm

Bạn cũng sẽ cần một thư mục

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
1 chứa tệp
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
3 để biến thư mục thành một gói và tệp
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
3 để giữ các bài kiểm tra đơn vị cho ứng dụng.

Đi trước và tạo bố cục dự án với cấu trúc sau:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt

Tệp

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
4 sẽ cung cấp mô tả và hướng dẫn của dự án để cài đặt và chạy ứng dụng. Thêm một tệp
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
4 mô tả và chi tiết vào dự án của bạn là một thông lệ tốt nhất trong lập trình, đặc biệt nếu bạn có kế hoạch phát hành dự án dưới dạng nguồn mở.

Tệp

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
6 sẽ cung cấp danh sách các phụ thuộc cho ứng dụng cần làm của bạn. Đi trước và điền vào nó với các nội dung sau:

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4

Bây giờ người dùng của bạn có thể tự động cài đặt các phụ thuộc được liệt kê bằng cách chạy lệnh sau:

(venv) $ python -m pip install -r requirements.txt

Cung cấp một

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
6 như thế này đảm bảo rằng người dùng của bạn sẽ cài đặt các phiên bản chính xác của các phụ thuộc bạn đã sử dụng để xây dựng dự án, tránh các vấn đề và hành vi bất ngờ.

Ngoại trừ

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
6, tất cả các tệp dự án của bạn phải trống vào thời điểm này. Bạn sẽ điền vào mỗi tệp với nội dung cần thiết di chuyển về phía trước thông qua hướng dẫn này. Trong phần sau, bạn sẽ mã hóa ứng dụng CLI CLI với Python và Typer.

Bước 2: Thiết lập ứng dụng CLI cần làm với Python và Typer

Tại thời điểm này, bạn nên có một bố cục dự án hoàn chỉnh cho ứng dụng việc cần làm của bạn. Bạn cũng nên có một môi trường ảo Python hoạt động với tất cả các công cụ và thư viện cần thiết. Vào cuối bước này, bạn sẽ có một ứng dụng Typer CLI chức năng. Sau đó, bạn sẽ có thể xây dựng trên chức năng tối thiểu của nó.

Bạn có thể tải xuống mã, kiểm tra đơn vị và tài nguyên bạn sẽ thêm vào phần này bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
9:

Bắn trình chỉnh sửa mã của bạn và mở tệp

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
3 từ thư mục
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1. Sau đó thêm mã sau vào nó:

"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}

Tại đây, bạn bắt đầu bằng cách xác định hai tên cấp mô-đun để giữ tên và phiên bản ứng dụng. Sau đó, bạn xác định một loạt các mã trả về và lỗi và gán số số nguyên cho chúng bằng cách sử dụng

(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.
2.
(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.
3 là một từ điển ánh xạ mã lỗi cho các thông báo lỗi có thể đọc được của con người. Bạn sẽ sử dụng các tin nhắn này để nói với người dùng những gì xảy ra với ứng dụng.

Với mã này, bạn đã sẵn sàng tạo bộ xương của ứng dụng Typer CLI của mình. Đó là những gì bạn sẽ làm trong phần sau.

Tạo ứng dụng Typer CLI

Trong phần này, bạn sẽ tạo một ứng dụng Typer CLI tối thiểu với sự hỗ trợ cho các tùy chọn

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
5,
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
7 và
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
8. Để làm như vậy, bạn sẽ sử dụng một ứng dụng Typer rõ ràng. Loại ứng dụng này phù hợp cho các dự án lớn bao gồm nhiều lệnh với một số tùy chọn và đối số.

Đi trước và mở

(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.
7 trong trình soạn thảo văn bản của bạn và nhập mã sau:

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return

Typer sử dụng gợi ý loại Python rộng rãi, vì vậy trong hướng dẫn này, bạn cũng sẽ sử dụng chúng. Đó là lý do tại sao bạn bắt đầu bằng cách nhập

(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.
8 từ
(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.
9. Tiếp theo, bạn nhập
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
00. Cuối cùng, bạn nhập
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
01 và
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
02 từ gói
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
6 của bạn.

Đây là cách mà phần còn lại của mã hoạt động:

  • Dòng 10 tạo ra một ứng dụng Typer rõ ràng,

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    04. creates an explicit Typer application,
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    04.

  • Dòng 12 đến 15 Xác định

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    05. Hàm này có một đối số boolean gọi là
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    06. Nếu
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    06 là
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08, thì hàm sẽ in tên và phiên bản ứng dụng bằng cách sử dụng
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    09. Sau đó, nó làm tăng một ngoại lệ
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    10 để thoát khỏi ứng dụng một cách sạch sẽ.
    define
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    05. This function takes a Boolean argument called
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    06. If
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    06 is
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08, then the function prints the application’s name and version using
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    09. After that, it raises a
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    10 exception to exit the application cleanly.

  • Các dòng 17 và 18 định nghĩa

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    11 là một cuộc gọi lại Typer bằng cách sử dụng trình trang trí
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    12.
    define
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    11 as a Typer callback using the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    12 decorator.

  • Dòng 19 xác định

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13, thuộc loại
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    14. Điều này có nghĩa là nó có thể là loại
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    15 hoặc
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    16. Đối số
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 mặc định là đối tượng
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    18, cho phép bạn tạo các tùy chọn dòng lệnh trong Typer.
    defines
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13, which is of type
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    14. This means it can be either of
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    15 or
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    16 type. The
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 argument defaults to a
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    18 object, which allows you to create command-line options in Typer.

  • Dòng 20 vượt qua

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    16 là đối số đầu tiên cho trình khởi tạo của
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    20. Đối số này là bắt buộc và cung cấp giá trị mặc định của tùy chọn.
    passes
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    16 as the first argument to the initializer of
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    20. This argument is required and supplies the option’s default value.

  • Các dòng 21 và 22 Đặt tên dòng lệnh cho tùy chọn

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13:
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    7 và
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    8.
    set the command-line names for the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 option:
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    7 and
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    8.

  • Dòng 23 cung cấp thông báo

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    24 cho tùy chọn
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13.
    provides a
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    24 message for the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 option.

  • Dòng 24 đính kèm chức năng gọi lại,

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    05, vào tùy chọn
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13, có nghĩa là chạy tùy chọn tự động gọi hàm.
    attaches a callback function,
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    05, to the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 option, which means that running the option automatically calls the function.

  • Dòng 25 đặt đối số

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    28 thành
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08. Đối số này cho Typer rằng tùy chọn dòng lệnh
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 được ưu tiên hơn các lệnh khác trong ứng dụng hiện tại.
    sets the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    28 argument to
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08. This argument tells Typer that the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    13 command-line option has precedence over other commands in the current application.

Với mã này, bạn đã sẵn sàng để tạo tập lệnh điểm nhập của ứng dụng. Đó là những gì bạn sẽ làm trong phần sau.

Tạo tập lệnh điểm nhập cảnh

Bạn gần như đã sẵn sàng để chạy ứng dụng việc cần làm của mình lần đầu tiên. Trước khi làm điều đó, bạn nên tạo một tập lệnh nhập cảnh cho ứng dụng. Bạn có thể tạo tập lệnh này theo một vài cách khác nhau. Trong hướng dẫn này, bạn sẽ thực hiện nó bằng mô -đun

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
5 bên trong gói
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
6. Bao gồm mô -đun
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
5 trong gói Python cho phép bạn chạy gói dưới dạng chương trình thực thi bằng lệnh
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
6.

Quay trở lại Trình chỉnh sửa mã của bạn và mở

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
5 từ thư mục
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1. Sau đó thêm mã sau:

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()

Trong

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
5, lần đầu tiên bạn nhập
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
38 và
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
01 từ
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
6. Sau đó, bạn xác định
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
11. Trong chức năng này, bạn gọi ứng dụng Typer với
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
42, chuyển tên ứng dụng của ứng dụng cho đối số
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
43. Cung cấp giá trị cho
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
43 đảm bảo rằng người dùng của bạn có được tên ứng dụng chính xác khi chạy tùy chọn
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
5 trên dòng lệnh của họ.

Với sự bổ sung cuối cùng này, bạn đã sẵn sàng để chạy ứng dụng việc cần làm của mình lần đầu tiên. Di chuyển đến cửa sổ thiết bị đầu cuối của bạn và thực hiện các lệnh sau:

(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.

Lệnh đầu tiên chạy tùy chọn

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
7, hiển thị phiên bản ứng dụng. Lệnh thứ hai chạy tùy chọn
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
5 để hiển thị thông báo trợ giúp thân thiện với người dùng cho toàn bộ ứng dụng. Typer tự động tạo và hiển thị thông báo trợ giúp này cho bạn.

Thiết lập các bài kiểm tra CLI ban đầu với pytest

Hành động cuối cùng mà bạn sẽ chạy trong phần này là thiết lập bộ thử nghiệm ban đầu cho ứng dụng việc cần làm của bạn. Để kết thúc này, bạn đã tạo gói

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
48 với một mô -đun có tên là
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
3. Như bạn đã biết trước đó, bạn sẽ sử dụng pytest để viết và chạy các bài kiểm tra đơn vị của bạn.

Kiểm tra ứng dụng Typer rất đơn giản vì thư viện tích hợp khá tốt với pytest. Bạn có thể sử dụng một lớp Typer có tên

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
1 để kiểm tra ứng dụng CLI CLI.
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
1 cho phép bạn tạo một người chạy mà bạn có thể sử dụng để kiểm tra cách ứng dụng của bạn CLI phản hồi các lệnh trong thế giới thực.

Quay trở lại Trình chỉnh sửa mã của bạn và mở

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
3 từ thư mục
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
1. Nhập mã sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
0

Đây là những gì mã này làm:

  • Dòng 3 nhập
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1 từ
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    55.
    imports
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1 from
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    55.
  • Dòng 5 nhập một vài đối tượng bắt buộc từ gói
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    6 của bạn.
    imports a few required objects from your
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    6 package.
  • Dòng 7 tạo ra một người chạy CLI bằng cách khởi tạo
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1.
    creates a CLI runner by instantiating
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1.
  • Dòng 9 xác định thử nghiệm đơn vị đầu tiên của bạn để kiểm tra phiên bản ứng dụng. defines your first unit test for testing the application’s version.
  • Dòng 10 gọi
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    58 trên
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    59 để chạy ứng dụng với tùy chọn
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    8. Bạn lưu trữ kết quả của cuộc gọi này trong
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    61.
    calls
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    58 on
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    59 to run the application with the
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    8 option. You store the result of this call in
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    61.
  • Dòng 11 khẳng định rằng mã thoát của ứng dụng (
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    62) bằng với
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    63 để kiểm tra xem ứng dụng có chạy thành công không.
    asserts that the application’s exit code (
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    62) is equal to
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    63 to check that the application ran successfully.
  • Dòng 12 khẳng định rằng phiên bản ứng dụng có mặt trong đầu ra tiêu chuẩn, có sẵn thông qua
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    64.
    asserts that the application’s version is present in the standard output, which is available through
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    64.

Typer sườn

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
1 là một lớp con của Click Click
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
1. Do đó, phương thức
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
58 của nó trả về một đối tượng
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
68, giữ kết quả của việc chạy ứng dụng CLI với các đối số và tùy chọn đích.
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
68 Các đối tượng cung cấp một số thuộc tính và thuộc tính hữu ích, bao gồm mã thoát và đầu ra tiêu chuẩn của ứng dụng. Hãy xem tài liệu lớp để biết thêm chi tiết.

Bây giờ bạn đã thiết lập thử nghiệm đơn vị đầu tiên cho ứng dụng Typer CLI của bạn, bạn có thể chạy thử nghiệm với pytest. Quay trở lại dòng lệnh của bạn và thực thi

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
70 từ thư mục gốc của dự án của bạn:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
1

Đó là nó! Bạn đã chạy thành công bộ thử nghiệm của mình lần đầu tiên! Vâng, bạn chỉ có một bài kiểm tra cho đến nay. Tuy nhiên, bạn sẽ thêm nhiều trong số chúng vào các phần sắp tới. Bạn cũng có thể thêm bài kiểm tra của riêng bạn nếu bạn muốn thách thức các kỹ năng kiểm tra của mình.

Với ứng dụng cần làm bộ xương, bây giờ bạn có thể nghĩ về việc thiết lập cơ sở dữ liệu việc cần làm để sẵn sàng sử dụng. Đó là những gì bạn sẽ làm trong phần sau.

Bước 3: Chuẩn bị cơ sở dữ liệu việc cần làm để sử dụng

Cho đến thời điểm này, bạn đã kết hợp một CLI cho ứng dụng cần làm của mình, tạo tập lệnh điểm nhập cảnh và lần đầu tiên chạy ứng dụng. Bạn cũng đã thiết lập và chạy bộ thử nghiệm tối thiểu cho ứng dụng. Bước tiếp theo là xác định cách ứng dụng của bạn sẽ khởi tạo và kết nối với cơ sở dữ liệu việc cần làm.

Bạn sẽ sử dụng tệp JSON để lưu trữ dữ liệu về việc làm của bạn. JSON là một định dạng liên kết dữ liệu nhẹ mà có thể đọc được và có thể đọc được. Thư viện tiêu chuẩn Python sườn bao gồm

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
2, đây là mô -đun cung cấp hỗ trợ cho định dạng tệp JSON ra khỏi hộp. Đó là những gì bạn sẽ sử dụng để quản lý cơ sở dữ liệu việc cần làm của mình.

Bạn có thể tải xuống toàn bộ mã cho phần này bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
72:

Vào cuối phần này, bạn sẽ viết mã để tạo, kết nối và khởi tạo cơ sở dữ liệu việc cần làm của mình để nó sẵn sàng để sử dụng. Tuy nhiên, bước đầu tiên là xác định cách ứng dụng của bạn sẽ tìm cơ sở dữ liệu việc cần làm trong hệ thống tệp của bạn.

Thiết lập các cấu hình ứng dụng

Bạn có thể sử dụng các kỹ thuật khác nhau để xác định cách ứng dụng kết nối và mở tệp trên hệ thống tệp của bạn. Bạn có thể cung cấp đường dẫn tệp một cách linh hoạt, tạo một biến môi trường để giữ đường dẫn tệp, tạo tệp cấu hình trong đó bạn lưu trữ đường dẫn tệp, v.v.

Trong hướng dẫn này, bạn sẽ cung cấp ứng dụng việc cần làm của mình với một tệp cấu hình trong thư mục nhà của bạn để lưu trữ đường dẫn đến cơ sở dữ liệu. Cuối cùng, bạn sẽ sử dụng

"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__ = "rptodo"
__version__ = "0.1.0"

(
    SUCCESS,
    DIR_ERROR,
    FILE_ERROR,
    DB_READ_ERROR,
    DB_WRITE_ERROR,
    JSON_ERROR,
    ID_ERROR,
) = range(7)

ERRORS = {
    DIR_ERROR: "config directory error",
    FILE_ERROR: "config file error",
    DB_READ_ERROR: "database read error",
    DB_WRITE_ERROR: "database write error",
    ID_ERROR: "to-do id error",
}
0 để làm việc với các đường dẫn hệ thống tệp và
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
3 để xử lý các tệp cấu hình. Cả hai gói đều có sẵn cho bạn trong thư viện tiêu chuẩn Python.

Bây giờ hãy quay lại trình chỉnh sửa mã của bạn và mở

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
8 từ
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1. Nhập mã sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
2

Ở đây, một sự cố về những gì mã này làm:

  • Dòng 4 Nhập khẩu

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    3. Mô -đun này cung cấp lớp
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    78, cho phép bạn xử lý các tệp cấu hình có cấu trúc tương tự như các tệp INI.
    imports
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    3. This module provides the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    78 class, which allows you to handle config files with a structure similar to INI files.

  • Dòng 5 Nhập khẩu

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    79 từ
    """Top-level package for RP To-Do."""
    # rptodo/__init__.py
    
    __app_name__ = "rptodo"
    __version__ = "0.1.0"
    
    (
        SUCCESS,
        DIR_ERROR,
        FILE_ERROR,
        DB_READ_ERROR,
        DB_WRITE_ERROR,
        JSON_ERROR,
        ID_ERROR,
    ) = range(7)
    
    ERRORS = {
        DIR_ERROR: "config directory error",
        FILE_ERROR: "config file error",
        DB_READ_ERROR: "database read error",
        DB_WRITE_ERROR: "database write error",
        ID_ERROR: "to-do id error",
    }
    
    0. Lớp này cung cấp một cách đa nền tảng để xử lý các đường dẫn hệ thống.
    imports
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    79 from
    """Top-level package for RP To-Do."""
    # rptodo/__init__.py
    
    __app_name__ = "rptodo"
    __version__ = "0.1.0"
    
    (
        SUCCESS,
        DIR_ERROR,
        FILE_ERROR,
        DB_READ_ERROR,
        DB_WRITE_ERROR,
        JSON_ERROR,
        ID_ERROR,
    ) = range(7)
    
    ERRORS = {
        DIR_ERROR: "config directory error",
        FILE_ERROR: "config file error",
        DB_READ_ERROR: "database read error",
        DB_WRITE_ERROR: "database write error",
        ID_ERROR: "to-do id error",
    }
    
    0. This class provides a cross-platform way to handle system paths.

  • Dòng 7 Nhập khẩu

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    00. imports
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    00.

  • Dòng 9 đến 11 Nhập một loạt các đối tượng cần thiết từ

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    6. import a bunch of required objects from
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    6.

  • Dòng 13 tạo ra

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    83 để giữ đường dẫn đến thư mục ứng dụng. Để có được đường dẫn này, bạn gọi
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    84 với tên ứng dụng là một đối số. Hàm này trả về một chuỗi đại diện cho đường dẫn đến một thư mục nơi bạn có thể lưu trữ cấu hình.
    creates
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    83 to hold the path to the app’s directory. To get this path, you call
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    84 with the application’s name as an argument. This function returns a string representing the path to a directory where you can store configurations.

  • Dòng 14 xác định

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    85 để giữ đường dẫn đến chính tệp cấu hình. defines
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    85 to hold the path to the configuration file itself.

  • Dòng 16 xác định

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    86. Hàm này khởi tạo tệp cấu hình và cơ sở dữ liệu của ứng dụng. defines
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    86. This function initializes the application’s configuration file and database.

  • Dòng 18 gọi hàm trợ giúp

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    87 mà bạn xác định trong các dòng 26 đến 35. Gọi hàm này tạo thư mục cấu hình bằng cách sử dụng
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    88. Nó cũng tạo tệp cấu hình bằng cách sử dụng
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    89. Cuối cùng,
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    87 trả về mã lỗi thích hợp nếu có điều gì đó sai xảy ra trong quá trình tạo thư mục và tệp. Nó trả về
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 nếu mọi thứ đều ổn.
    calls the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    87 helper function, which you define in lines 26 to 35. Calling this function creates the configuration directory using
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    88. It also creates the configuration file using
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    89. Finally,
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    87 returns the proper error codes if something wrong happens during the creation of the directory and file. It returns
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 if everything goes okay.

  • Dòng 19 Kiểm tra xem có xảy ra lỗi trong quá trình tạo tệp thư mục và cấu hình không và dòng 20 trả về mã lỗi tương ứng. checks if an error occurs during the creation of the directory and configuration file, and line 20 returns the error code accordingly.

  • Dòng 21 gọi hàm trợ giúp

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    92, tạo ra cơ sở dữ liệu việc cần làm. Hàm này trả về các mã lỗi thích hợp nếu có điều gì đó xảy ra trong khi tạo cơ sở dữ liệu. Nó trả về
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 nếu quá trình thành công.
    calls the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    92 helper function, which creates the to-do database. This function returns the appropriate error codes if something happens while creating the database. It returns
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 if the process succeeds.

  • Dòng 22 kiểm tra xem có xảy ra lỗi trong quá trình tạo cơ sở dữ liệu không. Nếu vậy, dòng 23 trả về mã lỗi tương ứng. checks if an error occurs during the creation of the database. If so, then line 23 returns the corresponding error code.

  • Dòng 24 trả về

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 nếu mọi thứ chạy ổn. returns
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 if everything runs okay.

Với mã này, bạn đã hoàn thành việc thiết lập tệp cấu hình ứng dụng để lưu trữ đường dẫn đến cơ sở dữ liệu việc cần làm. Bạn cũng đã thêm mã để tạo cơ sở dữ liệu việc cần làm dưới dạng tệp JSON. Bây giờ bạn có thể viết mã để khởi tạo cơ sở dữ liệu và sẵn sàng sử dụng. Đó là những gì bạn sẽ làm trong phần sau.

Chuẩn bị sẵn sàng sử dụng cơ sở dữ liệu việc cần làm

Để có cơ sở dữ liệu cần làm sẵn sàng để sử dụng, bạn cần thực hiện hai hành động. Đầu tiên, bạn cần một cách để truy xuất đường dẫn tệp cơ sở dữ liệu từ tệp cấu hình ứng dụng. Thứ hai, bạn cần khởi tạo cơ sở dữ liệu để giữ nội dung JSON.

Mở

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
9 từ
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1 trong Trình chỉnh sửa mã của bạn và viết mã sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
3

Trong tệp này, dòng 4 đến 7 thực hiện nhập khẩu cần thiết. Ở đây, những gì phần còn lại của mã làm:

  • Các dòng 9 đến 11 Xác định

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    97 để giữ đường dẫn tệp cơ sở dữ liệu mặc định. Ứng dụng sẽ sử dụng đường dẫn này nếu người dùng không cung cấp tùy chỉnh. define
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    97 to hold the default database file path. The application will use this path if the user doesn’t provide a custom one.

  • Dòng 13 đến 17 Xác định

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    98. Hàm này đưa đường dẫn đến tệp cấu hình ứng dụng làm đối số, đọc tệp đầu vào bằng cách sử dụng
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    99 và trả về đối tượng
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    79 đại diện cho đường dẫn đến cơ sở dữ liệu cần làm trên hệ thống tệp của bạn. Ví dụ
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    78 lưu trữ dữ liệu trong một từ điển. Khóa
    (venv) $ python -m pip install pytest==6.2.4
    
    02 đại diện cho phần tệp lưu trữ thông tin cần thiết. Khóa
    (venv) $ python -m pip install pytest==6.2.4
    
    03 lấy đường dẫn cơ sở dữ liệu.
    define
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    98. This function takes the path to the app’s config file as an argument, reads the input file using
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    99, and returns a
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    79 object representing the path to the to-do database on your file system. The
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    78 instance stores the data in a dictionary. The
    (venv) $ python -m pip install pytest==6.2.4
    
    02 key represents the file section that stores the required information. The
    (venv) $ python -m pip install pytest==6.2.4
    
    03 key retrieves the database path.

  • Dòng 19 đến 25 Xác định

    (venv) $ python -m pip install pytest==6.2.4
    
    04. Hàm này lấy một đường dẫn cơ sở dữ liệu và ghi một chuỗi đại diện cho một danh sách trống. Bạn gọi
    (venv) $ python -m pip install pytest==6.2.4
    
    05 trên đường dẫn cơ sở dữ liệu và danh sách khởi tạo cơ sở dữ liệu JSON với danh sách việc cần làm trống. Nếu quá trình chạy thành công, thì
    (venv) $ python -m pip install pytest==6.2.4
    
    04 sẽ trả về
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91. Nếu không, nó trả về mã lỗi thích hợp.
    define
    (venv) $ python -m pip install pytest==6.2.4
    
    04. This function takes a database path and writes a string representing an empty list. You call
    (venv) $ python -m pip install pytest==6.2.4
    
    05 on the database path, and the list initializes the JSON database with an empty to-do list. If the process runs successfully, then
    (venv) $ python -m pip install pytest==6.2.4
    
    04 returns
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91. Otherwise, it returns the appropriate error code.

Mát mẻ! Bây giờ bạn có một cách để truy xuất đường dẫn tệp cơ sở dữ liệu từ tệp cấu hình ứng dụng. Bạn cũng có một cách để khởi tạo cơ sở dữ liệu với một danh sách việc cần làm trống ở định dạng JSON. Đó là thời gian để thực hiện lệnh

(venv) $ python -m pip install -r requirements.txt
0 với Typer để người dùng của bạn có thể khởi tạo cơ sở dữ liệu việc cần làm của họ từ CLI.

Thực hiện lệnh (venv) $ python -m pip install -r requirements.txt 0 CLI

Bước cuối cùng để kết hợp tất cả các mã mà bạn đã viết trong phần này là thêm lệnh

(venv) $ python -m pip install -r requirements.txt
0 vào ứng dụng CLI của bạn. Lệnh này sẽ thực hiện một đường dẫn tệp cơ sở dữ liệu tùy chọn. Sau đó, nó sẽ tạo tệp cấu hình ứng dụng và cơ sở dữ liệu việc cần làm.

Đi trước và thêm

(venv) $ python -m pip install pytest==6.2.4
11 vào tệp
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7 của bạn:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
4

Ở đây, cách thức hoạt động của mã mới này:

  • Dòng 4 và 9 cập nhật nhập khẩu cần thiết. update the required imports.

  • Các dòng 13 và 14 định nghĩa

    (venv) $ python -m pip install pytest==6.2.4
    
    11 là lệnh Typer bằng cách sử dụng trình trang trí
    (venv) $ python -m pip install pytest==6.2.4
    
    14.
    define
    (venv) $ python -m pip install pytest==6.2.4
    
    11 as a Typer command using the
    (venv) $ python -m pip install pytest==6.2.4
    
    14 decorator.

  • Các dòng 15 đến 20 Xác định một thể hiện Typer

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    20 và gán nó là giá trị mặc định cho
    (venv) $ python -m pip install pytest==6.2.4
    
    16. Để cung cấp giá trị cho tùy chọn này, người dùng của bạn cần sử dụng
    (venv) $ python -m pip install pytest==6.2.4
    
    17 hoặc
    (venv) $ python -m pip install pytest==6.2.4
    
    18 theo sau là đường dẫn cơ sở dữ liệu. Đối số
    (venv) $ python -m pip install pytest==6.2.4
    
    19 hiển thị một lời nhắc yêu cầu vị trí cơ sở dữ liệu. Nó cũng cho phép bạn chấp nhận đường dẫn mặc định bằng cách nhấn Enter.
    define a Typer
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    20 instance and assign it as a default value to
    (venv) $ python -m pip install pytest==6.2.4
    
    16. To provide a value for this option, your users need to use
    (venv) $ python -m pip install pytest==6.2.4
    
    17 or
    (venv) $ python -m pip install pytest==6.2.4
    
    18 followed by a database path. The
    (venv) $ python -m pip install pytest==6.2.4
    
    19 argument displays a prompt asking for a database location. It also allows you to accept the default path by pressing Enter.

  • Dòng 23 gọi

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    86 để tạo tệp cấu hình ứng dụng và cơ sở dữ liệu việc cần làm. calls
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    86 to create the application’s configuration file and to-do database.

  • Các dòng 24 đến 29 Kiểm tra xem cuộc gọi đến

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    86 trả về lỗi. Nếu vậy, các dòng 25 đến 28 In một thông báo lỗi. Dòng 29 thoát khỏi ứng dụng với ngoại lệ
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    10 và mã thoát của
    (venv) $ python -m pip install pytest==6.2.4
    
    23 để báo hiệu rằng ứng dụng chấm dứt với lỗi.
    check if the call to
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    86 returns an error. If so, lines 25 to 28 print an error message. Line 29 exits the app with a
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    10 exception and an exit code of
    (venv) $ python -m pip install pytest==6.2.4
    
    23 to signal that the application terminated with an error.

  • Dòng 30 gọi

    (venv) $ python -m pip install pytest==6.2.4
    
    04 để khởi tạo cơ sở dữ liệu với danh sách việc cần làm trống. calls
    (venv) $ python -m pip install pytest==6.2.4
    
    04 to initialize the database with an empty to-do list.

  • Dòng 31 đến 38 Kiểm tra xem cuộc gọi đến

    (venv) $ python -m pip install pytest==6.2.4
    
    04 trả về lỗi. Nếu vậy, các dòng 32 đến 35 hiển thị thông báo lỗi và dòng 36 thoát khỏi ứng dụng. Nếu không, dòng 38 in một thông điệp thành công trong văn bản màu xanh lá cây. check if the call to
    (venv) $ python -m pip install pytest==6.2.4
    
    04 returns an error. If so, then lines 32 to 35 display an error message, and line 36 exits the application. Otherwise, line 38 prints a success message in green text.

Để in các tin nhắn trong mã này, bạn sử dụng

(venv) $ python -m pip install pytest==6.2.4
26. Hàm này có một đối số tiền cảnh,
(venv) $ python -m pip install pytest==6.2.4
27, cho phép bạn sử dụng các màu khác nhau khi in văn bản lên màn hình. Typer cung cấp một số màu tích hợp trong
(venv) $ python -m pip install pytest==6.2.4
28. Ở đó, bạn sẽ tìm thấy
(venv) $ python -m pip install pytest==6.2.4
29,
(venv) $ python -m pip install pytest==6.2.4
30,
(venv) $ python -m pip install pytest==6.2.4
31, v.v. Bạn có thể sử dụng những màu đó với
(venv) $ python -m pip install pytest==6.2.4
32 như bạn đã làm ở đây.

Tốt đẹp! Với tất cả mã này, bây giờ bạn có thể thử lệnh

(venv) $ python -m pip install -r requirements.txt
0. Quay trở lại thiết bị đầu cuối của bạn và chạy như sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
5

Lệnh này trình bày cho bạn một lời nhắc để nhập một vị trí cơ sở dữ liệu. Bạn có thể nhấn Enter để chấp nhận đường dẫn mặc định trong dấu ngoặc vuông hoặc bạn có thể nhập vào đường dẫn tùy chỉnh và sau đó nhấn Enter. Ứng dụng tạo cơ sở dữ liệu việc cần làm và cho bạn biết nơi nó sẽ cư trú từ thời điểm này.Enter to accept the default path in square brackets, or you can type in a custom path and then press Enter. The application creates the to-do database and tells you where it’ll reside from this point on.

Ngoài ra, bạn có thể cung cấp đường dẫn cơ sở dữ liệu tùy chỉnh trực tiếp bằng cách sử dụng

(venv) $ python -m pip install -r requirements.txt
0 với các tùy chọn
(venv) $ python -m pip install pytest==6.2.4
18 hoặc
(venv) $ python -m pip install pytest==6.2.4
17 theo sau là đường dẫn mong muốn. Trong mọi trường hợp, đường dẫn tùy chỉnh của bạn nên bao gồm tên tệp cơ sở dữ liệu.

Khi bạn đã chạy lệnh trên, hãy xem thư mục nhà của bạn. Bạn có thể có một tệp JSON được đặt tên theo tên tệp bạn đã sử dụng với

(venv) $ python -m pip install -r requirements.txt
0. Bạn cũng có một thư mục
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
1 chứa tệp
(venv) $ python -m pip install pytest==6.2.4
39 ở đâu đó trong thư mục nhà của bạn. Đường dẫn cụ thể đến tệp này sẽ phụ thuộc vào hệ điều hành hiện tại của bạn. Ví dụ, trên Ubuntu, tệp sẽ ở
(venv) $ python -m pip install pytest==6.2.4
40.

Bước 4: Thiết lập đầu trở lại ứng dụng cần làm

Cho đến thời điểm này, bạn đã tạo ra một cách để tạo, khởi tạo và kết nối với cơ sở dữ liệu việc cần làm. Bây giờ bạn có thể bắt đầu nghĩ về mô hình dữ liệu của bạn. Nói cách khác, bạn cần suy nghĩ về cách đại diện và lưu trữ dữ liệu về việc làm của bạn. Bạn cũng cần xác định cách ứng dụng của bạn sẽ xử lý giao tiếp giữa CLI và cơ sở dữ liệu.

Bạn có thể tải xuống mã và tất cả các tài nguyên bổ sung mà bạn sẽ sử dụng trong phần này bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

(venv) $ python -m pip install pytest==6.2.4
41:

Xác định một việc cần làm

Đầu tiên, hãy nghĩ về dữ liệu bạn cần để xác định một việc cần làm. Trong dự án này, một việc cần làm sẽ bao gồm các phần thông tin sau:

  • Mô tả: Làm thế nào để bạn mô tả điều này cần làm? How do you describe this to-do?
  • Ưu tiên: Ưu tiên này làm điều này có được trong phần còn lại của bạn không? What priority does this to-do have over the rest of your to-dos?
  • Xong: Điều này có phải làm được không? Is this to-do done?

Để lưu trữ thông tin này, bạn có thể sử dụng từ điển Python thông thường:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
6

Khóa

(venv) $ python -m pip install pytest==6.2.4
42 lưu trữ một chuỗi mô tả việc cần làm hiện tại. Khóa
(venv) $ python -m pip install pytest==6.2.4
43 có thể lấy ba giá trị có thể:
(venv) $ python -m pip install pytest==6.2.4
23 cho cao,
(venv) $ python -m pip install pytest==6.2.4
45 cho môi trường và
(venv) $ python -m pip install pytest==6.2.4
46 cho mức độ ưu tiên thấp. Khóa
(venv) $ python -m pip install pytest==6.2.4
47 giữ
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
08 khi bạn đã hoàn thành việc cần làm và
(venv) $ python -m pip install pytest==6.2.4
49 nếu không.

Giao tiếp với CLI

Để liên lạc với CLI, bạn sẽ sử dụng hai mẩu dữ liệu chứa thông tin cần thiết:

  1. (venv) $ python -m pip install pytest==6.2.4
    
    50: Từ điển giữ thông tin cho việc cần làm hiện tại
    : The dictionary holding the information for the current to-do
  2. (venv) $ python -m pip install pytest==6.2.4
    
    51: Mã trả về hoặc mã lỗi xác nhận nếu hoạt động hiện tại có thành công hay không
    : The return or error code confirming if the current operation was successful or not

Để lưu trữ dữ liệu này, bạn sẽ sử dụng một tuple có tên với các trường được đặt tên phù hợp. Mở mô -đun

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0 từ
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
6 để tạo bộ tuple có tên yêu cầu:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
7

Trong

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0, trước tiên bạn nhập một số đối tượng bắt buộc từ
(venv) $ python -m rptodo -v
rptodo v0.1.0

(venv) $ python -m rptodo --help
Usage: rptodo [OPTIONS] COMMAND [ARGS]...

Options:
  -v, --version         Show the application's version and exit.
  --install-completion  Install completion for the current shell.
  --show-completion     Show completion for the current shell, to copy it
                        or customize the installation.

  --help                Show this message and exit.
9. Trên dòng 6, bạn tạo một lớp con của
(venv) $ python -m pip install pytest==6.2.4
56 được gọi là
(venv) $ python -m pip install pytest==6.2.4
57 với hai trường
(venv) $ python -m pip install pytest==6.2.4
50 và
(venv) $ python -m pip install pytest==6.2.4
51.

Phân lớp

(venv) $ python -m pip install pytest==6.2.4
60 cho phép bạn tạo các bộ dữ liệu có tên với gợi ý loại cho các trường được đặt tên của chúng. Ví dụ: trường
(venv) $ python -m pip install pytest==6.2.4
50 ở trên chứa một từ điển với các khóa loại
(venv) $ python -m pip install pytest==6.2.4
62 và các giá trị của loại
(venv) $ python -m pip install pytest==6.2.4
63. Trường
(venv) $ python -m pip install pytest==6.2.4
51 giữ giá trị
(venv) $ python -m pip install pytest==6.2.4
65.

Giao tiếp với cơ sở dữ liệu

Bây giờ bạn cần một thùng chứa dữ liệu khác cho phép bạn gửi dữ liệu đến và truy xuất dữ liệu từ cơ sở dữ liệu việc cần làm. Trong trường hợp này, bạn sẽ sử dụng một tuple có tên khác với các trường sau:

  1. (venv) $ python -m pip install pytest==6.2.4
    
    66: Danh sách việc cần làm mà bạn sẽ viết và đọc từ cơ sở dữ liệu
    : The to-do list you’ll write to and read from the database
  2. (venv) $ python -m pip install pytest==6.2.4
    
    51: Số nguyên đại diện cho mã trả về liên quan đến hoạt động cơ sở dữ liệu hiện tại
    : An integer number representing a return code related to the current database operation

Cuối cùng, bạn sẽ tạo một lớp gọi là

(venv) $ python -m pip install pytest==6.2.4
68 để đọc và ghi dữ liệu vào cơ sở dữ liệu việc cần làm. Đi trước và mở
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
9. Khi bạn ở đó, hãy nhập mã sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
8

Đây là những gì mã này làm:

  • Dòng 4, 6 và 8 Thêm một số nhập khẩu cần thiết. add some required imports.

  • Các dòng 12 đến 14 Xác định

    (venv) $ python -m pip install pytest==6.2.4
    
    70 là một lớp con
    (venv) $ python -m pip install pytest==6.2.4
    
    60. Trường
    (venv) $ python -m pip install pytest==6.2.4
    
    66 là danh sách các từ điển đại diện cho cá nhân TOS, trong khi trường
    (venv) $ python -m pip install pytest==6.2.4
    
    51 giữ mã trả về số nguyên.
    define
    (venv) $ python -m pip install pytest==6.2.4
    
    70 as a
    (venv) $ python -m pip install pytest==6.2.4
    
    60 subclass. The
    (venv) $ python -m pip install pytest==6.2.4
    
    66 field is a list of dictionaries representing individual to-dos, while the
    (venv) $ python -m pip install pytest==6.2.4
    
    51 field holds an integer return code.

  • Dòng 16 Xác định

    (venv) $ python -m pip install pytest==6.2.4
    
    68, cho phép bạn đọc và ghi dữ liệu vào cơ sở dữ liệu việc cần làm bằng mô-đun
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    2 từ thư viện tiêu chuẩn.
    defines
    (venv) $ python -m pip install pytest==6.2.4
    
    68, which allows you to read and write data to the to-do database using the
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    2 module from the standard library.

  • Các dòng 17 và 18 Xác định trình khởi tạo lớp, có một đối số duy nhất đại diện cho đường dẫn đến cơ sở dữ liệu trên hệ thống tệp của bạn. define the class initializer, which takes a single argument representing the path to the database on your file system.

  • Dòng 20 Xác định

    (venv) $ python -m pip install pytest==6.2.4
    
    76. Phương pháp này đọc danh sách việc cần làm từ cơ sở dữ liệu và giải phóng nó. defines
    (venv) $ python -m pip install pytest==6.2.4
    
    76. This method reads the to-do list from the database and deserializes it.

  • Dòng 21 bắt đầu một câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    77 ____ ____278 để bắt bất kỳ lỗi nào xảy ra trong khi bạn đang mở cơ sở dữ liệu. Nếu xảy ra lỗi, thì dòng 28 trả về một thể hiện
    (venv) $ python -m pip install pytest==6.2.4
    
    70 với danh sách việc cần làm trống và
    (venv) $ python -m pip install pytest==6.2.4
    
    80.
    starts a
    (venv) $ python -m pip install pytest==6.2.4
    
    77 …
    (venv) $ python -m pip install pytest==6.2.4
    
    78 statement to catch any errors that occur while you’re opening the database. If an error occurs, then line 28 returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    70 instance with an empty to-do list and a
    (venv) $ python -m pip install pytest==6.2.4
    
    80.

  • Dòng 22 mở cơ sở dữ liệu để đọc bằng cách sử dụng câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    81. opens the database for reading using a
    (venv) $ python -m pip install pytest==6.2.4
    
    81 statement.

  • Dòng 23 bắt đầu một câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    77
    (venv) $ python -m pip install pytest==6.2.4
    
    78 khác để bắt bất kỳ lỗi nào xảy ra trong khi bạn đang tải và giải phóng nội dung JSON từ cơ sở dữ liệu việc cần làm.
    starts another
    (venv) $ python -m pip install pytest==6.2.4
    
    77 …
    (venv) $ python -m pip install pytest==6.2.4
    
    78 statement to catch any errors that occur while you’re loading and deserializing the JSON content from the to-do database.

  • Dòng 24 trả về một ví dụ

    (venv) $ python -m pip install pytest==6.2.4
    
    70 giữ kết quả gọi
    (venv) $ python -m pip install pytest==6.2.4
    
    85 với đối tượng cơ sở dữ liệu cần làm làm đối số. Kết quả này bao gồm một danh sách các từ điển. Mỗi từ điển đại diện cho một việc cần làm. Trường
    (venv) $ python -m pip install pytest==6.2.4
    
    51 của
    (venv) $ python -m pip install pytest==6.2.4
    
    70 giữ
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 để báo hiệu rằng hoạt động đã thành công.
    returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    70 instance holding the result of calling
    (venv) $ python -m pip install pytest==6.2.4
    
    85 with the to-do database object as an argument. This result consists of a list of dictionaries. Every dictionary represents a to-do. The
    (venv) $ python -m pip install pytest==6.2.4
    
    51 field of
    (venv) $ python -m pip install pytest==6.2.4
    
    70 holds
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 to signal that the operation was successful.

  • Dòng 25 bắt bất kỳ

    (venv) $ python -m pip install pytest==6.2.4
    
    89 nào trong khi tải nội dung JSON từ cơ sở dữ liệu và dòng 26 trả về với một danh sách trống và
    (venv) $ python -m pip install pytest==6.2.4
    
    90.
    catches any
    (venv) $ python -m pip install pytest==6.2.4
    
    89 while loading the JSON content from the database, and line 26 returns with an empty list and a
    (venv) $ python -m pip install pytest==6.2.4
    
    90.

  • Dòng 27 bắt gặp bất kỳ sự cố IO nào trong khi tải tệp JSON và dòng 28 trả về một thể hiện

    (venv) $ python -m pip install pytest==6.2.4
    
    70 với danh sách việc cần làm trống và
    (venv) $ python -m pip install pytest==6.2.4
    
    80.
    catches any file IO problems while loading the JSON file, and line 28 returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    70 instance with an empty to-do list and a
    (venv) $ python -m pip install pytest==6.2.4
    
    80.

  • Dòng 30 xác định

    (venv) $ python -m pip install pytest==6.2.4
    
    93, lấy một danh sách các từ điển cần làm và ghi nó vào cơ sở dữ liệu. defines
    (venv) $ python -m pip install pytest==6.2.4
    
    93, which takes a list of to-do dictionaries and writes it to the database.

  • Dòng 31 bắt đầu một câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    77 ____ ____278 để bắt bất kỳ lỗi nào xảy ra trong khi bạn đang mở cơ sở dữ liệu. Nếu xảy ra lỗi, thì dòng 36 trả về một thể hiện
    (venv) $ python -m pip install pytest==6.2.4
    
    70 với danh sách việc cần làm gốc và
    (venv) $ python -m pip install pytest==6.2.4
    
    80.
    starts a
    (venv) $ python -m pip install pytest==6.2.4
    
    77 …
    (venv) $ python -m pip install pytest==6.2.4
    
    78 statement to catch any errors that occur while you’re opening the database. If an error occurs, then line 36 returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    70 instance with the original to-do list and a
    (venv) $ python -m pip install pytest==6.2.4
    
    80.

  • Dòng 32 sử dụng câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    81 để mở cơ sở dữ liệu để viết. uses a
    (venv) $ python -m pip install pytest==6.2.4
    
    81 statement to open the database for writing.

  • Dòng 33 đổ danh sách việc cần làm dưới dạng tải trọng JSON vào cơ sở dữ liệu. dumps the to-do list as a JSON payload into the database.

  • Dòng 34 Trả về một ví dụ

    (venv) $ python -m pip install pytest==6.2.4
    
    70 giữ danh sách việc cần làm và mã
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91.
    returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    70 instance holding the to-do list and the
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    91 code.

Ồ! Đó là rất nhiều! Bây giờ bạn đã hoàn thành mã hóa

(venv) $ python -m pip install pytest==6.2.4
68 và thiết lập cơ chế trao đổi dữ liệu, bạn có thể nghĩ về cách kết nối chúng với ứng dụng CLI CLI.

Viết lớp bộ điều khiển, rptodo_project/ │ ├── rptodo/ │ ├── __init__.py │ ├── __main__.py │ ├── cli.py │ ├── config.py │ ├── database.py │ └── rptodo.py │ ├── tests/ │ ├── __init__.py │ └── test_rptodo.py │ ├── README.md └── requirements.txt 02

Để kết nối logic

(venv) $ python -m pip install pytest==6.2.4
68 với ứng dụng CLI của bạn, bạn sẽ viết một lớp gọi là
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02. Lớp này sẽ hoạt động tương tự như một bộ điều khiển trong mẫu điều khiển mô hình-xem.

Bây giờ quay lại

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0 và thêm mã sau:

(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
9

Mã này bao gồm một số nhập khẩu và định nghĩa của

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02. Lớp này sử dụng thành phần, do đó, nó có thành phần
(venv) $ python -m pip install pytest==6.2.4
68 để tạo điều kiện giao tiếp trực tiếp với cơ sở dữ liệu việc cần làm. Bạn sẽ thêm nhiều mã vào lớp này trong các phần sắp tới.

Trong phần này, bạn đã kết hợp rất nhiều thiết lập hình thành nên kết thúc ứng dụng cần làm của bạn sẽ hoạt động như thế nào. Bạn đã quyết định những cấu trúc dữ liệu nào sẽ sử dụng để lưu trữ dữ liệu việc cần làm. Bạn cũng đã xác định loại cơ sở dữ liệu mà bạn sẽ sử dụng để lưu thông tin việc cần làm và cách vận hành trên đó.

Với tất cả các thiết lập tại chỗ, giờ đây, bạn đã sẵn sàng để bắt đầu cung cấp giá trị cho người dùng của mình bằng cách cho phép họ điền vào danh sách việc cần làm của họ. Bạn cũng sẽ thực hiện một cách để hiển thị các công việc trên màn hình.

Bước 5: Mã hóa các chức năng bổ sung và liệt kê các chức năng to-dos

Trong phần này, bạn sẽ mã hóa một trong những tính năng chính của ứng dụng việc cần làm của bạn. Bạn sẽ cung cấp cho người dùng của mình một lệnh để thêm công việc mới vào danh sách hiện tại của họ. Youllll cũng cho phép người dùng liệt kê các công việc của họ trên màn hình theo định dạng bảng.

Trước khi làm việc trên các tính năng này, bạn sẽ thiết lập một bộ kiểm tra tối thiểu cho mã của bạn. Viết một bộ kiểm tra trước khi viết mã sẽ giúp bạn hiểu phát triển theo hướng thử nghiệm (TDD) là gì.

Để tải xuống mã, kiểm tra đơn vị và tất cả các tài nguyên bổ sung mà bạn sẽ thêm vào phần này, chỉ cần nhấp vào liên kết bên dưới và chuyển đến thư mục

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
08:

Xác định các bài kiểm tra đơn vị cho rptodo_project/ │ ├── rptodo/ │ ├── __init__.py │ ├── __main__.py │ ├── cli.py │ ├── config.py │ ├── database.py │ └── rptodo.py │ ├── tests/ │ ├── __init__.py │ └── test_rptodo.py │ ├── README.md └── requirements.txt 09

Trong phần này, bạn sẽ sử dụng pytest để viết và chạy bộ thử nghiệm tối thiểu cho

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
09. Phương pháp này sẽ quan tâm đến việc thêm các công việc mới vào cơ sở dữ liệu. Với bộ thử nghiệm tại chỗ, bạn sẽ viết mã cần thiết để vượt qua các bài kiểm tra, đây là một ý tưởng cơ bản đằng sau TDD.

Trước khi viết các bài kiểm tra cho

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11, hãy nghĩ về phương pháp này cần phải làm gì:

  1. Nhận mô tả việc cần làm và ưu tiêndescription and priority
  2. Tạo một từ điển để lưu giữ thông tin cần làmto-do information
  3. Đọc danh sách việc cần làm từ cơ sở dữ liệudatabase
  4. Nối thêm việc cần làm vào danh sách việc cần làm hiện tạinew to-do to the current to-do list
  5. Viết danh sách việc cần làm được cập nhật trở lại cơ sở dữ liệuupdated to-do list back to the database
  6. Trả về việc làm mới được thêm vào cùng với mã trả lại cho người gọinewly added to-do along with a return code back to the caller

Một thông lệ phổ biến trong kiểm tra mã là bắt đầu với chức năng chính của một phương thức hoặc chức năng nhất định. Bạn sẽ bắt đầu bằng cách tạo các trường hợp kiểm tra để kiểm tra xem

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11 có thêm thông tin mới vào cơ sở dữ liệu không.

Để kiểm tra

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11, bạn phải tạo một thể hiện
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02 với tệp JSON thích hợp làm cơ sở dữ liệu đích. Để cung cấp tệp đó, bạn sẽ sử dụng một vật cố định nhất.

Quay trở lại Trình chỉnh sửa mã của bạn và mở

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
3 từ thư mục
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
1. Thêm mã sau vào nó:

(venv) $ python -m pip install pytest==6.2.4
0

Tại đây, trước tiên bạn cập nhật nhập khẩu của mình để hoàn thành một số yêu cầu. Vật cố,

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
17, tạo và trả về một tệp JSON tạm thời,
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
18, với một danh sách việc cần làm một mục trong đó. Trong trận đấu này, bạn sử dụng
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
19, đây là đối tượng
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
20 mà PyTest sử dụng để cung cấp thư mục tạm thời cho mục đích thử nghiệm.

Bạn đã có một cơ sở dữ liệu việc cần làm tạm thời để sử dụng. Bây giờ bạn cần một số dữ liệu để tạo trường hợp thử nghiệm của mình:

(venv) $ python -m pip install pytest==6.2.4
1

Hai từ điển này cung cấp dữ liệu để kiểm tra

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
09. Hai khóa đầu tiên đại diện cho dữ liệu bạn sẽ sử dụng làm đối số cho
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11, trong khi khóa thứ ba giữ giá trị trả về dự kiến ​​của phương thức.

Bây giờ, thời gian để viết chức năng thử nghiệm đầu tiên của bạn cho

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11. Với pytest, bạn có thể sử dụng tham số hóa để cung cấp nhiều bộ đối số và kết quả dự kiến ​​cho một chức năng thử nghiệm duy nhất. Đây là một tính năng khá gọn gàng. Nó làm cho một chức năng thử nghiệm duy nhất hoạt động giống như một số chức năng thử nghiệm chạy các trường hợp thử nghiệm khác nhau.test function for
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11. With pytest, you can use parametrization to provide multiple sets of arguments and expected results to a single test function. This is a pretty neat feature. It makes a single test function behave like several test functions that run different test cases.

Tại đây, cách bạn có thể tạo chức năng kiểm tra của mình bằng cách sử dụng tham số trong pytest:

(venv) $ python -m pip install pytest==6.2.4
2

Người trang trí

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
24 đánh dấu
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
25 để tham số. Khi pytest chạy thử nghiệm này, nó gọi
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
25 hai lần. Mỗi cuộc gọi sử dụng một trong các bộ tham số từ dòng 7 đến 11 và dòng 12 đến 16.

Chuỗi trên dòng 5 giữ tên mô tả cho hai tham số cần thiết và cũng là tên giá trị trả về mô tả. Lưu ý rằng

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
25 có những tham số tương tự. Ngoài ra, tham số đầu tiên của
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
25 có cùng tên với vật cố bạn vừa xác định.

Bên trong

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
25, mã thực hiện các hành động sau:

  • Dòng 20 tạo ra một thể hiện là

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 với
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    31 làm đối số.
    creates an instance of
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 with
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    31 as an argument.

  • Dòng 21 khẳng định rằng một cuộc gọi đến

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    11 bằng cách sử dụng
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 và
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 làm đối số sẽ trả về
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    35.
    asserts that a call to
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    11 using
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 and
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 as arguments should return
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    35.

  • Dòng 22 đọc danh sách việc cần làm từ cơ sở dữ liệu tạm thời và lưu trữ nó trong

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    36. reads the to-do list from the temporary database and stores it in
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    36.

  • Dòng 23 khẳng định rằng độ dài của danh sách việc cần làm là

    (venv) $ python -m pip install pytest==6.2.4
    
    45. Tại sao
    (venv) $ python -m pip install pytest==6.2.4
    
    45? Bởi vì
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    17 trả về một danh sách với một việc cần làm và bây giờ bạn đã thêm một danh sách thứ hai.
    asserts that the length of the to-do list is
    (venv) $ python -m pip install pytest==6.2.4
    
    45. Why
    (venv) $ python -m pip install pytest==6.2.4
    
    45? Because
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    17 returns a list with one to-do, and now you’re adding a second one.

Mát mẻ! Bạn có một bài kiểm tra bao gồm chức năng chính của

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11. Bây giờ, thời gian để chạy bộ thử nghiệm của bạn một lần nữa. Quay trở lại dòng lệnh của bạn và chạy
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
70. Bạn sẽ nhận được một đầu ra tương tự như sau:

(venv) $ python -m pip install pytest==6.2.4
3

Các chữ cái F trong dòng được tô sáng có nghĩa là hai trường hợp thử nghiệm của bạn đã thất bại. Có các bài kiểm tra thất bại là bước đầu tiên cho TDD. Bước thứ hai là viết mã để vượt qua các bài kiểm tra đó. Đó là những gì bạn sẽ làm tiếp theo.

Thực hiện lệnh rptodo_project/ │ ├── rptodo/ │ ├── __init__.py │ ├── __main__.py │ ├── cli.py │ ├── config.py │ ├── database.py │ └── rptodo.py │ ├── tests/ │ ├── __init__.py │ └── test_rptodo.py │ ├── README.md └── requirements.txt 42 CLI

Trong phần này, bạn sẽ mã

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11 trong lớp
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02. Bạn cũng sẽ mã hóa lệnh
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
42 trong Typer CLI của bạn. Với hai đoạn mã này, người dùng của bạn sẽ có thể thêm các mục mới vào danh sách việc làm của họ.

Mỗi khi ứng dụng cần làm chạy, nó cần truy cập lớp

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02 và kết nối CLI với cơ sở dữ liệu. Để đáp ứng yêu cầu này, bạn sẽ thực hiện một hàm gọi là
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
47.

Quay trở lại với Trình chỉnh sửa mã của bạn và mở

 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7. Nhập mã sau:

(venv) $ python -m pip install pytest==6.2.4
4

Sau khi cập nhật nhập khẩu, bạn xác định

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
47 trên dòng 18. Dòng 19 Xác định một điều kiện kiểm tra xem tệp cấu hình ứng dụng có tồn tại không. Để làm như vậy, nó sử dụng
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
50.

Nếu tệp cấu hình tồn tại, thì dòng 20 sẽ có đường dẫn đến cơ sở dữ liệu từ nó. Điều khoản

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
51 chạy nếu tệp không tồn tại. Mệnh đề này in một thông báo lỗi lên màn hình và thoát ứng dụng với mã thoát
(venv) $ python -m pip install pytest==6.2.4
23 để báo hiệu lỗi.

Dòng 27 Kiểm tra nếu đường dẫn đến cơ sở dữ liệu tồn tại. Nếu vậy, dòng 28 tạo ra một thể hiện

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02 với đường dẫn như một đối số. Mặt khác, mệnh đề
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
51 bắt đầu trên dòng 29 in thông báo lỗi và thoát khỏi ứng dụng.

Bây giờ bạn có một thể hiện là

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02 với đường dẫn cơ sở dữ liệu hợp lệ, bạn có thể viết
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11. Quay lại mô -đun
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0 và cập nhật
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02:

(venv) $ python -m pip install pytest==6.2.4
5

Tại đây, cách thức

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11 hoạt động từng dòng:

  • Dòng 14 xác định

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    11, lấy
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 và
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 làm đối số. Mô tả là một danh sách các chuỗi. Typer xây dựng danh sách này từ các từ bạn nhập tại dòng lệnh để mô tả việc cần làm hiện tại. Trong trường hợp của
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34, nó có giá trị số nguyên đại diện cho mức độ ưu tiên của việc cần làm. Mặc định là
    (venv) $ python -m pip install pytest==6.2.4
    
    45, biểu thị mức độ ưu tiên trung bình.
    defines
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    11, which takes
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 and
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 as arguments. The description is a list of strings. Typer builds this list from the words you enter at the command line to describe the current to-do. In the case of
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34, it’s an integer value representing the to-do’s priority. The default is
    (venv) $ python -m pip install pytest==6.2.4
    
    45, indicating a medium priority.

  • Dòng 16 nối các thành phần mô tả thành một chuỗi bằng cách sử dụng

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    65. concatenates the description components into a single string using
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    65.

  • Các dòng 17 và 18 Thêm một khoảng thời gian (

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    66) vào cuối mô tả nếu người dùng không thêm nó. add a period (
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    66) to the end of the description if the user doesn’t add it.

  • Các dòng 19 đến 23 xây dựng một việc cần làm mới từ đầu vào của người dùng. build a new to-do from the user’s input.

  • Dòng 24 đọc danh sách việc cần làm từ cơ sở dữ liệu bằng cách gọi

    (venv) $ python -m pip install pytest==6.2.4
    
    76 trên trình xử lý cơ sở dữ liệu. reads the to-do list from the database by calling
    (venv) $ python -m pip install pytest==6.2.4
    
    76 on the database handler.

  • Dòng 25 kiểm tra nếu

    (venv) $ python -m pip install pytest==6.2.4
    
    76 trả về
    (venv) $ python -m pip install pytest==6.2.4
    
    80. Nếu vậy, dòng 26 trả về một bộ tuple có tên,
    (venv) $ python -m pip install pytest==6.2.4
    
    57, chứa mã tiếp theo và mã lỗi.
    checks if
    (venv) $ python -m pip install pytest==6.2.4
    
    76 returned a
    (venv) $ python -m pip install pytest==6.2.4
    
    80. If so, then line 26 returns a named tuple,
    (venv) $ python -m pip install pytest==6.2.4
    
    57, containing the current to-do and the error code.

  • Dòng 27 nối thêm việc cần làm vào danh sách. appends the new to-do to the list.

  • Dòng 28 ghi danh sách việc cần làm được cập nhật trở lại cơ sở dữ liệu bằng cách gọi

    (venv) $ python -m pip install pytest==6.2.4
    
    93 trên trình xử lý cơ sở dữ liệu. writes the updated to-do list back to the database by calling
    (venv) $ python -m pip install pytest==6.2.4
    
    93 on the database handler.

  • Dòng 29 Trả về một thể hiện

    (venv) $ python -m pip install pytest==6.2.4
    
    57 với việc cần làm hiện tại và mã trả lại thích hợp. returns an instance of
    (venv) $ python -m pip install pytest==6.2.4
    
    57 with the current to-do and an appropriate return code.

Bây giờ bạn có thể chạy bộ thử nghiệm của mình một lần nữa để kiểm tra xem

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11 có hoạt động chính xác không. Đi trước và chạy
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
70. Bạn sẽ nhận được một đầu ra tương tự như sau:

(venv) $ python -m pip install pytest==6.2.4
6

Ba chấm màu xanh lá cây có nghĩa là bạn có ba bài kiểm tra vượt qua. Nếu bạn đã tải xuống mã từ repo dự án trên GitHub, thì bạn sẽ nhận được một đầu ra với một vài thử nghiệm thành công hơn.

Sau khi bạn viết xong

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
11, bạn có thể truy cập
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7 và viết lệnh
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
42 cho ứng dụng của bạn CLI CLI:

(venv) $ python -m pip install pytest==6.2.4
7

Ở đây, một sự cố về những gì lệnh

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
42 làm:

  • Các dòng 7 và 8 định nghĩa

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    79 là lệnh Typer bằng cách sử dụng bộ trang trí python
    (venv) $ python -m pip install pytest==6.2.4
    
    14.
    define
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    79 as a Typer command using the
    (venv) $ python -m pip install pytest==6.2.4
    
    14 Python decorator.

  • Dòng 9 định nghĩa

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 là một đối số cho
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    79. Đối số này giữ một danh sách các chuỗi đại diện cho một mô tả việc cần làm. Để xây dựng đối số, bạn sử dụng
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    83. Khi bạn vượt qua dấu chấm lửng (
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    84) như là đối số đầu tiên cho hàm tạo của
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    85, bạn sẽ nói với Typer rằng
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 là bắt buộc. Thực tế là đối số này được yêu cầu có nghĩa là người dùng phải cung cấp mô tả việc cần làm tại dòng lệnh.
    defines
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 as an argument to
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    79. This argument holds a list of strings representing a to-do description. To build the argument, you use
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    83. When you pass an ellipsis (
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    84) as the first argument to the constructor of
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    85, you’re telling Typer that
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    33 is required. The fact that this argument is required means that the user must provide a to-do description at the command line.

  • Dòng 10 xác định

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 là tùy chọn Typer với giá trị mặc định là
    (venv) $ python -m pip install pytest==6.2.4
    
    45. Tên tùy chọn là
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    89 và
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    90. Như bạn đã quyết định trước đó,
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 chỉ chấp nhận ba giá trị có thể:
    (venv) $ python -m pip install pytest==6.2.4
    
    23,
    (venv) $ python -m pip install pytest==6.2.4
    
    45 hoặc
    (venv) $ python -m pip install pytest==6.2.4
    
    46. Để đảm bảo điều kiện này, bạn đặt
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    95 thành
    (venv) $ python -m pip install pytest==6.2.4
    
    23 và
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    97 thành
    (venv) $ python -m pip install pytest==6.2.4
    
    46. Bằng cách này, Typer tự động xác nhận đầu vào của người dùng và chỉ chấp nhận các số trong khoảng thời gian được chỉ định.
    defines
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 as a Typer option with a default value of
    (venv) $ python -m pip install pytest==6.2.4
    
    45. The option names are
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    89 and
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    90. As you decided earlier,
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    34 only accepts three possible values:
    (venv) $ python -m pip install pytest==6.2.4
    
    23,
    (venv) $ python -m pip install pytest==6.2.4
    
    45, or
    (venv) $ python -m pip install pytest==6.2.4
    
    46. To guarantee this condition, you set
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    95 to
    (venv) $ python -m pip install pytest==6.2.4
    
    23 and
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    97 to
    (venv) $ python -m pip install pytest==6.2.4
    
    46. This way, Typer automatically validates the user’s input and only accepts numbers within the specified interval.

  • Dòng 13 có một ví dụ

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 để sử dụng. gets a
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 instance to use.

  • Dòng 14 gọi

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    11 trên
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01 và giải nén kết quả thành
    (venv) $ python -m pip install pytest==6.2.4
    
    50 và
    (venv) $ python -m pip install pytest==6.2.4
    
    51.
    calls
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    11 on
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01 and unpacks the result into
    (venv) $ python -m pip install pytest==6.2.4
    
    50 and
    (venv) $ python -m pip install pytest==6.2.4
    
    51.

  • Các dòng 15 đến 25 Xác định một câu lệnh có điều kiện in thông báo lỗi và thoát khỏi ứng dụng nếu xảy ra lỗi trong khi thêm việc cần làm mới vào cơ sở dữ liệu. Nếu không có lỗi xảy ra, thì mệnh đề

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    51 trên dòng 20 sẽ hiển thị một thông báo thành công trên màn hình. define a conditional statement that prints an error message and exits the application if an error occurs while adding the new to-do to the database. If no error happens, then the
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    51 clause on line 20 displays a success message on the screen.

Bây giờ bạn có thể quay lại thiết bị đầu cuối của mình và thử lệnh

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
42 của bạn:

(venv) $ python -m pip install pytest==6.2.4
8

Trong ví dụ đầu tiên, bạn thực thi lệnh

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
42 với mô tả
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
07 và ưu tiên của
(venv) $ python -m pip install pytest==6.2.4
23. Để đặt mức độ ưu tiên, bạn sử dụng tùy chọn
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
90. Khi bạn nhấn Enter, ứng dụng sẽ thêm việc cần làm và thông báo cho bạn về việc bổ sung thành công. Ví dụ thứ hai hoạt động khá giống nhau. Lần này bạn sử dụng
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
89 để đặt ưu tiên việc cần làm thành
(venv) $ python -m pip install pytest==6.2.4
46.Enter, the application adds the to-do and informs you about the successful addition. The second example works pretty similarly. This time you use
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
89 to set the to-do priority to
(venv) $ python -m pip install pytest==6.2.4
46.

Trong ví dụ thứ ba, bạn cung cấp một mô tả việc cần làm mà không cần ưu tiên. Trong tình huống này, ứng dụng sử dụng giá trị ưu tiên mặc định, đó là

(venv) $ python -m pip install pytest==6.2.4
45.

Trong ví dụ thứ tư, bạn cố gắng thêm một việc cần làm mới với mức độ ưu tiên của

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
13. Vì giá trị ưu tiên này nằm ngoài phạm vi được phép, Typer hiển thị thông báo sử dụng cùng với thông báo lỗi. Lưu ý rằng Typer tự động hiển thị các tin nhắn này cho bạn. Bạn không cần thêm mã cho điều này xảy ra.

Tuyệt quá! Ứng dụng việc cần làm của bạn đã có một số chức năng thú vị. Bây giờ bạn cần một cách để liệt kê tất cả các công việc của mình để có ý tưởng về số lượng công việc bạn có trên đĩa của bạn. Trong phần sau, bạn sẽ thực hiện lệnh

(venv) $ python -m pip install -r requirements.txt
2 để giúp bạn thực hiện nhiệm vụ này.

Thực hiện lệnh (venv) $ python -m pip install -r requirements.txt 2

Trong phần này, bạn sẽ thêm lệnh

(venv) $ python -m pip install -r requirements.txt
2 vào ứng dụng CLI của bạn. Lệnh này sẽ cho phép người dùng của bạn liệt kê tất cả các to-dos hiện tại của họ. Trước khi thêm bất kỳ mã nào vào CLI của bạn, bạn cần một cách để truy xuất toàn bộ danh sách việc cần làm từ cơ sở dữ liệu. Để hoàn thành nhiệm vụ này, bạn sẽ thêm
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
17 vào lớp
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02.

Mở

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0 trong Trình chỉnh sửa mã hoặc IDE của bạn và thêm mã sau:

(venv) $ python -m pip install pytest==6.2.4
9

Bên trong

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
17, trước tiên bạn nhận được toàn bộ danh sách việc cần làm từ cơ sở dữ liệu bằng cách gọi
(venv) $ python -m pip install pytest==6.2.4
76 trên trình xử lý cơ sở dữ liệu. Cuộc gọi đến
(venv) $ python -m pip install pytest==6.2.4
76 trả về một tuple có tên,
(venv) $ python -m pip install pytest==6.2.4
70, chứa danh sách việc cần làm và mã trả lại. Tuy nhiên, bạn chỉ cần danh sách việc cần làm, vì vậy
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
17 chỉ trả về trường
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
25.

Với

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
17 tại chỗ, giờ đây bạn có thể thực hiện lệnh
(venv) $ python -m pip install -r requirements.txt
2 trong ứng dụng CLI CLI. Đi trước và thêm
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
28 vào
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
0

Ở đây, cách thức hoạt động của

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
28:

  • Các dòng 8 và 9 định nghĩa

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    28 là lệnh Typer bằng cách sử dụng trình trang trí
    (venv) $ python -m pip install pytest==6.2.4
    
    14. Đối số
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    33 cho người trang trí này đặt một tên tùy chỉnh cho lệnh, đó là
    (venv) $ python -m pip install -r requirements.txt
    
    2 ở đây. Lưu ý rằng
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    28 không có bất kỳ đối số hoặc tùy chọn nào. Nó chỉ liệt kê các to-dos khi người dùng chạy
    (venv) $ python -m pip install -r requirements.txt
    
    2 từ dòng lệnh.
    define
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    28 as a Typer command using the
    (venv) $ python -m pip install pytest==6.2.4
    
    14 decorator. The
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    33 argument to this decorator sets a custom name for the command, which is
    (venv) $ python -m pip install -r requirements.txt
    
    2 here. Note that
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    28 doesn’t take any argument or option. It just lists the to-dos when the user runs
    (venv) $ python -m pip install -r requirements.txt
    
    2 from the command line.

  • Dòng 11 có được thể hiện

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 mà bạn sẽ sử dụng. gets the
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 instance that you’ll use.

  • Dòng 12 Nhận danh sách việc cần làm từ cơ sở dữ liệu bằng cách gọi

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    17 trên
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01.
    gets the to-do list from the database by calling
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    17 on
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01.

  • Các dòng 13 đến 17 Xác định một câu lệnh có điều kiện để kiểm tra xem có ít nhất một việc cần làm trong danh sách không. Nếu không, thì khối mã

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    40 sẽ in thông báo lỗi vào màn hình và thoát khỏi ứng dụng. define a conditional statement to check if there’s at least one to-do in the list. If not, then the
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    40 code block prints an error message to the screen and exits the application.

  • Dòng 18 in một tiêu đề cấp cao nhất để trình bày danh sách việc cần làm. Trong trường hợp này,

    (venv) $ python -m pip install pytest==6.2.4
    
    32 có một đối số boolean bổ sung có tên
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    42, cho phép bạn hiển thị văn bản ở định dạng phông chữ in đậm.
    prints a top-level header to present the to-do list. In this case,
    (venv) $ python -m pip install pytest==6.2.4
    
    32 takes an additional Boolean argument called
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    42, which enables you to display text in a bolded font format.

  • Các dòng 19 đến 27 Xác định và in các cột cần thiết để hiển thị danh sách việc cần làm theo định dạng bảng. define and print the required columns to display the to-do list in a tabular format.

  • Các dòng 28 đến 36 chạy vòng lặp

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    43 để in từng việc cần làm trên hàng của riêng nó với phần đệm và dấu phân cách thích hợp. run a
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    43 loop to print every single to-do on its own row with appropriate padding and separators.

  • Dòng 37 in một dòng các dấu gạch ngang với ký tự nguồn cấp dữ liệu cuối cùng (

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    44) để tách biệt danh sách việc cần làm khỏi dấu nhắc dòng lệnh tiếp theo. prints a line of dashes with a final line feed character (
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    44) to visually separate the to-do list from the next command-line prompt.

Nếu bạn chạy ứng dụng bằng lệnh

(venv) $ python -m pip install -r requirements.txt
2, thì bạn sẽ nhận được đầu ra sau:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
1

Đầu ra này cho thấy tất cả các to-dos hiện tại trong một bảng được định dạng độc đáo. Bằng cách này, người dùng của bạn có thể theo dõi trạng thái của danh sách các tác vụ của họ. Lưu ý rằng đầu ra sẽ hiển thị trong phông chữ màu xanh trên cửa sổ thiết bị đầu cuối của bạn.

Bước 6: Mã chức năng hoàn thành việc cần làm

Tính năng tiếp theo mà bạn sẽ thêm vào ứng dụng việc cần làm của mình là lệnh Typer cho phép người dùng của bạn đặt một việc được đưa ra hoàn toàn. Bằng cách này, người dùng của bạn có thể theo dõi tiến trình của họ và biết bao nhiêu công việc còn lại.

Một lần nữa, bạn có thể tải xuống mã và tất cả các tài nguyên cho phần này, bao gồm các bài kiểm tra đơn vị bổ sung, bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
46:

Như thường lệ, bạn sẽ bắt đầu bằng cách mã hóa chức năng cần thiết trở lại trong

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02. Trong trường hợp này, bạn cần một phương thức lấy ID cần làm và đánh dấu các việc cần làm tương ứng như đã thực hiện. Quay lại
"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0 trong Trình chỉnh sửa mã của bạn và thêm mã sau:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
2

Phương pháp

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
49 mới của bạn thực hiện công việc cần thiết. Đây là cách: làm thế nào:

  • Dòng 10 Xác định

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    49. Phương thức lấy một đối số gọi là
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51, giữ một số nguyên đại diện cho ID của việc cần làm mà bạn muốn đánh dấu như đã thực hiện. ID cần làm là số được liên kết với một việc cần làm khi bạn liệt kê các công việc của mình bằng lệnh
    (venv) $ python -m pip install -r requirements.txt
    
    2. Vì bạn sử dụng danh sách Python để lưu trữ các công việc, bạn có thể biến ID này thành một chỉ mục dựa trên không và sử dụng nó để truy xuất việc cần làm cần thiết từ danh sách.
    defines
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    49. The method takes an argument called
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51, which holds an integer representing the ID of the to-do you want to mark as done. The to-do ID is the number associated with a given to-do when you list your to-dos using the
    (venv) $ python -m pip install -r requirements.txt
    
    2 command. Since you’re using a Python list to store the to-dos, you can turn this ID into a zero-based index and use it to retrieve the required to-do from the list.

  • Dòng 12 đọc tất cả các to-dos bằng cách gọi

    (venv) $ python -m pip install pytest==6.2.4
    
    76 trên trình xử lý cơ sở dữ liệu. reads all the to-dos by calling
    (venv) $ python -m pip install pytest==6.2.4
    
    76 on the database handler.

  • Dòng 13 Kiểm tra nếu có bất kỳ lỗi xảy ra trong quá trình đọc. Nếu vậy, dòng 14 trả về một tuple có tên,

    (venv) $ python -m pip install pytest==6.2.4
    
    57, với một việc cần làm trống và lỗi. checks if any error occurs during the reading. If so, then line 14 returns a named tuple,
    (venv) $ python -m pip install pytest==6.2.4
    
    57, with an empty to-do and the error.

  • Dòng 15 bắt đầu một câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    77 ____ ____278 để bắt ID không hợp lệ chuyển sang các chỉ số không hợp lệ trong danh sách việc cần làm cơ bản. Nếu xảy ra
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    57, thì dòng 18 trả về một thể hiện
    (venv) $ python -m pip install pytest==6.2.4
    
    57 với một việc cần làm trống và mã lỗi tương ứng.
    starts a
    (venv) $ python -m pip install pytest==6.2.4
    
    77 …
    (venv) $ python -m pip install pytest==6.2.4
    
    78 statement to catch invalid to-do IDs that translate to invalid indices in the underlying to-do list. If an
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    57 occurs, then line 18 returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    57 instance with an empty to-do and the corresponding error code.

  • Dòng 19 gán

    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08 cho khóa
    (venv) $ python -m pip install pytest==6.2.4
    
    47 trong từ điển để làm mục tiêu. Bằng cách này, bạn đã thiết lập việc cần làm như đã thực hiện.
    assigns
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08 to the
    (venv) $ python -m pip install pytest==6.2.4
    
    47 key in the target to-do dictionary. This way, you’re setting the to-do as done.

  • Dòng 20 ghi bản cập nhật trở lại cơ sở dữ liệu bằng cách gọi

    (venv) $ python -m pip install pytest==6.2.4
    
    93 trên trình xử lý cơ sở dữ liệu. writes the update back to the database by calling
    (venv) $ python -m pip install pytest==6.2.4
    
    93 on the database handler.

  • Dòng 21 trả về một thể hiện

    (venv) $ python -m pip install pytest==6.2.4
    
    57 với mục tiêu cần làm và mã trả về cho biết cách thức hoạt động diễn ra. returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    57 instance with the target to-do and a return code indicating how the operation went.

Với

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
49 tại chỗ, bạn có thể chuyển sang
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7 và viết lệnh
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
65. Ở đây, mã cần thiết:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
3

Hãy xem cách mã này hoạt động từng dòng:

  • Các dòng 8 và 9 định nghĩa

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    66 là lệnh Typer với bộ trang trí
    (venv) $ python -m pip install pytest==6.2.4
    
    14 thông thường. Trong trường hợp này, bạn sử dụng
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    65 cho tên lệnh. Hàm
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    66 có một đối số gọi là
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51, mặc định là một thể hiện là
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    83. Trường hợp này sẽ hoạt động như một đối số dòng lệnh bắt buộc.
    define
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    66 as a Typer command with the usual
    (venv) $ python -m pip install pytest==6.2.4
    
    14 decorator. In this case, you use
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    65 for the command name. The
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    66 function takes an argument called
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51, which defaults to an instance of
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    83. This instance will work as a required command-line argument.

  • Dòng 11 có được thể hiện

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 thông thường. gets the usual
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 instance.

  • Dòng 12 đặt việc cần làm với

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51 cụ thể như được thực hiện bằng cách gọi
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    49 trên
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01.
    sets the to-do with the specific
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51 as done by calling
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    49 on
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01.

  • Dòng 13 kiểm tra nếu có bất kỳ lỗi xảy ra trong quá trình. Nếu vậy, các dòng 14 đến 18 In một thông báo lỗi thích hợp và thoát ứng dụng bằng mã thoát

    (venv) $ python -m pip install pytest==6.2.4
    
    23. Nếu không có lỗi xảy ra, thì các dòng 20 đến 23 In một thông báo thành công trong phông chữ màu xanh lá cây. checks if any error occurs during the process. If so, then lines 14 to 18 print an appropriate error message and exit the application with an exit code of
    (venv) $ python -m pip install pytest==6.2.4
    
    23. If no error occurs, then lines 20 to 23 print a success message in green font.

Đó là nó! Bây giờ bạn có thể thử lệnh

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
65 mới của bạn. Quay lại cửa sổ thiết bị đầu cuối của bạn, chạy các lệnh sau:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
4

Đầu tiên, bạn liệt kê tất cả các công việc của mình để trực quan hóa ID tương ứng với từng người trong số họ. Sau đó, bạn sử dụng

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
65 để đặt việc cần làm với ID là
(venv) $ python -m pip install pytest==6.2.4
23 như đã thực hiện. Khi bạn liệt kê một lần nữa, bạn sẽ thấy rằng việc cần làm đầu tiên hiện được đánh dấu là
(venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
08 trong cột đã thực hiện.

Một chi tiết quan trọng cần lưu ý về lệnh

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
65 và phương thức
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
82 cơ bản là ID do-do không phải là giá trị cố định. Nếu bạn xóa một hoặc nhiều việc cần làm khỏi danh sách, thì ID của một số việc cần làm còn lại sẽ thay đổi. Nói về việc loại bỏ TO-DOS, đó là những gì bạn sẽ làm trong phần sau.

Bước 7: Mã chức năng xóa-dos

Loại bỏ TO-DOS khỏi danh sách là một tính năng hữu ích khác mà bạn có thể thêm vào ứng dụng việc cần làm của mình. Trong phần này, bạn sẽ thêm hai lệnh Typer mới vào ứng dụng CLI CLI bằng Python. Lệnh đầu tiên sẽ là

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
83. Nó sẽ cho phép người dùng của bạn xóa một việc cần làm bằng ID của nó. Lệnh thứ hai sẽ là
(venv) $ python -m pip install -r requirements.txt
5 và sẽ cho phép người dùng xóa tất cả các công việc hiện tại khỏi cơ sở dữ liệu.

Bạn có thể tải xuống mã, kiểm tra đơn vị và các tài nguyên bổ sung khác cho phần này bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
85:

Thực hiện lệnh typer==0.3.2 colorama==0.4.4 shellingham==1.4.0 pytest==6.2.4 83 CLI

Để thực hiện lệnh

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
83 trong ứng dụng CLI của bạn, trước tiên bạn cần mã hóa phương thức
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
88 cơ bản trong
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02. Phương pháp này sẽ cung cấp tất cả các chức năng để xóa một việc cần làm khỏi danh sách bằng ID làm. Hãy nhớ rằng bạn đã thiết lập ID cần làm thành một số nguyên liên quan đến một việc cần làm cụ thể. Để hiển thị ID cần làm, hãy chạy lệnh
(venv) $ python -m pip install -r requirements.txt
2.

Tại đây, cách thức bạn có thể mã

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
88 trong
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
5

Ở đây, mã của bạn làm như sau:

  • Dòng 6 xác định

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    88. Phương thức này lấy ID cần làm làm đối số và loại bỏ việc cần làm tương ứng khỏi cơ sở dữ liệu. defines
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    88. This method takes a to-do ID as an argument and removes the corresponding to-do from the database.

  • Dòng 8 đọc danh sách việc cần làm từ cơ sở dữ liệu bằng cách gọi

    (venv) $ python -m pip install pytest==6.2.4
    
    76 trên trình xử lý cơ sở dữ liệu. reads the to-do list from the database by calling
    (venv) $ python -m pip install pytest==6.2.4
    
    76 on the database handler.

  • Dòng 9 kiểm tra nếu có bất kỳ lỗi xảy ra trong quá trình đọc. Nếu vậy, dòng 10 trả về một tuple có tên,

    (venv) $ python -m pip install pytest==6.2.4
    
    57, giữ một việc cần làm trống và mã lỗi tương ứng. checks if any error occurs during the reading process. If so, then line 10 returns a named tuple,
    (venv) $ python -m pip install pytest==6.2.4
    
    57, holding an empty to-do and the corresponding error code.

  • Dòng 11 bắt đầu một câu lệnh

    (venv) $ python -m pip install pytest==6.2.4
    
    77
    (venv) $ python -m pip install pytest==6.2.4
    
    78 để bắt bất kỳ ID không hợp lệ nào đến từ đầu vào của người dùng.
    starts a
    (venv) $ python -m pip install pytest==6.2.4
    
    77 …
    (venv) $ python -m pip install pytest==6.2.4
    
    78 statement to catch any invalid ID coming from the user’s input.

  • Dòng 12 loại bỏ việc cần làm tại Index

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    98 khỏi danh sách việc cần làm. Nếu xảy ra
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    57 trong thao tác này, thì dòng 14 sẽ trả về một thể hiện
    (venv) $ python -m pip install pytest==6.2.4
    
    57 với một việc cần làm trống và mã lỗi tương ứng.
    removes the to-do at index
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    98 from the to-do list. If an
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    57 occurs during this operation, then line 14 returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    57 instance with an empty to-do and the corresponding error code.

  • Dòng 15 viết danh sách việc cần làm được cập nhật trở lại cơ sở dữ liệu. writes the updated to-do list back to the database.

  • Dòng 16 trả về một tuple

    (venv) $ python -m pip install pytest==6.2.4
    
    57 giữ phần cần làm bị loại bỏ và mã trả lại cho biết hoạt động thành công. returns a
    (venv) $ python -m pip install pytest==6.2.4
    
    57 tuple holding the removed to-do and a return code indicating a successful operation.

Bây giờ bạn đã hoàn thành mã hóa

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
88 trong
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02, bạn có thể truy cập
 1"""This module provides the RP To-Do CLI."""
 2# rptodo/cli.py
 3
 4from typing import Optional
 5
 6import typer
 7
 8from rptodo import __app_name__, __version__
 9
10app = typer.Typer()
11
12def _version_callback(value: bool) -> None:
13    if value:
14        typer.echo(f"{__app_name__} v{__version__}")
15        raise typer.Exit()
16
17@app.callback()
18def main(
19    version: Optional[bool] = typer.Option(
20        None,
21        "--version",
22        "-v",
23        help="Show the application's version and exit.",
24        callback=_version_callback,
25        is_eager=True,
26    )
27) -> None:
28    return
7 và thêm lệnh
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
83:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
6

Ồ! Đó là rất nhiều mã. Ở đây, cách thức hoạt động của nó:

  • Dòng 8 và 9 định nghĩa

    (venv) $ python -m pip install -r requirements.txt
    
    06 là lệnh Typer CLI. define
    (venv) $ python -m pip install -r requirements.txt
    
    06 as a Typer CLI command.

  • Dòng 10 định nghĩa

    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51 là đối số của loại
    (venv) $ python -m pip install pytest==6.2.4
    
    65. Trong trường hợp này,
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51 là một ví dụ cần thiết là
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    83.
    defines
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51 as an argument of type
    (venv) $ python -m pip install pytest==6.2.4
    
    65. In this case,
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    51 is a required instance of
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    83.

  • Dòng 11 định nghĩa

    (venv) $ python -m pip install -r requirements.txt
    
    11 là một tùy chọn cho lệnh
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    83. Nó có một tùy chọn Boolean cho phép bạn xóa một việc cần làm mà không cần xác nhận. Tùy chọn này mặc định là
    (venv) $ python -m pip install pytest==6.2.4
    
    49 (dòng 12) và các cờ của nó là
    (venv) $ python -m pip install -r requirements.txt
    
    14 và
    (venv) $ python -m pip install -r requirements.txt
    
    15 (dòng 13 và 14).
    defines
    (venv) $ python -m pip install -r requirements.txt
    
    11 as an option for the
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    83 command. It’s a Boolean option that allows you to delete a to-do without confirmation. This option defaults to
    (venv) $ python -m pip install pytest==6.2.4
    
    49 (line 12) and its flags are
    (venv) $ python -m pip install -r requirements.txt
    
    14 and
    (venv) $ python -m pip install -r requirements.txt
    
    15 (lines 13 and 14).

  • Dòng 15 Xác định thông báo trợ giúp cho tùy chọn

    (venv) $ python -m pip install -r requirements.txt
    
    11. defines a help message for the
    (venv) $ python -m pip install -r requirements.txt
    
    11 option.

  • Dòng 19 tạo ra ví dụ

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 cần thiết. creates the required
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 instance.

  • Các dòng 21 đến 33 Xác định hàm bên trong gọi là

    (venv) $ python -m pip install -r requirements.txt
    
    18. Nó có một chức năng trợ giúp cho phép bạn sử dụng lại chức năng xóa. Hàm loại bỏ một việc cần làm bằng ID của nó. Để làm điều đó, nó gọi
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    88 vào
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01.
    define an inner function called
    (venv) $ python -m pip install -r requirements.txt
    
    18. It’s a helper function that allows you to reuse the remove functionality. The function removes a to-do using its ID. To do that, it calls
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    88 on
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    01.

  • Dòng 35 Kiểm tra giá trị của

    (venv) $ python -m pip install -r requirements.txt
    
    11. Giá trị
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08 có nghĩa là người dùng muốn xóa việc cần làm mà không cần xác nhận. Trong tình huống này, dòng 36 gọi
    (venv) $ python -m pip install -r requirements.txt
    
    18 để chạy thao tác xóa.
    checks the value of
    (venv) $ python -m pip install -r requirements.txt
    
    11. A
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08 value means that the user wants to remove the to-do without confirmation. In this situation, line 36 calls
    (venv) $ python -m pip install -r requirements.txt
    
    18 to run the remove operation.

  • Dòng 37 bắt đầu một mệnh đề

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    51 chạy nếu
    (venv) $ python -m pip install -r requirements.txt
    
    11 là
    (venv) $ python -m pip install pytest==6.2.4
    
    49.
    starts an
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    51 clause that runs if
    (venv) $ python -m pip install -r requirements.txt
    
    11 is
    (venv) $ python -m pip install pytest==6.2.4
    
    49.

  • Dòng 38 Nhận toàn bộ danh sách việc cần làm từ cơ sở dữ liệu. gets the entire to-do list from the database.

  • Các dòng 39 đến 43 Xác định một tuyên bố

    (venv) $ python -m pip install pytest==6.2.4
    
    77 ____ ____278 lấy lại những việc cần làm mong muốn từ danh sách. Nếu xảy ra
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    57, thì dòng 42 in thông báo lỗi và dòng 43 thoát khỏi ứng dụng.
    define a
    (venv) $ python -m pip install pytest==6.2.4
    
    77 …
    (venv) $ python -m pip install pytest==6.2.4
    
    78 statement that retrieves the desired to-do from the list. If an
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    57 occurs, then line 42 prints an error message, and line 43 exits the application.

  • Các dòng 44 đến 46 gọi Typer từ

    (venv) $ python -m pip install -r requirements.txt
    
    30 và lưu trữ kết quả trong
    (venv) $ python -m pip install -r requirements.txt
    
    31. Hàm này cung cấp một cách khác để yêu cầu xác nhận. Nó cho phép bạn sử dụng lời nhắc xác nhận được tạo động như trên dòng 45.
    call Typer’s
    (venv) $ python -m pip install -r requirements.txt
    
    30 and store the result in
    (venv) $ python -m pip install -r requirements.txt
    
    31. This function provides an alternative way to ask for confirmation. It allows you to use a dynamically created confirmation prompt like the one on line 45.

  • Dòng 47 Kiểm tra xem

    (venv) $ python -m pip install -r requirements.txt
    
    31 là
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08, trong trường hợp đó, dòng 48 gọi
    (venv) $ python -m pip install -r requirements.txt
    
    18. Mặt khác, dòng 50 truyền đạt rằng hoạt động đã bị hủy bỏ.
    checks if
    (venv) $ python -m pip install -r requirements.txt
    
    31 is
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08, in which case line 48 calls
    (venv) $ python -m pip install -r requirements.txt
    
    18. Otherwise, line 50 communicates that the operation was canceled.

Bạn có thể dùng thử lệnh

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
83 bằng cách chạy phần sau trên dòng lệnh của bạn:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
7

Trong nhóm lệnh này, trước tiên bạn liệt kê tất cả các công việc hiện tại với lệnh

(venv) $ python -m pip install -r requirements.txt
2. Sau đó, bạn cố gắng xóa việc cần làm với số ID
(venv) $ python -m pip install pytest==6.2.4
23. Điều này trình bày cho bạn một lời nhắc xác nhận có (
(venv) $ python -m pip install -r requirements.txt
38) hoặc không (
(venv) $ python -m pip install -r requirements.txt
39). Nếu bạn nhấn Enter, thì ứng dụng sẽ chạy tùy chọn mặc định,
(venv) $ python -m pip install -r requirements.txt
39 và hủy bỏ hành động xóa.Enter, then the application runs the default option,
(venv) $ python -m pip install -r requirements.txt
39, and cancels the remove action.

Trong lệnh thứ ba, bạn cung cấp rõ ràng câu trả lời

(venv) $ python -m pip install -r requirements.txt
38, do đó ứng dụng sẽ loại bỏ việc cần làm với số ID
(venv) $ python -m pip install pytest==6.2.4
23. Nếu bạn liệt kê tất cả các TO-DOS một lần nữa, thì bạn sẽ thấy rằng việc cần làm
(venv) $ python -m pip install -r requirements.txt
43 không còn trong danh sách. Như một thử nghiệm, hãy tiếp tục và cố gắng sử dụng tùy chọn
(venv) $ python -m pip install -r requirements.txt
14 hoặc
(venv) $ python -m pip install -r requirements.txt
15 hoặc cố gắng xóa một việc cần làm mà không có trong danh sách.

Thực hiện lệnh (venv) $ python -m pip install -r requirements.txt 5 CLI

Trong phần này, bạn sẽ thực hiện lệnh

(venv) $ python -m pip install -r requirements.txt
5. Lệnh này sẽ cho phép người dùng của bạn loại bỏ tất cả các công việc từ cơ sở dữ liệu. Bên dưới lệnh
(venv) $ python -m pip install -r requirements.txt
5 là phương thức
(venv) $ python -m pip install -r requirements.txt
49 từ
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02, cung cấp chức năng back-end.

Quay lại

"""RP To-Do entry point script."""
# rptodo/__main__.py

from rptodo import cli, __app_name__

def main():
    cli.app(prog_name=__app_name__)

if __name__ == "__main__":
    main()
0 và thêm
(venv) $ python -m pip install -r requirements.txt
49 vào cuối
rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
02:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
8

Bên trong

(venv) $ python -m pip install -r requirements.txt
49, bạn loại bỏ tất cả các công việc từ cơ sở dữ liệu bằng cách thay thế danh sách việc cần làm hiện tại bằng một danh sách trống. Để thống nhất, phương thức trả về một bộ
(venv) $ python -m pip install pytest==6.2.4
57 với một từ điển trống và mã hoàn trả hoặc mã lỗi thích hợp.

Bây giờ bạn có thể thực hiện lệnh

(venv) $ python -m pip install -r requirements.txt
5 trong ứng dụng CLI CLI:

rptodo_project/
│
├── rptodo/
│   ├── __init__.py
│   ├── __main__.py
│   ├── cli.py
│   ├── config.py
│   ├── database.py
│   └── rptodo.py
│
├── tests/
│   ├── __init__.py
│   └── test_rptodo.py
│
├── README.md
└── requirements.txt
9

Ở đây, cách thức hoạt động của mã này:

  • Các dòng 8 và 9 định nghĩa

    (venv) $ python -m pip install -r requirements.txt
    
    57 là lệnh Typer sử dụng trình trang trí
    (venv) $ python -m pip install pytest==6.2.4
    
    14 với
    (venv) $ python -m pip install -r requirements.txt
    
    5 làm tên lệnh.
    define
    (venv) $ python -m pip install -r requirements.txt
    
    57 as a Typer command using the
    (venv) $ python -m pip install pytest==6.2.4
    
    14 decorator with
    (venv) $ python -m pip install -r requirements.txt
    
    5 as the command name.

  • Dòng 10 đến 14 Xác định

    (venv) $ python -m pip install -r requirements.txt
    
    11 là Typer
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    20. Nó là một tùy chọn cần thiết của loại Boolean. Đối số
    (venv) $ python -m pip install pytest==6.2.4
    
    19 yêu cầu người dùng nhập giá trị thích hợp vào
    (venv) $ python -m pip install -r requirements.txt
    
    11, có thể là
    (venv) $ python -m pip install -r requirements.txt
    
    38 hoặc
    (venv) $ python -m pip install -r requirements.txt
    
    65.
    define
    (venv) $ python -m pip install -r requirements.txt
    
    11 as a Typer
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    20. It’s a required option of the Boolean type. The
    (venv) $ python -m pip install pytest==6.2.4
    
    19 argument asks the user to enter a proper value to
    (venv) $ python -m pip install -r requirements.txt
    
    11, which can be either
    (venv) $ python -m pip install -r requirements.txt
    
    38 or
    (venv) $ python -m pip install -r requirements.txt
    
    65.

  • Dòng 13 cung cấp thông báo trợ giúp cho tùy chọn

    (venv) $ python -m pip install -r requirements.txt
    
    11. provides a help message for the
    (venv) $ python -m pip install -r requirements.txt
    
    11 option.

  • Dòng 17 có được ví dụ

    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 thông thường. gets the usual
    rptodo_project/
    │
    ├── rptodo/
    │   ├── __init__.py
    │   ├── __main__.py
    │   ├── cli.py
    │   ├── config.py
    │   ├── database.py
    │   └── rptodo.py
    │
    ├── tests/
    │   ├── __init__.py
    │   └── test_rptodo.py
    │
    ├── README.md
    └── requirements.txt
    
    02 instance.

  • Dòng 18 kiểm tra xem

    (venv) $ python -m pip install -r requirements.txt
    
    11 là
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08. Nếu vậy, thì khối mã
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    40 sẽ loại bỏ tất cả các công việc từ cơ sở dữ liệu bằng cách sử dụng
    (venv) $ python -m pip install -r requirements.txt
    
    49. Nếu có sự cố xảy ra trong quá trình này, ứng dụng sẽ in một thông báo lỗi và thoát (dòng 21 đến 25). Nếu không, nó in một thông báo thành công trên dòng 27.
    checks if
    (venv) $ python -m pip install -r requirements.txt
    
    11 is
    (venv) $ python -m pip install typer==0.3.2 colorama==0.4.4 shellingham==1.4.0
    
    08. If so, then the
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    40 code block removes all the to-dos from the database using
    (venv) $ python -m pip install -r requirements.txt
    
    49. If something goes wrong during this process, the application prints an error message and exits (lines 21 to 25). Otherwise, it prints a success message on line 27.

  • Dòng 29 chạy nếu người dùng hủy bỏ thao tác xóa bằng cách cung cấp giá trị sai, chỉ ra không, cho

    (venv) $ python -m pip install -r requirements.txt
    
    11. runs if the user cancels the remove operation by supplying a false value, indicating no, to
    (venv) $ python -m pip install -r requirements.txt
    
    11.

Để đưa ra lệnh

(venv) $ python -m pip install -r requirements.txt
5 mới này, hãy tiếp tục và chạy như sau trên thiết bị đầu cuối của bạn:

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
0

Trong ví dụ đầu tiên, bạn chạy

(venv) $ python -m pip install -r requirements.txt
5. Khi bạn nhấn Enter, bạn sẽ nhận được lời nhắc yêu cầu có (
(venv) $ python -m pip install -r requirements.txt
38) hoặc không xác nhận (
(venv) $ python -m pip install -r requirements.txt
39). Uppercase
(venv) $ python -m pip install -r requirements.txt
39 có nghĩa là không có câu trả lời mặc định, vì vậy nếu bạn nhấn Enter, bạn sẽ hủy bỏ hoạt động
(venv) $ python -m pip install -r requirements.txt
5 một cách hiệu quả.Enter, you get a prompt asking for yes (
(venv) $ python -m pip install -r requirements.txt
38) or no (
(venv) $ python -m pip install -r requirements.txt
39) confirmation. The uppercase
(venv) $ python -m pip install -r requirements.txt
39 means that no is the default answer, so if you press Enter, you effectively cancel the
(venv) $ python -m pip install -r requirements.txt
5 operation.

Trong ví dụ thứ hai, bạn lại chạy

(venv) $ python -m pip install -r requirements.txt
5. Lần này, bạn nhập rõ ràng
(venv) $ python -m pip install -r requirements.txt
38 làm câu trả lời cho lời nhắc. Câu trả lời này làm cho ứng dụng xóa toàn bộ danh sách việc cần làm khỏi cơ sở dữ liệu. Khi bạn chạy lệnh
(venv) $ python -m pip install -r requirements.txt
2, bạn sẽ nhận được thông báo rằng không có tác vụ nào trong danh sách việc cần làm hiện tại.

Đó là nó! Bây giờ bạn có một ứng dụng cần làm chức năng CLI được xây dựng với Python và Typer. Ứng dụng của bạn cung cấp các lệnh và tùy chọn để tạo các công việc mới, liệt kê tất cả các công việc của bạn, quản lý việc hoàn thành việc cần làm và xóa to-dos khi cần thiết. Có phải là tuyệt vời không?

Sự kết luận

Xây dựng các ứng dụng giao diện dòng lệnh (CLI) thân thiện với người dùng là một kỹ năng cơ bản để có nhà phát triển Python. Trong hệ sinh thái Python, bạn sẽ tìm thấy một số công cụ để tạo loại ứng dụng này. Các thư viện như

(venv) $ python -m pip install -r requirements.txt
82, Click và Typer là những ví dụ điển hình về các công cụ đó trong Python. Tại đây, bạn đã xây dựng một ứng dụng CLI để quản lý một danh sách các công việc bằng cách sử dụng Python và Typer.

Trong hướng dẫn này, bạn đã học được cách:

  • Xây dựng một ứng dụng cần làm với Python và Typerto-do application with Python and Typer
  • Thêm các lệnh, đối số và tùy chọn vào ứng dụng việc cần làm của bạn bằng Typercommands, arguments, and options to your to-do application using Typer
  • Kiểm tra ứng dụng việc cần làm của bạn bằng Typer từ
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1 và pytest trong PythonTyper’s
    typer==0.3.2
    colorama==0.4.4
    shellingham==1.4.0
    pytest==6.2.4
    
    1
    and pytest in Python

Bạn cũng đã thực hành một số kỹ năng bổ sung, chẳng hạn như làm việc với các tệp JSON bằng mô -đun Python, ____ ____42 và quản lý các tệp cấu hình với mô -đun Python tựa

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
3. Bây giờ bạn đã sẵn sàng để xây dựng các ứng dụng dòng lệnh.JSON files using Python’s
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
2 module and managing configuration files with Python’s
typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
3 module. Now you’re ready to build command-line applications.

Bạn có thể tải xuống toàn bộ mã và tất cả các tài nguyên cho dự án này bằng cách nhấp vào liên kết bên dưới và truy cập thư mục

typer==0.3.2
colorama==0.4.4
shellingham==1.4.0
pytest==6.2.4
4:

Bước tiếp theo

Trong hướng dẫn này, bạn đã xây dựng một ứng dụng cần làm chức năng cho dòng lệnh của mình bằng Python và Typer. Mặc dù ứng dụng chỉ cung cấp một tập hợp các tính năng tối thiểu, nhưng nó là một điểm khởi đầu tốt để bạn tiếp tục thêm các tính năng và tiếp tục học trong quá trình. Điều này sẽ giúp bạn đưa các kỹ năng Python của bạn lên một tầm cao mới.

Dưới đây là một vài ý tưởng bạn có thể thực hiện để tiếp tục mở rộng ứng dụng việc cần làm của mình:

  • Thêm hỗ trợ cho ngày và thời hạn: Bạn có thể sử dụng mô -đun

    (venv) $ python -m pip install -r requirements.txt
    
    87 để hoàn thành việc này. Tính năng này sẽ cho phép người dùng kiểm soát tốt hơn các nhiệm vụ của họ. You can use the
    (venv) $ python -m pip install -r requirements.txt
    
    87 module to get this done. This feature will allow the users to have better control over their tasks.

  • Viết thêm các bài kiểm tra đơn vị: Bạn có thể sử dụng pytest để viết thêm các bài kiểm tra cho mã của mình. Điều này sẽ tăng phạm vi bảo hiểm và giúp bạn cải thiện kỹ năng kiểm tra. Bạn có thể khám phá một số lỗi trong quá trình. Nếu vậy, hãy tiếp tục và đăng chúng trong các ý kiến. You can use pytest to write more tests for your code. This will increase the code coverage and help you improve your testing skills. You may discover some bugs in the process. If so, go ahead and post them in the comments.

  • Đóng gói ứng dụng và xuất bản nó lên PYPI: Bạn có thể sử dụng thơ hoặc một công cụ tương tự khác để đóng gói ứng dụng việc cần làm của mình và xuất bản nó lên PYPI. You can use Poetry or another similar tool to package your to-do application and publish it to PyPI.

Đây chỉ là một số ý kiến. Thực hiện thử thách và xây dựng một cái gì đó tuyệt vời trên đầu dự án này! Bạn sẽ học được rất nhiều trong quá trình.