https://sppas.org/

SPPAS 4.16

Module sppas.src.anndata

Class sppasTagCompare

Description

Comparison methods for sppasTag.

Label'tags can be of 3 types in anndata (str, num, bool) so

that this class allows to create different comparison methods

depending on the type of the tags.

Example

Three different ways to compare a tag content to a given string

Example

>>> tc = sppasTagCompare()
>>> tc.exact(sppasTag("abc"), u("abc"))
>>> tc.methods['exact'](sppasTag("abc"), u("abc"))
>>> tc.get('exact')(sppasTag("abc"), u("abc"))

Constructor

Create a sppasTagCompare instance.

def __init__(self):
    """Create a sppasTagCompare instance."""
    super(sppasTagCompare, self).__init__()
    self.methods['exact'] = sppasTagCompare.exact
    self.methods['iexact'] = sppasTagCompare.iexact
    self.methods['startswith'] = sppasTagCompare.startswith
    self.methods['istartswith'] = sppasTagCompare.istartswith
    self.methods['endswith'] = sppasTagCompare.endswith
    self.methods['iendswith'] = sppasTagCompare.iendswith
    self.methods['contains'] = sppasTagCompare.contains
    self.methods['icontains'] = sppasTagCompare.icontains
    self.methods['regexp'] = sppasTagCompare.regexp
    self.methods['greater'] = sppasTagCompare.greater
    self.methods['lower'] = sppasTagCompare.lower
    self.methods['equal'] = sppasTagCompare.equal
    self.methods['bool'] = sppasTagCompare.bool

Public functions

exact

Test if two texts strictly contain the same characters.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def exact(tag, text):
    """Test if two texts strictly contain the same characters.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return tag.get_content() == text
iexact

Case-insensitive exact.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def iexact(tag, text):
    """Case-insensitive exact.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return tag.get_content().lower() == text.lower()
startswith

Test if first text starts with the characters of the second text.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def startswith(tag, text):
    """Test if first text starts with the characters of the second text.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return tag.get_content().startswith(text)
istartswith

Case-insensitive startswith.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def istartswith(tag, text):
    """Case-insensitive startswith.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return tag.get_content().lower().startswith(text.lower())
endswith

Test if first text ends with the characters of the second text.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def endswith(tag, text):
    """Test if first text ends with the characters of the second text.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return tag.get_content().endswith(text)
iendswith

Case-insensitive endswith.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def iendswith(tag, text):
    """Case-insensitive endswith.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return tag.get_content().lower().endswith(text.lower())
contains

Test if the first text contains the second text.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def contains(tag, text):
    """Test if the first text contains the second text.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return text in tag.get_content()
icontains

Case-insensitive contains.

Parameters

  • tag: (sppasTag) Tag to compare.
  • text: (unicode) Unicode string to be compared with.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def icontains(tag, text):
    """Case-insensitive contains.

        :param tag: (sppasTag) Tag to compare.
        :param text: (unicode) Unicode string to be compared with.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if not isinstance(text, text_type):
        raise AnnDataTypeError(text, text_type)
    return text.lower() in tag.get_content().lower()
regexp

test if text matches pattern.

Parameters

  • tag: (sppasTag) Tag to compare.
  • pattern: (unicode) Pattern to search.

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def regexp(tag, pattern):
    """test if text matches pattern.

        :param tag: (sppasTag) Tag to compare.
        :param pattern: (unicode) Pattern to search.
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    text = tag.get_content()
    return True if re.match(pattern, text) else False
equal

Return True if numerical value of the tag is equal to x.

Parameters

  • tag: (sppasTag) Tag to compare.
  • x: (int, float)

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def equal(tag, x):
    """Return True if numerical value of the tag is equal to x.

        :param tag: (sppasTag) Tag to compare.
        :param x: (int, float)
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if tag.get_type() not in ['int', 'float']:
        raise AnnDataTypeError(tag, 'int/float')
    if sppasType().is_number(x) is False:
        raise AnnDataTypeError(x, 'int/float')
    return tag.get_typed_content() == x
greater

Return True if numerical value of the tag is greater than x.

Parameters

  • tag: (sppasTag) Tag to compare.
  • x: (int, float)

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def greater(tag, x):
    """Return True if numerical value of the tag is greater than x.

        :param tag: (sppasTag) Tag to compare.
        :param x: (int, float)
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if tag.get_type() not in ['int', 'float']:
        raise AnnDataTypeError(tag, 'int/float')
    if sppasType().is_number(x) is False:
        raise AnnDataTypeError(x, 'int/float')
    return tag.get_typed_content() > x
lower

Return True if numerical value of the tag is lower than x.

Parameters

  • tag: (sppasTag) Tag to compare.
  • x: (int, float)

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def lower(tag, x):
    """Return True if numerical value of the tag is lower than x.

        :param tag: (sppasTag) Tag to compare.
        :param x: (int, float)
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if tag.get_type() not in ['int', 'float']:
        raise AnnDataTypeError(tag, 'int/float')
    if sppasType().is_number(x) is False:
        raise AnnDataTypeError(x, 'int/float')
    return tag.get_typed_content() < x
bool

Return True if boolean value of the tag is equal to boolean x.

Parameters

  • tag: (sppasTag) Tag to compare.
  • x: (bool)

Returns

  • (bool)

Raises

AnnDataTypeError

@staticmethod
def bool(tag, x):
    """Return True if boolean value of the tag is equal to boolean x.

        :param tag: (sppasTag) Tag to compare.
        :param x: (bool)
        :returns: (bool)
        :raises: AnnDataTypeError

        """
    if isinstance(tag, sppasTag) is False:
        raise AnnDataTypeError(tag, 'sppasTag')
    if tag.get_type() != 'bool':
        raise AnnDataTypeError(tag, 'bool')
    if sppasType().is_bool(x) is False:
        raise AnnDataTypeError(x, 'bool')
    return tag.get_typed_content() == x