add main.py
This commit is contained in:
@@ -0,0 +1,106 @@
|
|||||||
|
"""
|
||||||
|
Main entry point for the AI Fluency Plan project.
|
||||||
|
|
||||||
|
This script demonstrates how to load and display a personal AI fluency plan that is stored in ``plan.txt``.
|
||||||
|
It also provides three example usages:
|
||||||
|
1. Print the entire plan.
|
||||||
|
2. Show only the first 5 lines of the plan.
|
||||||
|
3. Count the number of words in the plan.
|
||||||
|
|
||||||
|
The goal of this repository is to showcase a simple, well‑structured Python project that can be used as a template for future assignments.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import List
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Utility functions
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def read_plan_file(plan_path: str | Path) -> str:
|
||||||
|
"""Return the full text of the plan file.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
plan_path:
|
||||||
|
Path to ``plan.txt``. The function accepts either a string or a
|
||||||
|
:class:`pathlib.Path` instance.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
str
|
||||||
|
Raw contents of the plan file.
|
||||||
|
"""
|
||||||
|
path = Path(plan_path)
|
||||||
|
if not path.exists():
|
||||||
|
raise FileNotFoundError(f"Plan file {plan_path!s} does not exist")
|
||||||
|
return path.read_text(encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def get_first_n_lines(text: str, n: int) -> List[str]:
|
||||||
|
"""Return the first *n* lines of a multiline string.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
text:
|
||||||
|
Multiline string to split.
|
||||||
|
n:
|
||||||
|
Number of lines to return.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
list[str]
|
||||||
|
List containing up to ``n`` lines.
|
||||||
|
"""
|
||||||
|
return text.splitlines()[:n]
|
||||||
|
|
||||||
|
|
||||||
|
def count_words(text: str) -> int:
|
||||||
|
"""Return the number of words in *text*.
|
||||||
|
|
||||||
|
Words are split on whitespace. Empty strings are ignored.
|
||||||
|
"""
|
||||||
|
return len([w for w in text.split() if w])
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Main logic
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
"""Demonstrate the three example usages of the plan loader.
|
||||||
|
|
||||||
|
The function prints output to stdout. It is intentionally simple so
|
||||||
|
that it can be run in any environment without external dependencies.
|
||||||
|
"""
|
||||||
|
plan_path = Path(__file__).parent / "plan.txt"
|
||||||
|
try:
|
||||||
|
full_plan = read_plan_file(plan_path)
|
||||||
|
except FileNotFoundError as exc:
|
||||||
|
print(exc)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Example 1: Print the entire plan.
|
||||||
|
print("\n=== Full AI Fluency Plan ===")
|
||||||
|
print(full_plan)
|
||||||
|
|
||||||
|
# Example 2: Show only the first five lines.
|
||||||
|
print("\n=== First 5 lines of the plan ===")
|
||||||
|
for line in get_first_n_lines(full_plan, 5):
|
||||||
|
print(line)
|
||||||
|
|
||||||
|
# Example 3: Count words.
|
||||||
|
word_count = count_words(full_plan)
|
||||||
|
print(f"\nWord count: {word_count}")
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Entry point guard
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# End of file
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
Reference in New Issue
Block a user