Skip to content

Commit c860d30

Browse files
authored
Merge pull request statsmodels#5972 from bashtage/string_types
MAINT: Remove string_types in favor of str
2 parents 3e5e92d + 7252192 commit c860d30

File tree

169 files changed

+250
-407
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

169 files changed

+250
-407
lines changed

statsmodels/base/data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
Base tools for handling various kinds of data structures, attaching metadata to
33
results, and doing data cleaning
44
"""
5-
from statsmodels.compat.python import reduce, iteritems, lmap, zip, range
5+
from statsmodels.compat.python import iteritems, lmap
6+
7+
from functools import reduce
68

79
import numpy as np
810
from pandas import DataFrame, Series, isnull, MultiIndex

statsmodels/base/l1_solvers_common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import numpy as np
66

7-
from statsmodels.compat.python import range
87
from statsmodels.tools.sm_exceptions import ConvergenceWarning
98

109

statsmodels/base/model.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from statsmodels.compat.python import lzip, range, reduce
1+
from statsmodels.compat.python import lzip
2+
3+
from functools import reduce
4+
25
import numpy as np
36
from scipy import stats
47
from statsmodels.base.data import handle_data

statsmodels/base/tests/test_generic_methods.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Author: Josef Perktold
1212
"""
1313
from statsmodels.compat.pandas import assert_series_equal, assert_index_equal
14-
from statsmodels.compat.python import range
1514
from statsmodels.compat.platform import PLATFORM_OSX
1615

1716
import numpy as np

statsmodels/base/tests/test_shrink_pickle.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
66
Author: Josef Perktold
77
"""
8-
from statsmodels.compat.python import iterkeys, cPickle, BytesIO
8+
from statsmodels.compat.python import iterkeys
99

10+
import pickle
11+
from io import BytesIO
1012
import warnings
1113

1214
import numpy as np
@@ -18,10 +20,10 @@
1820

1921
def check_pickle(obj):
2022
fh = BytesIO()
21-
cPickle.dump(obj, fh, protocol=cPickle.HIGHEST_PROTOCOL)
23+
pickle.dump(obj, fh, protocol=pickle.HIGHEST_PROTOCOL)
2224
plen = fh.tell()
2325
fh.seek(0, 0)
24-
res = cPickle.load(fh)
26+
res = pickle.load(fh)
2527
fh.close()
2628
return res, plen
2729

@@ -97,7 +99,7 @@ def test_remove_data_docstring(self):
9799

98100
def test_pickle_wrapper(self):
99101

100-
fh = BytesIO() # use cPickle with binary content
102+
fh = BytesIO() # use pickle with binary content
101103

102104
# test unwrapped results load save pickle
103105
self.results._results.save(fh)

statsmodels/compat/__init__.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22

33
from .python import (
44
PY37,
5-
bytes, str, unicode, string_types,
6-
asunicode, asbytes, asstr, asstr2,
7-
range, zip, filter, map,
5+
asunicode, asbytes, asstr,
86
lrange, lzip, lmap, lfilter,
9-
cStringIO, StringIO, BytesIO,
10-
cPickle, pickle,
117
iteritems, iterkeys, itervalues,
12-
urlopen, urljoin, urlencode, HTTPError, URLError,
13-
reduce, long, unichr, zip_longest,
14-
getargspec, next, get_class
158
)
169

17-
__all__ = ['PY37', 'bytes', 'str', 'unicode', 'string_types',
18-
'asunicode', 'asbytes', 'asstr', 'asstr2', 'range', 'zip',
19-
'filter', 'map', 'lrange', 'lzip', 'lmap', 'lfilter', 'cStringIO',
20-
'StringIO', 'BytesIO', 'cPickle', 'pickle', 'iteritems',
21-
'iterkeys', 'itervalues', 'urlopen', 'urljoin', 'urlencode',
22-
'HTTPError', 'URLError', 'reduce', 'long', 'unichr', 'zip_longest',
23-
'getargspec', 'next', 'get_class', 'test']
10+
__all__ = ['PY37',
11+
'asunicode', 'asbytes', 'asstr',
12+
'lrange', 'lzip', 'lmap', 'lfilter',
13+
'iteritems', 'iterkeys', 'itervalues',
14+
'test']
2415

2516
test = PytestTester()

statsmodels/compat/python.py

Lines changed: 0 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
11
"""
22
Compatibility tools for differences between Python 2 and 3
33
"""
4-
__all__ = ['HTTPError', 'URLError', 'BytesIO']
5-
6-
import functools
7-
import itertools
84
import sys
9-
import urllib
10-
import inspect
11-
from collections import namedtuple
12-
from io import StringIO, BytesIO
13-
import pickle as cPickle
14-
import urllib.request
15-
import urllib.parse
16-
from urllib.error import HTTPError, URLError
175

186
PY37 = (sys.version_info[:2] == (3, 7))
197

20-
cStringIO = StringIO
21-
pickle = cPickle
22-
bytes = bytes
23-
str = str
24-
unicode = str
258
asunicode = lambda x, _: str(x) # noqa:E731
269

2710

@@ -37,26 +20,6 @@ def asstr(s):
3720
return s.decode('latin1')
3821

3922

40-
def asstr2(s): # added JP, not in numpy version
41-
if isinstance(s, str):
42-
return s
43-
elif isinstance(s, bytes):
44-
return s.decode('latin1')
45-
else:
46-
return str(s)
47-
48-
49-
# have to explicitly put builtins into the namespace
50-
range = range
51-
map = map
52-
zip = zip
53-
filter = filter
54-
reduce = functools.reduce
55-
long = int
56-
unichr = chr
57-
zip_longest = itertools.zip_longest
58-
59-
6023
# list-producing versions of the major Python iterating functions
6124
def lrange(*args, **kwargs):
6225
return list(range(*args, **kwargs))
@@ -74,49 +37,6 @@ def lfilter(*args, **kwargs):
7437
return list(filter(*args, **kwargs))
7538

7639

77-
urlopen = urllib.request.urlopen
78-
urljoin = urllib.parse.urljoin
79-
urlretrieve = urllib.request.urlretrieve
80-
urlencode = urllib.parse.urlencode
81-
string_types = str
82-
83-
ArgSpec = namedtuple('ArgSpec',
84-
['args', 'varargs', 'keywords', 'defaults'])
85-
86-
87-
def getargspec(func):
88-
"""
89-
Simple workaroung for getargspec deprecation that returns
90-
an ArgSpec-like object
91-
"""
92-
sig = inspect.signature(func)
93-
parameters = sig.parameters
94-
args, defaults = [], []
95-
varargs, keywords = None, None
96-
97-
for key in parameters:
98-
parameter = parameters[key]
99-
100-
if parameter.kind == inspect.Parameter.VAR_POSITIONAL:
101-
varargs = key
102-
elif parameter.kind == inspect.Parameter.VAR_KEYWORD:
103-
keywords = key
104-
else:
105-
args.append(key)
106-
if parameter.default is not parameter.empty:
107-
defaults.append(parameter.default)
108-
defaults = None if len(defaults) == 0 else defaults
109-
110-
return ArgSpec(args, varargs, keywords, defaults)
111-
112-
113-
try:
114-
next = next
115-
except NameError:
116-
def next(it):
117-
return it.next()
118-
119-
12040
def iteritems(obj, **kwargs):
12141
"""replacement for six's iteritems for Python2/3 compat
12242
uses 'iteritems' if available and otherwise uses 'items'.
@@ -143,14 +63,6 @@ def itervalues(obj, **kwargs):
14363
return func(**kwargs)
14464

14565

146-
def get_class(func):
147-
try:
148-
return func.im_class
149-
except AttributeError:
150-
# Python 3
151-
return func.__self__.__class__
152-
153-
15466
def with_metaclass(meta, *bases):
15567
"""Create a base class with a metaclass."""
15668
# This requires a bit of explanation: the basic idea is to make a dummy

statsmodels/compat/tests/test_itercompat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
66
Author: Josef Perktold
77
"""
8-
from statsmodels.compat import lrange, zip_longest
9-
from itertools import combinations
8+
from statsmodels.compat import lrange
9+
10+
from itertools import combinations, zip_longest
1011
from numpy.testing import assert_
1112

1213

statsmodels/datasets/tests/test_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from statsmodels.compat.python import HTTPError, URLError
2-
31
import os
42
from ssl import SSLError
53
from socket import timeout
4+
from urllib.error import HTTPError, URLError
65

76
import numpy as np
87
from numpy.testing import assert_, assert_array_equal

statsmodels/datasets/utils.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
from statsmodels.compat.python import (StringIO, urlopen, HTTPError, URLError,
2-
lrange, cPickle, urljoin, long)
1+
from statsmodels.compat.python import lrange
2+
3+
from io import StringIO
4+
import pickle
35
import shutil
46
from os import environ, makedirs
57
from os.path import expanduser, exists, dirname, abspath, join
8+
from urllib.error import HTTPError, URLError
9+
from urllib.request import urlopen
10+
from urllib.parse import urljoin
611

712
import numpy as np
813
from pandas import read_stata, read_csv, DataFrame, Series, Index
@@ -63,7 +68,7 @@ def __repr__(self):
6368
def process_pandas(data, endog_idx=0, exog_idx=None, index_idx=None):
6469
names = data.columns
6570

66-
if isinstance(endog_idx, (int, long)):
71+
if isinstance(endog_idx, int):
6772
endog_name = names[endog_idx]
6873
endog = data[endog_name].copy()
6974
if exog_idx is None:
@@ -75,7 +80,7 @@ def process_pandas(data, endog_idx=0, exog_idx=None, index_idx=None):
7580
endog_name = list(endog.columns)
7681
if exog_idx is None:
7782
exog = data.drop(endog_name, axis=1)
78-
elif isinstance(exog_idx, (int, long)):
83+
elif isinstance(exog_idx, int):
7984
exog = data[names[exog_idx]].copy()
8085
else:
8186
exog = data[names[exog_idx]].copy()
@@ -116,14 +121,14 @@ def _get_cache(cache):
116121
def _cache_it(data, cache_path):
117122
import zlib
118123
data = data.decode('utf-8')
119-
open(cache_path, "wb").write(zlib.compress(cPickle.dumps(data)))
124+
open(cache_path, "wb").write(zlib.compress(pickle.dumps(data)))
120125

121126

122127
def _open_cache(cache_path):
123128
import zlib
124129
data = zlib.decompress(open(cache_path, 'rb').read())
125130
# return as bytes object encoded in utf-8 for cross-compat of cached
126-
data = cPickle.loads(data).encode('utf-8')
131+
data = pickle.loads(data).encode('utf-8')
127132
return data
128133

129134

statsmodels/discrete/discrete_margins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#Splitting out maringal effects to see if they can be generalized
22

3-
from statsmodels.compat.python import lzip, range
3+
from statsmodels.compat.python import lzip
44
import numpy as np
55
from scipy.stats import norm
66
from statsmodels.tools.decorators import cache_readonly

statsmodels/discrete/discrete_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
__all__ = ["Poisson", "Logit", "Probit", "MNLogit", "NegativeBinomial",
1919
"GeneralizedPoisson", "NegativeBinomialP"]
2020

21-
from statsmodels.compat.python import range
2221
from scipy.special import loggamma
2322

2423
import numpy as np

statsmodels/discrete/tests/test_constrained.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
License: BSD-3
77
88
"""
9-
from statsmodels.compat.python import StringIO
9+
from io import StringIO
1010

1111
import numpy as np
1212
from numpy.testing import assert_allclose, assert_equal, assert_

statsmodels/discrete/tests/test_discrete.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
tests.
99
"""
1010
# pylint: disable-msg=E1101
11-
from statsmodels.compat.python import range
1211

1312
import os
1413
import warnings

statsmodels/distributions/empirical_distribution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def monotone_fn_inverter(fn, x, vectorized=True, **keywords):
167167
if __name__ == "__main__":
168168
#TODO: Make sure everything is correctly aligned and make a plotting
169169
# function
170-
from statsmodels.compat.python import urlopen
170+
from urllib.request import urlopen
171171
import matplotlib.pyplot as plt
172172
nerve_data = urlopen('http://www.statsci.org/data/general/nerve.txt')
173173
nerve_data = np.loadtxt(nerve_data)

statsmodels/distributions/mixture_rvs.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from statsmodels.compat.python import range
21
import numpy as np
32

43
def _make_index(prob,size):

statsmodels/emplike/elanova.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
1010
Owen, A. B. (2001). Empirical Likelihood. Chapman and Hall.
1111
"""
12-
from statsmodels.compat.python import range
1312
import numpy as np
1413
from .descriptive import _OptFuncts
1514
from scipy import optimize

statsmodels/examples/ex_generic_mle_tdist.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
55
Author: josef-pktd
66
"""
7-
8-
9-
from statsmodels.compat.python import zip
107
import numpy as np
118

129
from scipy import stats, special, optimize

statsmodels/examples/ex_pandas.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66

77
from statsmodels.compat.pandas import frequencies
8-
from statsmodels.compat.python import zip
98
from datetime import datetime
109

1110
import numpy as np

statsmodels/examples/ex_sandwich2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
Created on Fri Dec 16 12:52:13 2011
55
Author: Josef Perktold
66
"""
7+
from urllib.request import urlretrieve
78

8-
from statsmodels.compat.python import urlretrieve
99
import numpy as np
1010
from numpy.testing import assert_almost_equal
1111

statsmodels/examples/ex_sandwich3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
66
Author: Josef Perktold
77
"""
8-
from statsmodels.compat.python import urlretrieve
8+
from urllib.request import urlretrieve
9+
910
import numpy as np
1011
from numpy.testing import assert_almost_equal
1112

0 commit comments

Comments
 (0)