Custom Script Libraries Creation — Guided Lab
Scenario:
You have several Python scripts for repetitive tasks like data cleaning, API calls, or calculations. Instead of copying code across projects, you want to bundle them into reusable modules or libraries. Your AI assistant can help organize, refactor, and generate a library structure.
Step 0: Define Your Goal
Example scripts:
calculate_factorials.py– factorial functionvalidate_emails.py– API-based email validationprocess_csv.py– CSV processing functions
Goal: Create a Python library that bundles these scripts with proper module structure, docstrings, and importable functions.
Step 1: Craft the AI Prompt
Treat AI as a senior Python developer. Include:
- Scripts or functions to include
- Desired library structure
- Optional: docstrings, comments, and example usage
Example Prompt:
Create a Python library named `my_utils` that includes:
1. factorial() from calculate_factorials.py
2. validate_email() from validate_emails.py
3. process_csv() from process_csv.py
Include docstrings, example usage, and a __init__.py file for imports.
Step 2: AI Output Example
Directory Structure:
my_utils/
__init__.py
math_utils.py
email_utils.py
csv_utils.py
math_utils.py:
def factorial(n: int) -> int:
"""
Calculate the factorial of a non-negative integer n.
"""
if n < 0:
raise ValueError("Input must be non-negative")
result = 1
for i in range(2, n+1):
result *= i
return result
email_utils.py:
import requests
def validate_email(email: str) -> bool:
"""
Validate an email using an external API.
"""
response = requests.get(f"https://api.emailverify.com/verify?email={email}")
return response.json().get('valid', False)
csv_utils.py:
import csv
def write_csv(filename: str, data: list, headers: list):
"""
Write data to a CSV file with headers.
"""
with open(filename, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(headers)
writer.writerows(data)
init.py:
from .math_utils import factorial
from .email_utils import validate_email
from .csv_utils import write_csv
Step 3: Test the Library
from my_utils import factorial, validate_email, write_csv
print(factorial(5)) # 120
print(validate_email("test@example.com"))
write_csv("output.csv", [[1,2],[3,4]], ["A","B"])
- Verify functions are importable and working correctly
- Test each module separately before using in larger projects
Step 4: Mini Lab Challenges
- Add more utility functions for data cleaning or API handling.
- Generate unit tests for each function using AI.
- Ask AI to create documentation in Markdown for the library.
- Challenge: Package the library for pip installation locally or privately.
Step 5: Pro Tips
- Treat AI as a Python packaging expert for proper module structure
- Include docstrings, examples, and testing instructions
- Iteratively refine prompts to add features or improve readability
- Using AI for libraries reduces repetitive work and ensures reusable code
Lab Summary
- AI helps bundle scripts into a reusable Python library
- Clear prompts + function list = organized, importable modules
- Test each module and maintain documentation for easier collaboration
- Custom script libraries accelerate project development and code reuse


