runtime
219 / 500
question 219 Modules & Packages · hard

Run every exporter that can load

Export formats are plug-ins: every .py module in an exporters package (except __init__.py) has an export(rows) function that returns text. New plug-ins are added by dropping in a file, so the runner must discover them, not list them by hand. Some plug-ins need a library that isn't installed; the runner must skip those and say which library is missing, while every other plug-in still runs.

Functionrun_exporters(package, rows)
Discoverthe *.py files in the folder package (other files ignored), excluding __init__.py, in sorted order; each is imported as <package>.<file stem> with importlib.import_module
Returns(outputs, skipped): outputs maps each loaded plug-in's stem to export(rows); skipped maps each plug-in that raised ModuleNotFoundError to the missing module's name (err.name)
Herethe exporters package has csv_out, json_out and xml_out (which imports lxml, not installed); a reporting package also exists
Examples
callrun_exporters('exporters', [{'sku': 'HOM-52782', 'qty': 4}])returns({'csv_out': 'sku,qty\nHOM-52782,4\n', 'json_out': '[{"sku": "HOM-52782", "qty": 4}]'}, {'xml_out': 'lxml'})
callrun_exporters('exporters', [])returns({'csv_out': 'sku,qty\n', 'json_out': '[]'}, {'xml_out': 'lxml'})

Loading your progress…

Run every exporter that can load — Python practice · Runtime