Mini-Project: Building a Modular Application
10. Clean Software Architecture, Packaging & Mini-Projects
10.1Clean Layered Architecture
When software applications grow large, placing all code into a single giant file becomes unmaintainable. Professional developers organize code into three distinct layers (Layered Architecture):
- Domain/Model Layer: Contains core business data objects and business rules (e.g.
User,Product,Order). - Service/Logic Layer: Processes workflows and data operations (e.g.
OrderService,PaymentGateway). - Interface/Presentation Layer: Handles user interaction (e.g. Command Line Interface, Web API endpoints).
ANALOGY TIME
10.2Module Packaging & Import Resolution System
- Module: A single Python file (
utils.py). - Package: A folder containing Python files and a special file named
__init__.py. - Import Resolution (`sys.path`): When you write
import my_module, Python searches through folders listed in a system list calledsys.path. It checks the current directory first, followed by installed packages.
10.3Entry Point Guard (if __name__ == "__main__":)
Every Python file has a built-in variable named __name__:
- If you run the file directly (
python app.py), Python sets__name__ = "__main__". - If another file imports your file (
import app), Python sets__name__ = "app".
Using if __name__ == "__main__": at the bottom of your file ensures that setup script code runs ONLY when the file is executed directly as the main application, NOT when it is imported as a module elsewhere!
10.4CLI Argument Parsing (argparse module)
Command-line programs accept flags and arguments passed in the terminal (e.g. python script.py --verbose --count 5). Python's built-in argparse module provides a professional framework to parse these arguments and auto-generate help messages.
10.5Code Demo 1: CLI Argument Parsing Framework
Build a clean command-line interface tool:
10.6Code Demo 2: Modular Architecture Pattern
Demonstrating clean separation between domain logic and service handlers:
GOTCHA 1
Circular imports: Module A imports Module B, while Module B imports Module A. This raises an ImportError or AttributeError during initialization. Resolve by refactoring shared code into a third module.
GOTCHA 2
Running code with side-effects (like calling print() or modifying files) at top-level module scope without wrapping in if __name__ == '__main__':. These side-effects run unexpectedly upon import!
GOTCHA 3
Confusing relative imports (from .utils import helper) with absolute imports. Relative imports only work inside packages.
GOTCHA 4
Naming a local script json.py, random.py, or test.py, which breaks standard library imports.
WHERE THIS SHOWS UP IN REAL CODE
Professional software engineering is about organizing thousands of lines of code across multiple files, modules, and packages. Understanding imports, package entry points, and modular architecture is critical for real-world development.
TRY THIS YOURSELF
Modify Demo 2 to add a print statement showing the value of __name__ when executed.
Notice how separating model, service, and controller makes code clean and testable.
Project Plan (Click nodes to edit)
Demonstrate modular code architecture, module import resolution, sys.path, and main entry point guards.
Let's refactor calculations into a modular function pipeline!
Steps to Complete:
- Write
validate_input(val)returningTrueifval > 0. - Write
calculate_tax(val)returningval * 0.10. - Write
process_order(val)callingvalidate_inputand returningcalculate_tax. - Call
process_order(100)and printTax: 10.0.
Expected Output:
What is the value of the built-in `__name__` variable when a Python script is executed directly from the terminal (`python app.py`)?
What is the primary purpose of the `if __name__ == '__main__':` guard statement?
What is a circular import in Python?
What is recorded in `sys.path`?
What is the purpose of an `__init__.py` file inside a directory?
What is the difference between `import analytics` and `from analytics import calculate`?
Which layer in a modular application architecture should handle raw user CLI inputs and print formatting?
# comments · mini-project
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.