Department SQL Class Exercise
Hands-On Exercises: Python Modules & Packages
Exercise : Create Your String Tools Functions
Goal: Write simple functions that weβll later turn into a module
Create a new file called string_tools.py
and write these functions:
def reverse_string(text):
"""
Reverse a string
Example: reverse_string("hello") returns "olleh"
"""
# Your code here
pass
def count_vowels(text):
"""
Count vowels (a, e, i, o, u) in a string
Example: count_vowels("hello") returns 2
"""
# Your code here
pass
def is_palindrome(text):
"""
Check if a string reads the same forwards and backwards
Example: is_palindrome("racecar") returns True
"""
# Your code here
pass
Test your functions by adding this at the bottom of string_tools.py
:
if __name__ == "__main__":
# Test your functions here
print(reverse_string("hello"))
print(count_vowels("hello world"))
print(is_palindrome("racecar"))
Exercise 2: Use Your Module
Goal: Import and use your functions from another script
Create a new file called main.py
in the same folder as string_tools.py
:
# Import your functions here
# Try different import styles:
# 1. import string_tools
# 2. from string_tools import reverse_string
# 3. from string_tools import *
# Test your imported functions
test_word = "python"
# Use your functions and print the results
Questions to explore:
- Whatβs the difference between
import string_tools
andfrom string_tools import reverse_string
? - How do you call the functions with each import style?
Exercise 3: Convert to a Package Structure
Goal: Reorganize your code into a proper package
Create this folder structure:
texttools/ # This is your package folder
βββ __init__.py # Makes it a package
βββ string_utils.py # Move your functions here
βββ examples.py # Example usage
Step by step:
- Create a folder called
texttools
- Create an empty
__init__.py
file inside it - Move your functions from
string_tools.py
totexttools/string_utils.py
- Create
texttools/examples.py
with some example usage - Update
texttools/__init__.py
to expose your functions:
# In texttools/__init__.py
from .string_utils import reverse_string, count_vowels, is_palindrome
# Optional: define what gets imported with "from texttools import *"
__all__ = ['reverse_string', 'count_vowels', 'is_palindrome']
Exercise 4: Use Your Package
Goal: Import and use your package from outside its folder
Create a new file called test_package.py
outside the texttools
folder:
your_project/
βββ texttools/ # Your package
β βββ __init__.py
β βββ string_utils.py
β βββ examples.py
βββ test_package.py # This file
In test_package.py
, try different ways to import:
# Method 1: Import the package
import texttools
result1 = texttools.reverse_string("hello")
# Method 2: Import specific functions
from texttools import count_vowels, is_palindrome
result2 = count_vowels("programming")
# Method 3: Import everything
from texttools import *
result3 = reverse_string("python")
print(f"Reversed: {result1}")
print(f"Vowels: {result2}")
print(f"Reversed again: {result3}")
Exercise 5: Add More Features to Your Package
Goal: Expand your package with additional functionality
Add a new module texttools/text_stats.py
:
def word_count(text):
"""Count words in text"""
# Your code here
pass
def char_frequency(text):
"""Return a dictionary of character frequencies"""
# Your code here
pass
def longest_word(text):
"""Find the longest word in text"""
# Your code here
pass
Update your texttools/__init__.py
:
from .string_utils import reverse_string, count_vowels, is_palindrome
from .text_stats import word_count, char_frequency, longest_word
__all__ = [
'reverse_string', 'count_vowels', 'is_palindrome',
'word_count', 'char_frequency', 'longest_word'
]
Test your expanded package!
Bonus Challenge: Make It Installable
Goal: Create a package that can be installed with pip
Create a pyproject.toml
file in your project root:
[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "texttools-yourname" # Replace yourname with your actual name
version = "0.1.0"
description = "A simple text processing toolkit"
authors = [{name = "Your Name", email = "your.email@example.com"}]
readme = "README.md"
requires-python = ">=3.7"
[project.urls]
Homepage = "https://github.com/yourusername/texttools"
Install your package in development mode:
pip install -e .
Now you can import your package from anywhere!
Reflection Questions
- Whatβs the difference between a module and a package?
- Why do we need
__init__.py
files? - When would you use
from module import *
vsimport module
? - How does Python find your modules and packages?
- What are the advantages of organizing code into packages?