Skip to content

Commit

Permalink
refactor: use Python 3 stdlib instead of six
Browse files Browse the repository at this point in the history
  • Loading branch information
jsstevenson committed Jun 13, 2024
1 parent c4e620b commit 302a39b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 28 deletions.
1 change: 0 additions & 1 deletion misc/docker/seqrepo.df
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ RUN pip3 install --upgrade \
pygments \
pysam \
simplegeneric \
six \
tqdm \
traitlets \
wcwidth \
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ dependencies = [
"ipython ~= 8.4",
"pysam ~= 0.22",
"requests ~= 2.31",
"six ~= 1.16",
"tqdm ~= 4.66",
"yoyo-migrations ~= 8.2",
]
Expand Down
8 changes: 2 additions & 6 deletions src/biocommons/seqrepo/fastadir/bases.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import abc
from typing import Optional

import six


@six.add_metaclass(abc.ABCMeta)
class BaseReader:
class BaseReader(metaclass=abc.ABCMeta):
@abc.abstractmethod
def fetch(self, seq_id: str, start: Optional[int] = None, end: Optional[int] = None) -> str:
raise NotImplementedError
Expand All @@ -14,8 +11,7 @@ def __getitem__(self, ac: str) -> str:
return self.fetch(ac)


@six.add_metaclass(abc.ABCMeta)
class BaseWriter:
class BaseWriter(metaclass=abc.ABCMeta):
@abc.abstractmethod
def store(self, seq_id: str, seq: str) -> str:
pass # pragma: no cover
4 changes: 1 addition & 3 deletions src/biocommons/seqrepo/fastadir/fabgz.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from types import TracebackType
from typing import Optional, Type

import six
from pysam import FastaFile
from typing_extensions import Self

Expand Down Expand Up @@ -45,15 +44,14 @@ def _get_bgzip_version(exe: str) -> str:

def _find_bgzip() -> str:
"""return path to bgzip if found and meets version requirements, else exception"""
missing_file_exception = OSError if six.PY2 else FileNotFoundError
min_bgzip_version = ".".join(map(str, min_bgzip_version_info))
exe = os.environ.get("SEQREPO_BGZIP_PATH", shutil.which("bgzip") or "/usr/bin/bgzip")

try:
bgzip_version = _get_bgzip_version(exe)
except AttributeError:
raise RuntimeError("Didn't find version string in bgzip executable ({exe})".format(exe=exe))
except missing_file_exception:
except FileNotFoundError:
raise RuntimeError(
"{exe} doesn't exist; you need to install htslib and tabix "
"(See https://github.com/biocommons/biocommons.seqrepo#requirements)".format(exe=exe)
Expand Down
9 changes: 4 additions & 5 deletions tests/test_fabgz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import tempfile

import pytest
import six

from biocommons.seqrepo.fastadir.fabgz import FabgzReader, FabgzWriter

Expand All @@ -19,18 +18,18 @@ def test_write_reread():

# write sequences
faw = FabgzWriter(fabgz_fn)
for seq_id, seq in six.iteritems(sequences):
for seq_id, seq in sequences.items():
faw.store(seq_id, seq)
# add twice to demonstrate non-redundancy
for seq_id, seq in six.iteritems(sequences):
for seq_id, seq in sequences.items():
faw.store(seq_id, seq)
faw.close()

# now read them back
far = FabgzReader(fabgz_fn)
assert far.filename.startswith(tmpdir.encode())
assert far.filename.startswith(tmpdir.encode()) # type: ignore
assert set(far.keys()) == set(sequences.keys())
assert 5 == len(far), "expected 5 sequences"
assert 5 == len(far), "expected 5 sequences" # type: ignore
assert "l10" in far.keys()
assert far["l10"] == seed * 10
for seq_id in far.keys():
Expand Down
24 changes: 12 additions & 12 deletions tests/test_fastaiter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import StringIO

import pytest
import six
from six.moves import StringIO

from biocommons.seqrepo.fastaiter import FastaIter

Expand All @@ -12,7 +12,7 @@ def test_empty():

# should return an empty generator
with pytest.raises(StopIteration):
six.next(iterator)
next(iterator)


def test_noheader():
Expand All @@ -22,54 +22,54 @@ def test_noheader():

# should return an empty generator
with pytest.raises(StopIteration):
six.next(iterator)
next(iterator)


def test_single():
data = StringIO(">seq1\nACGT\n")

iterator = FastaIter(data)

header, seq = six.next(iterator)
header, seq = next(iterator)
assert header == "seq1"
assert seq == "ACGT"

# should be empty now
with pytest.raises(StopIteration):
six.next(iterator)
next(iterator)


def test_multiple():
data = StringIO(">seq1\nACGT\n>seq2\nTGCA\n\n>seq3\nTTTT")

iterator = FastaIter(data)

header, seq = six.next(iterator)
header, seq = next(iterator)
assert header == "seq1"
assert seq == "ACGT"

header, seq = six.next(iterator)
header, seq = next(iterator)
assert header == "seq2"
assert seq == "TGCA"

header, seq = six.next(iterator)
header, seq = next(iterator)
assert header == "seq3"
assert seq == "TTTT"

# should be empty now
with pytest.raises(StopIteration):
six.next(iterator)
next(iterator)


def test_multiline():
data = StringIO(">seq1\nACGT\nTGCA")

iterator = FastaIter(data)

header, seq = six.next(iterator)
header, seq = next(iterator)
assert header == "seq1"
assert seq == "ACGTTGCA"

# should be empty now
with pytest.raises(StopIteration):
six.next(iterator)
next(iterator)

0 comments on commit 302a39b

Please sign in to comment.