Skip to content

Commit fe940fc

Browse files
author
David Cooper
committed
Made resolving dynamic fields a bit quicker.
1 parent cdb2c1c commit fe940fc

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

fitparse/base.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,23 +165,28 @@ def parse_data_record(self, header):
165165
if isinstance(field, r.DynamicField):
166166
dynamic_fields[i] = bound_field
167167

168-
# XXX -- This could probably be refactored heavily. It's slow and a bit unclear.
169-
# Go through already bound fields that are dynamic fields
170-
for dynamic_field_index, bound_field in dynamic_fields.iteritems():
171-
# Go by the reference field name and possible values
172-
for ref_field_name, possible_values in bound_field.field.possibilities.iteritems():
173-
# Go through the definitions fields looking for the reference field
174-
for field_index, (field, f_size) in enumerate(definition.fields):
175-
# Did we find the refence field in the definition?
176-
if field.name == ref_field_name:
177-
# Get the reference field's value
178-
ref_field_value = fields[field_index].data
179-
# Is the reference field's value a value for a new dynamic field type?
180-
new_field = possible_values.get(ref_field_value)
181-
if new_field:
182-
# Set it to the new type with old bound field's raw data
183-
fields[dynamic_field_index] = r.BoundField(bound_field.raw_data, new_field)
184-
168+
# Closure so we can break out of the nested loops quickly
169+
def resolve_dynamic_fields():
170+
# XXX -- This could probably be refactored heavily. It's slow and a bit unclear.
171+
# Go through already bound fields that are dynamic fields
172+
for dynamic_field_index, bound_field in dynamic_fields.iteritems():
173+
# Go by the reference field name and possible values
174+
for ref_field_name, possible_values in bound_field.field.possibilities.iteritems():
175+
# Go through the definitions fields looking for the reference field
176+
for field_index, (field, f_size) in enumerate(definition.fields):
177+
# Did we find the refence field in the definition?
178+
if field.name == ref_field_name:
179+
# Get the reference field's value
180+
ref_field_value = fields[field_index].data
181+
# Is the reference field's value a value for a new dynamic field type?
182+
new_field = possible_values.get(ref_field_value)
183+
if new_field:
184+
# Set it to the new type with old bound field's raw data
185+
fields[dynamic_field_index] = r.BoundField(bound_field.raw_data, new_field)
186+
return
187+
188+
if dynamic_fields:
189+
resolve_dynamic_fields()
185190

186191
if header.type == r.RECORD_HEADER_COMPRESSED_TS:
187192
ts_field = definition.type.fields.get(r.TIMESTAMP_FIELD_DEF_NUM)

0 commit comments

Comments
 (0)