def _position_filter(mask): '''generate a filter for a position mask (dict, set, or where)''' if mask is None: pairs = False # the default format selector = lambda x: x elif type(mask) is set: import itertools _zip = getattr(itertools, 'izip', zip) if mask: from mystic.tools import _inverted _mask, pairs = _zip(*mask) _mask = set(_zip(_mask, _inverted(pairs))) else: _mask = mask pairs = True selector = lambda x: x - mask - _mask elif type(mask) is dict: pairs = False from mystic.tools import _symmetric selector = lambda x: dict((k, v) for (k, v) in ( (i, j - _symmetric((mask[i] if i in mask else set()))) for (i, j) in getattr(x, 'iteritems', x.items)()) if v) else: import itertools from mystic.tools import _inverted _zip = getattr(itertools, 'izip', zip) _mask, pairs = mask mask = _mask + _mask, tuple(pairs) + tuple(_inverted(pairs)) pairs = None #XXX: special case, use where notation selector = lambda x: tuple( _zip(*(i for i in _zip(*x) if i not in _zip(*mask))) ) #FIXME: searching set would be faster return selector, pairs
def _position_filter(mask): '''generate a filter for a position mask (dict, set, or where)''' if mask is None: pairs = False # the default format selector = lambda x: x elif type(mask) is set: import itertools _zip = getattr(itertools, 'izip', zip) if mask: from mystic.tools import _inverted _mask,pairs = _zip(*mask) _mask = set(_zip(_mask,_inverted(pairs))) else: _mask = mask pairs = True selector = lambda x: x - mask - _mask elif type(mask) is dict: pairs = False from mystic.tools import _symmetric selector = lambda x: dict((k,v) for (k,v) in ((i,j - _symmetric((mask[i] if i in mask else set()))) for (i,j) in getattr(x, 'iteritems', x.items)()) if v) else: import itertools from mystic.tools import _inverted _zip = getattr(itertools, 'izip', zip) _mask,pairs = mask mask = _mask+_mask, tuple(pairs)+tuple(_inverted(pairs)) pairs = None #XXX: special case, use where notation selector = lambda x: tuple(_zip(*(i for i in _zip(*x) if i not in _zip(*mask)))) #FIXME: searching set would be faster return selector, pairs
def _pair_selector(mask): '''generate a selector for a mask of tuples (pairs)''' # assume a sequence of pairs is given, exactly specifying the pairs from mystic.tools import _inverted return lambda x: (tuple(x) in mask or tuple(x) in _inverted(mask))