Skip to main content

A typed class generator for Avro Schemata

Project description

pyavro-gen

Standard Avro implementation for Python is typeless and operates on dicts. While this is convenient for small projects, larger ones, often with hundreds of schemas, certainly benefit from the ability to enforce schemas during record construction.

This library allows Python Avro users to employ Specific Records.

Usage:

pip install pyavro-gen
pyavrogen.py -v \
    -i my_schemas_dir \
    -o jaumoavro \
    -ie 'avsc' \
    -b com.jaumo.schema \
    -r com.jaumo.schema.rpc \
    -t com.jaumo.schema.type

Now you can import your classes like

from jaumoavro.com.jaumo.schema.domain.user import Updated

u = Updated(...)

Generation and test programmatically

from avro_preprocessor.avro_paths import AvroPaths
from pyavro_gen.generator import AvroGenerator

generator = AvroGenerator(
    AvroPaths(
        input_path='myschemas/',
        output_path='avroclasses/',
        input_schema_file_extension='avsc',
        base_namespace='com.jaumo.schema',
        rpc_namespace='com.jaumo.schema.rpc',
        types_namespace='com.jaumo.schema.type',
    )
)
generator.process()

Generation using custom classes:

from pyavro_gen.generation_classes import GENERATION_CLASSES, GenerationClassesType
from pyavro_gen.codewriters.base import Decorator, ClassWriter, Extension
from typing import Optional

# First define two custom ClassWriters

class RpcWriter(ClassWriter):
    def __init__(self,
                 fully_qualified_name: str,
                 doc: Optional[str] = None,
                 prefix: Optional[str] = None):
        super().__init__(fully_qualified_name, doc, prefix)

        self.extensions = [
            Extension('abc.ABC')
        ]


class UndictifiableClassWriter(ClassWriter):
    def __init__(self,
                 fully_qualified_name: str,
                 doc: Optional[str] = None,
                 prefix: Optional[str] = None):
        super().__init__(fully_qualified_name, doc, prefix)

        self.decorators = [
            Decorator('@type_checked_constructor()',
                      ClassWriter('undictify.type_checked_constructor')),
            Decorator('@dataclass', ClassWriter('dataclasses.dataclass'))
        ]


# Then, register them in the GENERATION_CLASSES variable

GENERATION_CLASSES[GenerationClassesType.RECORD_CLASS] = UndictifiableClassWriter
GENERATION_CLASSES[GenerationClassesType.RPC_CLASS] = RpcWriter

# Then, generate classes in module `avroclasses` as shown above.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyavro-gen-0.3.4.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyavro_gen-0.3.4-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

File details

Details for the file pyavro-gen-0.3.4.tar.gz.

File metadata

  • Download URL: pyavro-gen-0.3.4.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pyavro-gen-0.3.4.tar.gz
Algorithm Hash digest
SHA256 69cf492ec98d35b2088f9fe1f218bb40b3327b1c5cae7a65196c082052ee3ba3
MD5 e0181b418681a347074f49c88cc7aeeb
BLAKE2b-256 678717163c7d89b1879382406c78c2bb02f4fcff0c08caf9c097da122f80510f

See more details on using hashes here.

File details

Details for the file pyavro_gen-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: pyavro_gen-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 24.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for pyavro_gen-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4ac2af8787496439a9432477469123aa626d70c6886e30a8f1c1900895d8a6e6
MD5 9b883e40541f0508e93ad0e452540613
BLAKE2b-256 4b6e45febddc21d21974cfb766e163621a2037ce48819bde5bd5286b875a585b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page