Skip to content

Commit

Permalink
make mode consistent (biopython#4858)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdehoon authored Sep 26, 2024
1 parent e0c4d1a commit ac94e87
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Bio/Align/bigbed.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class AlignmentWriter(interfaces.AlignmentWriter):
"""Alignment file writer for the bigBed file format."""

fmt = "bigBed"
mode = "wb"
mode = "b"

def __init__(
self,
Expand Down
19 changes: 10 additions & 9 deletions Bio/Align/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ class AlignmentWriter(ABC): # noqa: B024
followed finally by write_footer().
Subclasses may define the following class attributes:
- mode - 'w' or 'wb' for text or binary files, respectively
- mode - 't' or 'b' for text or binary files, respectively
- fmt - a human-readable name for the file format.
"""

mode = "w" # assume text files by default
mode = "t" # assume text files by default
fmt: Optional[str] = None # to be defined in the subclass

def __init__(self, target):
Expand All @@ -229,18 +229,18 @@ def __init__(self, target):
"""
if target is not None:
# target is None if we only use the writer to format strings.
if self.mode == "w":
if self.mode == "t":
try:
target.write("")
except TypeError:
# target was opened in binary mode
raise StreamModeError("File must be opened in text mode.") from None
except AttributeError:
# target is a path
stream = open(target, self.mode)
stream = open(target, "w" + self.mode)
else:
stream = target
elif self.mode == "wb":
elif self.mode == "b":
try:
target.write(b"")
except TypeError:
Expand All @@ -250,13 +250,12 @@ def __init__(self, target):
) from None
except AttributeError:
# target is a path
stream = open(target, self.mode)
stream = open(target, "w" + self.mode)
else:
stream = target
else:
raise RuntimeError("Unknown mode '%s'" % self.mode)
self._stream = stream

self._target = target

def write_header(self, stream, alignments):
Expand All @@ -280,9 +279,11 @@ def format_alignment(self, alignment):
alignment - an Alignment object
"""
raise NotImplementedError("This method should be implemented")
if self.mode == "t":
raise NotImplementedError("This method should be implemented")
###################################################
# You MUST implement this method in the subclass. #
# if the file mode is text. #
###################################################

def write_single_alignment(self, stream, alignments):
Expand Down Expand Up @@ -318,7 +319,7 @@ def write_multiple_alignments(self, stream, alignments):
write_alignments = write_multiple_alignments

def write_file(self, stream, alignments):
"""Write the alignments to the file strenm, and return the number of alignments.
"""Write the alignments to the file stream, and return the number of alignments.
alignments - A list or iterator returning Alignment objects
stream - Output file stream.
Expand Down

0 comments on commit ac94e87

Please sign in to comment.