Skip to content

Commit 06b68d6

Browse files
TomFryersdtcooper
authored andcommitted
Remove Python 2 compatibility code
1 parent 301970f commit 06b68d6

9 files changed

Lines changed: 22 additions & 85 deletions

File tree

‎docs/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Requirements
5656

5757
The following are required to install :mod:`fitparse`,
5858

59-
* `Python <http://www.python.org/>`_ 2.7 and above
59+
* `Python <http://www.python.org/>`_ 3.6 and above
6060

6161

6262
API Documentation

‎fitparse/base.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
import struct
66
import warnings
77

8-
# Python 2 compat
9-
try:
10-
num_types = (int, float, long)
11-
except NameError:
12-
num_types = (int, float)
13-
148
from fitparse.processors import FitFileDataProcessor
159
from fitparse.profile import FIELD_TYPE_TIMESTAMP, MESSAGE_TYPES
1610
from fitparse.records import (
@@ -396,7 +390,7 @@ def _apply_scale_offset(self, field, raw_value):
396390
if isinstance(raw_value, tuple):
397391
# Contains multiple values, apply transformations to all of them
398392
return tuple(self._apply_scale_offset(field, x) for x in raw_value)
399-
elif isinstance(raw_value, num_types):
393+
elif isinstance(raw_value, (int, float)):
400394
if field.scale:
401395
raw_value = float(raw_value) / field.scale
402396
if field.offset:

‎fitparse/records.py‎

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import math
22
import struct
33

4-
# Python 2 compat
5-
try:
6-
int_types = (int, long,)
7-
byte_iter = bytearray
8-
except NameError:
9-
int_types = (int,)
10-
byte_iter = lambda x: x
11-
12-
try:
13-
from itertools import zip_longest
14-
except ImportError:
15-
from itertools import izip_longest as zip_longest
4+
from itertools import zip_longest
165

176

187
class RecordBase:
@@ -336,7 +325,7 @@ def render(self, raw_value):
336325
raw_value = unpacked_num
337326

338327
# Mask and shift like a normal number
339-
if isinstance(raw_value, int_types):
328+
if isinstance(raw_value, int):
340329
raw_value = (raw_value >> self.bit_offset) & ((1 << self.bits) - 1)
341330

342331
return raw_value
@@ -376,7 +365,7 @@ def format(value):
376365
@classmethod
377366
def calculate(cls, byte_arr, crc=0):
378367
"""Compute CRC for input bytes."""
379-
for byte in byte_iter(byte_arr):
368+
for byte in byte_arr:
380369
# Taken verbatim from FIT SDK docs
381370
tmp = cls.CRC_TABLE[crc & 0xF]
382371
crc = (crc >> 4) & 0x0FFF
@@ -390,10 +379,7 @@ def calculate(cls, byte_arr, crc=0):
390379

391380
def parse_string(string):
392381
try:
393-
try:
394-
s = string[:string.index(0x00)]
395-
except TypeError: # Python 2 compat
396-
s = string[:string.index('\x00')]
382+
s = string[:string.index('\x00')]
397383
except ValueError:
398384
# FIT specification defines the 'string' type as follows: "Null
399385
# terminated string encoded in UTF-8 format".

‎fitparse/utils.py‎

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
import io
22
import re
3-
try:
4-
from collections.abc import Iterable
5-
except ImportError:
6-
from collections.abc import Iterable
3+
from collections.abc import Iterable
74

8-
try:
9-
# Python 3.4+
10-
from pathlib import PurePath
11-
except ImportError:
12-
PurePath = None
5+
from pathlib import PurePath
136

147

158
class FitParseError(ValueError):
@@ -56,18 +49,14 @@ def fileish_open(fileish, mode):
5649
# BytesIO-like object
5750
return fileish
5851
elif isinstance(fileish, str):
59-
# Python2 - file path, file contents in the case of a TypeError
60-
# Python3 - file path
61-
try:
62-
return open(fileish, mode)
63-
except TypeError:
64-
return io.BytesIO(fileish)
52+
# file path
53+
return open(fileish, mode)
6554

66-
# Python 3 - pathlib obj
67-
if PurePath and isinstance(fileish, PurePath):
55+
# pathlib obj
56+
if isinstance(fileish, PurePath):
6857
return fileish.open(mode)
6958

70-
# Python 3 - file contents
59+
# file contents
7160
return io.BytesIO(fileish)
7261

7362

‎scripts/fitdump‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@ import datetime
77
import itertools
88
import json
99
import os.path
10-
import sys
1110
import types
1211

13-
# Python 2 compat
14-
try:
15-
BrokenPipeError
16-
except NameError:
17-
import socket
18-
BrokenPipeError = socket.error
19-
2012
import fitparse
2113

2214

@@ -44,7 +36,8 @@ def parse_args(args=None):
4436
)
4537
parser.add_argument('-v', '--verbose', action='count', default=0)
4638
parser.add_argument(
47-
'-o', '--output', type=argparse.FileType(mode='w'), default="-",
39+
'-o', '--output', type=argparse.FileType(mode='w', encoding="utf-8"),
40+
default="-",
4841
help='File to output data into (defaults to stdout)',
4942
)
5043
parser.add_argument(
@@ -65,12 +58,6 @@ def parse_args(args=None):
6558

6659
options = parser.parse_args(args)
6760

68-
# Work around argparse.FileType not accepting an `encoding` kwarg in
69-
# Python < 3.4 by closing and reopening the file (unless it's stdout)
70-
if options.output is not sys.stdout:
71-
options.output.close()
72-
options.output = codecs.open(options.output.name, 'w', encoding='UTF-8')
73-
7461
options.verbose = options.verbose >= 1
7562
options.with_defs = (options.type == "readable" and options.verbose)
7663
options.as_dict = (options.type != "readable" and options.verbose)

‎setup.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66

77
requires = None
8-
if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 3):
9-
sys.exit("Python 2.7 or Python 3.3+ are required.")
8+
if sys.version_info < (3, 6):
9+
sys.exit("Python 3.6+ is required.")
1010

1111

1212
setup(

‎tests/test.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,14 @@
44
import datetime
55
import os
66
from struct import pack
7-
import sys
87
import warnings
98

109
from fitparse import FitFile
1110
from fitparse.processors import UTC_REFERENCE, StandardUnitsDataProcessor
1211
from fitparse.records import BASE_TYPES, Crc
1312
from fitparse.utils import FitEOFError, FitCRCError, FitHeaderError, FitParseError
1413

15-
if sys.version_info >= (2, 7):
16-
import unittest
17-
else:
18-
import unittest2 as unittest
14+
import unittest
1915

2016

2117
def generate_messages(mesg_num, local_mesg_num, field_defs, endian='<', data=None):

‎tests/test_records.py‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
#!/usr/bin/env python
22

3-
import sys
4-
53
from fitparse.records import Crc
64

7-
if sys.version_info >= (2, 7):
8-
import unittest
9-
else:
10-
import unittest2 as unittest
11-
5+
import unittest
126

137
class RecordsTestCase(unittest.TestCase):
148
def test_crc(self):

‎tests/test_utils.py‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
import io
44
import os
5-
import sys
65
import tempfile
76

8-
try:
9-
# Python 3.4+
10-
from pathlib import Path
11-
except ImportError:
12-
Path = None
7+
from pathlib import Path
138

149
from fitparse.utils import fileish_open, is_iterable
1510

16-
if sys.version_info >= (2, 7):
17-
import unittest
18-
else:
19-
import unittest2 as unittest
11+
import unittest
2012

2113

2214
def testfile(filename):
@@ -44,8 +36,7 @@ def test_fopen(fileish):
4436
test_fopen(f.read())
4537
with open(testfile("nametest.FIT"), 'rb') as f:
4638
test_fopen(io.BytesIO(f.read()))
47-
if Path:
48-
test_fopen(Path(testfile('nametest.FIT')))
39+
test_fopen(Path(testfile('nametest.FIT')))
4940

5041
def test_fileish_open_write(self):
5142

0 commit comments

Comments
 (0)