03 / MODEL SYSTEMS AND CHANGES · LESSON 09

Read a clearly simulated fleet

Collect typed OS and hardware observations without confusing the browser with a real endpoint.

15 min · learn at your pace

Make the adapter boundary visible

Python's platform and os modules describe the environment where Python runs. In a browser that is a WebAssembly runtime, not the learner's Windows laptop or a remote server. This course supplies LabFleet from lab_fleet.py to make that distinction explicit. Its operating systems, processors and memory values come from fictional JSON records.

Call inspect with a host identity and transform the returned data into a compact inventory. Names are normalized, deduplicated and sorted before collection. Keep unavailable hosts in the report rather than omitting them. A connection failure, an access denial and invalid numeric data all limit assessment coverage.

Preserve useful units and types

Report memory in GiB by dividing bytes by 2**30, not by rounding a display string and parsing it again. A free-memory percentage needs positive total memory and a free value between zero and the total. Logical processors must be positive integers. Boolean values must not become a processor count by accident.

Successful records contain numeric values. Unavailable records use None for quantities that were not observed; zero would falsely imply an actual measurement. Diagnostic output stores the exception class, which is sufficient for this exercise without copying potentially sensitive exception text into a report.

LabFleet reads virtual files and never calls the host operating system. Normal Run can reuse saved simulation state, while Check starts with the original fixtures. These explicit boundaries let learners practise collection and failure policy before choosing a real platform-specific adapter later.

Try an example.py
from lab_fleet import LabFleet
row = LabFleet().inspect("node-a")
print(row["os"], row["logical_cpus"], row["memory_total_bytes"] / 2**30)
YOUR TURN

Put it into practice.

Define fleet_inventory(fleet, hosts). hosts are nonempty string names; strip/casefold, deduplicate and sort. Call fleet.inspect(host). Require nonempty string os, positive integer logical_cpus and memory_total_bytes, and integer memory_free_bytes between 0 and total; exclude bool. Return host, status="ready", os, logical_cpus, total_gib (bytes/2**30), free_percent, error="". Any ordinary Exception creates status="unavailable", empty os, None numeric fields and exception class name in error. Continue collecting; propagate BaseException cancellation. Do not change the adapter data.

See an explained solution
One way to solve it.py
def fleet_inventory(fleet, hosts):
    rows=[]
    for host in sorted({value.strip().casefold() for value in hosts}):
        try:
            data=fleet.inspect(host)
            total=data["memory_total_bytes"];free=data["memory_free_bytes"];cpus=data["logical_cpus"]
            if type(total) is not int or total<=0 or type(free) is not int or not 0<=free<=total or type(cpus) is not int or cpus<=0 or not isinstance(data["os"],str) or not data["os"]:
                raise ValueError("Invalid hardware observation")
            rows.append({"host":host,"status":"ready","os":data["os"],"logical_cpus":cpus,"total_gib":total/2**30,"free_percent":100*free/total,"error":""})
        except Exception as error:
            rows.append({"host":host,"status":"unavailable","os":"","logical_cpus":None,"total_gib":None,"free_percent":None,"error":type(error).__name__})
    return rows

if __name__ == "__main__":
    from lab_fleet import LabFleet
    print(fleet_inventory(LabFleet(), ["node-a", "node-c"]))

The inventory reports the fictional adapter’s observations and preserves missing coverage. It never presents browser runtime details as measurements of another machine.