Exemple #1
0
 def __str__(self):
     """Return the instance's string representation."""
     if len(self.data) == 1:
         return str(_misc.get_single_iter_elem(self.data))
     else:
         unsorted_strings = [str(elem) for elem in self.data]
         return '({elems})'.format(elems=' U '.join(elem for elem in sorted(unsorted_strings)))
Exemple #2
0
 def _process_nodes(nodes):
     for node in nodes:
         if node.nodeType == node.ELEMENT_NODE:
             # attributes and children are sets of Couplets.
             attributes = set(_process_attributes(node))
             children = set(_process_nodes(
                 node.childNodes))  # May include text (text_left).
             children = children.union(attributes)
             if len(children) == 1 and _contains_text_node(children):
                 # We have a single child that is a text node. Remove one layer of couplets.
                 yield _mo.Couplet(
                     left=_util.get_left_cached(node.tagName),
                     right=_misc.get_single_iter_elem(children).right,
                     direct_load=True)
             else:
                 yield _mo.Couplet(left=_util.get_left_cached(node.tagName),
                                   right=_mo.Set(children,
                                                 direct_load=True),
                                   direct_load=True)
         elif node.nodeType == node.TEXT_NODE:
             text_node_text = node.data.strip()
             if len(text_node_text) > 0:
                 yield _mo.Couplet(left=text_left,
                                   right=_mo.Atom(
                                       _get_atom_value(text_node_text),
                                       direct_load=True),
                                   direct_load=True)
         else:
             assert False  # Node type not supported.
Exemple #3
0
 def _process_nodes(nodes):
     for node in nodes:
         if node.nodeType == node.ELEMENT_NODE:
             # attributes and children are sets of Couplets.
             attributes = set(_process_attributes(node))
             children = set(_process_nodes(node.childNodes))  # May include text (text_left).
             children = children.union(attributes)
             if len(children) == 1 and _contains_text_node(children):
                 # We have a single child that is a text node. Remove one layer of couplets.
                 yield _mo.Couplet(
                     left=_util.get_left_cached(node.tagName),
                     right=_miscellaneous.get_single_iter_elem(children).right,
                     direct_load=True)
             else:
                 yield _mo.Couplet(
                     left=_util.get_left_cached(node.tagName),
                     right=_mo.Set(children, direct_load=True),
                     direct_load=True)
         elif node.nodeType == node.TEXT_NODE:
             text_node_text = node.data.strip()
             if len(text_node_text) > 0:
                 yield _mo.Couplet(
                     left=text_left,
                     right=_mo.Atom(_get_atom_value(text_node_text), direct_load=True),
                     direct_load=True)
         else:
             assert False  # Node type not supported.
Exemple #4
0
 def __str__(self):
     """Return the instance's string representation."""
     if len(self.data) == 1:
         return str(_misc.get_single_iter_elem(self.data))
     else:
         unsorted_strings = [str(elem) for elem in self.data]
         return '({elems})'.format(elems=' U '.join(
             elem for elem in sorted(unsorted_strings)))
Exemple #5
0
 def get_ground_set(self) -> _structure.Structure:
     """Return the :term:`ground set` of the lowest-level algebra of this :class:`Set`."""
     if len(self.data) == 0:
         return _structure.Structure()
     elements_ground_set = _structure.Union(elem.get_ground_set() for elem in self.data)
     if len(elements_ground_set.data) == 1:
         return _structure.PowerSet(_misc.get_single_iter_elem(elements_ground_set.data))
     else:
         return _structure.PowerSet(elements_ground_set)
Exemple #6
0
    def is_same(self, other: Structure) -> bool:
        """Return ``True`` if ``self`` is structurally equivalent to ``other``.

        :param other: Must be an instance of `Structure`.
        :return: ``True`` if ``self`` is structurally equivalent to ``other``.
        """
        assert isinstance(other, Structure)
        if isinstance(other, Union):
            return self.data == other.data
        else:
            if len(self.data) == 1:
                # Compare the one element against 'other'.
                return _misc.get_single_iter_elem(self.data).is_same(other)
            else:
                # self is a 'real' union, so the 'other' must also be a union.
                return False
Exemple #7
0
    def is_same(self, other: Structure) -> bool:
        """Return ``True`` if ``self`` is structurally equivalent to ``other``.

        :param other: Must be an instance of `Structure`.
        :return: ``True`` if ``self`` is structurally equivalent to ``other``.
        """
        assert isinstance(other, Structure)
        if isinstance(other, Union):
            return self.data == other.data
        else:
            if len(self.data) == 1:
                # Compare the one element against 'other'.
                return _misc.get_single_iter_elem(self.data).is_same(other)
            else:
                # self is a 'real' union, so the 'other' must also be a union.
                return False