Skip to content

Commit 3fa1fc0

Browse files
committed
FitFile.get_messages remove parsing names to int, remove Python 2 str shim
Use utils.is_iterable and use set for quicker in test
1 parent bef7623 commit 3fa1fc0

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

fitparse/base.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Python 2 compat
66
try:
77
num_types = (int, float, long)
8-
str = basestring
98
except NameError:
109
num_types = (int, float)
1110

@@ -16,7 +15,7 @@
1615
BASE_TYPES, BASE_TYPE_BYTE,
1716
add_dev_data_id, add_dev_field_description, get_dev_type
1817
)
19-
from fitparse.utils import fileish_open, FitParseError, FitEOFError, FitCRCError, FitHeaderError
18+
from fitparse.utils import fileish_open, is_iterable, FitParseError, FitEOFError, FitCRCError, FitHeaderError
2019

2120

2221
class FitFile(object):
@@ -407,17 +406,10 @@ def get_messages(self, name=None, with_definitions=False, as_dict=False):
407406
as_dict = False
408407

409408
if name is not None:
410-
if isinstance(name, (tuple, list)):
411-
names = name
409+
if is_iterable(name):
410+
names = set(name)
412411
else:
413-
names = [name]
414-
415-
# Convert any string numbers in names to ints
416-
# TODO: Revisit Python2/3 str/bytes typecheck issues
417-
names = set([
418-
int(n) if (isinstance(n, str) and n.isdigit()) else n
419-
for n in names
420-
])
412+
names = set((name,))
421413

422414
def should_yield(message):
423415
if with_definitions or message.type == 'data':

fitparse/utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import re
2-
31
import io
2+
import re
3+
from collections import Iterable
44

55

66
class FitParseError(ValueError):
@@ -56,3 +56,10 @@ def fileish_open(fileish, mode):
5656
else:
5757
# Python 3 - file contents
5858
return io.BytesIO(fileish)
59+
60+
61+
def is_iterable(obj):
62+
"""Check, if the obj is iterable but not string or bytes.
63+
:rtype bool"""
64+
# Speed: do not use iter() although it's more robust, see also https://stackoverflow.com/questions/1952464/
65+
return isinstance(obj, Iterable) and not isinstance(obj, (str, bytes))

tests/test_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
import tempfile
77

8-
from fitparse.utils import fileish_open
8+
from fitparse.utils import fileish_open, is_iterable
99

1010
if sys.version_info >= (2, 7):
1111
import unittest
@@ -61,6 +61,16 @@ def test_fopen(fileish):
6161
except OSError:
6262
pass
6363

64+
def test_is_iterable(self):
65+
self.assertFalse(is_iterable(None))
66+
self.assertFalse(is_iterable(1))
67+
self.assertFalse(is_iterable('1'))
68+
self.assertFalse(is_iterable(b'1'))
69+
70+
self.assertTrue(is_iterable((1, 2)))
71+
self.assertTrue(is_iterable([1, 2]))
72+
self.assertTrue(is_iterable(range(2)))
73+
6474

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

0 commit comments

Comments
 (0)