72 — Modules: a project across files
Guide 23 split a helper out of a program, and guide 67 used one storage module. A real project is bigger than one module: this guide builds a weather report from three modules — one that loads data, one that analyzes it, and one that prints. Each module does one job, and the main program imports only the names it needs.
Steps
The project needs data first. Create
weather.json— a list of one-day records, each with adayand atemp(temperature):
run it →[ {"day": "Mon", "temp": 21}, {"day": "Tue", "temp": 25}, {"day": "Wed", "temp": 18}, {"day": "Thu", "temp": 24}, {"day": "Fri", "temp": 27} ]fetch.nmeis the input module. Its only job is to read the file; theuse filemodule from guide 14 does the reading:
run it →# fetch.nme — loads the weather data. use file latest def load_weather(): return json_load("weather.json")The module's interface is one name:
load_weather.analyze.nmeis the brains.averageadds the temperatures and divides, like guide 54;hottestwalks the days with anif, remembering the warmest one:
run it →# analyze.nme — computes the average and the hottest day. def average(temps): total = 0 for t in temps: total = total + t return total / len(temps) def hottest(days): best = days[0] for day in days: if day["temp"] > best["temp"]: best = day return bestreport.nmeis the output module. It knows how to print a report and nothing else — no file reading, no math:
run it →# report.nme — prints the weather report. def print_report(days, avg, best): show f"Weather report: {len(days)} days" show f"Average: {avg:.1f}C" show f"Hottest: {best['day']} at {best['temp']}C"main.nmeties the project together. The import lines list each module's interface —from "fetch.nme" import load_weather, then the two analyze functions, then the report function. The main program decides the order: load, gather temperatures, compute, print. Save it next to the three modules:# main.nme — the weather report project. # Run: nme r main # weather.json must be in the same folder. from "fetch.nme" import load_weather from "analyze.nme" import average, hottest from "report.nme" import print_report days = load_weather() temps = [] for day in days: temps.append(day["temp"]) avg = average(temps) best = hottest(days) print_report(days, avg, best)daysis the loaded list,tempsthe column of temperatures,avgthe average, andbestthe warmest record — each value belongs to exactly one module, and main only moves values between the interfaces.Run the main program with the data file present:
nme r mainWeather report: 5 days Average: 23.0C Hottest: Fri at 27CThe average of 21, 25, 18, 24, 27 is 23.0, and Friday at 27 is the hottest day.
Only imported names cross the module boundary.
json_loadlives insidefetch.nmebecause of itsuse file latest, but main never sees it — main can use only the names in its import lists. A module's own helpers stay private the same way: add adef _celsius(f)helper toanalyze.nmeand it will stay inside. The import list is the interface, so changing a module's internals can never break the program that imports it.Korean writes the same project with
파일 사용 최신,json읽기,말해, and_komodule names such asfetch_ko.nme. The four Korean files are in the Korean guide.
Try it yourself
Add a coldest(days) function to analyze.nme, import it in main.nme, and
pass its result to print_report — the same walk as hottest but comparing
with <. Then give report.nme a header line that prints the city name.
What you learned
- A project splits into modules by job: input, logic, output.
- Each module exports a small interface — the names in the import list.
from "fetch.nme" import load_weatherbrings exactly one name across.- The main program moves values between module interfaces and nothing else.