tinycodinglm / code /appdev-0000.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
2.28 kB
opencode trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

Walk me through building a cli todo app: key concepts, concrete details and common pitfalls. Include a runnable code example with its expected output.

Assistant

CLI Todo App:

python
#!/usr/bin/env python3
"""Simple CLI todo list app."""
import json
import sys

TODO_FILE = 'todos.json'

def load_todos():
    try:
        with open(TODO_FILE) as f:
            return json.load(f)
    except FileNotFoundError:
        return []

def save_todos(todos):
    with open(TODO_FILE, 'w') as f:
        json.dump(todos, f, indent=2)

def add_todo(title):
    todos = load_todos()
    todos.append({'id': len(todos)+1, 'title': title, 'done': False})
    save_todos(todos)
    print(f'Added: {title}')

def list_todos():
    todos = load_todos()
    if not todos:
        print('No todos.')
        return
    for t in todos:
        mark = '✓' if t['done'] else ' '
        print(f"  [{mark}] {t['id']}. {t['title']}")

def done_todo(id):
    todos = load_todos()
    for t in todos:
        if t['id'] == id:
            t['done'] = True
    save_todos(todos)
    print(f'Todo {id} done!')

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: todo.py [add|list|done] [args]')
        sys.exit(1)
    cmd = sys.argv[1]
    if cmd == 'add':
        add_todo(' '.join(sys.argv[2:]))
    elif cmd == 'list':
        list_todos()
    elif cmd == 'done':
        done_todo(int(sys.argv[2]))
bash
python todo.py add "Buy groceries"
python todo.py add "Write code"
python todo.py list
python todo.py done 1
python todo.py list