Skip to content

Commit

Permalink
Merge pull request gwpy#1222 from alurban/drop-six
Browse files Browse the repository at this point in the history
Drop dependence on the six package and remove python < 3 workarounds
  • Loading branch information
duncanmmacleod committed Apr 15, 2020
2 parents e5aefb3 + 615aac8 commit 8d26dc8
Show file tree
Hide file tree
Showing 53 changed files with 96 additions and 248 deletions.
3 changes: 1 addition & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@
import shutil
import subprocess
import warnings
from configparser import (ConfigParser, NoOptionError)
from string import Template

from six.moves.configparser import (ConfigParser, NoOptionError)

import matplotlib

from sphinx.util import logging
Expand Down
1 change: 0 additions & 1 deletion docs/install/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ GWpy has the following strict requirements:
|numpy|_ ``>=1.12.0``
|dateutil|_
|scipy|_ ``>=1.2.0``
|six|_ ``>=1.5``
|tqdm|_ ``>=4.10.0``
================== ===========================

Expand Down
10 changes: 2 additions & 8 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import os
import re
import sys
import warnings
from contextlib import contextmanager
from pathlib import Path
Expand All @@ -34,9 +33,6 @@

__author__ = "Duncan Macleod <duncan.macleod@ligo.org>"

if sys.version_info < (3, 6): # python < 3.6
pytest.skip('example tests will only run on python >= 3.6',
allow_module_level=True)
pytest.importorskip("matplotlib", minversion="2.2.0")

use('agg') # force non-interactive backend
Expand All @@ -59,11 +55,11 @@
@contextmanager
def cwd(path):
oldpwd = Path.cwd()
os.chdir(path)
os.chdir(str(path))
try:
yield
finally:
os.chdir(oldpwd)
os.chdir(str(oldpwd))


@pytest.fixture(autouse=True)
Expand All @@ -84,8 +80,6 @@ def test_example(script):
with cwd(script.parent):
with script.open('r') as example:
raw = example.read()
if not isinstance(raw, bytes): # python < 3
raw = raw.encode('utf-8')
code = compile(raw, str(script), "exec")
try:
exec(code, globals())
Expand Down
5 changes: 1 addition & 4 deletions gwpy/cli/tests/test_gwpy_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"""

from pathlib import Path
try:
from unittest import mock
except ImportError: # python < 3
import mock
from unittest import mock

import pytest

Expand Down
3 changes: 1 addition & 2 deletions gwpy/detector/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
import re
from copy import copy
from math import ceil

from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse

from astropy import units
from astropy.io import registry as io_registry
Expand Down
6 changes: 2 additions & 4 deletions gwpy/detector/io/clf.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@
"""

import re
import configparser
from collections import OrderedDict

from six import string_types
from six.moves import configparser

from numpy import inf

from ...io import registry
Expand Down Expand Up @@ -107,7 +105,7 @@ def read_channel_list_file(*source):
if 'flow' in params or 'fhigh' in params:
low = params.pop('flow', 0)
high = params.pop('fhigh', inf)
if isinstance(high, string_types) and high.lower() == 'nyquist':
if isinstance(high, str) and high.lower() == 'nyquist':
high = inf
frange = float(low), float(high)
else:
Expand Down
2 changes: 0 additions & 2 deletions gwpy/frequencyseries/hist.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
"""This module provides a spectral-variation histogram class
"""

from six.moves import range

import numpy

from astropy.io import registry as io_registry
Expand Down
6 changes: 2 additions & 4 deletions gwpy/io/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
from collections import (namedtuple, OrderedDict)
from pathlib import Path

from six import string_types

from ..time import LIGOTimeGPS
from .utils import (FILE_LIKE, file_path)

Expand Down Expand Up @@ -239,7 +237,7 @@ def write_cache(cache, fobj, format=None):
- ``'ffl'`` : write an FFL-format cache
"""
# open file
if isinstance(fobj, string_types):
if isinstance(fobj, str):
with open(fobj, 'w') as fobj2:
return write_cache(cache, fobj2, format=format)

Expand Down Expand Up @@ -274,7 +272,7 @@ def is_cache(cache):
`True` if the input object is a cache, or a file in LAL cache format,
otherwise `False`
"""
if isinstance(cache, string_types + FILE_LIKE):
if isinstance(cache, (str,) + FILE_LIKE):
try:
return bool(len(read_cache(cache)))
except (TypeError, ValueError, UnicodeDecodeError, ImportError):
Expand Down
5 changes: 2 additions & 3 deletions gwpy/io/datafind.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@
import re
import warnings
from functools import wraps

from six.moves.http_client import HTTPException
from six.moves.urllib.parse import urlparse
from http.client import HTTPException
from urllib.parse import urlparse

from ligo.segments import (
segment as LigoSegment,
Expand Down
9 changes: 2 additions & 7 deletions gwpy/io/gwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
"""

import warnings

import six
from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse

import numpy

Expand All @@ -33,10 +31,7 @@
__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'

# first 4 bytes of any valid GWF file (see LIGO-T970130 §4.3.1)
if six.PY2:
GWF_SIGNATURE = 'IGWD'
else:
GWF_SIGNATURE = b'IGWD'
GWF_SIGNATURE = b'IGWD'


# -- i/o ----------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions gwpy/io/kerberos.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import sys
from collections import OrderedDict

from six.moves import input

from ..utils.shell import which

__author__ = "Duncan Macleod <duncan.macleod@ligo.org>"
Expand Down
16 changes: 4 additions & 12 deletions gwpy/io/ligolw.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,14 @@
an 'io' subdirectory of the containing directory for that class.
"""

import builtins
import os.path
import re
from contextlib import contextmanager
from functools import wraps
from importlib import import_module
from numbers import Integral

# note: because the "future" module provides a builtins namespace,
# we need to do this the other way around compared to normal
try:
import __builtin__ as builtins
except ImportError: # python >= 3
import builtins

from six import string_types

import numpy

try:
Expand Down Expand Up @@ -415,7 +407,7 @@ def open_xmldoc(fobj, **kwargs):
use_in(kwargs.setdefault('contenthandler', LIGOLWContentHandler))

try: # try and load existing file
if isinstance(fobj, string_types):
if isinstance(fobj, str):
return load_filename(fobj, **kwargs)
if isinstance(fobj, FILE_LIKE):
return load_fileobj(fobj, **kwargs)[0]
Expand Down Expand Up @@ -530,7 +522,7 @@ def write_tables(target, tables, append=False, overwrite=False, **kwargs):
target, contenthandler=kwargs.pop('contenthandler',
LIGOLWContentHandler))
# fail on existing document and not overwriting
elif (not overwrite and isinstance(target, string_types) and
elif (not overwrite and isinstance(target, str) and
os.path.isfile(target)):
raise IOError("File exists: {}".format(target))
else: # or create a new document
Expand All @@ -540,7 +532,7 @@ def write_tables(target, tables, append=False, overwrite=False, **kwargs):
write_tables_to_document(xmldoc, tables, overwrite=overwrite)

# write file
if isinstance(target, string_types):
if isinstance(target, str):
kwargs.setdefault('gz', target.endswith('.gz'))
ligolw_utils.write_filename(xmldoc, target, **kwargs)
elif isinstance(target, FILE_LIKE):
Expand Down
4 changes: 1 addition & 3 deletions gwpy/io/nds2.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import re
import warnings
from collections import OrderedDict
from functools import wraps

from six.moves import reduce
from functools import (reduce, wraps)

from ..time import to_gps
from ..utils.enum import NumpyTypeEnum
Expand Down
9 changes: 2 additions & 7 deletions gwpy/io/tests/test_datafind.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
"""

import os
from http.client import (HTTPConnection, HTTPException)
from io import BytesIO
from itertools import cycle

import six
from six.moves.http_client import (HTTPConnection, HTTPException)

import pytest

import gwdatafind
Expand All @@ -36,10 +34,7 @@

__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'

if six.PY2:
OPEN = '__builtin__.open'
else:
OPEN = 'builtins.open'
OPEN = 'builtins.open'

# -- mock the environment -----------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion gwpy/io/tests/test_gwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""Unit tests for :mod:`gwpy.io.gwf`
"""

from six.moves.urllib.parse import urljoin
from urllib.parse import urljoin

import pytest

Expand Down
30 changes: 11 additions & 19 deletions gwpy/io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,16 @@

import gzip
import tempfile

from six import string_types
from six.moves.urllib.parse import urlparse
from urllib.parse import urlparse

__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'

# build list of file-like types
try: # python < 3
FILE_LIKE = (
file, gzip.GzipFile,
tempfile._TemporaryFileWrapper, # pylint: disable=protected-access
)
except NameError: # python >= 3
from io import IOBase
FILE_LIKE = (
IOBase, gzip.GzipFile,
tempfile._TemporaryFileWrapper, # pylint: disable=protected-access
)
from io import IOBase
FILE_LIKE = (
IOBase, gzip.GzipFile,
tempfile._TemporaryFileWrapper, # pylint: disable=protected-access
)

GZIP_SIGNATURE = b'\x1f\x8b\x08'

Expand All @@ -64,7 +56,7 @@ def identify(origin, filepath, fileobj, *args, **kwargs):
"""Identify the given extensions in a file object/path
"""
# pylint: disable=unused-argument
if (isinstance(filepath, string_types) and
if (isinstance(filepath, str) and
filepath.endswith(extensions)):
return True
return False
Expand Down Expand Up @@ -134,13 +126,13 @@ def file_list(flist):
if the input `flist` cannot be interpreted as any of the above inputs
"""
# open a cache file and return list of paths
if (isinstance(flist, string_types) and
if (isinstance(flist, str) and
flist.endswith(('.cache', '.lcf', '.ffl'))):
from .cache import read_cache
return read_cache(flist)

# separate comma-separate list of names
if isinstance(flist, string_types):
if isinstance(flist, str):
return flist.split(',')

# parse list of entries (of some format)
Expand Down Expand Up @@ -189,9 +181,9 @@ def file_path(fobj):
>>> file_path("file:///home/user/test.txt")
'/home/user/test.txt'
"""
if isinstance(fobj, string_types) and fobj.startswith("file:"):
if isinstance(fobj, str) and fobj.startswith("file:"):
return urlparse(fobj).path
if isinstance(fobj, string_types):
if isinstance(fobj, str):
return fobj
if (isinstance(fobj, FILE_LIKE) and hasattr(fobj, "name")):
return fobj.name
Expand Down
8 changes: 2 additions & 6 deletions gwpy/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@
import itertools
import importlib
import warnings
try:
from collections.abc import (KeysView, ValuesView)
except ImportError: # python < 3.3
from collections import (KeysView, ValuesView)

from six.moves import zip_longest
from collections.abc import (KeysView, ValuesView)
from itertools import zip_longest

import numpy

Expand Down
9 changes: 4 additions & 5 deletions gwpy/segments/flag.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
from io import BytesIO
from collections import OrderedDict
from copy import (copy as shallowcopy, deepcopy)
from functools import reduce
from math import (floor, ceil)
from queue import Queue
from threading import Thread

from six.moves import reduce
from six.moves.urllib.error import (URLError, HTTPError)
from six.moves.urllib.parse import urlparse
from six.moves.queue import Queue
from urllib.error import (URLError, HTTPError)
from urllib.parse import urlparse

from numpy import inf

Expand Down
Loading

0 comments on commit 8d26dc8

Please sign in to comment.