parametric_component¶
- photonforge.parametric_component(decorated_function=None, name_prefix=None, gdsii_safe_name=True, use_parametric_cache_default=True)¶
Decorator to create parametric components from functions.
If the name of the created component is empty, this decorator sets it with name prefix and the values of the function arguments when called.
Components can be cached to avoid duplication. They are cached based on the calling arguments (specifically, argument
id
). Regardless of the default setting, each component can use or skip caching by setting thebool
keyword argumentuse_parametric_cache
in the decorated function call.- Parameters:
decorated_function (Callable | None) – Function that returns a Component.
name_prefix (str | None) – Prefix for the component name. If
None
, the decorated function name is used.gdsii_safe_name (bool) – If set, only use GDSII-safe characters in the name (
name_prefix
is not modified by this flag).use_parametric_cache_default (bool) – Controls the default caching behavior for the decorated function.
- Return type:
Callable
Examples
>>> @parametric_component ... def straight(*, length, port_spec_name, technology): ... port_spec = technology.ports[port_spec_name] ... c = Component(technology=technology) ... for layer, path in port_spec.get_paths((0, 0)): ... c.add(layer, path.segment((length, 0))) ... c.add_port(Port(center=(0, 0), input_direction=0, spec=port_spec)) ... c.add_port(Port(center=(length, 0), input_direction=180, spec=port_spec)) ... c.add_model(Tidy3DModel(port_symmetries=[("0", "1", {"1": "0"})])) ... return c ... >>> technology = basic_technology() >>> component = straight(length=5, port_spec_name="Strip", technology=technology) >>> print(component.name) straight_10_Strip_Basic_Technology_1.0
Caching behavior:
>>> component1 = straight(length=2, port_spec_name="Strip", technology=technology) >>> component2 = straight(length=2, port_spec_name="Strip", technology=technology) >>> component3 = straight( ... length=2, port_spec_name="Strip", technology=technology, use_parametric_cache=False ... ) >>> component2 == component1 True >>> component2 is component1 True >>> component3 == component1 True >>> component3 is component1 False
Note
It is generally a good idea to force parametric components to accept only keyword arguments (by using the
*
as first argument in the argument list), because those are stored for future updates of the created component withComponent.update()
.See also