Python six.string_types() Examples

The following are 30 code examples of six.string_types(). 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 also want to check out all available functions/classes of the module six , or try the search function .
Example #1
Source File: NLP.py    From Financial-NLP with Apache License 2.0 7 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 #2
Source File: models.py    From figures with 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 #3
Source File: events.py    From python-esppy with 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 #4
Source File: project.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def save_xml(self, dest, mode='w', pretty=True, **kwargs):
        '''
        Save the project 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 #5
Source File: test_gquery.py    From grlc with 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 #6
Source File: generators.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def _gen_schema(self, copy_var=None):
        # generate schema if not provided by user
        valid_output_dict, _ = _to_valid_dict(self.output_dict)
        type_dict = {'str': 'string', 'float': 'double'}
        schema = ['id*:int64']
        for key, value in valid_output_dict.items():
            for jmp_type, esp_type in type_dict.items():
                value = value.replace(jmp_type, esp_type)
            schema.append(key + ':' + value)

        if copy_var is not None:
            if isinstance(copy_var, six.string_types):
                schema.append(copy_var)
            elif isinstance(copy_var, (tuple, list)):
                schema = schema + list(copy_var)
        return schema 
Example #7
Source File: compute.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def add_field_plugin(self, plugin, function):
        '''
        Add a field plugin

        Parameters
        ----------
        plugin : string
            The name of the plugin
        function : string or list-of-strings
            The name(s) of the function

        '''
        if isinstance(function, six.string_types):
            function = [function]
        for func in function:
            self.field_plugins.append(FieldPlugin(plugin, func)) 
Example #8
Source File: project.py    From python-esppy with Apache License 2.0 6 votes vote down vote up
def add_edge(self, source, targets):
        '''
        Add connector edges

        Parameters
        ----------
        source : string
           Name of the source
        targets : string or list-of-strings
           If a string, then it is a single target.  If a list, then
           it is a list of target names.

        '''
        if isinstance(targets, six.string_types):
            targets = [targets]
        self.edges.append(Edge(source, targets)) 
Example #9
Source File: utils.py    From MPContribs with 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 #10
Source File: plotting.py    From python-esppy with 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']) 
Example #11
Source File: json_serializers.py    From dustmaps with 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 #12
Source File: test_loaders.py    From grlc with 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 #13
Source File: utils.py    From Att-ChemdNER with 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 #14
Source File: template.py    From python-esppy with 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 #15
Source File: inspector.py    From goodtables-py with 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 #16
Source File: command.py    From Paradrop with 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 #17
Source File: pdos.py    From Paradrop with 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 #18
Source File: simple.py    From py2swagger with 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 #19
Source File: dockerfile.py    From Paradrop with 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 #20
Source File: generator_utils.py    From fine-lm with 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 #21
Source File: base.py    From django-anonymizer with 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 #22
Source File: header.py    From securityheaders with 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 #23
Source File: simplesam.py    From simplesam with 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 #24
Source File: models.py    From figures with 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 #25
Source File: globset.py    From pointnet-registration-framework with 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 #26
Source File: xml.py    From python-esppy with 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 #27
Source File: xdict.py    From python-esppy with 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 #28
Source File: contquery.py    From python-esppy with 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 #29
Source File: mas.py    From python-esppy with 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 #30
Source File: mas.py    From python-esppy with 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