Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FitFile.get_messages remove parsing names to int, remove Python 2 str shim #66

Merged
merged 1 commit into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()