From c1ac63be48b1243c37f20d5dc020526fcf18c064 Mon Sep 17 00:00:00 2001 From: Diane Trout Date: Fri, 20 Mar 2015 14:13:54 -0700 Subject: [PATCH] Use isinstance(object, (types)) pattern instead of type(object) == types.Type --- htsworkflow/pipelines/retrieve_config.py | 8 +++----- htsworkflow/pipelines/sequences.py | 3 ++- htsworkflow/pipelines/summary.py | 4 +--- htsworkflow/submission/condorfastq.py | 3 ++- htsworkflow/submission/daf.py | 3 ++- htsworkflow/submission/encoded.py | 6 +++--- htsworkflow/util/rdfhelp.py | 13 +++++++------ htsworkflow/util/test/test_rdfhelp.py | 7 +++---- samples/models.py | 3 ++- 9 files changed, 25 insertions(+), 25 deletions(-) diff --git a/htsworkflow/pipelines/retrieve_config.py b/htsworkflow/pipelines/retrieve_config.py index c8fa9d2..26ff2d1 100644 --- a/htsworkflow/pipelines/retrieve_config.py +++ b/htsworkflow/pipelines/retrieve_config.py @@ -7,8 +7,7 @@ from optparse import OptionParser, IndentedHelpFormatter import os import sys import types -import urllib -import urllib2 +import six try: import json @@ -403,12 +402,11 @@ def format_pooled_libraries(shared, library): sequences = library.get('index_sequence', None) if sequences is None: return [] - elif (type(sequences) in types.StringTypes and - sequences.lower().startswith('err')): + elif isinstance(sequences, six.string_types): shared['Index'] = '' shared['SampleProject'] = library['library_id'] return [shared] - elif (type(sequences) == types.DictType): + elif isinstance(sequences, dict): pooled = [] multiplex_ids = sequences.keys() multiplex_ids.sort(key=natural_sort_key) diff --git a/htsworkflow/pipelines/sequences.py b/htsworkflow/pipelines/sequences.py index 17e65cf..1d746c9 100644 --- a/htsworkflow/pipelines/sequences.py +++ b/htsworkflow/pipelines/sequences.py @@ -7,6 +7,7 @@ import os import types import re import sys +import six from six.moves.urllib.parse import urljoin, urlparse import RDF @@ -376,7 +377,7 @@ def scan_for_sequences(dirs): Scan through a list of directories for sequence like files """ sequences = [] - if type(dirs) in types.StringTypes: + if isinstance(dirs, six.string_types): raise ValueError("You probably want a list or set, not a string") for d in dirs: diff --git a/htsworkflow/pipelines/summary.py b/htsworkflow/pipelines/summary.py index c1108a2..8c3ec77 100644 --- a/htsworkflow/pipelines/summary.py +++ b/htsworkflow/pipelines/summary.py @@ -6,7 +6,6 @@ from __future__ import print_function, unicode_literals import os import logging import re -import types from pprint import pprint from lxml import html @@ -287,8 +286,7 @@ class LaneResultSummary(object): value = getattr(self, variable_name) if value is None: continue - # it looks like a sequence - elif type(value) in (types.TupleType, types.ListType): + elif isinstance(value, (tuple, list)): element = make_mean_range_element( lane_result, tag, diff --git a/htsworkflow/submission/condorfastq.py b/htsworkflow/submission/condorfastq.py index 705d47e..8c35e57 100644 --- a/htsworkflow/submission/condorfastq.py +++ b/htsworkflow/submission/condorfastq.py @@ -5,6 +5,7 @@ import os from pprint import pformat,pprint import sys import types +import six from six.moves.urllib.parse import urljoin, urlparse from htsworkflow.pipelines.sequences import scan_for_sequences, \ @@ -335,7 +336,7 @@ class SequenceResult(object): self.cycle = fromTypedNode(result['cycle']) self.lane_number = fromTypedNode(result['lane_number']) self.read = fromTypedNode(result['read']) - if type(self.read) in types.StringTypes: + if isinstance(self.read, six.string_types): self.read = 1 self.library = result['library'] self.library_id = fromTypedNode(result['library_id']) diff --git a/htsworkflow/submission/daf.py b/htsworkflow/submission/daf.py index b2d7419..90993de 100644 --- a/htsworkflow/submission/daf.py +++ b/htsworkflow/submission/daf.py @@ -1,5 +1,6 @@ """Parse UCSC DAF File """ +import collections import logging import os from pprint import pformat @@ -265,7 +266,7 @@ class UCSCSubmission(object): else: self.model = get_model() - if hasattr(daf_file, 'next'): + if isinstance(daf_file, collections.Iterable): # its some kind of stream self.daf = daf_file.read() else: diff --git a/htsworkflow/submission/encoded.py b/htsworkflow/submission/encoded.py index 334a03f..5a7866f 100644 --- a/htsworkflow/submission/encoded.py +++ b/htsworkflow/submission/encoded.py @@ -11,7 +11,7 @@ import json import jsonschema import os import requests -import types +import six from six.moves.urllib.parse import urljoin, urlparse, urlunparse LOGGER = logging.getLogger(__name__) @@ -132,7 +132,7 @@ class ENCODED: and I needed a way to properly compute a the correct base URL. ''' # pretend strings aren't iterable - if type(obj) in types.StringTypes: + if isinstance(obj, six.string_types): return # recurse on container types @@ -223,7 +223,7 @@ class ENCODED: obj_type = obj.get('@type') if not obj_type: raise ValueError('None type') - if type(obj_type) in types.StringTypes: + if isinstance(obj_type, six.string_types): raise ValueError('@type should be a list, not a string') if not isinstance(obj_type, collections.Sequence): raise ValueError('@type is not a sequence') diff --git a/htsworkflow/util/rdfhelp.py b/htsworkflow/util/rdfhelp.py index ea1020c..ebd2d26 100644 --- a/htsworkflow/util/rdfhelp.py +++ b/htsworkflow/util/rdfhelp.py @@ -5,6 +5,7 @@ from __future__ import print_function import collections from datetime import datetime from glob import glob +import six from six.moves import urllib import logging import os @@ -78,7 +79,7 @@ def blankOrUri(value=None): node = None if value is None: node = RDF.Node() - elif type(value) in types.StringTypes: + elif isinstance(value, six.string_types): node = RDF.Node(uri_string=value) elif isinstance(value, RDF.Node): node = value @@ -89,16 +90,16 @@ def blankOrUri(value=None): def toTypedNode(value, language="en"): """Convert a python variable to a RDF Node with its closest xsd type """ - if type(value) == types.BooleanType: + if isinstance(value, bool): value_type = xsdNS['boolean'].uri if value: value = u'1' else: value = u'0' - elif type(value) in (types.IntType, types.LongType): + elif isinstance(value, int): value_type = xsdNS['decimal'].uri value = unicode(value) - elif type(value) == types.FloatType: + elif isinstance(value, float): value_type = xsdNS['float'].uri value = unicode(value) elif isinstance(value, datetime): @@ -252,7 +253,7 @@ def get_model(model_name=None, directory=None, use_contexts=True): def load_into_model(model, parser_name, path, ns=None): - if type(ns) in types.StringTypes: + if isinstance(ns, six.string_types): ns = RDF.Uri(ns) if isinstance(path, RDF.Node): @@ -304,7 +305,7 @@ def load_string_into_model(model, parser_name, data, ns=None): def fixup_namespace(ns): if ns is None: ns = RDF.Uri("http://localhost/") - elif type(ns) in types.StringTypes: + elif isinstance(ns, six.string_types): ns = RDF.Uri(ns) elif not(isinstance(ns, RDF.Uri)): errmsg = "Namespace should be string or uri not {0}" diff --git a/htsworkflow/util/test/test_rdfhelp.py b/htsworkflow/util/test/test_rdfhelp.py index 2ea38da..c71d3d7 100644 --- a/htsworkflow/util/test/test_rdfhelp.py +++ b/htsworkflow/util/test/test_rdfhelp.py @@ -3,8 +3,8 @@ from __future__ import print_function import os import types from unittest import TestCase - from datetime import datetime +import six from htsworkflow.util.rdfhelp import \ add_default_schemas, \ @@ -69,7 +69,7 @@ try: s = "Argh matey" node = toTypedNode(s) self.assertEqual(fromTypedNode(node), s) - self.assertEqual(type(fromTypedNode(node)), types.UnicodeType) + self.assertTrue(isinstance(fromTypedNode(node), six.text_type)) def test_blank_or_uri_blank(self): node = blankOrUri() @@ -90,8 +90,7 @@ try: def test_unicode_node_roundtrip(self): literal = u'\u5927' roundtrip = fromTypedNode(toTypedNode(literal)) - self.assertEqual(roundtrip, literal) - self.assertEqual(type(roundtrip), types.UnicodeType) + self.assertTrue(isinstance(roundtrip, six.text_type)) def test_datetime_no_microsecond(self): dateTimeType = xsdNS['dateTime'].uri diff --git a/samples/models.py b/samples/models.py index 94cd075..eaa433b 100644 --- a/samples/models.py +++ b/samples/models.py @@ -7,6 +7,7 @@ from django.contrib.auth.models import User, UserManager from django.core import urlresolvers from django.db.models.signals import pre_save, post_save from django.db import connection +import six logger = logging.getLogger(__name__) @@ -281,7 +282,7 @@ class Library(models.Model): sequences = self.index_sequences() if sequences is None: return "" - if type(sequences) in types.StringTypes: + if isinstance(sequences, six.string_types): return sequences multiplex_ids = sequences.keys() multiplex_ids.sort() -- 2.30.2