Skip to content

Codegen

pypic.codegen exports pypic's canonical tables — field aliases, the recipe registry, per-species templates, and field metadata — as JSON, so the sibling tools in the ecosystem can generate the same names and units without reimplementing them.

The tables are the authority; this module is a serializer over them. A quantity added with register_recipe shows up in the bundle automatically.

Each function is also a CLI subcommand — pypic export bundle, pypic export aliases, pypic export recipes, pypic export fields. See Command Line.

JSON export of pypic's canonical tables for cross-language codegen.

The schema is already exported via pypic.schema._export; this module adds the rest of the name/physics authority — compute aliases, the recipe registry, species templates, and field metadata — as a single JSON bundle. Cross-language consumers (webpic's Zod/TS codegen, rustpic tooling) read the bundle instead of re-typing the tables by hand.

Pure (stdlib only, no typer): the thin CLI lives in pypic._codegen_cli, mirroring the schema._export / _schema_cli split. JSON keys are camelCase for ergonomic consumption by the TypeScript side.

export_aliases()

Return the alias tables (compute aliases, group aliases, species regex).

Examples:

>>> sorted(export_aliases())
['computeAliases', 'groupAliases', 'speciesSuffixRe']
Source code in src/pypic/codegen.py
83
84
85
86
87
88
89
90
91
92
93
94
95
def export_aliases() -> dict[str, Any]:
    """Return the alias tables (compute aliases, group aliases, species regex).

    Examples
    --------
    >>> sorted(export_aliases())
    ['computeAliases', 'groupAliases', 'speciesSuffixRe']
    """
    return {
        "computeAliases": dict(COMPUTE_ALIASES),
        "groupAliases": dict(GROUP_ALIASES),
        "speciesSuffixRe": SPECIES_SUFFIX_RE.pattern,
    }

export_recipes()

Return the recipe registry and per-species templates (metadata only).

Examples:

The camelCase keys below are the wire contract webpic reads:

>>> recipes = export_recipes()["recipes"]
>>> recipes["v_A"]["fields"]
['|B|', 'rho_m']
>>> recipes["v_A"]["supportsRelativistic"], recipes["v_A"]["passesGeometry"]
(True, False)
Source code in src/pypic/codegen.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def export_recipes() -> dict[str, Any]:
    """Return the recipe registry and per-species templates (metadata only).

    Examples
    --------
    The camelCase keys below are the wire contract webpic reads:

    >>> recipes = export_recipes()["recipes"]
    >>> recipes["v_A"]["fields"]
    ['|B|', 'rho_m']
    >>> recipes["v_A"]["supportsRelativistic"], recipes["v_A"]["passesGeometry"]
    (True, False)
    """
    return {
        "recipes": {key: _recipe_dict(recipe) for key, recipe in RECIPES.items()},
        "speciesTemplates": {
            key: _template_dict(template) for key, template in SPECIES_TEMPLATES.items()
        },
    }

export_fields()

Return static field metadata: units, LaTeX symbols, long names.

Examples:

>>> b1 = export_fields()["fields"]["B_1"]
>>> b1["quantityType"], b1["siUnit"], b1["latex"]
('b_field', 'T', '$B_1$')
Source code in src/pypic/codegen.py
119
120
121
122
123
124
125
126
127
128
129
130
def export_fields() -> dict[str, Any]:
    """Return static field metadata: units, LaTeX symbols, long names.

    Examples
    --------
    >>> b1 = export_fields()["fields"]["B_1"]
    >>> b1["quantityType"], b1["siUnit"], b1["latex"]
    ('b_field', 'T', '$B_1$')
    """
    return {
        "fields": {name: _fieldinfo_dict(info) for name, info in _FIELD_INFO.items()}
    }

export_bundle(*, schema_version=SCHEMA_VERSION, inline_single_use_defs=False, include_x_extensions=False)

Return the unified bundle: schema + aliases + recipes + field metadata.

inline_single_use_defs / include_x_extensions pass through to pypic.schema._export.build_schema (the former yields flatter Zod).

Examples:

>>> bundle = export_bundle()
>>> bundle["schemaVersion"]
'2.0'
>>> sorted(bundle)
['computeAliases', 'fields', 'groupAliases', 'jsonSchema', 'recipes',
 'schemaVersion', 'speciesSuffixRe', 'speciesTemplates']
Source code in src/pypic/codegen.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
def export_bundle(
    *,
    schema_version: str = SCHEMA_VERSION,
    inline_single_use_defs: bool = False,
    include_x_extensions: bool = False,
) -> dict[str, Any]:
    """Return the unified bundle: schema + aliases + recipes + field metadata.

    ``inline_single_use_defs`` / ``include_x_extensions`` pass through to
    `pypic.schema._export.build_schema` (the former yields flatter Zod).

    Examples
    --------
    >>> bundle = export_bundle()
    >>> bundle["schemaVersion"]
    '2.0'
    >>> sorted(bundle)  # doctest: +NORMALIZE_WHITESPACE
    ['computeAliases', 'fields', 'groupAliases', 'jsonSchema', 'recipes',
     'schemaVersion', 'speciesSuffixRe', 'speciesTemplates']
    """
    return {
        "schemaVersion": schema_version,
        "jsonSchema": build_schema(
            include_x_extensions=include_x_extensions,
            inline_single_use_defs=inline_single_use_defs,
            schema_version=schema_version,
        ),
        **export_aliases(),
        **export_recipes(),
        **export_fields(),
    }