Permalink
Browse files

Avoid comparing types, use isinstance() instead (fixes PEP8 E721).

E721 do not compare types, use 'isinstance()'
  • Loading branch information...
1 parent d3ad376 commit 8d16e8f4e7c712528d1d1bdca90d3f180c22e230 @cbrueffer cbrueffer committed Dec 3, 2012
View
@@ -23,7 +23,7 @@ def wrap_line(line):
return output
def validate_key(key):
- if type(key) != type(''):
+ if not isinstance(key, str):
raise CrystalError('chain requires a string label')
if len(key) != 1:
raise CrystalError('chain label should contain one letter')
@@ -37,7 +37,7 @@ class Hetero(object):
"""
def __init__(self, data):
# Enforce string storage
- if type(data) != type(""):
+ if not isinstance(data, str):
raise CrystalError('Hetero data must be an alphameric string')
if data.isalnum() == 0:
raise CrystalError('Hetero data must be an alphameric string')
@@ -66,12 +66,12 @@ def __len__(self): return len(self.data)
class Chain(object):
def __init__(self, residues = ''):
self.data = []
- if type(residues) == type(''):
+ if isinstance(residues, str):
residues = residues.replace('*', ' ')
residues = residues.strip()
elements = residues.split()
self.data = map(Hetero, elements)
- elif type(residues) == type([]):
+ elif isinstance(residues, list):
for element in residues:
if not isinstance(element, Hetero):
raise CrystalError('Text must be a string')
@@ -185,23 +185,23 @@ def index(self, item):
def __add__(self, other):
if isinstance(other, Chain):
return self.__class__(self.data + other.data)
- elif type(other) == type(''):
+ elif isinstance(other, str):
return self.__class__(self.data + Chain(other).data)
else:
raise TypeError
def __radd__(self, other):
if isinstance(other, Chain):
return self.__class__(other.data + self.data)
- elif type(other) == type(''):
+ elif isinstance(other, str):
return self.__class__(Chain(other).data + self.data)
else:
raise TypeError
def __iadd__(self, other):
if isinstance(other, Chain):
self.data += other.data
- elif type(other) == type(''):
+ elif isinstance(other, str):
self.data += Chain(other).data
else:
raise TypeError
@@ -210,7 +210,7 @@ def __iadd__(self, other):
class Crystal(object):
def __init__(self, data = {}):
# Enforcestorage
- if type(data) != type({}):
+ if not isinstance(data, dict):
raise CrystalError('Crystal must be a dictionary')
self.data = data
self.fix()
@@ -221,7 +221,7 @@ def fix(self):
element = data[key]
if isinstance(element, Chain):
pass
- elif type(element) == type(''):
+ elif isinstance(element, str):
data[key] = Chain(element)
else:
raise TypeError
@@ -250,7 +250,7 @@ def __getitem__(self, key): return self.data[key]
def __setitem__(self, key, item):
if isinstance(item, Chain):
self.data[key] = item
- elif type(item) == type(''):
+ elif isinstance(item, str):
self.data[ key ] = Chain(item)
else:
raise TypeError
View
@@ -53,14 +53,14 @@ def random_population(genome_alphabet, genome_size, num_organisms,
letter_rand = random.Random()
# figure out what type of characters are in the alphabet
- if type(genome_alphabet.letters[0]) == type("A"):
+ if isinstance(genome_alphabet.letters[0], str):
if sys.version_info[0] == 3:
alphabet_type = "u" #Use unicode string on Python 3
else:
alphabet_type = "c" #Use byte string on Python 2
- elif type(genome_alphabet.letters[0]) == type(1):
+ elif isinstance(genome_alphabet.letters[0], int):
alphabet_type = "i"
- elif type(genome_alphabet.letters[0]) == type(1.0):
+ elif isinstance(genome_alphabet.letters[0], float):
alphabet_type = "d"
else:
raise ValueError(\
View
@@ -42,14 +42,14 @@ def __str__( self ):
att_keys.sort()
for key in att_keys:
contents = self.entity_attributes[ key ]
- if( type( contents ) == type( [] ) ):
+ if isinstance(contents, list):
for item in contents:
try:
output = output + '%s: %s\n' % ( key, item[ :40 ] )
output = output + out_block( item[ 40: ] )
except:
pass
- elif( type( contents ) == type( '' ) ):
+ elif isinstance(contents, str):
output = output + '%s: %s\n' % ( key, contents[ :40 ] )
output = output + out_block( contents[ 40: ] )
else:
@@ -457,9 +457,9 @@ def set_page_size(self, pagesize, orientation):
Set the size of the drawing
"""
- if type(pagesize) == type('a'): # A string, so translate
+ if isinstance(pagesize, str): # A string, so translate
pagesize = page_sizes(pagesize)
- elif type(pagesize) == type((1,2)): # A tuple, so don't translate
+ elif isinstance(pagesize, tuple): # A tuple, so don't translate
pagesize = pagesize
else:
raise ValueError("Page size %s not recognised" % pagesize)
@@ -87,9 +87,9 @@ def translate(self, color=None, colour=None):
elif isinstance(color, basestring):
#Assume its a named reportlab color like "red".
color = colors.toColor(color)
- elif type(color) == type((1., 2., 3.)) and type(color[0]) == type(1.):
+ elif isinstance(color, tuple) and isinstance(color[0], float):
color = self.float1_color(color)
- elif type(color) == type((1, 2, 3)) and type(color[0]) == type(1):
+ elif isinstance(color, tuple) and isinstance(color[0], int):
color = self.int255_color(color)
return color
@@ -42,8 +42,8 @@ def write(self, pattern_list, output_handle):
"""
for pattern in pattern_list:
# deal with signatures, concatentate them with the separator
- if (type(pattern) == type([]) or
- type(pattern) == type(tuple([]))):
+ if (isinstance(pattern, list) or
+ isinstance(pattern, tuple)):
string_pattern = self.separator.join(pattern)
# deal with the normal cases
else:
View
@@ -142,7 +142,7 @@ def _make_mmcif_dict(self):
continue
try:
value=mmcif_dict[entry]
- if type(value)==type([]):
+ if isinstance(value, list):
for item in value:
print item
else:
View
@@ -57,7 +57,7 @@ def UniprotIterator(handle, alphabet=Alphabet.ProteinAlphabet(), return_raw_comm
raise ValueError("Wrong alphabet %r" % alphabet)
if not hasattr(handle, "read"):
- if type(handle) == type(''):
+ if isinstance(handle, str):
handle = StringIO(handle)
else:
raise Exception('An XML-containing handler or an XML string must be passed')
View
@@ -164,7 +164,7 @@ def xGC_skew(seq, window = 1000, zoom = 100,
def molecular_weight(seq):
"""Calculate the molecular weight of a DNA sequence."""
- if type(seq) == type(''): seq = Seq(seq, IUPAC.unambiguous_dna)
+ if isinstance(seq, str): seq = Seq(seq, IUPAC.unambiguous_dna)
weight_table = IUPACData.unambiguous_dna_weights
return sum(weight_table[x] for x in seq)
View
@@ -157,7 +157,7 @@ def __init__(self, data=None, alphabet=None, mat_name='', build_later=0):
# matrix after creating the instance. Constructor builds a half matrix
# filled with zeroes.
- assert type(mat_name) == type('')
+ assert isinstance(mat_name, str)
# "data" may be:
# 1) None --> then self.data is an empty dictionary
@@ -251,8 +251,8 @@ def print_full_mat(self,f=None,format="%4d",topformat="%4s",
f = f or sys.stdout
# create a temporary dictionary, which holds the full matrix for
# printing
- assert non_sym == None or type(non_sym) == type(1.) or \
- type(non_sym) == type(1)
+ assert non_sym == None or isinstance(non_sym, float) or \
+ isinstance(non_sym, int)
full_mat = copy.copy(self)
for i in self:
if i[0] != i[1]:
View
@@ -167,7 +167,7 @@ def end_tr( self ):
( self.context == 'seq_info' ) ):
try:
contents = self.queue[ self.master_key ][ self.key_waiting ]
- if( type( contents ) == type( [] ) ):
+ if isinstance(contents, list):
contents.append( text )
else:
self.queue[ self.master_key ][ self.key_waiting ] = \
@@ -196,10 +196,10 @@ def print_item( self, item, level = 1 ):
indent = ' '
for j in range( 0, level ):
indent = indent + ' '
- if( type( item ) == type( '' ) ):
+ if isinstance(item, str):
if( item != '' ):
print '%s%s' % ( indent, item )
- elif( type( item ) == type([])):
+ elif isinstance(item, list):
for subitem in item:
self.print_item( subitem, level + 1 )
elif( isinstance( item, UserDict.UserDict ) ):

0 comments on commit 8d16e8f

Please sign in to comment.