SPPAS 4.20

Module sppas.config

Class sppasUnicode

Description

Make a string as unicode and operates on it.

Constructor

Create a sppasUnicode instance.

Parameters
  • entry: (str or unicode or bytes in python 2)
View Source
def __init__(self, entry):
    """Create a sppasUnicode instance.

    :param entry: (str or unicode or bytes in python 2)

    """
    if isinstance(entry, (binary_type, text_type)) is False:
        raise sppasTypeError(entry, type(entry))
    self._entry = entry

Public functions

unicode

Return the unicode string of the given entry.

Returns
  • unicode
View Source
def unicode(self):
    """Return the unicode string of the given entry.

        :returns: unicode

        """
    e = self._entry
    if isinstance(self._entry, binary_type):
        e = u(self._entry)
    return e
to_lower

Return the unicode string with lower case.

Returns
  • unicode
View Source
def to_lower(self):
    """Return the unicode string with lower case.

        :returns: unicode

        """
    e = self.unicode()
    self._entry = e.lower()
    return self._entry
to_strip

Strip the string.

Remove also multiple whitespace, tab and CR/LF inside the string.

Returns
  • unicode
View Source
def to_strip(self):
    """Strip the string.

        Remove also multiple whitespace, tab and CR/LF inside the string.

        :returns: unicode

        """
    e = self.unicode()
    self._entry = re.sub('[\\s]+', ' ', e)
    if self._entry.startswith(' '):
        self._entry = re.sub('^[ ]+', '', self._entry)
    if self._entry.endswith(' '):
        self._entry = re.sub('[ ]+$', '', self._entry)
    if '\ufeff' in self._entry:
        self._entry = re.sub('\ufeff', '', self._entry)
    return self._entry
clear_whitespace

Replace the whitespace by underscores.

Returns
  • unicode
View Source
def clear_whitespace(self):
    """Replace the whitespace by underscores.

        :returns: unicode

        """
    e = self.to_strip()
    e = re.sub('\\s', '_', e)
    self._entry = e
    return self._entry
to_ascii

Replace the non-ASCII characters by underscores.

Returns
  • unicode
View Source
def to_ascii(self):
    """Replace the non-ASCII characters by underscores.

        :returns: unicode

        """
    e = self.unicode()
    e = re.sub('[^\\x00-\\x7F]', '_', e)
    self._entry = e
    return self._entry
is_restricted_ascii

Check if the entry key is using only a-Z_ characters.

Returns
  • (bool)
View Source
def is_restricted_ascii(self):
    """Check if the entry key is using only a-Z_ characters.

        :returns: (bool)

        """
    ra = re.sub('[^a-zA-Z0-9_]', '', self._entry)
    return self._entry == ra