本文整理汇总了Python中fastavro.six.MemoryIO类的典型用法代码示例。如果您正苦于以下问题:Python MemoryIO类的具体用法?Python MemoryIO怎么用?Python MemoryIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MemoryIO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check
def check(filename):
with open(filename, 'rb') as fo:
reader = fastavro.reader(fo)
assert hasattr(reader, 'schema'), 'no schema on file'
if basename(filename) in NO_DATA:
return
records = list(reader)
assert len(records) > 0, 'no records found'
new_file = MemoryIO()
fastavro.writer(new_file, reader.schema, records, reader.codec)
new_file_bytes = new_file.getvalue()
new_file = NoSeekMemoryIO(new_file_bytes)
new_reader = fastavro.reader(new_file)
assert hasattr(new_reader, 'schema'), "schema wasn't written"
assert new_reader.schema == reader.schema
assert new_reader.codec == reader.codec
new_records = list(new_reader)
assert new_records == records
# Test schema migration with the same schema
new_file = NoSeekMemoryIO(new_file_bytes)
schema_migration_reader = fastavro.reader(new_file, reader.schema)
assert schema_migration_reader.reader_schema == reader.schema
new_records = list(schema_migration_reader)
assert new_records == records
示例2: roundtrip
def roundtrip(record, writer_schema, reader_schema):
new_file = MemoryIO()
fastavro.writer(new_file, writer_schema, [record])
new_file.seek(0)
new_records = list(fastavro.reader(new_file, reader_schema))
return new_records[0]
示例3: test_schema_migration_array_with_union_promotion
def test_schema_migration_array_with_union_promotion():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "array",
"items": ["boolean", "long"]
},
}]
}
new_schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "array",
"items": ["string", "float"]
},
}]
}
new_file = MemoryIO()
records = [{"test": [1, 2, 3]}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
new_records = list(new_reader)
assert new_records == records
示例4: test_schema_migration_maps_with_union_promotion
def test_schema_migration_maps_with_union_promotion():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "map",
"values": ["string", "int"]
},
}]
}
new_schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "map",
"values": ["string", "long"]
},
}]
}
new_file = MemoryIO()
records = [{"test": {"foo": 1}}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
new_records = list(new_reader)
assert new_records == records
示例5: test_schema_migration_maps_failure
def test_schema_migration_maps_failure():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "map",
"values": "string"
},
}]
}
new_schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "map",
"values": "long"
},
}]
}
new_file = MemoryIO()
records = [{"test": {"foo": "a"}}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
list(new_reader)
示例6: test_schema_migration_array_failure
def test_schema_migration_array_failure():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "array",
"items": ["string", "int"]
},
}]
}
new_schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "array",
"items": ["string", "boolean"]
},
}]
}
new_file = MemoryIO()
records = [{"test": [1, 2, 3]}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
try:
list(new_reader)
except fastavro._reader.SchemaResolutionError:
pass
else:
assert False
示例7: test_schema_migration_array_failure
def test_schema_migration_array_failure():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "array",
"items": ["string", "int"]
},
}]
}
new_schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "array",
"items": ["string", "boolean"]
},
}]
}
new_file = MemoryIO()
records = [{"test": [1, 2, 3]}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
list(new_reader)
示例8: test_schema_migration_maps_failure
def test_schema_migration_maps_failure():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "map",
"values": "string"
},
}]
}
new_schema = {
"type": "record",
"fields": [{
"name": "test",
"type": {
"type": "map",
"values": "long"
},
}]
}
new_file = MemoryIO()
records = [{"test": {"foo": "a"}}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
try:
list(new_reader)
except fastavro._reader.SchemaResolutionError:
pass
else:
assert False
示例9: test_schema_migration_schema_mismatch
def test_schema_migration_schema_mismatch():
schema = {
"type": "record",
"fields": [{
"name": "test",
"type": "string",
}]
}
new_schema = {
"type": "enum",
"name": "test",
"symbols": ["FOO", "BAR"],
}
new_file = MemoryIO()
records = [{"test": "test"}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
try:
list(new_reader)
except fastavro._reader.SchemaResolutionError:
pass
else:
assert False
示例10: test_fastavro_errors_read_enum
def test_fastavro_errors_read_enum():
fo = MemoryIO()
writer_schema = {
"type": "enum",
"name": "Suit",
"symbols": [
"SPADES",
"HEARTS",
"DIAMONDS",
"CLUBS",
]
}
reader_schema = {
"type": "enum",
"name": "Suit",
"symbols": [
"SPADES",
"HEARTS",
"DIAMONDS",
]
}
given = "CLUBS"
write_data(fo, given, writer_schema)
fo.seek(0)
try:
read_data(fo, reader_schema)
assert False, 'bad schema did not raise!'
except AvroValueError as e:
assert '<enum>' in str(e)
示例11: roundtrip
def roundtrip(schema, records, new_schema):
new_file = MemoryIO()
fastavro.writer(new_file, schema, records)
new_file.seek(0)
reader = fastavro.reader(new_file, new_schema)
new_records = list(reader)
return new_records
示例12: test_default_values
def test_default_values():
schema = {"type": "record", "fields": [{"name": "default_field", "type": "string", "default": "default_value"}]}
new_file = MemoryIO()
records = [{}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file)
new_records = list(new_reader)
assert new_records == [{"default_field": "default_value"}]
示例13: test_metadata
def test_metadata():
schema = {"type": "record", "fields": []}
new_file = MemoryIO()
records = [{}]
metadata = {"key": "value"}
fastavro.writer(new_file, schema, records, metadata=metadata)
new_file.seek(0)
new_reader = fastavro.reader(new_file)
assert new_reader.metadata["key"] == metadata["key"]
示例14: test_schema_migration_add_default_field
def test_schema_migration_add_default_field():
schema = {"type": "record", "fields": []}
new_schema = {"type": "record", "fields": [{"name": "test", "type": "string", "default": "default"}]}
new_file = MemoryIO()
records = [{}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
new_records = list(new_reader)
assert new_records == [{"test": "default"}]
示例15: test_schema_migration_reader_union
def test_schema_migration_reader_union():
schema = {"type": "record", "fields": [{"name": "test", "type": "int"}]}
new_schema = {"type": "record", "fields": [{"name": "test", "type": ["string", "int"]}]}
new_file = MemoryIO()
records = [{"test": 1}]
fastavro.writer(new_file, schema, records)
new_file.seek(0)
new_reader = fastavro.reader(new_file, new_schema)
new_records = list(new_reader)
assert new_records == records