Skip to content

Commit

Permalink
FitFile.get_messages remove parsing names to int, remove Python 2 str…
Browse files Browse the repository at this point in the history
… shim

Use utils.is_iterable and use set for quicker in test
  • Loading branch information
xmedeko committed Apr 25, 2018
1 parent bef7623 commit 3fa1fc0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
16 changes: 4 additions & 12 deletions fitparse/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# Python 2 compat
try:
num_types = (int, float, long)
str = basestring
except NameError:
num_types = (int, float)

Expand All @@ -16,7 +15,7 @@
BASE_TYPES, BASE_TYPE_BYTE,
add_dev_data_id, add_dev_field_description, get_dev_type
)
from fitparse.utils import fileish_open, FitParseError, FitEOFError, FitCRCError, FitHeaderError
from fitparse.utils import fileish_open, is_iterable, FitParseError, FitEOFError, FitCRCError, FitHeaderError


class FitFile(object):
Expand Down Expand Up @@ -407,17 +406,10 @@ def get_messages(self, name=None, with_definitions=False, as_dict=False):
as_dict = False

if name is not None:
if isinstance(name, (tuple, list)):
names = name
if is_iterable(name):
names = set(name)
else:
names = [name]

# Convert any string numbers in names to ints
# TODO: Revisit Python2/3 str/bytes typecheck issues
names = set([
int(n) if (isinstance(n, str) and n.isdigit()) else n
for n in names
])
names = set((name,))

def should_yield(message):
if with_definitions or message.type == 'data':
Expand Down
11 changes: 9 additions & 2 deletions fitparse/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

import io
import re
from collections import Iterable


class FitParseError(ValueError):
Expand Down Expand Up @@ -56,3 +56,10 @@ def fileish_open(fileish, mode):
else:
# Python 3 - file contents
return io.BytesIO(fileish)


def is_iterable(obj):
"""Check, if the obj is iterable but not string or bytes.
:rtype bool"""
# Speed: do not use iter() although it's more robust, see also https://stackoverflow.com/questions/1952464/
return isinstance(obj, Iterable) and not isinstance(obj, (str, bytes))
12 changes: 11 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
import tempfile

from fitparse.utils import fileish_open
from fitparse.utils import fileish_open, is_iterable

if sys.version_info >= (2, 7):
import unittest
Expand Down Expand Up @@ -61,6 +61,16 @@ def test_fopen(fileish):
except OSError:
pass

def test_is_iterable(self):
self.assertFalse(is_iterable(None))
self.assertFalse(is_iterable(1))
self.assertFalse(is_iterable('1'))
self.assertFalse(is_iterable(b'1'))

self.assertTrue(is_iterable((1, 2)))
self.assertTrue(is_iterable([1, 2]))
self.assertTrue(is_iterable(range(2)))


if __name__ == '__main__':
unittest.main()

0 comments on commit 3fa1fc0

Please sign in to comment.