Python six.text_type() Examples

The following are 30 code examples for showing how to use six.text_type(). 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: oslo.i18n   Author: openstack   File: test_message.py    License: Apache License 2.0 6 votes vote down vote up
def test_translate_message_with_param_from_unicoded_obj(self,
                                                            mock_translation):
        message_with_params = 'A message: %s'
        es_translation = 'A message in Spanish: %s'
        param = 'A Message param'

        translations = {message_with_params: es_translation}
        translator = fakes.FakeTranslations.translator({'es': translations})
        mock_translation.side_effect = translator

        msg = _message.Message(message_with_params)
        msg = msg % param

        default_translation = message_with_params % param
        expected_translation = es_translation % param

        obj = utils.SomeObject(msg)
        unicoded_obj = six.text_type(obj)

        self.assertEqual(expected_translation, unicoded_obj.translation('es'))
        self.assertEqual(default_translation, unicoded_obj.translation('XX')) 
Example 3
Project: multibootusb   Author: mbusb   File: _util.py    License: GNU General Public License v2.0 6 votes vote down vote up
def property_value_to_bytes(value):
    """
    Return a byte string, which represents the given ``value`` in a way
    suitable as raw value of an udev property.

    If ``value`` is a boolean object, it is converted to ``'1'`` or ``'0'``,
    depending on whether ``value`` is ``True`` or ``False``.  If ``value`` is a
    byte string already, it is returned unchanged.  Anything else is simply
    converted to a unicode string, and then passed to
    :func:`ensure_byte_string`.
    """
    # udev represents boolean values as 1 or 0, therefore an explicit
    # conversion to int is required for boolean values
    if isinstance(value, bool):
        value = int(value)
    if isinstance(value, bytes):
        return value
    else:
        return ensure_byte_string(six.text_type(value)) 
Example 4
Project: fine-lm   Author: akzaidi   File: text_encoder.py    License: MIT License 6 votes vote down vote up
def _escape_token(token, alphabet):
  """Escape away underscores and OOV characters and append '_'.

  This allows the token to be expressed as the concatenation of a list
  of subtokens from the vocabulary. The underscore acts as a sentinel
  which allows us to invertibly concatenate multiple such lists.

  Args:
    token: A unicode string to be escaped.
    alphabet: A set of all characters in the vocabulary's alphabet.

  Returns:
    escaped_token: An escaped unicode string.

  Raises:
    ValueError: If the provided token is not unicode.
  """
  if not isinstance(token, six.text_type):
    raise ValueError("Expected string type for token, got %s" % type(token))

  token = token.replace(u"\\", u"\\\\").replace(u"_", u"\\u")
  ret = [c if c in alphabet and c != u"\n" else r"\%d;" % ord(c) for c in token]
  return u"".join(ret) + "_" 
Example 5
Project: django-payfast   Author: PiDelport   File: forms.py    License: MIT License 6 votes vote down vote up
def is_payfast_ip_address(ip_address_str):
    """
    Return True if ip_address_str matches one of PayFast's server IP addresses.

    Setting: `PAYFAST_IP_ADDRESSES`

    :type ip_address_str: str
    :rtype: bool
    """
    # TODO: Django system check for validity?
    payfast_ip_addresses = getattr(settings, 'PAYFAST_IP_ADDRESSES',
                                   conf.DEFAULT_PAYFAST_IP_ADDRESSES)

    if sys.version_info < (3,):
        # Python 2 usability: Coerce str to unicode, to avoid very common TypeErrors.
        # (On Python 3, this should generally not happen:
        #  let unexpected bytes values fail as expected.)
        ip_address_str = unicode(ip_address_str)  # noqa: F821
        payfast_ip_addresses = [unicode(address) for address in payfast_ip_addresses]  # noqa: F821

    return any(ip_address(ip_address_str) in ip_network(payfast_address)
               for payfast_address in payfast_ip_addresses) 
Example 6
Project: django-payfast   Author: PiDelport   File: api.py    License: MIT License 6 votes vote down vote up
def _prepare_signable_fields(
        valid_field_order,  # type: Sequence[str]
        data_fields,  # type: Mapping[str, str]
):  # type: (...) -> SignableFields
    """
    Prepare PayFast submission variables for signing, using the given field order.

    :raise ValueError:
        If `data_fields` contains any unexpected field names not in `valid_field_order`.
    """
    present_fields = (set(data_fields.keys()) if sys.version_info < (3,) else
                      data_fields.keys())
    extra_fields = present_fields - set(valid_field_order)
    if extra_fields:
        raise ValueError('Data contains unexpected fields: {!r}'.format(extra_fields))

    return [
        (name, data_fields[name]) for name in valid_field_order
        if name in data_fields
    ] 
Example 7
Project: gnocchi   Author: gnocchixyz   File: resource_type.py    License: Apache License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(ResourceTypeSchemaManager, self).__init__(*args, **kwargs)
        type_schemas = tuple([ext.plugin.meta_schema()
                              for ext in self.extensions])
        self._schema = voluptuous.Schema({
            "name": six.text_type,
            voluptuous.Required("attributes", default={}): {
                six.text_type: voluptuous.Any(*tuple(type_schemas))
            }
        })

        type_schemas = tuple([ext.plugin.meta_schema(for_update=True)
                              for ext in self.extensions])
        self._schema_for_update = voluptuous.Schema({
            "name": six.text_type,
            voluptuous.Required("attributes", default={}): {
                six.text_type: voluptuous.Any(*tuple(type_schemas))
            }
        }) 
Example 8
Project: gnocchi   Author: gnocchixyz   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def patch(self):
        ap = pecan.request.indexer.get_archive_policy(self.archive_policy)
        if not ap:
            abort(404, six.text_type(
                indexer.NoSuchArchivePolicy(self.archive_policy)))
        enforce("update archive policy", ap)

        body = deserialize_and_validate(voluptuous.Schema({
            voluptuous.Required("definition"): ArchivePolicyDefinitionSchema,
        }))
        # Validate the data
        try:
            ap_items = [archive_policy.ArchivePolicyItem(**item) for item in
                        body['definition']]
        except ValueError as e:
            abort(400, six.text_type(e))

        try:
            return pecan.request.indexer.update_archive_policy(
                self.archive_policy, ap_items)
        except indexer.UnsupportedArchivePolicyChange as e:
            abort(400, six.text_type(e)) 
Example 9
Project: gnocchi   Author: gnocchixyz   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def post(self):
        enforce("create archive policy rule", {})
        ArchivePolicyRuleSchema = voluptuous.Schema({
            voluptuous.Required("name"): six.text_type,
            voluptuous.Required("metric_pattern"): six.text_type,
            voluptuous.Required("archive_policy_name"): six.text_type,
            })

        body = deserialize_and_validate(ArchivePolicyRuleSchema)
        enforce("create archive policy rule", body)
        try:
            ap = pecan.request.indexer.create_archive_policy_rule(
                body['name'], body['metric_pattern'],
                body['archive_policy_name']
            )
        except indexer.ArchivePolicyRuleAlreadyExists as e:
            abort(409, six.text_type(e))
        except indexer.NoSuchArchivePolicy as e:
            abort(400, e)

        location = "/archive_policy_rule/" + ap.name
        set_resp_location_hdr(location)
        pecan.response.status = 201
        return ap 
Example 10
Project: gnocchi   Author: gnocchixyz   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def post(self):
        creator = pecan.request.auth_helper.get_current_user(
            pecan.request)
        body = deserialize_and_validate(self.MetricSchema)

        resource_id = body.get('resource_id')
        if resource_id is not None:
            resource_id = resource_id[1]

        try:
            m = pecan.request.indexer.create_metric(
                uuid.uuid4(),
                creator,
                resource_id=resource_id,
                name=body.get('name'),
                unit=body.get('unit'),
                archive_policy_name=body['archive_policy_name'])
        except indexer.NoSuchArchivePolicy as e:
            abort(400, six.text_type(e))
        except indexer.NamedMetricAlreadyExists as e:
            abort(400, e)
        set_resp_location_hdr("/metric/" + str(m.id))
        pecan.response.status = 201
        return m 
Example 11
Project: gnocchi   Author: gnocchixyz   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def _lookup(self, name, *remainder):
        m = pecan.request.indexer.list_metrics(
            details=True,
            attribute_filter={"and": [
                {"=": {"name": name}},
                {"=": {"resource_id": self.resource_id}},
            ]})
        if m:
            return MetricController(m[0]), remainder

        resource = pecan.request.indexer.get_resource(self.resource_type,
                                                      self.resource_id)
        if resource:
            abort(404, six.text_type(indexer.NoSuchMetric(name)))
        else:
            abort(404, six.text_type(indexer.NoSuchResource(self.resource_id))) 
Example 12
Project: gnocchi   Author: gnocchixyz   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def post(self):
        resource = pecan.request.indexer.get_resource(
            self.resource_type, self.resource_id)
        if not resource:
            abort(404, six.text_type(indexer.NoSuchResource(self.resource_id)))
        enforce("update resource", resource)
        metrics = deserialize_and_validate(MetricsSchema)
        try:
            r = pecan.request.indexer.update_resource(
                self.resource_type,
                self.resource_id,
                metrics=metrics,
                append_metrics=True,
                create_revision=False)
        except (indexer.NoSuchMetric,
                indexer.NoSuchArchivePolicy,
                ValueError) as e:
            abort(400, six.text_type(e))
        except indexer.NamedMetricAlreadyExists as e:
            abort(409, six.text_type(e))
        except indexer.NoSuchResource as e:
            abort(404, six.text_type(e))

        return r.metrics 
Example 13
Project: gnocchi   Author: gnocchixyz   File: api.py    License: Apache License 2.0 6 votes vote down vote up
def post(self):
        schema = pecan.request.indexer.get_resource_type_schema()
        body = deserialize_and_validate(schema)
        body["state"] = "creating"

        try:
            rt = schema.resource_type_from_dict(**body)
        except resource_type.InvalidResourceAttribute as e:
            abort(400, "Invalid input: %s" % e)

        enforce("create resource type", body)
        try:
            rt = pecan.request.indexer.create_resource_type(rt)
        except indexer.ResourceTypeAlreadyExists as e:
            abort(409, six.text_type(e))
        set_resp_location_hdr("/resource_type/" + rt.name)
        pecan.response.status = 201
        return rt 
Example 14
Project: gnocchi   Author: gnocchixyz   File: test_rest.py    License: Apache License 2.0 6 votes vote down vote up
def test_delete_resource_with_metrics(self):
        metric = self.app.post_json(
            "/v1/metric",
            params={'archive_policy_name': "high"})
        metric_id = json.loads(metric.text)['id']
        metric_name = six.text_type(uuid.uuid4())
        self.attributes['metrics'] = {metric_name: metric_id}
        self.app.get("/v1/metric/" + metric_id,
                     status=200)
        self.app.post_json("/v1/resource/" + self.resource_type,
                           params=self.attributes)
        self.app.get("/v1/resource/" + self.resource_type + "/"
                     + self.attributes['id'],
                     status=200)
        self.app.delete("/v1/resource/" + self.resource_type + "/"
                        + self.attributes['id'],
                        status=204)
        self.app.get("/v1/resource/" + self.resource_type + "/"
                     + self.attributes['id'],
                     status=404)
        self.app.get("/v1/metric/" + metric_id,
                     status=404) 
Example 15
Project: oslo.i18n   Author: openstack   File: _message.py    License: Apache License 2.0 5 votes vote down vote up
def __mod__(self, other):
        # When we mod a Message we want the actual operation to be performed
        # by the base class (i.e. unicode()), the only thing  we do here is
        # save the original msgid and the parameters in case of a translation
        params = self._sanitize_mod_params(other)
        unicode_mod = self._safe_translate(six.text_type(self), params)
        modded = Message(self.msgid,
                         msgtext=unicode_mod,
                         params=params,
                         domain=self.domain)
        return modded 
Example 16
Project: oslo.i18n   Author: openstack   File: _message.py    License: Apache License 2.0 5 votes vote down vote up
def _copy_param(self, param):
        try:
            return copy.deepcopy(param)
        except Exception:
            # Fallback to casting to unicode this will handle the
            # python code-like objects that can't be deep-copied
            return six.text_type(param) 
Example 17
Project: oslo.i18n   Author: openstack   File: _translate.py    License: Apache License 2.0 5 votes vote down vote up
def translate(obj, desired_locale=None):
    """Gets the translated unicode representation of the given object.

    If the object is not translatable it is returned as-is.

    If the desired_locale argument is None the object is translated to
    the system locale.

    :param obj: the object to translate
    :param desired_locale: the locale to translate the message to, if None the
                           default system locale will be used
    :returns: the translated object in unicode, or the original object if
              it could not be translated

    """
    from oslo_i18n import _message  # avoid circular dependency at module level
    message = obj
    if not isinstance(message, _message.Message):
        # If the object to translate is not already translatable,
        # let's first get its unicode representation
        message = six.text_type(obj)
    if isinstance(message, _message.Message):
        # Even after unicoding() we still need to check if we are
        # running with translatable unicode before translating
        return message.translation(desired_locale)
    return obj 
Example 18
Project: oslo.i18n   Author: openstack   File: fixture.py    License: Apache License 2.0 5 votes vote down vote up
def immediate(self, msg):
        """Return a string as though it had been translated immediately.

        :param msg: Input message string. May optionally include
                    positional or named string interpolation markers.
        :type msg: str or unicode

        """
        return six.text_type(msg) 
Example 19
Project: oslo.i18n   Author: openstack   File: utils.py    License: Apache License 2.0 5 votes vote down vote up
def __unicode__(self):
            return six.text_type(self.value) 
Example 20
Project: oslo.i18n   Author: openstack   File: test_fixture.py    License: Apache License 2.0 5 votes vote down vote up
def test_immediate(self):
        msg = self.trans_fixture.immediate('this is a lazy message')
        self.assertNotIsInstance(msg, _message.Message)
        self.assertIsInstance(msg, six.text_type)
        self.assertEqual(u'this is a lazy message', msg) 
Example 21
Project: oslo.i18n   Author: openstack   File: test_message.py    License: Apache License 2.0 5 votes vote down vote up
def test_message_is_unicode(self):
        message = _message.Message('some %s') % 'message'
        self.assertIsInstance(message, six.text_type) 
Example 22
Project: oslo.i18n   Author: openstack   File: test_message.py    License: Apache License 2.0 5 votes vote down vote up
def test_translation_returns_unicode(self):
        message = _message.Message('some %s') % 'message'
        self.assertIsInstance(message.translation(), six.text_type) 
Example 23
Project: oslo.i18n   Author: openstack   File: test_message.py    License: Apache License 2.0 5 votes vote down vote up
def test_translate_message_from_unicoded_object(self, mock_translation):
        en_message = 'A message in the default locale'
        es_translation = 'A message in Spanish'
        message = _message.Message(en_message)
        es_translations = {en_message: es_translation}
        translations_map = {'es': es_translations}
        translator = fakes.FakeTranslations.translator(translations_map)
        mock_translation.side_effect = translator

        # Here we are not testing the Message object directly but the result
        # of unicoding() an object whose unicode representation is a Message
        obj = utils.SomeObject(message)
        unicoded_obj = six.text_type(obj)

        self.assertEqual(es_translation, unicoded_obj.translation('es')) 
Example 24
Project: neural-fingerprinting   Author: StephanZheng   File: work_data.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def update_work_as_completed(self, worker_id, work_id, other_values=None,
                               error=None):
    """Updates work piece in datastore as completed.

    Args:
      worker_id: ID of the worker which did the work
      work_id: ID of the work which was done
      other_values: dictionary with additonal values which should be saved
        with the work piece
      error: if not None then error occurred during computation of the work
        piece. In such case work will be marked as completed with error.

    Returns:
      whether work was successfully updated
    """
    client = self._datastore_client
    try:
      with client.transaction() as transaction:
        work_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id,
                              KIND_WORK, work_id)
        work_entity = client.get(work_key, transaction=transaction)
        if work_entity['claimed_worker_id'] != worker_id:
          return False
        work_entity['is_completed'] = True
        if other_values:
          work_entity.update(other_values)
        if error:
          work_entity['error'] = text_type(error)
        transaction.put(work_entity)
    except:
      return False
    return True 
Example 25
Project: multibootusb   Author: mbusb   File: _util.py    License: GNU General Public License v2.0 5 votes vote down vote up
def ensure_unicode_string(value):
    """
    Return the given ``value`` as unicode string.

    If the given ``value`` is not a unicode string, but a byte string, it is
    decoded with the filesystem encoding (as in
    :func:`sys.getfilesystemencoding()`).
    """
    if not isinstance(value, six.text_type):
        value = value.decode(sys.getfilesystemencoding())
    return value 
Example 26
Project: multibootusb   Author: mbusb   File: _qt_base.py    License: GNU General Public License v2.0 5 votes vote down vote up
def make_monitor_observer(qobject, signal, socket_notifier):
        """Generates an observer for device events integrating into the
        PyQt{4,5} mainloop.

        This class inherits :class:`~PyQt{4,5}.QtCore.QObject` to turn device
        events into Qt signals:

        >>> from pyudev import Context, Monitor
        >>> from pyudev.pyqt4 import MonitorObserver
        >>> context = Context()
        >>> monitor = Monitor.from_netlink(context)
        >>> monitor.filter_by(subsystem='input')
        >>> observer = MonitorObserver(monitor)
        >>> def device_event(device):
        ...     print('event {0} on device {1}'.format(device.action, device))
        >>> observer.deviceEvent.connect(device_event)
        >>> monitor.start()

        This class is a child of :class:`~{PyQt{4,5}, PySide}.QtCore.QObject`.

        """
        return type(
           str("QUDevMonitorObserver"),
           (qobject, QUDevMonitorObserverMixin),
           {
              str("__init__") : make_init(qobject, socket_notifier),
              #: emitted upon arbitrary device events
              str("deviceEvent") : signal(six.text_type, Device),
              #: emitted if a device was added
              str("deviceAdded") : signal(Device),
              #: emitted if a device was removed
              str("deviceRemoved") : signal(Device),
              #: emitted if a device was changed
              str("deviceChanged") : signal(Device),
              #: emitted if a device was moved
              str("deviceMoved") : signal(Device)
           }
        ) 
Example 27
Project: goodtables-py   Author: frictionlessdata   File: duplicate_row.py    License: MIT License 5 votes vote down vote up
def check_row(self, cells):
        errors = []

        # Get pointer
        try:
            values = list(six.text_type(cell.get('value')) for cell in cells)
            # https://github.com/frictionlessdata/goodtables-py/issues/329
            pointer = hash(json.dumps(list(six.text_type(len(value)) + value for value in values)))
            references = self.__row_index.setdefault(pointer, [])
        except TypeError:
            pointer = None

        # Found pointer
        if pointer:

            row_number = cells[0].get('row-number')

            # Add error
            if references:
                message_substitutions = {
                    'row_numbers': ', '.join(map(str, references)),
                }
                error = Error(
                    'duplicate-row',
                    row_number=row_number,
                    message_substitutions=message_substitutions
                )
                errors.append(error)

            # Clear cells
            if references:
                del cells[:]

            # Update references
            references.append(row_number)

        return errors 
Example 28
Project: smbprotocol   Author: jborean93   File: test_structure.py    License: MIT License 5 votes vote down vote up
def test_set_bytes(self):
        field = self.StructureTest()['field']
        field.set_value(self.STRING_VALUE.encode('utf-8'))
        expected = self.STRING_VALUE
        actual = field.get_value()
        assert isinstance(field.value, six.text_type)
        assert actual == expected 
Example 29
Project: smbprotocol   Author: jborean93   File: test_structure.py    License: MIT License 5 votes vote down vote up
def test_set_none(self):
        field = self.StructureTest()['field']
        field.set_value(None)
        expected = u""
        actual = field.get_value()
        assert isinstance(field.value, six.text_type)
        assert actual == expected 
Example 30
Project: smbprotocol   Author: jborean93   File: _text.py    License: MIT License 5 votes vote down vote up
def to_text(value, encoding='utf-8'):
    """
    Makes sure the value is decoded as a text string.

    :param value: The Python byte string value to decode.
    :param encoding: The encoding to use.
    :return: The text/unicode string that was decoded.
    """
    if isinstance(value, six.text_type):
        return value
    return value.decode(encoding)


# On Python 2 a native string is a byte string and on Python 3 a native string is a text (unicode) string.