Last active
September 12, 2023 20:20
-
-
Save edgarrmondragon/9b6d962e232a37a883e577150a723c09 to your computer and use it in GitHub Desktop.
generate_singer_messages.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
https://mimesis.name/en/master/schema.html | |
""" | |
import functools | |
import json | |
import sys | |
import typing as t | |
from mimesis import Field, Schema | |
from mimesis.locales import Locale | |
from singer_sdk import typing as th | |
class SingerField(th.Property): | |
def __init__(self, *args, generator: t.Callable, **kwargs): | |
super().__init__(*args, **kwargs) | |
self.generator = generator | |
class SingerSchema(th.PropertiesList): | |
def __init__(self, *fields: SingerField, examples: int = 10, **kwargs): | |
super().__init__(*fields, **kwargs) | |
self._schema = self._get_schema(examples=examples) | |
def _get_schema(self, *, examples: int): | |
return Schema( | |
schema=lambda: { | |
name: field.generator() for name, field in self.wrapped.items() | |
}, | |
iterations=examples, | |
) | |
def create(self): | |
return self._schema.create() | |
field = Field(locale=Locale.EN) | |
schema = SingerSchema( | |
SingerField("id", th.StringType, generator=functools.partial(field, "increment")), | |
SingerField( | |
"name", | |
th.StringType, | |
generator=functools.partial(field, "person.name"), | |
), | |
) | |
sys.stdout.write( | |
json.dumps({"type": "SCHEMA", "stream": "users", "schema": schema.to_dict()}) | |
+ "\n", | |
) | |
for result in schema.create(): | |
sys.stdout.write( | |
json.dumps({"type": "RECORD", "stream": "example", "record": result}) + "\n", | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mimesis | |
singer-sdk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: