← Back to Blog

Modules Mini Project — Write Once, Import Everywhere

What you're building

Three files. One module with shared functions. Two scripts that import from it independently.

The same function. Written once. Used everywhere.

What you need to know first

  • Creating your own module
  • import and from ... import
  • Functions — defining and returning
  • if __name__ == "__main__"

The structure

project/
    squad_utils.py    ← your module
    briefing.py       ← imports from squad_utils
    report.py         ← imports from squad_utils

All three files in the same folder.

The brief

squad_utils.py contains:

  • format_name(name) — capitalizes a name and strips whitespace
  • is_active(status) — returns True if status is "active"
  • squad_summary(members) — takes a list of dicts, returns total count and how many are active

briefing.py:

  • Has a hardcoded list of squad members
  • Uses format_name() to print a clean roster
  • Uses is_active() to mark each member's status

report.py:

  • Has the same list of squad members
  • Uses squad_summary() to print total and active count

Think before you code

  • What does squad_summary() need to return — one value or two?
  • How does briefing.py import only what it needs?
  • How does report.py import only what it needs?

Expected output — briefing.py

--- Squad Roster ---
Raven     active
Wolf      inactive
Ghost     active
Viper     active
Bull      inactive

Expected output — report.py

--- Squad Report ---
Total members: 5
Active:        3
Inactive:      2

The solution

squad_utils.py:

def format_name(name):
    return name.strip().capitalize()

def is_active(status):
    return status == "active"

def squad_summary(members):
    total  = len(members)
    active = sum(1 for m in members if m["status"] == "active")
    return total, active

if __name__ == "__main__":
    print(format_name("  raven  "))
    print(is_active("active"))
    print(squad_summary([{"name": "Raven", "status": "active"}]))

briefing.py:

from squad_utils import format_name, is_active

squad = [
    {"name": "raven",  "status": "active"},
    {"name": "wolf",   "status": "inactive"},
    {"name": "ghost",  "status": "active"},
    {"name": "viper",  "status": "active"},
    {"name": "bull",   "status": "inactive"}
]

print("--- Squad Roster ---")
for member in squad:
    name   = format_name(member["name"])
    status = "active" if is_active(member["status"]) else "inactive"
    print(f"{name:<10}{status}")

report.py:

from squad_utils import squad_summary

squad = [
    {"name": "Raven",  "status": "active"},
    {"name": "Wolf",   "status": "inactive"},
    {"name": "Ghost",  "status": "active"},
    {"name": "Viper",  "status": "active"},
    {"name": "Bull",   "status": "inactive"}
]

total, active = squad_summary(squad)

print("--- Squad Report ---")
print(f"Total members: {total}")
print(f"Active:        {active}")
print(f"Inactive:      {total - active}")
[ login to bookmark ] // copied! 18 views · 1 min
// resources
Exercise report.py Exercise briefing.py Exercise squad_utils.py
← prev What If I Could Make My Own Module? next → Modules — The Full Picture
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.