Skip to content

Custom Generators and Converters

Use a custom component only when the generated catalogs do not provide the required behavior. Keep the .scr.py file inside the project, load it explicitly, and make inputs visible in the descriptor.

Component script

When DATAMIMIC loads a project .scr.py file, the base classes Generator, Converter, and CustomConverter are available automatically. Do not import internal DATAMIMIC implementation modules to define a project component.

script/custom.scr.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
class CustomStringGenerator(Generator):
    def __init__(self, prefix: str = "") -> None:
        self.prefix = prefix
        self.counter = 0

    def generate(self) -> str:
        self.counter += 1
        return f"{self.prefix}{self.counter}"


class CustomUpperConverter(Converter):
    def convert(self, value: str) -> str:
        return value.upper()

Load and use the component

datamimic.xml
1
2
3
4
5
6
<setup numProcess="1">
    <execute uri="script/custom.scr.py"/>
    <generate name="custom_output" count="10" target="LogExporter">
        <key name="code" generator="CustomStringGenerator(prefix='item-')" converter="CustomUpperConverter"/>
    </generate>
</setup>

This example uses numProcess="1" because its generator owns a counter. A component intended for parallel generation should not rely on mutable process-local state.

Custom scripts execute code and therefore belong to the project's trusted code base. Review them like application code and avoid hidden network or filesystem side effects. For read-only runtime values or memstore reads, use the public Scripting API instead of importing engine internals.