Skip to content

Commit

Permalink
Avoid needing 2to3 nonzero fixer by defining __bool__
Browse files Browse the repository at this point in the history
By defining both __nonzero__ (for Python 2) and __bool__
(for Python 3) we avoid needing the 2to3 nonzero fixer,
and a runtime choice of method name.

The redundant method seems harmless.
  • Loading branch information
peterjc committed Nov 3, 2013
1 parent 9a26528 commit d6aa77c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
7 changes: 5 additions & 2 deletions Bio/Phylo/BaseTree.py
Expand Up @@ -1017,14 +1017,17 @@ def __len__(self):
"""Number of clades directy under the root."""
return len(self.clades)

def __nonzero__(self):
"""Boolean value of an instance of this class.
#Python 3:
def __bool__(self):
"""Boolean value of an instance of this class (True).
NB: If this method is not defined, but ``__len__`` is, then the object
is considered true if the result of ``__len__()`` is nonzero. We want
Clade instances to always be considered True.
"""
return True
#Python 2:
__nonzero__ = __bool__

def __str__(self):
if self.name:
Expand Down
6 changes: 5 additions & 1 deletion Bio/SearchIO/_model/hit.py
Expand Up @@ -149,9 +149,13 @@ def __iter__(self):
def __len__(self):
return len(self.hsps)

def __nonzero__(self):
#Python 3:
def __bool__(self):
return bool(self.hsps)

#Python 2:
__nonzero__= __bool__

def __contains__(self, hsp):
return hsp in self._items

Expand Down
6 changes: 5 additions & 1 deletion Bio/SearchIO/_model/hsp.py
Expand Up @@ -289,9 +289,13 @@ def __contains__(self, fragment):
def __len__(self):
return len(self._items)

def __nonzero__(self):
#Python 3:
def __bool__(self):
return bool(self._items)

#Python 2:
__nonzero__= __bool__

def __str__(self):

lines = []
Expand Down
6 changes: 5 additions & 1 deletion Bio/SearchIO/_model/query.py
Expand Up @@ -289,9 +289,13 @@ def __contains__(self, hit_key):
def __len__(self):
return len(self._items)

def __nonzero__(self):
#Python 3:
def __bool__(self):
return bool(self._items)

#Python 2:
__nonzero__= __bool__

def __repr__(self):
return "QueryResult(id=%r, %r hits)" % (self.id, len(self))

Expand Down
8 changes: 6 additions & 2 deletions Bio/SeqFeature.py
Expand Up @@ -338,8 +338,9 @@ def extract(self, parent_sequence):
"""
return self.location.extract(parent_sequence)

def __nonzero__(self):
"""Returns True regardless of the length of the feature.
#Python 3:
def __bool__(self):
"""Boolean value of an instance of this class (True).
This behaviour is for backwards compatibility, since until the
__len__ method was added, a SeqFeature always evaluated as True.
Expand All @@ -352,6 +353,9 @@ def __nonzero__(self):
"""
return True

#Python 2:
__nonzero__= __bool__

def __len__(self):
"""Returns the length of the region described by a feature.
Expand Down
8 changes: 6 additions & 2 deletions Bio/SeqRecord.py
Expand Up @@ -711,8 +711,9 @@ def __len__(self):
"""
return len(self.seq)

def __nonzero__(self):
"""Returns True regardless of the length of the sequence.
#Python 3:
def __bool__(self):
"""Boolean value of an instance of this class (True).
This behaviour is for backwards compatibility, since until the
__len__ method was added, a SeqRecord always evaluated as True.
Expand All @@ -726,6 +727,9 @@ def __nonzero__(self):
"""
return True

#Python 2:
__nonzero__= __bool__

def __add__(self, other):
"""Add another sequence or string to this sequence.
Expand Down
10 changes: 4 additions & 6 deletions do2to3.py
Expand Up @@ -34,13 +34,11 @@
import lib2to3.main
from io import StringIO

#Dictionary of troublesome files needing 2to3,
#keys are filenames (with leading ./ prefix),
#values are a list of 2to3 fixer names (strings):
troublesome = {
"./Bio/SeqFeature.py": ["nonzero"],
"./Bio/SeqRecord.py": ["nonzero"],
"./Bio/Phylo/BaseTree.py": ["nonzero", "unicode"],
"./Bio/SearchIO/_model/hit.py": ["nonzero"],
"./Bio/SearchIO/_model/hsp.py": ["nonzero"],
"./Bio/SearchIO/_model/query.py": ["nonzero"],
"./Bio/Phylo/BaseTree.py": ["unicode"],
"./Bio/Entrez/Parser.py": ["unicode"],
"./Bio/Phylo/PhyloXMLIO.py": ["unicode"],
"./Bio/SearchIO/BlastIO/blast_xml.py": ["unicode"],
Expand Down

0 comments on commit d6aa77c

Please sign in to comment.