Skip to content

Instantly share code, notes, and snippets.

@rcmdnk
Created January 26, 2025 09:43
Show Gist options
  • Save rcmdnk/70ca8154b6b487caef02d7f393bb361d to your computer and use it in GitHub Desktop.
Save rcmdnk/70ca8154b6b487caef02d7f393bb361d to your computer and use it in GitHub Desktop.

PythonでTodoリストを作成するプロジェクトの基本的なディレクトリ構造と、それぞれのディレクトリ内に配置する基本的なファイル を次に示します。この構造は典型的なPythonプロジェクトのレイアウトで、拡張性や保守性に優れています。

プロジェクトのディレクトリ構造

todo_project/
│
├── README.md
├── requirements.txt
├── setup.py
├── .gitignore
├── todo/
│   ├── __init__.py
│   ├── main.py
│   ├── utils.py
│   └── models.py
│
├── tests/
│   ├── __init__.py
│   └── test_main.py
│
└── data/
    └── todos.json

各ファイルの内容

README.md

プロジェクトの説明やセットアップ手順を書くためのファイルです。

# Todo List Project

This is a simple Todo List application written in Python.

## Setup

1. Clone the repository.
2. Install dependencies using `pip install -r requirements.txt`.

## Usage

Run the application with:

python -m todo.main

requirements.txt

プロジェクトで使用するPythonパッケージを列挙します(必要なパッケージを追加してください)。

# Example dependencies
pytest

setup.py

パッケージ化や配布時に使用する設定を記述します。

from setuptools import setup, find_packages

setup(
    name='todo_project',
    version='0.1',
    packages=find_packages(),
    install_requires=[
        # Add package dependencies here
    ],
)

.gitignore

Gitで無視するファイルやディレクトリを指定します。

__pycache__/
*.pyc
*.pyo
env/
venv/

todo/__init__.py

パッケージとして認識させるための空ファイルです。

# This file is intentionally left blank

todo/main.py

アプリケーションのエントリーポイントとなるファイルです。

from .utils import add_todo, view_todos

def main():
    # Example interactions
    add_todo("Buy groceries")
    add_todo("Read a book")
    view_todos()

if __name__ == "__main__":
    main()

todo/utils.py

Todoリストの操作を行う関数を定義するファイルです。

import json
from .models import Todo

TODO_FILE = 'data/todos.json'

def add_todo(task):
    todo = Todo(task=task)
    todos = load_todos()
    todos.append(todo)
    save_todos(todos)

def view_todos():
    todos = load_todos()
    for todo in todos:
        print(todo)

def load_todos():
    try:
        with open(TODO_FILE, 'r') as file:
            todos_data = json.load(file)
            return [Todo(**todo) for todo in todos_data]
    except FileNotFoundError:
        return []

def save_todos(todos):
    todos_data = [todo.to_dict() for todo in todos]
    with open(TODO_FILE, 'w') as file:
        json.dump(todos_data, file, indent=4)

todo/models.py

Todoリストのデータモデルを定義するファイルです。

class Todo:
    def __init__(self, task):
        self.task = task

    def __str__(self):
        return f'Task: {self.task}'

    def to_dict(self):
        return {"task": self.task}

tests/__init__.py

テストパッケージとして認識させるための空ファイルです。

# This file is intentionally left blank

tests/test_main.py

テストを記述するためのファイルです(pytestなどを使用します)。

def test_example():
    assert True  # Replace with real tests

data/todos.json

Todoリストを保存するためのファイルです(初期状態は空のJSONファイルとして置いておきます)。

[]

これで、基本的な構造を持つTodoリストプロジェクトができました。必要に応じて機能を追加したり、テストを充実させてください。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment