Skip to content

Commit

Permalink
Code cleanup and version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
tebeka committed Dec 27, 2015
1 parent 7abe68a commit d63f7cf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2015-12-27 version 0.9.7
* Python 2.6 support (@antonymayi in PR #44)
* Some code cleanup

2015-10-14 version 0.9.6
* Support streaming IO (@mjwillson in PR #40)

Expand Down
2 changes: 1 addition & 1 deletion fastavro/__init__.py
Expand Up @@ -42,7 +42,7 @@
writer(out, schema, records)
'''

__version__ = '0.9.6'
__version__ = '0.9.7'


try:
Expand Down
7 changes: 3 additions & 4 deletions fastavro/__main__.py
Expand Up @@ -19,7 +19,7 @@ def main(argv=None):
parser.add_argument('--codecs', help='print supported codecs',
action='store_true', default=False)
parser.add_argument('--version', action='version',
version='fastavro {0}'.format(avro.__version__))
version='fastavro %s' % avro.__version__)
parser.add_argument('-p', '--pretty', help='pretty print json',
action='store_true', default=False)
args = parser.parse_args(argv[1:])
Expand All @@ -37,13 +37,12 @@ def main(argv=None):
try:
fo = open(filename, 'rb')
except IOError as e:
raise SystemExit(
'error: cannot open {0} - {1}'.format(filename, e))
raise SystemExit('error: cannot open %s - %s' % (filename, e))

try:
reader = avro.reader(fo)
except ValueError as e:
raise SystemExit('error: {0}'.format(e))
raise SystemExit('error: %s' % e)

if args.schema:
json_dump(reader.schema, True)
Expand Down
40 changes: 23 additions & 17 deletions fastavro/reader.py
Expand Up @@ -82,7 +82,7 @@ def match_types(writer_type, reader_type):


def match_schemas(w_schema, r_schema):
error_msg = 'Schema mismatch: {0} is not {1}'.format(w_schema, r_schema)
error_msg = 'Schema mismatch: %s is not %s' % (w_schema, r_schema)
if isinstance(w_schema, list):
# If the writer is a union, checks will happen in read_union after the
# correct schema is known
Expand Down Expand Up @@ -198,8 +198,9 @@ def read_enum(fo, writer_schema, reader_schema=None):
index = read_long(fo)
symbol = writer_schema['symbols'][index]
if reader_schema and symbol not in reader_schema['symbols']:
raise SchemaResolutionError('{0} not found in reader symbol list {1}'
.format(symbol, reader_schema['symbols']))
symlist = reader_schema['symbols']
msg = '%s not found in reader symbol list %s' % (symbol, symlist)
raise SchemaResolutionError(msg)
return symbol


Expand All @@ -215,10 +216,12 @@ def read_array(fo, writer_schema, reader_schema=None):
count in this case is the absolute value of the count written.
'''
if reader_schema:
item_reader = lambda fo, w_schema, r_schema: read_data(
fo, w_schema['items'], r_schema['items'])
def item_reader(fo, w_schema, r_schema):
return read_data(fo, w_schema['items'], r_schema['items'])
else:
item_reader = lambda fo, w_schema, _: read_data(fo, w_schema['items'])
def item_reader(fo, w_schema, _):
return read_data(fo, w_schema['items'])

read_items = []

block_count = read_long(fo)
Expand Down Expand Up @@ -248,10 +251,12 @@ def read_map(fo, writer_schema, reader_schema=None):
count in this case is the absolute value of the count written.
'''
if reader_schema:
item_reader = lambda fo, w_schema, r_schema: read_data(
fo, w_schema['values'], r_schema['values'])
def item_reader(fo, w_schema, r_schema):
return read_data(fo, w_schema['values'], r_schema['values'])
else:
item_reader = lambda fo, w_schema, _: read_data(fo, w_schema['values'])
def item_reader(fo, w_schema, _):
return read_data(fo, w_schema['values'])

read_items = {}
block_count = read_long(fo)
while block_count != 0:
Expand Down Expand Up @@ -285,8 +290,9 @@ def read_union(fo, writer_schema, reader_schema=None):
for schema in reader_schema:
if match_types(writer_schema[index], schema):
return read_data(fo, writer_schema[index], schema)
raise SchemaResolutionError('Schema mismatch: {0} not found in {1}'
.format(writer_schema, reader_schema))
msg = 'schema mismatch: %s not found in %s' % \
(writer_schema, reader_schema)
raise SchemaResolutionError(msg)
else:
return read_data(fo, writer_schema[index])

Expand Down Expand Up @@ -315,8 +321,8 @@ def read_record(fo, writer_schema, reader_schema=None):
for field in writer_schema['fields']:
record[field['name']] = read_data(fo, field['type'])
else:
readers_field_dict = dict([(f['name'], f) for f in
reader_schema['fields']])
readers_field_dict = \
dict((f['name'], f) for f in reader_schema['fields'])
for field in writer_schema['fields']:
readers_field = readers_field_dict.get(field['name'])
if readers_field:
Expand All @@ -336,8 +342,8 @@ def read_record(fo, writer_schema, reader_schema=None):
if default:
record[field['name']] = default
else:
raise SchemaResolutionError('No default value for {0}'
.format(field['name']))
msg = 'No default value for %s' % field['name']
raise SchemaResolutionError(msg)

return record

Expand Down Expand Up @@ -447,7 +453,7 @@ def _iter_avro(fo, header, codec, writer_schema, reader_schema):

read_block = BLOCK_READERS.get(codec)
if not read_block:
raise ValueError('Unrecognized codec: {0!r}'.format(codec))
raise ValueError('Unrecognized codec: %r' % codec)

block_count = 0
while True:
Expand Down Expand Up @@ -480,7 +486,7 @@ def __init__(self, fo, reader_schema=None):

# `meta` values are bytes. So, the actual decoding has to be external.
self.metadata = \
dict([(k, btou(v)) for k, v in iteritems(self._header['meta'])])
dict((k, btou(v)) for k, v in iteritems(self._header['meta']))

self.schema = self.writer_schema = \
json.loads(self.metadata['avro.schema'])
Expand Down
32 changes: 16 additions & 16 deletions fastavro/writer.py
Expand Up @@ -162,14 +162,14 @@ def validate(datum, schema):

if record_type == 'int':
return (
isinstance(datum, (int, long,))
and INT_MIN_VALUE <= datum <= INT_MAX_VALUE
isinstance(datum, (int, long,)) and
INT_MIN_VALUE <= datum <= INT_MAX_VALUE
)

if record_type == 'long':
return (
isinstance(datum, (int, long,))
and LONG_MIN_VALUE <= datum <= LONG_MAX_VALUE
isinstance(datum, (int, long,)) and
LONG_MIN_VALUE <= datum <= LONG_MAX_VALUE
)

if record_type in ['float', 'double']:
Expand All @@ -187,21 +187,21 @@ def validate(datum, schema):

if record_type == 'array':
return (
isinstance(datum, Iterable)
and all(validate(d, schema['items']) for d in datum)
isinstance(datum, Iterable) and
all(validate(d, schema['items']) for d in datum)
)

if record_type == 'map':
return (
isinstance(datum, Mapping)
and all(is_str(k) for k in datum.keys())
and all(validate(v, schema['values']) for v in datum.values())
isinstance(datum, Mapping) and
all(is_str(k) for k in datum.keys()) and
all(validate(v, schema['values']) for v in datum.values())
)

if record_type in ('record', 'error', 'request',):
return (
isinstance(datum, Mapping)
and all(
isinstance(datum, Mapping) and
all(
validate(datum.get(f['name']), f['type'])
for f in schema['fields']
)
Expand All @@ -210,7 +210,7 @@ def validate(datum, schema):
if record_type in SCHEMA_DEFS:
return validate(datum, SCHEMA_DEFS[record_type])

raise ValueError("I don't know what a {0} is.".format(record_type))
raise ValueError('unkown record type - %s' % record_type)


def write_union(fo, datum, schema):
Expand All @@ -223,8 +223,8 @@ def write_union(fo, datum, schema):
if validate(datum, candidate):
break
else:
raise ValueError('{0!r} (type {1}) do not match {2}'.format(
datum, pytype, schema))
msg = '%r (type %s) do not match %s' % (datum, pytype, schema)
raise ValueError(msg)

# write data
write_long(fo, index)
Expand Down Expand Up @@ -272,7 +272,7 @@ def write_record(fo, datum, schema):
'string',
]

SCHEMA_DEFS = dict([(typ, typ) for typ in _base_types])
SCHEMA_DEFS = dict((typ, typ) for typ in _base_types)


def write_data(fo, datum, schema):
Expand Down Expand Up @@ -356,7 +356,7 @@ def writer(fo,
try:
block_writer = BLOCK_WRITERS[codec]
except KeyError:
raise ValueError('Unrecognized codec: {0!r}'.format(codec))
raise ValueError('unrecognized codec: %r' % codec)

def dump():
write_long(fo, block_count)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_str_py3.py
Expand Up @@ -17,7 +17,7 @@ def gen_id():

keys = ['first', 'second', 'third', 'fourth']

testdata = [dict([(key, gen_id()) for key in keys]) for _ in range(50)]
testdata = [dict((key, gen_id()) for key in keys) for _ in range(50)]

schema = {
"fields": [{'name': key, 'type': 'string'} for key in keys],
Expand Down

0 comments on commit d63f7cf

Please sign in to comment.