☆ Exercise 1
In this exercise we are going to use the a simple statistics library https://github.com/Bristol-Training/demo-stats-lib.Repository: https://github.com/Bristol-Training/demo-stats-lib
Form groups of 3-6 people and assign roles within your group:
- 1 person: Repository Owner (creates fork and manages PRs)
- 1-2 people: Feature Developers A (add std_dev function)
- 1-2 people: Feature Developers B (add NA handling)
- 1-2 people: Code Formatters (apply Black formatting)
Part 1: Repository Setup
Repository Owner does these steps:
Step 1: Fork the Repository
- Go to
https://github.com/Bristol-Training/demo-stats-lib - Click Fork (top-right)
- This creates a copy under your GitHub account
Step 2: Add Collaborators
- In your forked repository, go to Settings → Collaborators
- Click Add people
- Add each team member by their GitHub username
- Team members will receive an email invitation they must accept
Step 3: Everyone Clones the Fork
All team members (including owner):
# Replace OWNER-USERNAME with the repository owner's username
git clone https://github.com/OWNER-USERNAME/demo-stats-lib.git
cd demo-stats-libPart 2: Parallel Development
Objective: Make statistics functions robust to missing values NA.
Task Instructions
Step 1: Create and switch to your feature branch
git checkout main
git pull origin main
git checkout -b feature/na-handlingStep 2: Modify stats_lib.py
Add NA handling to existing functions. For example:
# Filter out None values
clean_data = [x for x in data if x is not None]Step 3: Update stats_demo.py to test with NA values
# Add test cases with None values
test_data_with_na = [1, 2, None, 4, 5, None, 7]
print(f"Mean (with NAs): {mean(test_data_with_na)}")
print(f"Variance (with NAs): {variance(test_data_with_na)}")Step 4: Test your changes
python stats_demo.py data/temp1995.csvStep 5: Commit and push
git add stats_lib.py stats_demo.py
git commit -m "Add NA handling to statistics functions"
git push origin feature/na-handlingStep 6: Create Pull Request
Go to GitHub repository
Click “Compare & pull request”
Title:
"Add NA handling to statistics functions"Description:
## Changes - Added None value filtering to all statistics functions - Updated demo.py with NA test cases ## Testing - Tested with datasets containing None values - All functions handle empty datasets appropriatelyClick “Create pull request” (don’t merge yet!)
Objective: Add a new standard deviation function to the statistics library.
Task Instructions
Step 1: Create your feature branch
git checkout main
git pull origin main
git checkout -b feature/add-stdevStep 2: Add the std_dev function to stats_lib.py
You can use:
import math
math.sqrt(variance(data))Step 3: Update stats_demo.py to demonstrate std_dev
from stats_lib import mean, variance, std_dev# Demonstrate standard deviation
print("\n--- Standard Deviation ---")
print(f"Standard deviation: {std_dev(data)}")Step 4: Test your function
python stats_demo.py data/temp1995.csvYou should see the standard deviation calculated for the temperature data.
Step 5: Commit and push
git add stats_lib.py demo.py
git commit -m "Add std_dev function to calculate standard deviation"
git push origin feature/add-stdevStep 6: Create Pull Request
Go to GitHub repository
Click “Compare & pull request”
Title:
"Add standard deviation function"Description:
## Changes - Added `std_dev(data)` function to statistics.py - Function calculates standard deviation as sqrt(variance) - Updated stats_demo.py to demonstrate the new function ## Testing - Manually tested with temperature datasets - Verified output matches expected resultsClick “Create pull request” (don’t merge yet!)
Objective: Reformat the codebase using Black for consistent style.
Task Instructions
Step 1: Create your feature branch
git checkout main
git pull origin main
git checkout -b style/black-formattingStep 2: Apply Black formatting to your project
# Format the entire project
black .
# You should see output like:
# reformatted stats_lib.py
# reformatted stats_demo.py
# 2 files reformatted, X files left unchangedStep 3: Review the changes
git diffNotice how Black has: - Adjusted line spacing - Reformatted string quotes - Fixed indentation - Wrapped long lines
Step 4: Commit and push
git add .
git commit -m "Apply Black formatting to entire codebase"
git push origin style/black-formattingStep 5: Create Pull Request
Go to GitHub repository
Click “Compare & pull request”
Title:
"Apply Black code formatting"Description:
## Changes - Applied Black formatter to all Python files - Ensures PEP 8 compliance - No functional changes, style only ## Testing - Verified all existing tests still pass - Code functionality unchangedClick “Create pull request” (don’t merge yet!)
Part 3: The Merge Challenge
Everyone gathers together for this part.
Step 1: Observe the Pull Requests
Repository Owner should now have THREE open pull requests: - feature/na-handling (modifies existing functions + demo.py) - feature/add-stdev (adds new function + demo.py) - style/black-formatting (reformats all files)
All three modified stats_lib.py and demo.py!
Step 2: Merge the First PR
Repository Owner:
- Choose one PR (e.g.,
feature/na-handling) - Review the changes on GitHub
- Click “Merge pull request”
- Click “Confirm merge”
- Optionally delete the branch
✅ First PR merged successfully!
Step 3: Attempt to Merge the Second PR
Now try to merge the second PR (e.g., feature/add-stdev):
❌ GitHub shows: “This branch has conflicts that must be resolved”
Why conflicts exist:
stats_lib.py: If Group A modified existing functions and Group B added a new function, there might be conflicts in imports or at the end of the filestats_demo.py: Both groups added different test code to the same file