def _insert_item(self, key, new_item: abc.Iterable, instance: int, is_after: bool): """Insert a new item before or after another item.""" index = self.key_index(key, instance) index = index + 1 if is_after else index if isinstance(new_item, abc.Mapping): tuple_iter = new_item.items() else: tuple_iter = new_item self.insert(index, tuple_iter)
raise NotImplementedError @abstractmethod def _copy(self): raise NotImplementedError @abstractmethod def _set(self): raise NotImplementedError @abstractmethod def unwrap(self): raise NotImplementedError Iterable.register(MutableWrapper) Sized.register(MutableWrapper) Container.register(MutableWrapper) class MutableWrapperDictList(MutableWrapper): __slots__ = () def _subset(self, k, v): new = self._copy() new[k] = v self._set(new) def __getitem__(self, k): ret = self._getter()[k] if isinstance(ret, dict):
def _check_index(self, index): if not isinstance(index, tuple): raise TypeError('NList index must be a tuple') if len(index) != self.rank: raise TypeError('NList index must be rank %s' % self.rank) if any(not isinstance(x, int) for x in index): raise TypeError('Indexes must consist of integers') if not self._in_bounds(index): raise IndexError('NList index out of range') def _in_bounds(self, index): for i, x in enumerate(index): if not 0 <= x < self.shape[i]: return False return True def _index_to_flat(self, index): self._check_index(index) return sum(self._strides[k] * index[k] for k in range(self.rank)) @staticmethod def _check_shape(shape): for x in shape: if not isinstance(x, int): raise TypeError('Dimensions must be integers') if x < 0: raise ValueError('Dimensions cannot be negative') Container.register(NList) Iterable.register(NList)
from libtbx.str_utils import format_value if six.PY3: from collections.abc import Iterable, Sequence else: from collections import Iterable, Sequence # Register extension classes that look like a sequence, ie. have a # length and adressable elements, as a Sequence. Same for Iterable. for entry in ext.__dict__.values(): # Only consider types (=classes), not object instances if not isinstance(entry, type): continue # The Iterable interface means the type contains retrievable items. # If the type fulfills this but is not already a known Iterable then # register it as such. if hasattr(entry, "__getitem__") and not issubclass(entry, Iterable): Iterable.register(entry) # A Sequence is an Iterable that also has a determinable length. if hasattr(entry, "__getitem__") and hasattr(entry, "__len__") \ and not issubclass(entry, Sequence): Sequence.register(entry) def bool_md5(self): return hashlib.md5(self.__getstate__()[1]) bool.md5 = bool_md5 @boost.python.inject_into(grid) class _():
@abstractmethod def __init__(self, startcell, walker): if not isinstance(startcell, cons): raise TypeError("Expected a cons, got {} with value {}".format( type(startcell), startcell)) self.walker = iter(walker( startcell)) # iter() needed to support gtrampolined generators def __iter__(self): return self def __next__(self): return next(self.walker) Iterable.register(ConsIterator) Iterator.register(ConsIterator) class LinkedListIterator(ConsIterator): """Iterator for linked lists built from cons cells.""" def __init__(self, head, _fullerror=True): def walker(head): cell = head while cell is not nil: yield cell.car if isinstance(cell.cdr, cons) or cell.cdr is nil: cell = cell.cdr else: if _fullerror: raise TypeError("Not a linked list: {}".format(head))
def __hash__(self): """ Hash based on value of elements. >>> m = pmap({pbag([1, 2]): "it's here!"}) >>> m[pbag([2, 1])] "it's here!" >>> pbag([1, 1, 2]) in m False """ return hash(self._counts) Container.register(PBag) Iterable.register(PBag) Sized.register(PBag) Hashable.register(PBag) def b(*elements): """ Construct a persistent bag. Takes an arbitrary number of arguments to insert into the new persistent bag. >>> b(1, 2, 3, 2) pbag([1, 2, 2, 3]) """ return pbag(elements)