Python six.string_types() Examples

The following are 30 code examples for showing how to use six.string_types(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module six , or try the search function .

Example 1
Project: figures   Author: appsembler   File: models.py    License: MIT License 7 votes vote down vote up
def get_prep_value(self, value):
        if value is self.Empty or value is None:
            return ''  # CharFields should use '' as their empty value, rather than None

        if isinstance(value, six.string_types):
            value = self.KEY_CLASS.from_string(value)

        assert isinstance(value, self.KEY_CLASS), "%s is not an instance of %s" % (value, self.KEY_CLASS)
        serialized_key = six.text_type(_strip_value(value))
        if serialized_key.endswith('\n'):
            # An opaque key object serialized to a string with a trailing newline.
            # Log the value - but do not modify it.
            log.warning(u'{}:{}:{}:get_prep_value: Invalid key: {}.'.format(
                self.model._meta.db_table,  # pylint: disable=protected-access
                self.name,
                self.KEY_CLASS.__name__,
                repr(serialized_key)
            ))
        return serialized_key 
Example 2
Project: Financial-NLP   Author: Coldog2333   File: NLP.py    License: Apache License 2.0 6 votes vote down vote up
def similarity_label(self, words, normalization=True):
        """
        you can calculate more than one word at the same time.
        """
        if self.model==None:
            raise Exception('no model.')
        if isinstance(words, string_types):
            words=[words]
        vectors=np.transpose(self.model.wv.__getitem__(words))
        if normalization:
            unit_vector=unitvec(vectors,ax=0) # 这样写比原来那样速度提升一倍
            #unit_vector=np.zeros((len(vectors),len(words)))
            #for i in range(len(words)):
            #    unit_vector[:,i]=matutils.unitvec(vectors[:,i])
            dists=np.dot(self.Label_vec_u, unit_vector)
        else:
            dists=np.dot(self.Label_vec, vectors)
        return dists 
Example 3
Project: Att-ChemdNER   Author: lingluodlut   File: utils.py    License: Apache License 2.0 6 votes vote down vote up
def get_from_module(identifier, module_params, module_name,
                    instantiate=False, kwargs=None):
    #{{{
    if isinstance(identifier, six.string_types):
        res = module_params.get(identifier)
        if not res:
            raise ValueError('Invalid ' + str(module_name) + ': ' +
                             str(identifier))
        if instantiate and not kwargs:
            return res()
        elif instantiate and kwargs:
            return res(**kwargs)
        else:
            return res
    elif isinstance(identifier, dict):
        name = identifier.pop('name')
        res = module_params.get(name)
        if res:
            return res(**identifier)
        else:
            raise ValueError('Invalid ' + str(module_name) + ': ' +
                             str(identifier))
    return identifier
#}}} 
Example 4
Project: MPContribs   Author: materialsproject   File: utils.py    License: MIT License 6 votes vote down vote up
def clean_value(value, unit="", convert_to_percent=False, max_dgts=3):
    """return clean value with maximum digits and optional unit and percent"""
    dgts = max_dgts
    value = str(value) if not isinstance(value, six.string_types) else value
    try:
        value = Decimal(value)
        dgts = len(value.as_tuple().digits)
        dgts = max_dgts if dgts > max_dgts else dgts
    except DecimalException:
        return value
    if convert_to_percent:
        value = Decimal(value) * Decimal("100")
        unit = "%"
    val = "{{:.{}g}}".format(dgts).format(value)
    if unit:
        val += " {}".format(unit)
    return val 
Example 5
Project: dustmaps   Author: gregreen   File: json_serializers.py    License: GNU General Public License v2.0 6 votes vote down vote up
def deserialize_dtype(d):
    """
    Deserializes a JSONified :obj:`numpy.dtype`.

    Args:
        d (:obj:`dict`): A dictionary representation of a :obj:`dtype` object.

    Returns:
        A :obj:`dtype` object.
    """
    if isinstance(d['descr'], six.string_types):
        return np.dtype(d['descr'])
    descr = []
    for col in d['descr']:
        col_descr = []
        for c in col:
            if isinstance(c, six.string_types):
                col_descr.append(str(c))
            elif type(c) is list:
                col_descr.append(tuple(c))
            else:
                col_descr.append(c)
        descr.append(tuple(col_descr))
    return np.dtype(descr) 
Example 6
Project: grlc   Author: CLARIAH   File: test_loaders.py    License: MIT License 6 votes vote down vote up
def test_getTextFor(self):
        files = self.loader.fetchFiles()

        # the contents of each file
        for fItem in files:
            text = self.loader.getTextFor(fItem)

            # Should be some text
            self.assertIsInstance(text, six.string_types, "Should be some text")

            # Should be non-empty for existing items
            self.assertGreater(len(text), 0, "Should be non-empty")

        # Should raise exception for invalid file items
        with self.assertRaises(Exception, msg="Should raise exception for invalid file items"):
            text = self.loader.getTextFor({}) 
Example 7
Project: grlc   Author: CLARIAH   File: test_loaders.py    License: MIT License 6 votes vote down vote up
def test_getTextFor(self):
        files = self.loader.fetchFiles()

        # the contents of each file
        for fItem in files:
            text = self.loader.getTextFor(fItem)

            # Should be some text
            self.assertIsInstance(text, six.string_types, "Should be some text")

            # Should be non-empty for existing items
            self.assertGreater(len(text), 0, "Should be non-empty")

        # Should raise exception for invalid file items
        with self.assertRaises(Exception, msg="Should raise exception for invalid file items"):
            text = self.loader.getTextFor({}) 
Example 8
Project: grlc   Author: CLARIAH   File: test_loaders.py    License: MIT License 6 votes vote down vote up
def test_getTextFor(self):
        files = self.loader.fetchFiles()

        # the contents of each file
        for fItem in files:
            text = self.loader.getTextFor(fItem)

            # Should be some text
            self.assertIsInstance(text, six.string_types, "Should be some text")

            # Should be non-empty for existing items
            self.assertGreater(len(text), 0, "Should be non-empty")

        # Should raise exception for invalid file items
        with self.assertRaises(Exception, msg="Should raise exception for invalid file items"):
            text = self.loader.getTextFor({}) 
Example 9
Project: grlc   Author: CLARIAH   File: test_gquery.py    License: MIT License 6 votes vote down vote up
def test_get_yaml_decorators(self):
        rq, _ = self.loader.getTextForName('test-sparql')

        decorators = gquery.get_yaml_decorators(rq)

        # Query always exist -- the rest must be present on the file.
        self.assertIn('query', decorators, 'Should have a query field')
        self.assertIn('summary', decorators, 'Should have a summary field')
        self.assertIn('pagination', decorators,
                      'Should have a pagination field')
        self.assertIn('enumerate', decorators, 'Should have a enumerate field')

        self.assertIsInstance(
            decorators['summary'], six.string_types, 'Summary should be text')
        self.assertIsInstance(
            decorators['pagination'], int, 'Pagination should be numeric')
        self.assertIsInstance(
            decorators['enumerate'], list, 'Enumerate should be a list') 
Example 10
Project: grlc   Author: CLARIAH   File: test_gquery.py    License: MIT License 6 votes vote down vote up
def test_get_json_decorators(self):
        rq, _ = self.loader.getTextForName('test-sparql-jsonconf')

        decorators = gquery.get_yaml_decorators(rq)

        # Query always exist -- the rest must be present on the file.
        self.assertIn('query', decorators, 'Should have a query field')
        self.assertIn('summary', decorators, 'Should have a summary field')
        self.assertIn('pagination', decorators,
                      'Should have a pagination field')
        self.assertIn('enumerate', decorators, 'Should have a enumerate field')

        self.assertIsInstance(
            decorators['summary'], six.string_types, 'Summary should be text')
        self.assertIsInstance(
            decorators['pagination'], int, 'Pagination should be numeric')
        self.assertIsInstance(
            decorators['enumerate'], list, 'Enumerate should be a list') 
Example 11
Project: VSE-C   Author: ExplorerFreda   File: saliency_visualization.py    License: MIT License 6 votes vote down vote up
def _prepare_batch(sents, projector):
    if isinstance(sents, six.string_types):
        sents = [sents]

    sents = [np.array(projector(s)) for s in sents]
    lengths = [len(s) for s in sents]
    sents = _pad_sequences(sents, 0, max(lengths))

    idx = np.array(sorted(range(len(lengths)), key=lambda x: lengths[x], reverse=True))
    inv = np.array(sorted(range(len(lengths)), key=lambda x: idx[x]))
    sents = sents[idx]
    lengths = np.array(lengths)[idx].tolist()

    sents = as_variable(sents)
    if torch.cuda.is_available():
        sents = sents.cuda()
    return sents, lengths, idx, inv 
Example 12
Project: goodtables-py   Author: frictionlessdata   File: inspector.py    License: MIT License 6 votes vote down vote up
def __get_source_preset(self, source, preset=None):
        if preset is None:
            preset = 'table'
            if isinstance(source, six.string_types):
                source_path = source.lower()
                if source_path.endswith('datapackage.json') or source_path.endswith(
                    '.zip'
                ):
                    preset = 'datapackage'
            elif isinstance(source, dict):
                if 'resources' in source:
                    preset = 'datapackage'
            elif isinstance(source, list):
                if source and isinstance(source[0], dict) and 'source' in source[0]:
                    preset = 'nested'

        return preset 
Example 13
Project: py2swagger   Author: Arello-Mobile   File: simple.py    License: MIT License 6 votes vote down vote up
def run(self, arguments, endpoints=None, *args, **kwargs):
        """
        Return part of swagger object. This part contains "paths", "definitions" and "securityDefinitions"
        :return: dict
        """
        if endpoints is None:
            raise Py2SwaggerPluginException('Configuration is missed. Please add PLUGIN_SETTINGS[\'endpoints\'] to your '
                                            'configuration file.')

        for path, method, callback in endpoints:
            if isinstance(callback, six.string_types):
                callback = load_class(callback)
            self._introspect(path, method, callback)

        return {
            'paths': self._paths,
            'securityDefinitions': self._security_definitions
        } 
Example 14
Project: Paradrop   Author: ParadropLabs   File: command.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, command, parent=None, ignoreFailure=False):
        """
        Construct command object.

        command: array of strings specifying command and arguments
                 Passing a single string is also supported if there are
                 no spaces within arguments (only between them).
        parent: parent object (should be ConfigObject subclass)
        """
        self.parent = parent
        self.ignoreFailure = ignoreFailure

        if type(command) == list:
            self.command = [str(v) for v in command]
        elif isinstance(command, six.string_types):
            self.command = command.split()

        # These are set after execute completes.
        self.pid = None
        self.result = None 
Example 15
Project: Paradrop   Author: ParadropLabs   File: pdos.py    License: Apache License 2.0 6 votes vote down vote up
def writeFile(filename, line, mode="a"):
    """Adds the following cfg (either str or list(str)) to this Chute's current
        config file (just stored locally, not written to file."""
    try:
        if isinstance(line, list):
            data = "\n".join(line) + "\n"
        elif isinstance(line, six.string_types):
            data = "%s\n" % line
        else:
            out.err("Bad line provided for %s\n" % filename)
            return
        fd = open(filename, mode)
        fd.write(data)
        fd.flush()
        fd.close()

    except Exception as e:
        out.err('Unable to write file: %s\n' % (str(e))) 
Example 16
Project: Paradrop   Author: ParadropLabs   File: dockerfile.py    License: Apache License 2.0 6 votes vote down vote up
def isValid(self):
        """
        Check if configuration is valid.

        Returns a tuple (True/False, None or str).
        """
        # Check required fields.
        for field in Dockerfile.requiredFields:
            if getattr(self.service, field, None) is None:
                return (False, "Missing required field {}".format(field))

        command = self.service.command
        if not isinstance(command, six.string_types + (list, )):
            return (False, "Command must be either a string or list of strings")

        packages = self.service.build.get("packages", [])
        if not isinstance(packages, list):
            return (False, "Packages must be specified as a list")
        for pkg in packages:
            if re.search(r"\s", pkg):
                return (False, "Package name ({}) contains whitespace".format(pkg))

        return (True, None) 
Example 17
Project: fine-lm   Author: akzaidi   File: generator_utils.py    License: MIT License 6 votes vote down vote up
def to_example(dictionary):
  """Helper: build tf.Example from (string -> int/float/str list) dictionary."""
  features = {}
  for (k, v) in six.iteritems(dictionary):
    if not v:
      raise ValueError("Empty generated field: %s" % str((k, v)))
    if isinstance(v[0], six.integer_types):
      features[k] = tf.train.Feature(int64_list=tf.train.Int64List(value=v))
    elif isinstance(v[0], float):
      features[k] = tf.train.Feature(float_list=tf.train.FloatList(value=v))
    elif isinstance(v[0], six.string_types):
      if not six.PY2:  # Convert in python 3.
        v = [bytes(x, "utf-8") for x in v]
      features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
    elif isinstance(v[0], bytes):
      features[k] = tf.train.Feature(bytes_list=tf.train.BytesList(value=v))
    else:
      raise ValueError("Value for %s is not a recognized type; v: %s type: %s" %
                       (k, str(v[0]), str(type(v[0]))))
  return tf.train.Example(features=tf.train.Features(feature=features)) 
Example 18
Project: django-anonymizer   Author: BetterWorks   File: base.py    License: MIT License 6 votes vote down vote up
def __init__(self):
        super(Anonymizer, self).__init__()

        assert self.attributes is not None, '"attributes" attribute must be set'
        assert self.model is not None, '"model" attribute must be set'

        self.replacers = []
        for attname, replacer in self.attributes:
            if replacer == 'SKIP':
                continue

            if isinstance(replacer, six.string_types):
                # 'email' is shortcut for: replacers.email
                replacer = getattr(replacers, replacer)
            elif not callable(replacer):
                raise Exception("Expected callable or string to be passed, got %r." % replacer)

            field = self.model._meta.get_field(attname)
            self.replacers.append((attname, field, replacer)) 
Example 19
Project: securityheaders   Author: koenbuyens   File: header.py    License: Apache License 2.0 6 votes vote down vote up
def parse(self, unparsed_string):
        result = {}
        if unparsed_string:
            separator = self.directiveclass.valueseperator()
            directiveTokens = unparsed_string.split(self.directiveclass.directiveseperator())
            for directiveToken in directiveTokens:
                directiveToken.strip();

                """ Split directive tokens into directive name and directive values. """
                if separator and separator in directiveToken:
                    directiveParts = directiveToken.split(separator)
                else:
                    directiveParts = directiveToken.split()
                if isinstance(directiveParts, list) and not isinstance(directiveParts, six.string_types) and len(directiveParts) > 0:
                    directiveName = directiveParts[0].lower().strip()
                    try:
                        directive = self.directiveclass(directiveName)
                    except ValueError:
                        directive = directiveName #koen: parser erorr, unknown directive; should be a finding
                    result[directive] = []
                    for directiveValue in directiveParts[1:]:
                        result[directive].append(self.normalizeDirectiveValue(directiveValue))
        return result; 
Example 20
Project: simplesam   Author: mdshw5   File: simplesam.py    License: MIT License 6 votes vote down vote up
def encode_tag(tag, data):
    """ Write a SAM tag in the format ``TAG:TYPE:data``. Infers the data type
    from the Python object type.

    >>> encode_tag('YM', '#""9O"1@!J')
    'YM:Z:#""9O"1@!J'
    """
    if isinstance(data, string_types):
        data_type = 'Z'
    elif isinstance(data, int):
        data_type = 'i'
    elif isinstance(data, float):
        data_type = 'f'
    else:
        raise NotImplementedError("Data {0} cannot be encoded as string, integer, or float tag.".format(data))
    value = ':'.join((tag, data_type, str(data)))
    return value 
Example 21
Project: figures   Author: appsembler   File: models.py    License: MIT License 6 votes vote down vote up
def to_python(self, value):
        if value is self.Empty or value is None:
            return None

        error_message = "%s is not an instance of six.string_types or %s" % (value, self.KEY_CLASS)
        assert isinstance(value, six.string_types + (self.KEY_CLASS,)), error_message
        if value == '':
            # handle empty string for models being created w/o fields populated
            return None

        if isinstance(value, six.string_types):
            if value.endswith('\n'):
                # An opaque key with a trailing newline has leaked into the DB.
                # Log and strip the value.
                log.warning(u'{}:{}:{}:to_python: Invalid key: {}. Removing trailing newline.'.format(
                    self.model._meta.db_table,  # pylint: disable=protected-access
                    self.name,
                    self.KEY_CLASS.__name__,
                    repr(value)
                ))
                value = value.rstrip()
            return self.KEY_CLASS.from_string(value)
        else:
            return value 
Example 22
Project: pointnet-registration-framework   Author: vinits5   File: globset.py    License: MIT License 6 votes vote down vote up
def __init__(self, rootdir, pattern, fileloader, transform=None, classinfo=None):
        super().__init__()

        if isinstance(pattern, six.string_types):
            pattern = [pattern]

        if classinfo is not None:
            classes, class_to_idx = classinfo
        else:
            classes, class_to_idx = find_classes(rootdir)

        samples = glob_dataset(rootdir, class_to_idx, pattern)
        if not samples:
            raise RuntimeError("Empty: rootdir={}, pattern(s)={}".format(rootdir, pattern))

        self.rootdir = rootdir
        self.pattern = pattern
        self.fileloader = fileloader
        self.transform = transform

        self.classes = classes
        self.class_to_idx = class_to_idx
        self.samples = samples 
Example 23
Project: python-esppy   Author: sassoftware   File: contquery.py    License: Apache License 2.0 6 votes vote down vote up
def save_xml(self, dest, mode='w', pretty=True, **kwargs):
        '''
        Save the continuous query XML to a file

        Parameters
        ----------
        dest : string or file-like
            The destination of the XML content
        mode : string, optional
            The write mode for the output file (only used if `dest` is a string)
        pretty : boolean, optional
            Should the XML include whitespace for readability?

        '''
        if isinstance(dest, six.string_types):
            with open(dest, mode=mode, **kwargs) as output:
                output.write(self.to_xml(pretty=pretty))
        else:
            dest.write(self.to_xml(pretty=pretty)) 
Example 24
Project: python-esppy   Author: sassoftware   File: mas.py    License: Apache License 2.0 6 votes vote down vote up
def from_element(cls, data, session=None):
        if isinstance(data, six.string_types):
            data = xml.from_xml(data)

        out = cls(data.attrib['member'], data.attrib['SHAkey'],
                  data.attrib['type'])

        for item in data.findall('./description'):
            out.description = item.text

        for item in data.findall('./code'):
            out.code = item.text
        for item in data.findall('./code-file'):
            out.code_file = item.text

        return out 
Example 25
Project: python-esppy   Author: sassoftware   File: mas.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, language, module, func_names, mas_store=None,
                 mas_store_version=None, description=None, code_file=None,
                 code=None):
        ESPObject.__init__(self)
        self.module = module or gen_name('mas_')
        self.language = language
        if isinstance(func_names, six.string_types):
            func_names = re.split(r'\s*,\s*', func_names.strip())
        if isinstance(func_names, six.string_types):
            self.func_names = re.split(r'\s*,\s', func_names.strip())
        else:
            self.func_names = list(func_names)
        self.mas_store = mas_store
        self.mas_store_version = mas_store_version
        self.description = description
        self.code_file = code_file
        self.code = code
        self.module_members = []
        self.project = None 
Example 26
Project: python-esppy   Author: sassoftware   File: xdict.py    License: Apache License 2.0 6 votes vote down vote up
def _is_compound_key(key, types=six.string_types + (six.text_type, six.binary_type)):
    '''
    Check for a compound key name

    Parameters
    ----------
    key : string
        The key name to check
    types : list of types, optional
        The types of object to check

    Returns
    -------
    True
        If the key is compound (i.e., contains a '.')
    False
        If the key is not compound

    '''
    return isinstance(key, types) and '.' in key 
Example 27
Project: python-esppy   Author: sassoftware   File: xml.py    License: Apache License 2.0 6 votes vote down vote up
def ensure_element(data):
    '''
    Ensure the given object is an ElementTree.Element

    Parameters
    ----------
    data : string or Element

    Returns
    -------
    :class:`ElementTree.Element`

    '''
    if isinstance(data, six.string_types):
        return from_xml(data)
    return data 
Example 28
Project: python-esppy   Author: sassoftware   File: events.py    License: Apache License 2.0 6 votes vote down vote up
def get_schema(obj, window):
    ''' Retrieve the schema for the specified window '''
    try:
        get_window_class    # noqa: F821
    except:
        from ..windows import get_window_class

    if isinstance(window, six.string_types):
        path = window.replace('.', '/')
    else:
        if getattr(window, 'schema') and window.schema.fields:
            return window.schema.copy(deep=True)
        path = window.fullname.replace('.', '/')

    res = obj._get(urllib.parse.urljoin(obj.base_url, 'windows/%s' % path),
                   params=dict(schema='true'))

    for item in res.findall('./*'):
        try:
            wcls = get_window_class(item.tag)
        except KeyError:
            raise TypeError('Unknown window type: %s' % item.tag)
        return wcls.from_xml(item, session=obj.session).schema 
Example 29
Project: python-esppy   Author: sassoftware   File: template.py    License: Apache License 2.0 6 votes vote down vote up
def save_xml(self, dest, mode='w', pretty=True, **kwargs):
        '''
        Save the template XML to a file

        Parameters
        ----------
        dest : string or file-like
            The destination of the XML content
        mode : string, optional
            The write mode for the output file (only used if `dest` is a string)
        pretty : boolean, optional
            Should the XML include whitespace for readability or not, default value is True

        '''
        if isinstance(dest, six.string_types):
            with open(dest, mode=mode, **kwargs) as output:
                output.write(self.to_xml(pretty=pretty))
        else:
            dest.write(self.to_xml(pretty=pretty)) 
Example 30
Project: python-esppy   Author: sassoftware   File: plotting.py    License: Apache License 2.0 6 votes vote down vote up
def _alias_colors(self, dataset, color):

        def autoify(value):
            if isinstance(value, (tuple, list)):
                return ['auto(%s)' % x for x in value]
            return 'auto(%s)' % value

        if not dataset.get('borderColor') and not dataset.get('backgroundColor'):
            dataset['borderColor'] = autoify(color)
            dataset['backgroundColor'] = autoify(color)

        elif dataset.get('borderColor') and not dataset.get('backgroundColor'):
            dataset['backgroundColor'] = autoify(dataset['borderColor'])

        elif dataset.get('backgroundColor') and not dataset.get('borderColor'):
            if isinstance(dataset['backgroundColor'], six.string_types):
                if dataset['backgroundColor'].startswith('url'):
                    dataset['borderColor'] = autoify(color)
                else:
                    dataset['borderColor'] = autoify(dataset['backgroundColor'])