How to use the fastavro.six.iteritems function in fastavro

To help you get started, we’ve selected a few fastavro examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github fastavro / fastavro / fastavro / json_writer.py View on Github external
else:
            best_records_type = extract_record_type(best_match_schema)
            if best_records_type in ('record', 'enum', 'fixed'):
                key = best_match_schema['name']
            else:
                key = best_match_schema
            return {key: _write_json(datum, best_match_schema)}

    elif record_type == 'array':
        dtype = schema['items']
        return [_write_json(item, dtype) for item in datum]

    elif record_type == 'map':
        vtype = schema['values']
        result = {}
        for key, value in iteritems(datum):
            result[key] = _write_json(value, vtype)
        return result

    elif record_type in ('record', 'error', 'request',):
        result = {}
        for field in schema['fields']:
            result[field['name']] = _write_json(
                datum.get(field['name'], field.get('default')),
                field['type'],
            )
        return result

    else:
        raise ValueError('unknown record type - %s' % record_type)
github fastavro / fastavro / fastavro / _schema_py.py View on Github external
def parse_field(field, namespace, named_schemas):
    parsed_field = {
        key: value
        for key, value in iteritems(field)
        if key not in RESERVED_FIELD_PROPERTIES
    }

    for prop in OPTIONAL_FIELD_PROPERTIES:
        if prop in field:
            parsed_field[prop] = field[prop]

    # Aliases must be a list
    aliases = parsed_field.get("aliases", [])
    if not isinstance(aliases, list):
        msg = "aliases must be a list, not {}".format(aliases)
        raise SchemaParseException(msg)

    parsed_field["name"] = field["name"]
    parsed_field["type"] = _parse_schema(
        field["type"], namespace, False, named_schemas
github fastavro / fastavro / fastavro / json_reader.py View on Github external
):
            return _read_json(value, dtype)

        for single_schema in schema:
            if (extract_record_type(single_schema) in ('record, enum, fixed')
                    and single_schema.get('name') == dtype):
                return _read_json(value, single_schema)

    elif record_type == 'array':
        dtype = schema['items']
        return [_read_json(item, dtype) for item in datum]

    elif record_type == 'map':
        vtype = schema['values']
        result = {}
        for key, value in iteritems(datum):
            result[key] = _read_json(value, vtype)
        return result

    elif record_type in ('record', 'error', 'request',):
        result = {}
        for field in schema['fields']:
            result[field['name']] = _read_json(
                datum.get(field['name'], field.get('default')),
                field['type'],
            )
        return result

    else:
        raise ValueError('unknown record type - %s' % record_type)
github fastavro / fastavro / fastavro / _write_py.py View on Github external
def write_map(encoder, datum, schema):
    """Maps are encoded as a series of blocks.

    Each block consists of a long count value, followed by that many key/value
    pairs.  A block with count zero indicates the end of the map.  Each item is
    encoded per the map's value schema.

    If a block's count is negative, then the count is followed immediately by a
    long block size, indicating the number of bytes in the block. The actual
    count in this case is the absolute value of the count written."""
    encoder.write_map_start()
    if len(datum) > 0:
        encoder.write_item_count(len(datum))
        vtype = schema['values']
        for key, val in iteritems(datum):
            encoder.write_utf8(key)
            write_data(encoder, val, vtype)
    encoder.write_map_end()
github fastavro / fastavro / fastavro / __main__.py View on Github external
def _clean_json_record(data):
    if isinstance(data, dict):
        for k, v in iteritems(data):
            _clean_json_value(data, k, v)
    elif isinstance(data, list):
        for i, v in enumerate(data):
            _clean_json_value(data, i, v)
github fastavro / fastavro / fastavro / _write_py.py View on Github external
def __init__(self,
                 schema,
                 metadata=None,
                 validator=None):
        self.schema = parse_schema(schema)
        self.validate_fn = validate if validator is True else validator
        self.metadata = metadata or {}

        if isinstance(schema, dict):
            schema = {
                key: value
                for key, value in iteritems(schema)
                if key != "__fastavro_parsed"
            }

        self.metadata['avro.schema'] = json.dumps(schema)
github fastavro / fastavro / fastavro / _schema_py.py View on Github external
if '.' not in schema and namespace:
            schema = namespace + '.' + schema

        if schema not in SCHEMA_DEFS:
            raise UnknownType(schema)
        else:
            return schema

    else:
        # Remaining valid schemas must be dict types
        schema_type = schema["type"]

        parsed_schema = {
            key: value
            for key, value in iteritems(schema)
            if key not in RESERVED_PROPERTIES
        }
        parsed_schema["type"] = schema_type

        # Correctness checks for logical types
        logical_type = parsed_schema.get("logicalType")
        if logical_type == "decimal":
            scale = parsed_schema.get("scale")
            if scale and not isinstance(scale, int):
                raise SchemaParseException(
                    "decimal scale must be a postive integer, "
                    + "not {}".format(scale)
                )
            precision = parsed_schema.get("precision")
            if precision and not isinstance(precision, int):
                raise SchemaParseException(
github fastavro / fastavro / fastavro / _write_py.py View on Github external
def write_header(encoder, metadata, sync_marker):
    header = {
        'magic': MAGIC,
        'meta': {key: utob(value) for key, value in iteritems(metadata)},
        'sync': sync_marker
    }
    write_data(encoder, header, HEADER_SCHEMA)