Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def deserialize(schema, binary):
bytes_writer = BytesIO()
bytes_writer.write(binary)
bytes_writer.seek(0)
res = fastavro.schemaless_reader(bytes_writer, schema)
return res
def test_boolean_roundtrip():
schema = {
"type": "record",
"name": "test_boolean_roundtrip",
"fields": [{
"name": "field",
"type": "boolean"
}]
}
record = {"field": True}
new_file = MemoryIO()
fastavro.schemaless_writer(new_file, schema, record)
new_file.seek(0)
new_record = fastavro.schemaless_reader(new_file, schema)
assert record == new_record
record = {"field": False}
new_file = MemoryIO()
fastavro.schemaless_writer(new_file, schema, record)
new_file.seek(0)
new_record = fastavro.schemaless_reader(new_file, schema)
assert record == new_record
def deserialize(schema, binary):
bytes_writer = BytesIO()
bytes_writer.write(binary)
bytes_writer.seek(0)
res = fastavro.schemaless_reader(bytes_writer, schema)
return res
"string"
],
}],
"name": "description",
"doc": "A description of the thing."
}
other_type_schema = CustomDict(schema)
record = {
'description': 'value',
}
new_file = MemoryIO()
fastavro.schemaless_writer(new_file, schema, record)
new_file.seek(0)
new_record = fastavro.schemaless_reader(new_file, other_type_schema)
assert record == new_record
'doc': 'test',
'type': 'int'
}, {
'name': 'good_compatible_field',
'doc': 'test',
'default': 1,
'type': 'int'
}],
'type': 'record'
}
record = {'good_field': 1}
new_file = MemoryIO()
fastavro.schemaless_writer(new_file, writer_schema, record)
new_file.seek(0)
new_record = fastavro.schemaless_reader(
new_file,
writer_schema,
reader_schema,
)
assert new_record == {'good_field': 1, 'good_compatible_field': 1}
def decode(self, encoded):
with BytesIO(encoded) as buf:
return AvroRecord(schemaless_reader(buf, self.parsed_schema))
def read_schemaless(iostream, schema, num_records, runs=1):
times = []
schema = parse_schema(schema)
for _ in range(runs):
for _ in range(num_records):
iostream.seek(0)
start = time.time()
record = schemaless_reader(iostream, schema)
end = time.time()
times.append(end - start)
print('... {0} runs averaged {1} seconds'.format(runs, (sum(times) / runs)))
return records
def decode(self, encoded):
with BytesIO(encoded) as buf:
return AvroRecord(schemaless_reader(buf, self.parsed_schema))
"""Read data and decode with a given Avro schema.
Parameters
----------
bytes_io : `_io.BytesIO`
Data to be decoded.
json_schema : `dict`
The reader Avro schema for decoding data.
Returns
-------
`dict`
Decoded data.
"""
bytes_io.seek(0)
message = fastavro.schemaless_reader(bytes_io, json_schema)
return message