コード例 #1
0
 def __eq__(self, other):
     if not is_iterable(other):
         return False
     if len(self) != len(other):
         return False
     for myval, otherval in zip(self, other):
         if myval != otherval:
             return False
     return True
コード例 #2
0
 def __init__(self, inner=frozenset()):
     if isinstance(inner, AbcSet):
         self._inner = inner
     elif is_iterable(inner):
         # Piggyback on ordered-ness of dictionaries:
         self._inner = {k: None for k in inner}.keys()
         # TODO: this hashes the elements;
         #       we likely want a dedicated ordered set class.
     else:
         raise TypeError
コード例 #3
0
 def __lt__(self, other):
     # NOTE: subclasses will need further type restrictions.
     # For example, `[1,2] <= (1,2)` raises a TypeError.
     if not is_iterable(other):
         return NotImplemented
     for v1, v2 in zip(self, other):
         if v1 == v2:
             continue
         return v1 < v2
     return len(self) < len(other)
コード例 #4
0
def _patt_replace(list_tree: List, from_obj: object, to_obj: object = _REMOVE):
    """
    >>> _patt_replace([[], [2, None]], None, 3)
    [[], [2, 3]]
    >>> _patt_replace([[], [None, 7]], None, _REMOVE)
    [[], [7]]
    """
    for idx, child in enumerate(list_tree):
        if child is from_obj:
            if to_obj is _REMOVE:
                return list_tree[:idx] + list_tree[idx + 1:]
            else:
                return [(to_obj if x is from_obj else x) for x in list_tree]
        if not is_iterable(child):
            continue
        newchild = _patt_replace(child, from_obj, to_obj)
        if newchild is not child:
            # Found it; make a copy of this list with the new item:
            newlist = list(list_tree)
            newlist[idx] = newchild
            return newlist
    # nothing changed; re-use the original list
    return list_tree