Permalink
Browse files

Deprecate our next methods, my_iterator.next() --> next(my_iterator)

This was based on running 2to3 -f next, and then manually
adding deprecated alias for the next method under Python 2
to preserve backward compatibility (except for SearchIO
where we are free to break the API like this).

Note this also disables the next fixer in do2to3.py
  • Loading branch information...
1 parent 41cf95f commit 79ffacd0c612e39939240d87b6f1dbfd4e93b4d3 @peterjc peterjc committed Oct 11, 2013
View
@@ -1,4 +1,4 @@
-# Copyright 2006-2010 by Peter Cock. All rights reserved.
+# Copyright 2006-2013 by Peter Cock. All rights reserved.
#
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
@@ -84,7 +84,7 @@ def write_alignment(self, alignment):
class ClustalIterator(AlignmentIterator):
"""Clustalw alignment iterator."""
- def next(self):
+ def __next__(self):
handle = self.handle
try:
#Header we saved from when we were parsing
View
@@ -1,4 +1,4 @@
-# Copyright 2008-2010 by Peter Cock. All rights reserved.
+# Copyright 2008-2013 by Peter Cock. All rights reserved.
#
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
@@ -67,7 +67,7 @@ class EmbossIterator(AlignmentIterator):
call the "pairs" and "simple" formats.
"""
- def next(self):
+ def __next__(self):
handle = self.handle
View
@@ -1,4 +1,4 @@
-# Copyright 2008-2010 by Peter Cock. All rights reserved.
+# Copyright 2008-2013 by Peter Cock. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
@@ -10,9 +10,10 @@
from __future__ import print_function
+import sys # for checking if Python 2
+
from Bio.Alphabet import single_letter_alphabet
-# TODO - Review self.next() method for Python 3 support...
class AlignmentIterator(object):
"""Base class for building MultipleSeqAlignment iterators.
@@ -45,7 +46,7 @@ def __init__(self, handle, seq_count=None,
# or if additional arguments are required. #
#####################################################
- def next(self):
+ def __next__(self):
"""Return the next alignment in the file.
This method should be replaced by any derived class to do something
@@ -57,6 +58,16 @@ def next(self):
# into MultipleSeqAlignment objects. #
#####################################################
+ if sys.version_info[0] < 3:
+ def next(self):
+ """Deprecated Python 2 style alias for Python 3 style __next__ method."""
+ import warnings
+ from Bio import BiopythonDeprecationWarning
+ warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), "
+ "the .next() method is deprecated and will be removed in a "
+ "future release of Biopython.", BiopythonDeprecationWarning)
+ return self.__next__()
+
def __iter__(self):
"""Iterate over the entries as MultipleSeqAlignment objects.
@@ -69,7 +80,7 @@ def __iter__(self):
print record.id
print record.seq
myFile.close()"""
- return iter(self.next, None)
+ return iter(self.__next__, None)
class AlignmentWriter(object):
View
@@ -1,4 +1,4 @@
-# Copyright 2006-2011 by Peter Cock. All rights reserved.
+# Copyright 2006-2013 by Peter Cock. All rights reserved.
# Revisions copyright 2011 Brandon Invergo. All rights reserved.
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
@@ -186,7 +186,7 @@ def _split_id(self, line):
seq = line[self.id_width:].strip().replace(' ', '')
return seq_id, seq
- def next(self):
+ def __next__(self):
handle = self.handle
try:
@@ -371,7 +371,7 @@ class SequentialPhylipIterator(PhylipIterator):
the next. According to the PHYLIP documentation for input file formatting,
newlines and spaces may optionally be entered at any point in the sequences.
"""
- def next(self):
+ def __next__(self):
handle = self.handle
try:
@@ -1,4 +1,5 @@
-# Copyright 2006-2010 by Peter Cock. All rights reserved.
+# Copyright 2006-2013 by Peter Cock. All rights reserved.
+#
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
@@ -311,7 +312,7 @@ class StockholmIterator(AlignmentIterator):
"OC": "organism_classification",
"LO": "look"}
- def next(self):
+ def __next__(self):
try:
line = self._header
del self._header
@@ -1636,7 +1636,7 @@ def __init__(self, handle, parser=None):
self._parser = parser
self._header = []
- def next(self):
+ def __next__(self):
"""next(self) -> object
Return the next Blast record from the file. If no more records,
@@ -1684,8 +1684,18 @@ def next(self):
return self._parser.parse(StringIO(data))
return data
+ if sys.version_info[0] < 3:
+ def next(self):
+ """Deprecated Python 2 style alias for Python 3 style __next__ method."""
+ import warnings
+ from Bio import BiopythonDeprecationWarning
+ warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), "
+ "the .next() method is deprecated and will be removed in a "
+ "future release of Biopython.", BiopythonDeprecationWarning)
+ return self.__next__()
+
def __iter__(self):
- return iter(self.next, None)
+ return iter(self.__next__, None)
def blastall(blastcmd, program, database, infile, align_view='7', **keywds):
@@ -55,7 +55,7 @@ def __init__(self, handle):
self._n = 0
self._in_header = 1
- def next(self):
+ def __next__(self):
self.table_record = BlastTableRec()
self._n += 1
inline = self._lookahead
@@ -76,6 +76,16 @@ def next(self):
self._in_header = 1
return self.table_record
+ if sys.version_info[0] < 3:
+ def next(self):
+ """Deprecated Python 2 style alias for Python 3 style __next__ method."""
+ import warnings
+ from Bio import BiopythonDeprecationWarning
+ warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), "
+ "the .next() method is deprecated and will be removed in a "
+ "future release of Biopython.", BiopythonDeprecationWarning)
+ return self.__next__()
+
def _consume_entry(self, inline):
current_entry = BlastTableEntry(inline)
self.table_record.add_entry(current_entry)
View
@@ -139,9 +139,19 @@ def __init__(self, query, connection=connection):
sys.stderr.write("Query params: %s\n" % query.params)
self.cursor.execute(query.statement, query.params)
- def next(self):
+ def __next__(self):
return self.row_class(self.cursor)
+ if sys.version_info[0] < 3:
+ def next(self):
+ """Deprecated Python 2 style alias for Python 3 style __next__ method."""
+ import warnings
+ from Bio import BiopythonDeprecationWarning
+ warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), "
+ "the .next() method is deprecated and will be removed in a "
+ "future release of Biopython.", BiopythonDeprecationWarning)
+ return self.__next__()
+
class QuerySingle(Query, QueryRow):
ignore_warnings = 0
View
@@ -1,5 +1,6 @@
# Copyright 1999 by Jeffrey Chang. All rights reserved.
-# Copyright 2009-2012 by Peter Cock. All rights reserved.
+# Copyright 2009-2013 by Peter Cock. All rights reserved.
+#
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
@@ -19,6 +20,7 @@
import codecs
import os
+import sys
import contextlib
import itertools
@@ -112,12 +114,22 @@ def __init__(self, handle):
def __iter__(self):
return self
- def next(self):
+ def __next__(self):
next = self.readline()
if not next:
raise StopIteration
return next
+ if sys.version_info[0] < 3:
+ def next(self):
+ """Deprecated Python 2 style alias for Python 3 style __next__ method."""
+ import warnings
+ from Bio import BiopythonDeprecationWarning
+ warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), "
+ "the .next() method is deprecated and will be removed in a "
+ "future release of Biopython.", BiopythonDeprecationWarning)
+ return self.__next__()
+
def readlines(self, *args, **keywds):
lines = self._saved + self._handle.readlines(*args, **keywds)
self._saved = []
View
@@ -1,5 +1,6 @@
# Copyright 2000 by Jeffrey Chang, Brad Chapman. All rights reserved.
-# Copyright 2006-2011 by Peter Cock. All rights reserved.
+# Copyright 2006-2013 by Peter Cock. All rights reserved.
+#
# This code is part of the Biopython distribution and governed by its
# license. Please see the LICENSE file that should have been included
# as part of this package.
@@ -41,6 +42,7 @@
from __future__ import print_function
import re
+import sys # for checking if Python 2
# other Biopython stuff
from Bio import SeqFeature
@@ -369,7 +371,7 @@ def __init__(self, handle, parser = None):
self.handle = handle
self._parser = parser
- def next(self):
+ def __next__(self):
"""Return the next GenBank record from the handle.
Will return None if we ran out of records.
@@ -389,8 +391,18 @@ def next(self):
except StopIteration:
return None
+ if sys.version_info[0] < 3:
+ def next(self):
+ """Deprecated Python 2 style alias for Python 3 style __next__ method."""
+ import warnings
+ from Bio import BiopythonDeprecationWarning
+ warnings.warn("Please use next(my_iterator) instead of my_iterator.next(), "
+ "the .next() method is deprecated and will be removed in a "
+ "future release of Biopython.", BiopythonDeprecationWarning)
+ return self.__next__()
+
def __iter__(self):
- return iter(self.next, None)
+ return iter(self.__next__, None)
class ParserFailureError(Exception):
View
@@ -128,7 +128,7 @@ def read(handle, format):
shown in the example above). Instead use:
>>> from Bio import Motif
- >>> motif = Motif.parse(open("Motif/alignace.out"), "AlignAce").next()
+ >>> motif = next(Motif.parse(open("Motif/alignace.out"), "AlignAce"))
>>> motif.consensus()
Seq('TCTACGATTGAG', IUPACUnambiguousDNA())
@@ -171,4 +171,3 @@ def _test():
if __name__ == "__main__":
#Run the doctests
_test()
-
Oops, something went wrong.

0 comments on commit 79ffacd

Please sign in to comment.