Choosing a Module by Name

Dev’s digest can now load the peak chart only when it’s requested. The controller parsers have another reason to load at runtime: each plant’s entry in plants.json names the make to use. The main boiler and sports hall need Halden’s parser, while the science block needs Ostra’s. You’ll let that name select the module, replacing the digest’s hand-maintained parser table.

Build the Module Name

The Python package already keeps one parser module per controller make. In boiler/controllers/__init__.py, the loader builds a dotted module name:

Language: Python
import importlib

def load_controller(make):
    return importlib.import_module(f"{__name__}.{make}")

Here __name__ is "boiler.controllers", so "halden" selects boiler.controllers.halden. The function returns that module, leaving the caller to use its parser.

JavaScript’s import() also accepts a string built at runtime. Save this complete loader as boiler/controllers/index.js:

Language: JavaScript Filename: boiler/controllers/index.js
export async function loadController(make) {
  return import(`./${make}.js`);
}

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.