Fragment Reuse Lab¶
This lab expands on the fragment rebuild script in examples to teach how to
package reusable fragments, distribute them to other teams, and reuse them in
new DDI instances.
Note
Install the project in editable mode (pip install -e .) so the
examples can locate ddi_l modules.
Locate packaged examples
The fragment scripts load XML assets bundled in the wheel under
ddi_l.examples instead of reading from a cloned repository. Resolve
the packaged files with importlib.resources and pass the resolved path
into your loaders (for example DDIDocument.from_xml):
1. Rebuild the example fragment bundle¶
- Execute
python -m ddi_l.examples.build_and_validate --refresh --refresh-fragmentto regenerate the sample bundle. The script rewritesexamples/example_instance.xmland the default fragment atexamples/example_fragment.xmlwhile printing confirmation that both payloads validate successfully. - Inspect the refreshed instance and fragment XML outputs to see how the maintainables are packaged and how the validation messages describe the rebuild.
- Re-run the command with
--fragment-outputif you want the fragment to be written to another location (for example--fragment-output fragments/catalog/example_fragment.xml) so teams know where to pick up the reusable artefact.
2. Publish fragments for team consumption¶
Compliance review before distribution
De-identify fragment payloads, strip production URNs, and align with your organisation's data-handling rules before sharing archives with other teams. Follow the redaction guidance in the CLI automation playbook to keep tutorials consistent.
- Copy the generated fragments into a shared location (for example
fragments/catalog). - Create a README that documents the fragment purpose, maintainable identifiers, and versioning scheme.
- Optionally zip the fragments and validation report together so downstream consumers have a single artefact to download.
3. Consume fragments in a new project¶
-
Start a new Python script and load both the target document and fragment:
-
Iterate over :meth:
ddi_l.document.DDIFragment.iter_fragment_payloadsand attach each maintainable to the document explicitly: -
Serialise the enriched instance and run
ddi validateto confirm the fragments integrate cleanly.
4. Maintain fragment provenance¶
- Track fragment versions in your source control system and tag releases so consuming teams can pin to specific builds.
- Include a changelog with each fragment release that notes schema versions and lint expectations.
- Automate fragment rebuilds in CI to ensure new changes remain compatible with the canonical instance.