Skip to content

Commit

Permalink
FitFile._read_struct does not allow endian in fmt
Browse files Browse the repository at this point in the history
Like before 5d8bb3a
And microoptimization for string concat
  • Loading branch information
xmedeko committed Apr 12, 2018
1 parent 5d8bb3a commit bef7623
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 12 deletions.
11 changes: 2 additions & 9 deletions fitparse/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ def _read(self, size):
return data

def _read_struct(self, fmt, endian='<', data=None, always_tuple=False):
if fmt.startswith('<') or fmt.startswith('>'):
# fmt contains endian
fmt_with_endian = fmt
else:
fmt_with_endian = "%s%s" % (endian, fmt)
fmt_with_endian = endian + fmt
size = struct.calcsize(fmt_with_endian)
if size <= 0:
raise FitParseError("Invalid struct format: %s" % fmt_with_endian)
Expand Down Expand Up @@ -247,10 +243,7 @@ def _parse_raw_values_from_data_message(self, def_mesg):
base_type = field_def.base_type
is_byte = base_type.name == 'byte'
# Struct to read n base types (field def size / base type size)
struct_fmt = '%d%s' % (
field_def.size / base_type.size,
base_type.fmt,
)
struct_fmt = str(int(field_def.size / base_type.size)) + base_type.fmt

# Extract the raw value, ask for a tuple if it's a byte type
raw_value = self._read_struct(
Expand Down
2 changes: 1 addition & 1 deletion fitparse/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ class Crc(object):
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400,
)

FMT = '<H'
FMT = 'H'

def __init__(self, value=0, byte_arr=None):
self.value = value
Expand Down
4 changes: 2 additions & 2 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def generate_fitfile(data=None, endian='<'):

# Prototcol version 1.0, profile version 1.52
header = pack('<2BHI4s', 14, 16, 152, len(fit_data), b'.FIT')
file_data = header + pack(Crc.FMT, Crc.calculate(header)) + fit_data
return file_data + pack(Crc.FMT, Crc.calculate(file_data))
file_data = header + pack('<' + Crc.FMT, Crc.calculate(header)) + fit_data
return file_data + pack('<' + Crc.FMT, Crc.calculate(file_data))


def secs_to_dt(secs):
Expand Down

0 comments on commit bef7623

Please sign in to comment.